diff --git a/src/gb/core.c b/src/gb/core.c index a351e4ef0..f5e157cb1 100644 --- a/src/gb/core.c +++ b/src/gb/core.c @@ -170,6 +170,12 @@ static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) { return GBLoadSave(core->board, vf); } +static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) { + struct GB* gb = core->board; + GBSavedataMask(gb, vf); + return true; // TODO: Return a real value +} + static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) { if (!vf) { return false; @@ -459,6 +465,7 @@ struct mCore* GBCoreCreate(void) { core->loadROM = _GBCoreLoadROM; core->loadBIOS = _GBCoreLoadBIOS; core->loadSave = _GBCoreLoadSave; + core->loadTemporarySave = _GBCoreLoadTemporarySave; core->loadPatch = _GBCoreLoadPatch; core->unloadROM = _GBCoreUnloadROM; core->reset = _GBCoreReset; diff --git a/src/gb/gb.c b/src/gb/gb.c index f8f762603..c8e3ec3c5 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -99,6 +99,7 @@ bool GBLoadROM(struct GB* gb, struct VFile* vf) { bool GBLoadSave(struct GB* gb, struct VFile* vf) { gb->sramVf = vf; + gb->sramRealVf = vf; if (vf) { // TODO: Do this in bank-switching code if (vf->size(vf) < 0x20000) { @@ -109,6 +110,31 @@ bool GBLoadSave(struct GB* gb, struct VFile* vf) { return gb->memory.sram; } +static void GBSramDeinit(struct GB* gb) { + if (gb->sramVf) { + gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x20000); + gb->sramVf = 0; + } else if (gb->memory.sram) { + mappedMemoryFree(gb->memory.sram, 0x20000); + } + gb->memory.sram = 0; +} + +void GBSavedataMask(struct GB* gb, struct VFile* vf) { + GBSramDeinit(gb); + gb->sramVf = vf; + gb->memory.sram = vf->map(vf, 0x20000, MAP_READ); +} + +void GBSavedataUnmask(struct GB* gb) { + GBSramDeinit(gb); + if (gb->sramVf == gb->sramRealVf) { + return; + } + gb->sramVf = gb->sramRealVf; + gb->memory.sram = gb->sramVf->map(gb->sramVf, 0x20000, MAP_WRITE); +} + void GBUnloadROM(struct GB* gb) { // TODO: Share with GBAUnloadROM if (gb->memory.rom && gb->memory.romBase != gb->memory.rom) { @@ -131,13 +157,7 @@ void GBUnloadROM(struct GB* gb) { gb->romVf = 0; } - if (gb->sramVf) { - gb->sramVf->unmap(gb->sramVf, gb->memory.sram, 0x8000); - gb->sramVf = 0; - } else if (gb->memory.sram) { - mappedMemoryFree(gb->memory.sram, 0x8000); - } - gb->memory.sram = 0; + GBSramDeinit(gb); } void GBLoadBIOS(struct GB* gb, struct VFile* vf) { @@ -262,6 +282,8 @@ void GBReset(struct LR35902Core* cpu) { GBIOReset(gb); GBAudioReset(&gb->audio); GBSIOReset(&gb->sio); + + GBSavedataUnmask(gb); } void GBUpdateIRQs(struct GB* gb) { diff --git a/src/gb/gb.h b/src/gb/gb.h index 6f28e66bd..0cfcc730c 100644 --- a/src/gb/gb.h +++ b/src/gb/gb.h @@ -66,6 +66,7 @@ struct GB { struct VFile* romVf; struct VFile* biosVf; struct VFile* sramVf; + struct VFile* sramRealVf; struct mAVStream* stream; @@ -113,6 +114,9 @@ void GBUnloadROM(struct GB* gb); void GBLoadBIOS(struct GB* gb, struct VFile* vf); +void GBSavedataMask(struct GB* gb, struct VFile* vf); +void GBSavedataUnmask(struct GB* gb); + struct Patch; void GBApplyPatch(struct GB* gb, struct Patch* patch); diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index acb4e48d6..a8f4e5cb0 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -891,7 +891,6 @@ void Window::setupMenu(QMenuBar* menubar) { QAction* loadTemporarySave = new QAction(tr("Load temporary save..."), fileMenu); connect(loadTemporarySave, &QAction::triggered, [this]() { this->selectSave(true); }); m_gameActions.append(loadTemporarySave); - m_gbaActions.append(loadTemporarySave); addControlledAction(fileMenu, loadTemporarySave, "loadTemporarySave"); addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");