Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GBC] Palette selection for *.gbc games causes the core to bootloop and never enter the game #192

Closed
maij opened this issue Dec 28, 2021 · 10 comments

Comments

@maij
Copy link
Contributor

maij commented Dec 28, 2021

The Gameboy Color has the option to select a different colour palette when booting up original *.gb gameboy games by pressing the directional button on the "GAMEBOY" start-up screen. However, if you do this for a *.gbc game the core will continually reset itself and never enter the game. I think the ability to select palettes was disabled for .gbc games, but don't have the physical hardware to verify this at the moment.

I've tested this with a couple of games (Wario Land 2, Legend of Zelda : Oracle of the Ages), could try others though I suspect its a pervasive problem.

@maij
Copy link
Contributor Author

maij commented Nov 6, 2022

Closing this issue, it seems to have been fixed.

@maij maij closed this as completed Nov 6, 2022
@maij
Copy link
Contributor Author

maij commented Nov 10, 2022

I have done some more testing and this is still an issue.

It only appears for the gameboy games that can be run in cgb mode or gb mode. If a game that can be run in cgb mode (e.g. Pokemon Gold/Silver, Wario Land II etc.) but the palette registers are changed with the boot rom (i.e. pressing any dpad button), then the game boot-loops with corrupted graphics.

I have tested the latest Sameboy cgb bootroms and it doesn't bootloop, but it still doesn't reach the game. Not entirely sure where this is happening.

@maij maij reopened this Nov 10, 2022
@paulb-nl
Copy link
Collaborator

I think this is a Sameboy bootrom feature because the original Nintendo CGB bootrom doesn't seem to respond to the directional pad when booting a Gameboy Color game.

The bootrom writes to KEY0 ($FF4C) to enable CGB or DMG mode and OPRI ($FF6C) to switch to CGB or DMG sprite priority. Those registers will need to be implemented in the core. Currently the core just changes the behavior depending on what kind of ROM is loaded (gb or gbc).

The Sameboy bootrom apparently tries to use those registers to force DMG mode when a palette is chosen but since those are not implemented the core still runs in CGB mode.

@maij
Copy link
Contributor Author

maij commented Nov 16, 2022

I have tried to implement these registers as well as a DMG emulation mode (as the Sameboy bootrom tries to do) but I haven't had much luck. I will spend a bit more time making custom bootroms to see if there is a simpler way forward for the core.

It is worth noting that this does not only affect games that can be run in GB/CGB compatibility modes. It also affects some CGB only games. It is strange that some games will bootloop (e.g. Metal Gear Solid, Wario Land 3) while others (e.g. Perfect Dark) will correctly load the DMG screen saying this game must be played on a CGB. It is also important to note that the DMG bootrom does not bootloop, and all CGB-only games I have tested arrive at their incompatibility screens.

@paulb-nl
Copy link
Collaborator

paulb-nl commented Nov 18, 2022

I have implemented the registers and it works correctly with the original CGB boot rom switching to DMG mode for GB only games.

The original boot rom always writes 0x11 to $FF50 to disable the bootrom but the Sameboy CGB boot rom writes 0x1 after setting DMG mode (apparently only with a GBC game, it does write 0x11 with a GB only game even when forcing a palette) . This is what causes the boot loops because the CGB boot rom is not disabled when writing 0x1.

IMO the boot rom needs to be changed because I dont think it is accurate to disable the boot rom with 0x1 on a GBC.

@maij
Copy link
Contributor Author

maij commented Nov 18, 2022

As in you have added KEY0 and OPRI? Nice!
Yeah, I tried to implement the GB switching based on the KEY0 value, but I must have made a mistake somewhere.

It would be worth checking on real hardware, maybe it is inaccurate switching to DMG mode on a GBC for compatibility. I have implemented a bootrom Makefile process, which I would be happy to add to the repo. It would be nice to make our own roms, rather than being dependent on Sameboy

@maij
Copy link
Contributor Author

maij commented Dec 3, 2022

Hi @paulb-nl, just following up on your implementation of KEY0 and OPRI, from the build you shared in #143 .

The core works well, I have done some regression testing and haven't found any issues. I have not heard any feedback from the community on discord, but I think people have downloaded and used the core.

I wanted to clarify the implementation of the KEY0 register. I think it should be a full byte. The bootrom writes the full CGB compatibility byte to the KEY0 reg (https://gbdev.io/pandocs/The_Cartridge_Header.html?highlight=PGB#0143--cgb-flag). It's noted that 0x80 and 0xC0 are typical values, but then there's also the GB/PGB bits 2-3 which can be written to (as you have implemented). I don't know if there's any functionality that depends on this, but better safe than sorry. This would also change the cpu_di write from {4'hF, ff4C_key0, 2'h3} to just ff4C_key0.

I also think it might be nice to at least add a menu option to allow DMG mode for DMG compatible CGB games, playing on the CGB console (say that ten times fast...). This would only modify the boot disable line to:

if ((isGBC && cpu_do[7:0]==8'h11) || ((!isGBC || !isGBC_mode ) && cpu_do[0])): 
    // Can also AND isGBC_mode with a menu option
    boot_rom_enabled <= 1'b0;

The main reason is just to prevent bootlooping. The alternative is to modify the bootrom, which is pretty easy, but I cannot currently generate a new bootrom with the 'GameBoy' logo we have (see #227).

@paulb-nl
Copy link
Collaborator

paulb-nl commented Dec 3, 2022

You should check the link to the Nesdev forum that I placed in the comments above the implementation of KEY0.

Nocash investigated the register and found that bit 1 and 4-7 are "not used" aka open bus so they will always read as 1. That only leaves bits 2-3 and bit 0. Bit 0 can be written to but the purpose is unknown.

Since this register can only be read and written to by the boot rom I don't think it is useful to add bit 0 to the register. The leaked official documentation only mentions bit 2-3 anyway.

I also think it might be nice to at least add a menu option to allow DMG mode for DMG compatible CGB games, playing on the CGB console (say that ten times fast...). This would only modify the boot disable line to:

I don't want to be inaccurate to hardware. As far as I know it is required to write 0x11 to disable the boot rom on GBC hardware. Or at least bit 0&4 high. The schematics of the DMG chip by Furrtek show that the original DMG only needed bit 0 high to disable the boot rom so I am guessing the GBC is similar except it is bit 0&4. I would prefer to fix the boot rom instead.

@maij
Copy link
Contributor Author

maij commented Dec 3, 2022

Fair enough, I appreciate your point of view. I think these changes are sound then, hopefully we can get new bootroms set up soon!

I've started putting together a bootrom compiler, https://github.com/maij/Gameboy_MiSTer/tree/bootrom_compiler
Currently the makefile only works on linux, but it shouldn't be too hard to add a Windows switch.

@maij maij mentioned this issue Jan 17, 2023
6 tasks
@maij maij closed this as completed Feb 12, 2023
@maij
Copy link
Contributor Author

maij commented Feb 12, 2023

Closed with #231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants