Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

ROM loading issues with larger ROMs #1

Open
matthew-5pl opened this issue Feb 25, 2024 · 3 comments
Open

ROM loading issues with larger ROMs #1

matthew-5pl opened this issue Feb 25, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@matthew-5pl
Copy link
Owner

matthew-5pl commented Feb 25, 2024

Issue Description

Trying to load ROMs larger than ~200 KBs in size causes a crash before emulation can even start, logging an error in the read_rom_to_ram function before (presumably) the ESP32's crash handler resets the device.

Theories on why this happens

The SD Card API functions (specifically the File.readBytes method) expects a char* (aka int8_t*) as a buffer to write the file data to, but Peanut-GB wants the ROM to be an array of uint8_ts.

Since I'm a bad programmer this was my solution:

// rom data is read to char* readRom
uint8_t *rom = (uint8_t*)readRom;
// return rom to Peanut-GB

I think this somehow breaks when ROMs are too big, where loading the ROM into RAM exceeds the Cardputer's 512 KB of SRAM. At the same time, I only log that message when the read_rom_to_ram function returns NULL, so the function is definitely reaching its end.

I don't really have the time or energy to look into or debug this much since I'm busy with school right now, but since other people seem to be interested in Game Boy emulation on the Cardputer I think this would be a good place for other people (who probably have a better understanding of C than me) to potentially fix this issue.

@matthew-5pl matthew-5pl added the bug Something isn't working label Feb 25, 2024
@matthew-5pl matthew-5pl self-assigned this Feb 25, 2024
@foopod
Copy link

foopod commented Mar 12, 2024

Trying to load ROMs larger than ~200 MBs

Do you mean >200kb?

I think the 512kb SRAM could potentially be a hard limit here. Maybe we could look at moving the cart to the flash memory on load and use it from there, but I'm not sure how that would impact performance.

It also looks like there are some minor things we could tweak to hopefully allow loading of 256kb roms. For example it looks like we are using a framebuffer for the full resolution of the cardputer, but a decent chunk isn't used due to the aspect ratio.

@matthew-5pl
Copy link
Owner Author

Oops! Yeah I meant KB, my bad

Addressing a few things here:

  • AFAIK excluding some bigger ROMs which can be around ~2MB, the Cardputer's 512KB of RAM should be more than enough for most games + the Game Boy's 8KB of RAM and the two framebuffers (I'll expand on why I'm using two framebuffers later, but now that you mentioned it I should really think of a better way to implement that)
  • Writing to flash might be a good idea, but it would probably be better to implement some sort of "chunking" system to reduce read times which would probably require some more in-depth knowledge on the Game Boy's architecture and possibly some edits to the Peanut-GB code. AFAIK the reason the Game Boy could handle such big ROMs despite its RAM size is because it had a dedicated bus to the cartridge, which it could use to request an address and get the corresponding data back from ROM quickly. With the ESP32 we might not be able to do something like that.
  • The reason I'm using two framebuffers (the standard Peanut-GB one and another one called swap_fb) is to be able to quickly draw frames to the screen. When I first got the emulator to work I remember it getting to the Tetris copyright screen and then seemingly just freezing there. However, I did some experiments and figured out that the Cardputer's display refreshes a Game Boy frame pretty slowly, so I added another framebuffer which I write into after every frame draw and then check when the next frame is ready to only redraw the pixels that changed. Without this the emulator is unusable, so unusable that getting past that Tetris copyright screen took around 5 minutes when it should take a few seconds.
  • As for the size of the framebuffer, I'm afraid I can't do much about that as Peanut-GB expects a framebuffer of that size and trying to change that would cause a segfault. Again, this might be fixed by editing the Peanut-GB code but I don't think it's worth it for what should be a few hundred bytes of extra memory.

With that said thanks for your interest! Sadly as I've already said in this reddit post I won't really be able to get to trying any of this right now as I'm pretty busy with school :(

@matthew-5pl
Copy link
Owner Author

Also please do correct me if I'm wrong with any of what I wrote, I'm definitely not an expert here and I'd love to learn more lol

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants