GB MBC: Fix initialization and swapping
Vicki Pfau vi@endrift.com
Fri, 02 Jun 2017 11:26:53 -0700
4 files changed,
22 insertions(+),
13 deletions(-)
M
src/gb/gb.c
→
src/gb/gb.c
@@ -124,7 +124,7 @@ gb->yankedRomSize = 0;
gb->memory.romBase = gb->memory.rom; gb->memory.romSize = gb->pristineRomSize; gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize); - GBMBCSwitchBank(gb, gb->memory.currentBank); + GBMBCInit(gb); if (gb->cpu) { struct LR35902Core* cpu = gb->cpu;@@ -135,12 +135,6 @@ // TODO: error check
return true; } -bool GBLoadSave(struct GB* gb, struct VFile* vf) { - gb->sramVf = vf; - gb->sramRealVf = vf; - return vf; -} - static void GBSramDeinit(struct GB* gb) { if (gb->sramVf) { gb->sramVf->unmap(gb->sramVf, gb->memory.sram, gb->sramSize);@@ -152,6 +146,16 @@ } else if (gb->memory.sram) {
mappedMemoryFree(gb->memory.sram, gb->sramSize); } gb->memory.sram = 0; +} + +bool GBLoadSave(struct GB* gb, struct VFile* vf) { + GBSramDeinit(gb); + gb->sramVf = vf; + gb->sramRealVf = vf; + if (gb->sramSize) { + GBResizeSram(gb, gb->sramSize); + } + return vf; } void GBResizeSram(struct GB* gb, size_t size) {@@ -280,6 +284,7 @@ gb->romVf->close(gb->romVf);
gb->romVf = NULL; } gb->memory.rom = NULL; + gb->memory.mbcType = GB_MBC_AUTODETECT; gb->isPristine = false; GBSavedataUnmask(gb);
M
src/gb/mbc.c
→
src/gb/mbc.c
@@ -204,6 +204,14 @@ gb->memory.mbc = _GBMBC5;
break; } + gb->memory.currentBank = 1; + gb->memory.sramCurrentBank = 0; + gb->memory.sramAccess = false; + gb->memory.rtcAccess = false; + gb->memory.activeRtcReg = 0; + gb->memory.rtcLatched = false; + memset(&gb->memory.rtcRegs, 0, sizeof(gb->memory.rtcRegs)); + GBResizeSram(gb, gb->sramSize); if (gb->memory.mbcType == GB_MBC3_RTC) {
M
src/gb/memory.c
→
src/gb/memory.c
@@ -158,12 +158,6 @@ gb->memory.hdmaEvent.name = "GB HDMA";
gb->memory.hdmaEvent.callback = _GBMemoryHDMAService; gb->memory.hdmaEvent.priority = 0x41; - gb->memory.sramAccess = false; - gb->memory.rtcAccess = false; - gb->memory.activeRtcReg = 0; - gb->memory.rtcLatched = false; - memset(&gb->memory.rtcRegs, 0, sizeof(gb->memory.rtcRegs)); - memset(&gb->memory.hram, 0, sizeof(gb->memory.hram)); switch (gb->memory.mbcType) { case GB_MBC1:
M
src/gb/overrides.c
→
src/gb/overrides.c
@@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba/internal/gb/overrides.h> #include <mgba/internal/gb/gb.h> +#include <mgba/internal/gb/mbc.h> #include <mgba-util/configuration.h> #include <mgba-util/crc32.h>@@ -102,6 +103,7 @@ }
if (override->mbc != GB_MBC_AUTODETECT) { gb->memory.mbcType = override->mbc; + GBMBCInit(gb); } }