[GB] Save MBC7 EEPROM data to `gbRam`

Previously, MBC7 EEPROM was saved at address 0xa000 in memory, rather
than inside the `gbRam` data buffer. This was inconsistent with other
mappers, resulting in issues like the EEPROM data being cleared on
`gbReset`.

Fixes #1173
This commit is contained in:
Fabrice de Gans 2023-08-27 15:46:12 -07:00
parent 24b6ac5a28
commit 803ab35269
3 changed files with 15 additions and 9 deletions

View File

@ -422,7 +422,8 @@ bool gbInitializeRom(size_t romSize) {
const size_t ramSize = g_gbCartData.ram_size(); const size_t ramSize = g_gbCartData.ram_size();
if (g_gbCartData.HasRam()) { if (g_gbCartData.HasRam()) {
gbRam = (uint8_t*)malloc(ramSize); // Always allocate 4 KiB to prevent access issues down the line.
gbRam = (uint8_t*)malloc(std::max(k4KiB, ramSize));
if (gbRam == nullptr) { if (gbRam == nullptr) {
return false; return false;
} }
@ -3062,6 +3063,9 @@ void gbReset()
memset(&gbDataHuC1, 0, sizeof(gbDataHuC1)); memset(&gbDataHuC1, 0, sizeof(gbDataHuC1));
gbDataHuC1.mapperROMBank = 1; gbDataHuC1.mapperROMBank = 1;
memset(&gbDataMBC7, 0, sizeof(gbDataMBC7));
gbDataMBC7.mapperROMBank = 1;
memset(&gbDataHuC3, 0, sizeof(gbDataHuC3)); memset(&gbDataHuC3, 0, sizeof(gbDataHuC3));
gbDataHuC3.mapperROMBank = 1; gbDataHuC3.mapperROMBank = 1;
gbDataHuC3.mapperRAMValue = 1; gbDataHuC3.mapperRAMValue = 1;

View File

@ -640,8 +640,9 @@ void mapperMBC7ROM(uint16_t address, uint8_t value)
if (value < 8) { if (value < 8) {
tmpAddress = (value & 3) << 13; tmpAddress = (value & 3) << 13;
tmpAddress &= g_gbCartData.ram_mask(); tmpAddress &= g_gbCartData.ram_mask();
gbMemoryMap[0x0a] = &gbMemory[0xa000]; gbMemoryMap[0x0a] = &gbRam[0];
gbMemoryMap[0x0b] = &gbMemory[0xb000]; // Only one RAM bank for MBC7 so wrap around.
gbMemoryMap[0x0b] = &gbRam[0];
gbDataMBC7.mapperRAMBank = value; gbDataMBC7.mapperRAMBank = value;
gbDataMBC7.mapperRAMAddress = tmpAddress; gbDataMBC7.mapperRAMAddress = tmpAddress;
@ -694,8 +695,8 @@ void mapperMBC7RAM(uint16_t address, uint8_t value)
if (!oldCs && gbDataMBC7.cs) { if (!oldCs && gbDataMBC7.cs) {
if (gbDataMBC7.state == 5) { if (gbDataMBC7.state == 5) {
if (gbDataMBC7.writeEnable) { if (gbDataMBC7.writeEnable) {
gbMemory[0xa000 + gbDataMBC7.address * 2] = gbDataMBC7.buffer >> 8; gbRam[gbDataMBC7.address * 2] = gbDataMBC7.buffer >> 8;
gbMemory[0xa000 + gbDataMBC7.address * 2 + 1] = gbDataMBC7.buffer & 0xff; gbRam[gbDataMBC7.address * 2 + 1] = gbDataMBC7.buffer & 0xff;
systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED;
} }
gbDataMBC7.state = 0; gbDataMBC7.state = 0;
@ -762,8 +763,8 @@ void mapperMBC7RAM(uint16_t address, uint8_t value)
} else if ((gbDataMBC7.address >> 6) == 1) { } else if ((gbDataMBC7.address >> 6) == 1) {
if (gbDataMBC7.writeEnable) { if (gbDataMBC7.writeEnable) {
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
gbMemory[0xa000 + i * 2] = gbDataMBC7.buffer >> 8; gbRam[i * 2] = gbDataMBC7.buffer >> 8;
gbMemory[0xa000 + i * 2 + 1] = gbDataMBC7.buffer & 0xff; gbRam[i * 2 + 1] = gbDataMBC7.buffer & 0xff;
systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED;
} }
} }
@ -771,7 +772,7 @@ void mapperMBC7RAM(uint16_t address, uint8_t value)
} else if ((gbDataMBC7.address >> 6) == 2) { } else if ((gbDataMBC7.address >> 6) == 2) {
if (gbDataMBC7.writeEnable) { if (gbDataMBC7.writeEnable) {
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
WRITE16LE((uint16_t*)&gbMemory[0xa000 + i * 2], 0xffff); WRITE16LE((uint16_t*)&gbRam[i * 2], 0xffff);
systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED; systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED;
} }
gbDataMBC7.state = 5; gbDataMBC7.state = 5;
@ -793,7 +794,7 @@ void mapperMBC7RAM(uint16_t address, uint8_t value)
if (gbDataMBC7.count == 1) { if (gbDataMBC7.count == 1) {
gbDataMBC7.state = 4; gbDataMBC7.state = 4;
gbDataMBC7.count = 0; gbDataMBC7.count = 0;
gbDataMBC7.buffer = (gbMemory[0xa000 + gbDataMBC7.address * 2] << 8) | (gbMemory[0xa000 + gbDataMBC7.address * 2 + 1]); gbDataMBC7.buffer = (gbRam[gbDataMBC7.address * 2] << 8) | (gbRam[gbDataMBC7.address * 2 + 1]);
} }
break; break;
case 3: case 3:

View File

@ -157,6 +157,7 @@ extern mapperMBC1 gbDataMBC1;
extern mapperMBC2 gbDataMBC2; extern mapperMBC2 gbDataMBC2;
extern mapperMBC3 gbDataMBC3; extern mapperMBC3 gbDataMBC3;
extern mapperMBC5 gbDataMBC5; extern mapperMBC5 gbDataMBC5;
extern mapperMBC7 gbDataMBC7;
extern mapperHuC1 gbDataHuC1; extern mapperHuC1 gbDataHuC1;
extern mapperHuC3 gbDataHuC3; extern mapperHuC3 gbDataHuC3;
extern mapperHuC3RTC gbRTCHuC3; extern mapperHuC3RTC gbRTCHuC3;