binji / binjgb

Gameboy emulator implemented in C, that also runs in the browser
https://binji.github.io/binjgb/
MIT License
534 stars 61 forks source link

Rejects ROMs as invalid #43

Open basxto opened 2 years ago

basxto commented 2 years ago

334B is the minimum size needed. 334th Byte is the header checksum, which is the last header byte touched by the bootrom. Global checksum doesn't get verified. SIERPINSKIBOY should therefore have everything, the bootrom needs.

$ bin/binjgb testrom/sierpinskiboy.smol.cgb
/tmp/binjgb/src/emulator.c:4847: File size (334) should be a multiple of minimum rom size (32768).

TINYMIRRORBOY has the correct first half of the Nintendo logo and a correct header checksum. That's everything CGB needs to boot.

$ bin/binjgb testrom/tinymirrorboy.mirrored.32k.cgb
/tmp/binjgb/src/emulator.c:1178: Invalid ROM.

These ROMs work on Sameboy, Emulicious and BGB. And they also boot with EVERDRIVE GB X3 on my CGB-D.

binji commented 2 years ago

You're right, I added these checks a long time ago, but never verified they were correct. I'll see if I can remove them so these roms will work.

basxto commented 2 years ago

I don’t know if they will really work, but it would be great if they at least started. They strongly depend on registers set by CGB bootrom and that the (modified) nintendo logo gets upscaled and loaded into vram. SIERPINKSIBOY further depends on builtin color palettes for pre-cgb nintendo games and that the sound registers are initialized from the start bling. Both run in CGB’s DMG mode, but are intentionally incompatible with DMG and SGB. Those are the things, I actually wanted to test.

binji commented 2 years ago

Oh, those things almost certainly won't work haha! Maybe not too hard to set some initial state to allow it though.

basxto commented 2 years ago

The initial state is quite complex, if I remember correctly in CGB’s DMG mode with the licensee set to nintendo, one register will be a leftover of the title checksum calculation and that depends on the title of the ROM. The logo is also a bit more complicated algorithm, because it upscales the 4x4 logo stored on the ROM to 8x8 and my demos abuse that to load a custom 4x4 font. Easiest solution is probably to support bootroms, I don’t know what they all need, execution starts at `0x0. Since they overlay the actual ROM, there has to be some mechanism to to switch to the game ROM. CGB has a byte with which the bootrom enables/disables CGB mode, in DMG mode it enables it to set up the palettes and then disables it. Sameboy is also MIT licensed and it has custom BootROMs available, where it should be enough to change SameBoyLogo.png to BINJGB. Or just allow to boot with a user specified bootrom, though I don’t know how that would work in webasm then.