From 6fb5b3b0e8c511251d4a18b6cb4141b2f0764e16 Mon Sep 17 00:00:00 2001 From: tomcw Date: Sat, 10 Nov 2018 10:30:19 +0000 Subject: [PATCH] Refactor: consolidate 'modechanging' optimisations into a single function --- source/LanguageCard.cpp | 18 ++------------ source/Memory.cpp | 53 +++++++++++++++++++++++++---------------- source/Memory.h | 2 +- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/source/LanguageCard.cpp b/source/LanguageCard.cpp index 2ae13fc3..24d3c64e 100644 --- a/source/LanguageCard.cpp +++ b/source/LanguageCard.cpp @@ -85,22 +85,8 @@ BYTE __stdcall LanguageCardUnit::IO(WORD PC, WORD uAddr, BYTE bWrite, BYTE uValu // - if (IS_APPLE2E) - { - // IF THE EMULATED PROGRAM HAS JUST UPDATED THE MEMORY WRITE MODE AND IS - // ABOUT TO UPDATE THE MEMORY READ MODE, HOLD OFF ON ANY PROCESSING UNTIL - // IT DOES SO. - // - // NB. A 6502 interrupt occurring between these memory write & read updates could lead to incorrect behaviour. - // - although any data-race is probably a bug in the 6502 code too. - if ((PC < 0xC000) && - (((*(LPDWORD)(mem+PC) & 0x00FFFEFF) == 0x00C0048D) || // Next: STA $C004 or STA $C005 - ((*(LPDWORD)(mem+PC) & 0x00FFFEFF) == 0x00C0028D))) // Next: STA $C002 or STA $C003 - { - SetModeChanging(1); - return bWrite ? 0 : MemReadFloatingBus(nExecutedCycles); - } - } + if (MemOptimizeForModeChanging(PC, uAddr)) + return bWrite ? 0 : MemReadFloatingBus(nExecutedCycles); // IF THE MEMORY PAGING MODE HAS CHANGED, UPDATE OUR MEMORY IMAGES AND // WRITE TABLES. diff --git a/source/Memory.cpp b/source/Memory.cpp index da8caa25..0799589e 100644 --- a/source/Memory.cpp +++ b/source/Memory.cpp @@ -972,11 +972,6 @@ static bool IsCardInSlot(const UINT uSlot) //=========================================================================== -void SetModeChanging(BOOL value) -{ - modechanging = value; -} - DWORD GetMemMode(void) { return memmode; @@ -1953,21 +1948,8 @@ BYTE __stdcall MemSetPaging(WORD programcounter, WORD address, BYTE write, BYTE } } - if (IS_APPLE2E) - { - // IF THE EMULATED PROGRAM HAS JUST UPDATED THE MEMORY WRITE MODE AND IS - // ABOUT TO UPDATE THE MEMORY READ MODE, HOLD OFF ON ANY PROCESSING UNTIL - // IT DOES SO. - // - // NB. A 6502 interrupt occurring between these memory write & read updates could lead to incorrect behaviour. - // - although any data-race is probably a bug in the 6502 code too. - if ((address >= 4) && (address <= 5) && - ((*(LPDWORD)(mem+programcounter) & 0x00FFFEFF) == 0x00C0028D)) // Next: STA $C002 or STA $C003 - { - modechanging = 1; - return 0; // For $C004 & $C005: entry to this func is always via a write to $C004 or $C005 - } - } + if (MemOptimizeForModeChanging(programcounter, address)) + return write ? 0 : MemReadFloatingBus(nExecutedCycles); // IF THE MEMORY PAGING MODE HAS CHANGED, UPDATE OUR MEMORY IMAGES AND // WRITE TABLES. @@ -2012,6 +1994,37 @@ BYTE __stdcall MemSetPaging(WORD programcounter, WORD address, BYTE write, BYTE //=========================================================================== +bool MemOptimizeForModeChanging(WORD programcounter, WORD address) +{ + if (IS_APPLE2E) + { + // IF THE EMULATED PROGRAM HAS JUST UPDATED THE MEMORY WRITE MODE AND IS + // ABOUT TO UPDATE THE MEMORY READ MODE, HOLD OFF ON ANY PROCESSING UNTIL + // IT DOES SO. + // + // NB. A 6502 interrupt occurring between these memory write & read updates could lead to incorrect behaviour. + // - although any data-race is probably a bug in the 6502 code too. + if ((address >= 4) && (address <= 5) && // Now: RAMWRTOFF or RAMWRTON + ((*(LPDWORD)(mem+programcounter) & 0x00FFFEFF) == 0x00C0028D)) // Next: STA $C002(RAMRDOFF) or STA $C003(RAMRDON) + { + modechanging = 1; + return true; + } + + if ((address >= 0x80) && (address <= 0x8F) && (programcounter < 0xC000) && // Now: LC + (((*(LPDWORD)(mem+programcounter) & 0x00FFFEFF) == 0x00C0048D) || // Next: STA $C004(RAMWRTOFF) or STA $C005(RAMWRTON) + ((*(LPDWORD)(mem+programcounter) & 0x00FFFEFF) == 0x00C0028D))) // or STA $C002(RAMRDOFF) or STA $C003(RAMRDON) + { + modechanging = 1; + return true; + } + } + + return false; +} + +//=========================================================================== + LPVOID MemGetSlotParameters(UINT uSlot) { _ASSERT(uSlot < NUM_SLOTS); diff --git a/source/Memory.h b/source/Memory.h index 6538ae01..7ae9c469 100644 --- a/source/Memory.h +++ b/source/Memory.h @@ -66,7 +66,7 @@ LPBYTE MemGetBankPtr(const UINT nBank); LPBYTE MemGetCxRomPeripheral(); DWORD GetMemMode(void); void SetMemMode(DWORD memmode); -void SetModeChanging(BOOL value); +bool MemOptimizeForModeChanging(WORD programcounter, WORD address); bool MemIsAddrCodeMemory(const USHORT addr); void MemInitialize (); void MemInitializeROM(void);