Skip to content

Commit 8acfd25

Browse files
authored
Merge pull request #7 from Jamiras/bugfix/sram_flicker
circumvent mappers for memory exposure
2 parents fd57961 + 8d9ee93 commit 8acfd25

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/wx/retroachievements.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,25 @@ void RA_Init(HWND hWnd)
167167

168168
extern uint8_t gbReadMemory(uint16_t address);
169169
extern void gbWriteMemory(uint16_t address, uint8_t value);
170+
extern uint8_t* gbMemoryMap[16];
170171

171172
// GB/GBC Basic byte reader/writer
172-
static unsigned char ByteReader(unsigned int nOffs) { return gbReadMemory(nOffs); }
173-
static void ByteWriter(unsigned int nOffs, unsigned char nVal) { gbWriteMemory(nOffs, nVal); }
173+
static unsigned char ByteReader(unsigned int nOffs) { return gbMemoryMap[nOffs >> 12][nOffs & 0x0fff]; }
174+
static void ByteWriter(unsigned int nOffs, unsigned char nVal) { gbMemoryMap[nOffs >> 12][nOffs & 0x0fff] = nVal; }
174175

175-
// GBC Byte reader/writer offset by the size of the map until the end of the work RAM
176-
static unsigned char PostRAMByteReader(unsigned int nOffs) { return gbReadMemory(nOffs + 0xE000); }
176+
// GB/GBC Byte reader/writer offset by the size of the map until the end of the work RAM
177+
static unsigned char PostRAMByteReader(unsigned int nOffs)
178+
{
179+
if (nOffs >= 0x1F03 && nOffs <= 0x1F0E)
180+
{
181+
// prevents "Undocumented memory register read" message
182+
if (nOffs == 0x1F03 || nOffs >= 0x1F08)
183+
return 0xFF;
184+
}
185+
return gbReadMemory(nOffs + 0xE000);
186+
}
177187
static void PostRAMByteWriter(unsigned int nOffs, unsigned char nVal) { gbWriteMemory(nOffs + 0xE000, nVal); }
178188

179-
// GBC RAM reader/writer targeting the first bank on work RAM
180-
static unsigned char GBCFirstRAMBankReader(unsigned int nOffs) { return gbWram ? gbWram[nOffs + 0x1000] : 0; }
181-
static void GBCFirstRAMBankWriter(unsigned int nOffs, unsigned char nVal) { if (gbWram) gbWram[nOffs + 0x1000] = nVal; }
182-
183189
// GBC RAM reader/writer targeting work RAM banks 2-7
184190
static unsigned char GBCBankedRAMReader(unsigned int nOffs) { return gbWram ? gbWram[nOffs + 0x2000] : 0; }
185191
static void GBCBankedRAMWriter(unsigned int nOffs, unsigned char nVal) { if (gbWram) gbWram[nOffs + 0x2000] = nVal; }
@@ -200,18 +206,18 @@ void RA_OnLoadNewRom(ConsoleID nConsole, uint8_t* rom, size_t size, const char*
200206
switch (nConsole)
201207
{
202208
case GB:
203-
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0x10000);
209+
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0xE000); // Direct mapping ($0000-$DFFF)
210+
RA_InstallMemoryBank(1, PostRAMByteReader, PostRAMByteWriter, 0x2000); // Echo RAM + controller registers ($E000-$FFFF)
204211
break;
205212

206213
case GBC:
207214
// GBC has a weird quirk where the memory from $D000-$DFFF is a virtual block of memory pointing at one of the work RAM banks.
208215
// Bits 0-2 of $FF70 indicate which bank is currently accessible to the program in the $D000-$DFFF range.
209216
// Since that makes it hard to work with the memory in that region when building/running achievements, the memory exposed to
210217
// the achievements in $D000-$DFFF is always the first bank. The remaining banks are exposed in virtual memory above $10000.
211-
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0xD000); // Direct mapping ($0000-$CFFF)
212-
RA_InstallMemoryBank(1, GBCFirstRAMBankReader, GBCFirstRAMBankWriter, 0x1000); // First bank ($D000-$DFFF)
213-
RA_InstallMemoryBank(2, PostRAMByteReader, PostRAMByteWriter, 0x2000); // Direct mapping ($E000-$FFFF)
214-
RA_InstallMemoryBank(3, GBCBankedRAMReader, GBCBankedRAMWriter, 0x6000); // RAM banks 2-7 ($10000-$15FFF)
218+
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0xE000); // Direct mapping ($0000-$DFFF)
219+
RA_InstallMemoryBank(1, PostRAMByteReader, PostRAMByteWriter, 0x2000); // Echo RAM + controller registers ($E000-$FFFF)
220+
RA_InstallMemoryBank(2, GBCBankedRAMReader, GBCBankedRAMWriter, 0x6000); // RAM banks 2-7 ($10000-$15FFF)
215221
break;
216222

217223
case GBA:

0 commit comments

Comments
 (0)