Skip to content

Commit

Permalink
[GB] Save MBC7 EEPROM data to gbRam
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Steelskin committed Aug 28, 2023
1 parent 24b6ac5 commit 803ab35
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/gb/GB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ bool gbInitializeRom(size_t romSize) {

const size_t ramSize = g_gbCartData.ram_size();
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) {
return false;
}
Expand Down Expand Up @@ -3062,6 +3063,9 @@ void gbReset()
memset(&gbDataHuC1, 0, sizeof(gbDataHuC1));
gbDataHuC1.mapperROMBank = 1;

memset(&gbDataMBC7, 0, sizeof(gbDataMBC7));
gbDataMBC7.mapperROMBank = 1;

memset(&gbDataHuC3, 0, sizeof(gbDataHuC3));
gbDataHuC3.mapperROMBank = 1;
gbDataHuC3.mapperRAMValue = 1;
Expand Down
17 changes: 9 additions & 8 deletions src/gb/gbMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,9 @@ void mapperMBC7ROM(uint16_t address, uint8_t value)
if (value < 8) {
tmpAddress = (value & 3) << 13;
tmpAddress &= g_gbCartData.ram_mask();
gbMemoryMap[0x0a] = &gbMemory[0xa000];
gbMemoryMap[0x0b] = &gbMemory[0xb000];
gbMemoryMap[0x0a] = &gbRam[0];
// Only one RAM bank for MBC7 so wrap around.
gbMemoryMap[0x0b] = &gbRam[0];

gbDataMBC7.mapperRAMBank = value;
gbDataMBC7.mapperRAMAddress = tmpAddress;
Expand Down Expand Up @@ -694,8 +695,8 @@ void mapperMBC7RAM(uint16_t address, uint8_t value)
if (!oldCs && gbDataMBC7.cs) {
if (gbDataMBC7.state == 5) {
if (gbDataMBC7.writeEnable) {
gbMemory[0xa000 + gbDataMBC7.address * 2] = gbDataMBC7.buffer >> 8;
gbMemory[0xa000 + gbDataMBC7.address * 2 + 1] = gbDataMBC7.buffer & 0xff;
gbRam[gbDataMBC7.address * 2] = gbDataMBC7.buffer >> 8;
gbRam[gbDataMBC7.address * 2 + 1] = gbDataMBC7.buffer & 0xff;
systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED;
}
gbDataMBC7.state = 0;
Expand Down Expand Up @@ -762,16 +763,16 @@ void mapperMBC7RAM(uint16_t address, uint8_t value)
} else if ((gbDataMBC7.address >> 6) == 1) {
if (gbDataMBC7.writeEnable) {
for (int i = 0; i < 256; i++) {
gbMemory[0xa000 + i * 2] = gbDataMBC7.buffer >> 8;
gbMemory[0xa000 + i * 2 + 1] = gbDataMBC7.buffer & 0xff;
gbRam[i * 2] = gbDataMBC7.buffer >> 8;
gbRam[i * 2 + 1] = gbDataMBC7.buffer & 0xff;
systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED;
}
}
gbDataMBC7.state = 5;
} else if ((gbDataMBC7.address >> 6) == 2) {
if (gbDataMBC7.writeEnable) {
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;
}
gbDataMBC7.state = 5;
Expand All @@ -793,7 +794,7 @@ void mapperMBC7RAM(uint16_t address, uint8_t value)
if (gbDataMBC7.count == 1) {
gbDataMBC7.state = 4;
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;
case 3:
Expand Down
1 change: 1 addition & 0 deletions src/gb/gbMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ extern mapperMBC1 gbDataMBC1;
extern mapperMBC2 gbDataMBC2;
extern mapperMBC3 gbDataMBC3;
extern mapperMBC5 gbDataMBC5;
extern mapperMBC7 gbDataMBC7;
extern mapperHuC1 gbDataHuC1;
extern mapperHuC3 gbDataHuC3;
extern mapperHuC3RTC gbRTCHuC3;
Expand Down

0 comments on commit 803ab35

Please sign in to comment.