@@ -33,12 +33,7 @@ DebugTilemapInfo GbPpuTools::GetTilemap(GetTilemapOptions options, BaseState& ba
3333
3434 for (int column = 0 ; column < 32 ; column++) {
3535 uint16_t addr = (baseOffset + column);
36-
37- // This is defined as an int to avoid a bug where MSVC calculates
38- // the tileStart value wrong. The issue doesn't seem to be caused
39- // by the technically undefined behavior of the uint8_t -> int8_t
40- // cast, either. This seems to be the simplest fix.
41- int tileIndex = vram[addr];
36+ uint8_t tileIndex = vram[addr];
4237
4338 uint8_t attributes = isCgb ? vram[addr | 0x2000 ] : 0 ;
4439
@@ -48,7 +43,16 @@ DebugTilemapInfo GbPpuTools::GetTilemap(GetTilemapOptions options, BaseState& ba
4843 bool vMirror = (attributes & 0x40 ) != 0 ;
4944 // bool bgPriority = (attributes & 0x80) != 0;
5045
51- uint16_t tileStart = baseTile + (baseTile ? (int8_t )tileIndex * 16 : tileIndex * 16 );
46+ uint16_t tileStart = baseTile;
47+ if (baseTile) {
48+ // This is done manually to avoid a bug where MSVC calculates the tileStart value wrong.
49+ // The issue doesn't seem to be caused by the technically undefined behavior of casting uint8_t to int8_t.
50+ // Calculating the negative value manually works, so use that for now.
51+ tileStart += (tileIndex >= 0x80 ? -(0x80 - (tileIndex & 0x7F )) : tileIndex) * 16 ;
52+ } else {
53+ tileStart += tileIndex * 16 ;
54+ }
55+
5256 tileStart |= tileBank;
5357
5458 for (int y = 0 ; y < 8 ; y++) {
0 commit comments