From 29c02d6bf292454ead2e191b89a79adea145757f Mon Sep 17 00:00:00 2001 From: TomCh Date: Sat, 24 Aug 2024 20:18:28 +0100 Subject: [PATCH] Gh1267 update paging (#1326) MemGetBankPtr(): simplify with a default arg. (#1262, PR #1326) . UpdatePaging(): improve comment for page0 & page1 and memdirty --- source/Debugger/Debug.cpp | 4 ++-- source/Memory.cpp | 39 +++++++++++++++++++++------------------ source/Memory.h | 2 +- source/VidHD.cpp | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/source/Debugger/Debug.cpp b/source/Debugger/Debug.cpp index 9850a6d0..31447d41 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, true) : mem; + BYTE * const pMemBankBase = bBankSpecified ? MemGetBankPtr(nBank) : 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, true) : mem; + const BYTE * const pMemBankBase = bBankSpecified ? MemGetBankPtr(nBank) : mem; if (!pMemBankBase) { ConsoleBufferPush( TEXT( "Error: Bank out of range." ) ); diff --git a/source/Memory.cpp b/source/Memory.cpp index 14bd34cb..a9c15f10 100644 --- a/source/Memory.cpp +++ b/source/Memory.cpp @@ -1269,8 +1269,11 @@ static void UpdatePaging(BOOL initialize) // PAGING SHADOW TABLE // // NB. the condition 'loop <= 1' is there because: - // . Page0 (ZP) : memdirty[0] is set when the 6502 CPU does a ZP-write, but perhaps older versions didn't set this flag (eg. the asm version?). - // . Page1 (stack) : memdirty[1] is NOT set when the 6502 CPU writes to this page with JSR, etc. + // . Page0 (ZP) and Page1 (stack) are written to so often that it's almost certain that they'll be dirty every time this function is called. + // Note also that: + // . Page0 (ZP) : memdirty[0] is set when the 6502 CPU writes to ZP. + // . Page1 (stack) : memdirty[1] is NOT set when the 6502 CPU writes to this page with JSR, PHA, etc. + // Ultimately this is an optimisation (due to Page1 writes not setting memdirty[1]) and Page0 could be optimised to also not set memdirty[0]. for (loop = 0x00; loop < 0x100; loop++) { @@ -1347,19 +1350,6 @@ bool MemCheckINTCXROM() //=========================================================================== -static void BackMainImage(void) -{ - for (UINT loop = 0; loop < 256; loop++) - { - if (memshadow[loop] && ((*(memdirty+loop) & 1) || (loop <= 1))) - memcpy(memshadow[loop], mem+(loop << 8), 256); - - *(memdirty+loop) &= ~1; - } -} - -//=========================================================================== - static LPBYTE MemGetPtrBANK1(const WORD offset, const LPBYTE pMemBase) { if ((offset & 0xF000) != 0xC000) // Requesting RAM at physical addr $Cxxx (ie. 4K RAM BANK1) @@ -1445,14 +1435,27 @@ LPBYTE MemGetMainPtr(const WORD offset) //=========================================================================== +static void BackMainImage(void) +{ + for (UINT loop = 0; loop < 256; loop++) + { + if (memshadow[loop] && ((*(memdirty + loop) & 1) || (loop <= 1))) + memcpy(memshadow[loop], mem + (loop << 8), 256); + + *(memdirty + loop) &= ~1; + } +} + +//------------------------------------- + // Used by: // . Savestate: MemSaveSnapshotMemory(), MemLoadSnapshotAux() // . VidHD : SaveSnapshot(), LoadSnapshot() // . Debugger : CmdMemorySave(), CmdMemoryLoad() -LPBYTE MemGetBankPtr(const UINT nBank, const bool isSaveSnapshotOrDebugging) +LPBYTE MemGetBankPtr(const UINT nBank, const bool isSaveSnapshotOrDebugging/*=true*/) { // 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) + // - for snapshot *loads* it's redundant, and worse it can corrupt pages 0 & 1 for aux banks, so must be avoided (GH#1262) if (isSaveSnapshotOrDebugging) BackMainImage(); // Flush any dirty pages to back-buffer @@ -2412,7 +2415,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, true); + LPBYTE pMemBase = MemGetBankPtr(bank); if (bIsMainMem) { diff --git a/source/Memory.h b/source/Memory.h index a3616148..82b0379b 100644 --- a/source/Memory.h +++ b/source/Memory.h @@ -57,7 +57,7 @@ bool MemCheckSLOTC3ROM(); bool MemCheckINTCXROM(); LPBYTE MemGetAuxPtr(const WORD); LPBYTE MemGetMainPtr(const WORD); -LPBYTE MemGetBankPtr(const UINT nBank, const bool isSaveSnapshotOrDebugging); +LPBYTE MemGetBankPtr(const UINT nBank, const bool isSaveSnapshotOrDebugging = true); LPBYTE MemGetCxRomPeripheral(); DWORD GetMemMode(void); void SetMemMode(DWORD memmode); diff --git a/source/VidHD.cpp b/source/VidHD.cpp index 723fc360..66a05ba8 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, true); + LPBYTE pMemBase = MemGetBankPtr(1); yamlSaveHelper.SaveMemory(pMemBase, (SHR_MEMORY_END + 1) - TEXT_PAGE1_BEGIN, TEXT_PAGE1_BEGIN); } }