From 0e29be87355966dcc432fc0c6d3740d73bb8167d Mon Sep 17 00:00:00 2001 From: Fabrice de Gans Date: Thu, 13 Apr 2023 14:14:06 -0700 Subject: [PATCH] Support multiple RAM sizes for MBC7 We rely on the rom size in the cartridge header as a proxy for the RAM size. Only 3 cartridges exist that use MBC7, Korokoro Kirby / Kirby Tilt'n'Rumble and Command Master. Both versions of Kirby use a 256 bytes EEPROM while Command Master uses a 512 bytes EEPROM. There does not seem to be any other way to reliably get the EEPROM size for MBC7 cartridges. --- src/gb/GB.cpp | 16 +++------------- src/gb/gbCartData.cpp | 14 +++++++++++--- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/gb/GB.cpp b/src/gb/GB.cpp index 734b4110..1d100944 100644 --- a/src/gb/GB.cpp +++ b/src/gb/GB.cpp @@ -58,10 +58,6 @@ struct VBamIoVec { }; std::vector g_vbamIoVecs; -// TODO: This is set to 256 bytes for compatibility with older versions. We -// should figure out how to actually get the MBC7 RAM size. -static constexpr size_t kMbc7RamSizeForSaving = k256B; - void ResetMBC3RTC() { time(&gbDataMBC3.mapperLastTime); struct tm* lt; @@ -472,17 +468,12 @@ bool gbInitializeRom(size_t romSize) { HUC3_RTC_DATA_SIZE, -4, &ResetHuc3RTC}); break; - case gbCartData::MapperType::kMbc7: - // For MBC7, we don't save the RAM data from the same location. - // TODO: Unify handling and find how to get the MBC7 RAM size. - g_vbamIoVecs.clear(); - g_vbamIoVecs.push_back({&gbMemory[0xa000], kMbc7RamSizeForSaving}); - break; case gbCartData::MapperType::kNone: case gbCartData::MapperType::kMbc1: case gbCartData::MapperType::kMbc2: case gbCartData::MapperType::kMbc5: case gbCartData::MapperType::kMbc6: + case gbCartData::MapperType::kMbc7: case gbCartData::MapperType::kHuC1: case gbCartData::MapperType::kMmm01: case gbCartData::MapperType::kPocketCamera: @@ -3181,13 +3172,12 @@ bool gbReadGSASnapshot(const char* fileName) case gbCartData::MapperType::kMbc1: case gbCartData::MapperType::kMbc3: case gbCartData::MapperType::kMbc5: + case gbCartData::MapperType::kMbc7: case gbCartData::MapperType::kHuC1: FREAD_UNCHECKED(gbRam, 1, g_gbCartData.ram_size(), file); break; case gbCartData::MapperType::kMbc2: - case gbCartData::MapperType::kMbc7: - // TODO: Figure out how to get the RAM size for MBC7. - FREAD_UNCHECKED(&gbMemory[0xa000], 1, kMbc7RamSizeForSaving, file); + FREAD_UNCHECKED(&gbMemory[0xa000], 1, k256B, file); break; default: systemMessage(MSG_UNSUPPORTED_SNAPSHOT_FILE, diff --git a/src/gb/gbCartData.cpp b/src/gb/gbCartData.cpp index bf44c4cf..8253b552 100644 --- a/src/gb/gbCartData.cpp +++ b/src/gb/gbCartData.cpp @@ -356,10 +356,18 @@ gbCartData::gbCartData(const uint8_t* romData, size_t romDataSize) { break; case 0x22: // MBC7 header does not specify a RAM size so set it here. - // TODO: Figure out how to get the RAM size for MBC7. It is either - // 256 or 512 Bytes. mapper_type_ = MapperType::kMbc7; - ram_size_ = k512B; + if (header->rom_size == 0x05) { + // Kirby Tilt 'n' Tumble / Korokoro Kirby. + ram_size_ = k256B; + } else if (header->rom_size == 0x06) { + // Command Master. + ram_size_ = k512B; + } else { + // Not a licensed MBC7 cart. Default to the larger size and hope + // for the best. + ram_size_ = k512B; + } skip_ram = true; has_battery_ = true; has_rumble_ = true;