Skip to content

Commit 16676b6

Browse files
trekawekclaude
andcommitted
Blank the background in DMG-compat mode when LCDC.0 is off
A DMG ROM running on the CGB gets the DMG semantics for LCDC.0: the background blanks to color 0 (through the compat BGP remap). Only in CGB-native mode does the bit merely drop the background's priority below the objects. Previously the compat path used the CGB-native master-priority behavior, so mid-line LCDC.0 toggles were invisible. Mealybug m3_lcdc_bg_en_change on the CGB core: 2250 -> 407 diff pixels vs the CPU CGB D photo. Full battery and controller suite green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J1wLWscyUGS7CFwc3CjJR8
1 parent adca532 commit 16676b6

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

core/src/main/java/eu/rekawek/coffeegb/core/gpu/ColorPixelFifo.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,17 @@ private int resolvePixel(int entry) {
101101
int spritePalette = (entry >> 8) & 0b111;
102102
boolean spriteBgPriority = (entry & (1 << 11)) != 0;
103103

104+
// in DMG compatibility mode LCDC.0 blanks the background like on the DMG;
105+
// in CGB mode it only drops the background's priority
106+
boolean compatMode = speedMode != null && speedMode.isDmgCompat();
107+
if (compatMode && !lcdc.isBgAndWindowDisplay()) {
108+
bgPixel = 0;
109+
bgAttrPriority = false;
110+
}
111+
104112
boolean drawSprite = false;
105113
if (spritePixel != 0 && lcdc.isObjDisplay()) {
106-
if (!lcdc.isBgAndWindowDisplay()) {
114+
if (!lcdc.isBgAndWindowDisplay() && !compatMode) {
107115
// "master priority": sprites always on top
108116
drawSprite = true;
109117
} else if (bgAttrPriority) {
@@ -116,17 +124,16 @@ private int resolvePixel(int entry) {
116124
}
117125
// in DMG compatibility mode the BGP/OBPx registers remap the color index before
118126
// the palette RAM lookup (the boot ROM loads the compatibility colors there)
119-
boolean compat = speedMode != null && speedMode.isDmgCompat();
120127
if (drawSprite) {
121128
int pixel = spritePixel;
122-
if (compat) {
129+
if (compatMode) {
123130
int obp = r.get(spritePalette == 0 ? GpuRegister.OBP0 : GpuRegister.OBP1);
124131
pixel = (obp >> (pixel * 2)) & 0b11;
125132
}
126133
return oamPalette.getPalette(spritePalette)[pixel];
127134
} else {
128135
int pixel = bgPixel;
129-
if (compat) {
136+
if (compatMode) {
130137
pixel = (r.get(GpuRegister.BGP) >> (pixel * 2)) & 0b11;
131138
}
132139
return bgPalette.getPalette(bgPaletteIndex)[pixel];

0 commit comments

Comments
 (0)