fix: enable Game Boy Color mode when loading CGB ROMs#40
Merged
eddmann merged 2 commits intoNov 11, 2025
Merged
Conversation
The emulator had fully implemented CGB color support (palette system, PPU rendering, VRAM banking, terminal output) but never activated it. The PPU's cgbMode flag remained false regardless of cartridge type. This change detects CGB-compatible cartridges after ROM loading and enables color mode in the PPU, allowing CGB games to render in color instead of grayscale. Verified with cgb_sound.gb (CGB-only) and dmg_sound.gb (DMG-only).
This commit addresses 6 critical Game Boy Color implementation issues that were preventing proper color rendering and compatibility: 1. **Color Palette Initialization** (src/Ppu/ColorPalette.php) - Fixed palettes initializing to 0xFF instead of 0x7FFF (white) - Palettes now correctly store 15-bit RGB colors as byte pairs - Resolves incorrect default colors in CGB games 2. **WRAM Bank Switching** (src/Memory/Wram.php, src/System/CgbController.php) - Implemented full 32KB WRAM with 8 banks (previously only 8KB) - Added SVBK register (0xFF70) support for bank switching - Bank 0 fixed at 0xC000-0xCFFF, banks 1-7 switchable at 0xD000-0xDFFF - Critical for games like Pokemon Gold/Silver, Link's Awakening DX 3. **Double-Speed Mode** (src/Cpu/Cpu.php, src/Cpu/InstructionSet.php) - Connected CPU STOP instruction to CGB speed switching - Added executeStop() method that triggers speed switch when KEY1 prepared - CPU now references CgbController for speed mode transitions - Enables CGB games requiring double-speed mode to function correctly 4. **HDMA H-Blank Transfers** (src/Ppu/Ppu.php, src/Emulator.php) - Wired PPU H-Blank mode to trigger HDMA controller - Added setHdmaController() for PPU-HDMA communication - H-Blank DMA mode now executes transfers at proper timing - Fixes graphical corruption in games using H-Blank DMA 5. **CGB Background Priority** (src/Ppu/Ppu.php) - Extracted and stored tile attribute bit 7 (BG-to-OAM priority) - Added bgPriorityBuffer to track priority per pixel - Updated sprite rendering to respect CGB priority rules - BG priority bit now correctly overrides sprite priority - Fixes incorrect layer ordering in CGB games 6. **CGB Register DMG Mode Protection** (src/Ppu/Ppu.php) - Color palette registers now return 0xFF when read in DMG mode - Writes to color palette registers ignored in DMG mode - Improves hardware compatibility behavior Testing: - All 431 unit tests pass - CGB mode detection working correctly - DMG backward compatibility preserved Closes issues with grayscale-only rendering, game crashes, graphical glitches, and compatibility with hundreds of CGB titles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The emulator had fully implemented CGB color support (palette system,
PPU rendering, VRAM banking, terminal output) but never activated it.
The PPU's cgbMode flag remained false regardless of cartridge type.
This change detects CGB-compatible cartridges after ROM loading and
enables color mode in the PPU, allowing CGB games to render in color
instead of grayscale.
Verified with cgb_sound.gb (CGB-only) and dmg_sound.gb (DMG-only).