Skip to content

Commit

Permalink
Merge pull request #7 from Jamiras/bugfix/sram_flicker
Browse files Browse the repository at this point in the history
circumvent mappers for memory exposure
  • Loading branch information
Jamiras authored Apr 6, 2024
2 parents fd57961 + 8d9ee93 commit 8acfd25
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/wx/retroachievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,25 @@ void RA_Init(HWND hWnd)

extern uint8_t gbReadMemory(uint16_t address);
extern void gbWriteMemory(uint16_t address, uint8_t value);
extern uint8_t* gbMemoryMap[16];

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

// GBC Byte reader/writer offset by the size of the map until the end of the work RAM
static unsigned char PostRAMByteReader(unsigned int nOffs) { return gbReadMemory(nOffs + 0xE000); }
// GB/GBC Byte reader/writer offset by the size of the map until the end of the work RAM
static unsigned char PostRAMByteReader(unsigned int nOffs)
{
if (nOffs >= 0x1F03 && nOffs <= 0x1F0E)
{
// prevents "Undocumented memory register read" message
if (nOffs == 0x1F03 || nOffs >= 0x1F08)
return 0xFF;
}
return gbReadMemory(nOffs + 0xE000);
}
static void PostRAMByteWriter(unsigned int nOffs, unsigned char nVal) { gbWriteMemory(nOffs + 0xE000, nVal); }

// GBC RAM reader/writer targeting the first bank on work RAM
static unsigned char GBCFirstRAMBankReader(unsigned int nOffs) { return gbWram ? gbWram[nOffs + 0x1000] : 0; }
static void GBCFirstRAMBankWriter(unsigned int nOffs, unsigned char nVal) { if (gbWram) gbWram[nOffs + 0x1000] = nVal; }

// GBC RAM reader/writer targeting work RAM banks 2-7
static unsigned char GBCBankedRAMReader(unsigned int nOffs) { return gbWram ? gbWram[nOffs + 0x2000] : 0; }
static void GBCBankedRAMWriter(unsigned int nOffs, unsigned char nVal) { if (gbWram) gbWram[nOffs + 0x2000] = nVal; }
Expand All @@ -200,18 +206,18 @@ void RA_OnLoadNewRom(ConsoleID nConsole, uint8_t* rom, size_t size, const char*
switch (nConsole)
{
case GB:
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0x10000);
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0xE000); // Direct mapping ($0000-$DFFF)
RA_InstallMemoryBank(1, PostRAMByteReader, PostRAMByteWriter, 0x2000); // Echo RAM + controller registers ($E000-$FFFF)
break;

case GBC:
// 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.
// Bits 0-2 of $FF70 indicate which bank is currently accessible to the program in the $D000-$DFFF range.
// Since that makes it hard to work with the memory in that region when building/running achievements, the memory exposed to
// the achievements in $D000-$DFFF is always the first bank. The remaining banks are exposed in virtual memory above $10000.
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0xD000); // Direct mapping ($0000-$CFFF)
RA_InstallMemoryBank(1, GBCFirstRAMBankReader, GBCFirstRAMBankWriter, 0x1000); // First bank ($D000-$DFFF)
RA_InstallMemoryBank(2, PostRAMByteReader, PostRAMByteWriter, 0x2000); // Direct mapping ($E000-$FFFF)
RA_InstallMemoryBank(3, GBCBankedRAMReader, GBCBankedRAMWriter, 0x6000); // RAM banks 2-7 ($10000-$15FFF)
RA_InstallMemoryBank(0, ByteReader, ByteWriter, 0xE000); // Direct mapping ($0000-$DFFF)
RA_InstallMemoryBank(1, PostRAMByteReader, PostRAMByteWriter, 0x2000); // Echo RAM + controller registers ($E000-$FFFF)
RA_InstallMemoryBank(2, GBCBankedRAMReader, GBCBankedRAMWriter, 0x6000); // RAM banks 2-7 ($10000-$15FFF)
break;

case GBA:
Expand Down

0 comments on commit 8acfd25

Please sign in to comment.