From ee68e9742de3acab9dd0f221cf5a52d3df2defca Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 20 Jan 2022 22:15:03 -0800 Subject: [PATCH] GB: Fix temporary saves --- CHANGES | 1 + src/gb/gb.c | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index d45fafc7e..2a601ccb8 100644 --- a/CHANGES +++ b/CHANGES @@ -25,6 +25,7 @@ Emulation fixes: Other fixes: - Core: Don't attempt to restore rewind diffs past start of rewind - FFmpeg: Fix crash when encoding audio with some containers + - GB: Fix temporary saves Misc: - Core: Suspend runloop when a core crashes - GB Video: Add default SGB border diff --git a/src/gb/gb.c b/src/gb/gb.c index 79a5d60b0..a350f4067 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -202,6 +202,14 @@ void GBResizeSram(struct GB* gb, size_t size) { if (gb->memory.sram) { vf->unmap(vf, gb->memory.sram, gb->sramSize); } + if (vf->size(vf) < gb->sramSize) { + void* sram = vf->map(vf, vf->size(vf), MAP_READ); + struct VFile* newVf = VFileMemChunk(sram, vf->size(vf)); + vf->unmap(vf, sram,vf->size(vf)); + vf = newVf; + gb->sramVf = newVf; + vf->truncate(vf, size); + } gb->memory.sram = vf->map(vf, size, MAP_READ); } if (gb->memory.sram == (void*) -1) { @@ -239,10 +247,12 @@ void GBSramClean(struct GB* gb, uint32_t frameCount) { if (gb->memory.mbcType == GB_MBC3_RTC) { GBMBCRTCWrite(gb); } - if (gb->memory.sram && gb->sramVf->sync(gb->sramVf, gb->memory.sram, gb->sramSize)) { - mLOG(GB_MEM, INFO, "Savedata synced"); - } else { - mLOG(GB_MEM, INFO, "Savedata failed to sync!"); + if (gb->sramVf == gb->sramRealVf) { + if (gb->memory.sram && gb->sramVf->sync(gb->sramVf, gb->memory.sram, gb->sramSize)) { + mLOG(GB_MEM, INFO, "Savedata synced"); + } else { + mLOG(GB_MEM, INFO, "Savedata failed to sync!"); + } } size_t c; @@ -263,7 +273,7 @@ void GBSavedataMask(struct GB* gb, struct VFile* vf, bool writeback) { } gb->sramVf = vf; gb->sramMaskWriteback = writeback; - gb->memory.sram = vf->map(vf, gb->sramSize, MAP_READ); + GBResizeSram(gb, gb->sramSize); GBMBCSwitchSramBank(gb, gb->memory.sramCurrentBank); }