From 52447ad7d9123749eac8713aecf858b0041983f2 Mon Sep 17 00:00:00 2001 From: tomcw Date: Sun, 31 Dec 2023 10:14:47 +0000 Subject: [PATCH] Load save-state: fix page 0 & 1 corruption when loading save-state with a RAMWorks card with 2 or more aux 64K banks (#1262) --- source/6522.h | 2 +- source/Debugger/Debug.cpp | 4 ++-- source/Memory.cpp | 12 ++++++++---- source/Memory.h | 2 +- source/VidHD.cpp | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/source/6522.h b/source/6522.h index 508276a3..7259a956 100644 --- a/source/6522.h +++ b/source/6522.h @@ -49,7 +49,7 @@ public: _ASSERT(0); return 0; } - BYTE GetBusViewOfORB(void) { return m_regs.ORB & m_regs.DDRB; } // Return how the AY8913 sees ORB on the bus (ie. not CPU's view which will be OR'd with !DDRB) + BYTE GetBusViewOfORB(void) { return m_regs.ORB & m_regs.DDRB; } // Return how the AY8913 sees ORB on the bus (ie. not CPU's view which will be OR'd with !DDRB) USHORT GetRegT1C(void) { return m_regs.TIMER1_COUNTER.w; } USHORT GetRegT2C(void) { return m_regs.TIMER2_COUNTER.w; } void GetRegs(BYTE regs[SIZE_6522_REGS]) { memcpy(®s[0], (BYTE*)&m_regs, SIZE_6522_REGS); } // For debugger diff --git a/source/Debugger/Debug.cpp b/source/Debugger/Debug.cpp index d678af8d..7df7736c 100644 --- a/source/Debugger/Debug.cpp +++ b/source/Debugger/Debug.cpp @@ -4493,7 +4493,7 @@ Update_t CmdMemoryLoad (int nArgs) } const std::string sLoadSaveFilePath = g_sCurrentDir + g_sMemoryLoadSaveFileName; // TODO: g_sDebugDir - BYTE * const pMemBankBase = bBankSpecified ? MemGetBankPtr(nBank) : mem; + BYTE * const pMemBankBase = bBankSpecified ? MemGetBankPtr(nBank, true) : mem; if (!pMemBankBase) { ConsoleBufferPush( TEXT( "Error: Bank out of range." ) ); @@ -4832,7 +4832,7 @@ Update_t CmdMemorySave (int nArgs) } sLoadSaveFilePath += g_sMemoryLoadSaveFileName; - const BYTE * const pMemBankBase = bBankSpecified ? MemGetBankPtr(nBank) : mem; + const BYTE * const pMemBankBase = bBankSpecified ? MemGetBankPtr(nBank, true) : mem; if (!pMemBankBase) { ConsoleBufferPush( TEXT( "Error: Bank out of range." ) ); diff --git a/source/Memory.cpp b/source/Memory.cpp index ca170daa..854fa45c 100644 --- a/source/Memory.cpp +++ b/source/Memory.cpp @@ -1436,10 +1436,14 @@ LPBYTE MemGetMainPtr(const WORD offset) // Used by: // . Savestate: MemSaveSnapshotMemory(), MemLoadSnapshotAux() +// . VidHD : SaveSnapshot(), LoadSnapshot() // . Debugger : CmdMemorySave(), CmdMemoryLoad() -LPBYTE MemGetBankPtr(const UINT nBank) +LPBYTE MemGetBankPtr(const UINT nBank, const bool isSaveSnapshotOrDebugging) { - BackMainImage(); // Flush any dirty pages to back-buffer + // Only call BackMainImage() when a consistent 64K bank is needed, eg. for saving snapshot or debugging + // - for snapshot loads it's pointless, and worse it can corrupt pages 0 & 1 for aux banks (GH#1262) + if (isSaveSnapshotOrDebugging) + BackMainImage(); // Flush any dirty pages to back-buffer #ifdef RAMWORKS if (nBank > g_uMaxExPages) @@ -2246,7 +2250,7 @@ static const std::string& MemGetSnapshotAuxMemStructName(void) static void MemSaveSnapshotMemory(YamlSaveHelper& yamlSaveHelper, bool bIsMainMem, UINT bank=0, UINT size=64*1024) { - LPBYTE pMemBase = MemGetBankPtr(bank); + LPBYTE pMemBase = MemGetBankPtr(bank, true); if (bIsMainMem) { @@ -2447,7 +2451,7 @@ static void MemLoadSnapshotAuxCommon(YamlLoadHelper& yamlLoadHelper, const std:: for(UINT uBank = 1; uBank <= g_uMaxExPages; uBank++) { - LPBYTE pBank = MemGetBankPtr(uBank); + LPBYTE pBank = MemGetBankPtr(uBank, false); if (!pBank) { pBank = RWpages[uBank-1] = ALIGNED_ALLOC(_6502_MEM_LEN); diff --git a/source/Memory.h b/source/Memory.h index 1a0ef3c1..2d733463 100644 --- a/source/Memory.h +++ b/source/Memory.h @@ -73,7 +73,7 @@ bool MemCheckSLOTC3ROM(); bool MemCheckINTCXROM(); LPBYTE MemGetAuxPtr(const WORD); LPBYTE MemGetMainPtr(const WORD); -LPBYTE MemGetBankPtr(const UINT nBank); +LPBYTE MemGetBankPtr(const UINT nBank, const bool isSaveSnapshotOrDebugging); LPBYTE MemGetCxRomPeripheral(); DWORD GetMemMode(void); void SetMemMode(DWORD memmode); diff --git a/source/VidHD.cpp b/source/VidHD.cpp index 86924bdb..723fc360 100644 --- a/source/VidHD.cpp +++ b/source/VidHD.cpp @@ -210,7 +210,7 @@ void VidHDCard::SaveSnapshot(YamlSaveHelper& yamlSaveHelper) // Save [$400-$9FFF] YamlSaveHelper::Label state(yamlSaveHelper, "%s:\n", MemGetSnapshotAuxMemStructName().c_str()); - LPBYTE pMemBase = MemGetBankPtr(1); + LPBYTE pMemBase = MemGetBankPtr(1, true); yamlSaveHelper.SaveMemory(pMemBase, (SHR_MEMORY_END + 1) - TEXT_PAGE1_BEGIN, TEXT_PAGE1_BEGIN); } } @@ -232,7 +232,7 @@ bool VidHDCard::LoadSnapshot(YamlLoadHelper& yamlLoadHelper, UINT version) if (!yamlLoadHelper.GetSubMap(MemGetSnapshotAuxMemStructName())) throw std::runtime_error("Memory: Missing map name: " + MemGetSnapshotAuxMemStructName()); - LPBYTE pMemBase = MemGetBankPtr(1); + LPBYTE pMemBase = MemGetBankPtr(1, false); yamlLoadHelper.LoadMemory(pMemBase, (SHR_MEMORY_END + 1) - TEXT_PAGE1_BEGIN, TEXT_PAGE1_BEGIN); yamlLoadHelper.PopMap();