Skip to content

Commit 786cfb7

Browse files
alloncmavivace
andauthored
Add CPU mode select register - KEY0 (#580)
* Add CPU mode select register - KEY0 Add the register decription to the CGB Registers page and updated the CGB flag cartrigde and the Power Up Sequence with refs to the new register PGB mode remains a mystery * Remove the leaked info reference and try to make PGB mode references less confusing * Review fixes Move the pgb mode brief to the research needed box and add a link to a new issue regarding this topic * Update src/CGB_Registers.md * Apply suggestions from code review Co-authored-by: Antonio Vivace <[email protected]> --------- Co-authored-by: Antonio Vivace <[email protected]>
1 parent f4455fa commit 786cfb7

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/CGB_Registers.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,39 @@ ON/OFF pulses (length 10us ON, 17.5us OFF each) instead of a permanent
219219
880us LED ON signal. Even though being generally CGB compatible, the GBA
220220
does not include an infra-red port.
221221

222+
### FF4C — KEY0 (CGB Mode only): CPU mode select
223+
224+
This GBC-only register (which is not officially documented) is written only by the CGB boot ROM,
225+
as it gets locked after the bootrom finish execution (by a write to the [BANK register](<#Monochrome models (DMG0, DMG, MGB)>)).
226+
227+
Once it is locked, the behavior of the system can't be changed without a reset (this behavior can be observed using [this test ROM](https://github.com/alloncm/MagenTests?tab=readme-ov-file#key0-cpu-mode-register-lock-after-boot)).
228+
229+
As a result of the above most of the behavior is not directly testable without hardware manipulation.
230+
Even though we can't test its behavior directly we can inspect the disassembly of the CGB bootrom and infer the following:
231+
232+
{{#bits 8 >
233+
"KEY0" 2:"DMG compatibility mode"
234+
}}
235+
236+
- **DMG compatibility mode**: `0` = Disabled (full CGB mode, for regular CGB cartridges), `1` = Enabled (for DMG only cartridges)
237+
238+
#### PGB mode
239+
240+
:::tip Research needed
241+
242+
It has been speculated that setting bit 3 is related to a special mode called "PGB" for controlling the LCD externally.
243+
244+
This mode is not well researched nor documented yet, you are welcome to help [here!](https://github.com/gbdev/pandocs/issues/581)
245+
246+
:::
247+
222248
### FF6C — OPRI (CGB Mode only): Object priority mode
223249

224250
This register serves as a flag for which object priority mode to use. While
225251
the DMG prioritizes objects by x-coordinate, the CGB prioritizes them by
226252
location in OAM. This flag is set by the CGB bios after checking the game's CGB compatibility.
227253

228-
OPRI has an effect if a PGB value (`0xX8`, `0xXC`) is written to KEY0 but STOP hasn't been executed yet, and the write takes effect instantly.
254+
OPRI has an effect if a [PGB](<#PGB mode>) value (`0xX8`, `0xXC`) is written to [KEY0](<#FF4C — KEY0 (CGB Mode only): CPU mode select>) but STOP hasn't been executed yet, and the write takes effect instantly.
229255

230256
:::warning TO BE VERIFIED
231257

src/Power_Up_Sequence.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ Then, like the monochrome boot ROMs, the header logo is checked *from the buffer
8686
For unknown reasons, however, only the first half of the logo is checked, despite the full logo being present in the HRAM buffer.
8787

8888
Finally, the boot ROM fades all BG palettes to white, and sets the hardware to compatibility mode.
89-
If [the CGB compatibility byte](<#0143 — CGB flag>) indicates CGB compatibility, the byte is written directly to `KEY0` ($FF4C), potentially enabling PGB mode; otherwise, $04 is written to `KEY0` (enabling DMG compatibility mode in the CPU), $01 is written to [`OPRI`](<#FF6C — OPRI (CGB Mode only): Object priority mode>) (enabling [DMG OBJ priority](<#Object Priority and Conflicts>)), and the [compatibility palettes](<#Compatibility palettes>) are written.
89+
If [the CGB compatibility byte](<#0143 — CGB flag>) indicates CGB compatibility, the byte is written directly to [`KEY0`](<#FF4C — KEY0 (CGB Mode only): CPU mode select>), potentially [enabling "PGB mode"](<#PGB mode>);
90+
otherwise, $04 is written to [`KEY0`](<#FF4C — KEY0 (CGB Mode only): CPU mode select>) (enabling DMG compatibility mode in the CPU),
91+
$01 is written to [`OPRI`](<#FF6C — OPRI (CGB Mode only): Object priority mode>) (enabling [DMG OBJ priority](<#Object Priority and Conflicts>)), and the [compatibility palettes](<#Compatibility palettes>) are written.
9092
Additionally, the DMG logo tilemap is written [if the compatibility requests it](<#Compatibility palettes>).
9193

9294
Like all other boot ROMs, the last thing the color boot ROMs do is hand off execution at the same time as they unmap themselves, though they write $11 instead of $01 or $FF.

src/The_Cartridge_Header.md

+1-8
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ Value | Meaning
5656
`$80` | The game supports CGB enhancements, but is backwards compatible with monochrome Game Boys
5757
`$C0` | The game works on CGB only (the hardware ignores bit 6, so this really functions the same as `$80`)
5858

59-
Values with bit 7 and either bit 2 or 3 set will switch the Game Boy into a special non-CGB-mode called "PGB mode".
60-
61-
:::tip Research needed
62-
63-
The PGB mode is not well researched or documented yet.
64-
Help is welcome!
65-
66-
:::
59+
Setting bit 7 will trigger a write of this register value to [KEY0 register](<#FF4C — KEY0 (CGB Mode only): CPU mode select>) which sets the CPU mode.
6760

6861
## 0144–0145 — New licensee code
6962

0 commit comments

Comments
 (0)