From f491bb19cdabc679cb71eb7ec2b1637b4e40b320 Mon Sep 17 00:00:00 2001
From: tomcw <tomcw@users.noreply.github.com>
Date: Mon, 8 Jun 2020 22:06:17 +0100
Subject: [PATCH] Improved fix for 80eb330 . During init: only need to call
 IoHandlerCardsOut() when loading save-state . MemInitialize() -> MemReset()
 -> ResetPaging() which always inits memmode

---
 source/Memory.cpp    | 22 +++++++---------------
 source/Memory.h      |  2 +-
 source/SaveState.cpp |  4 ++--
 3 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/source/Memory.cpp b/source/Memory.cpp
index 48b71c28..05075352 100644
--- a/source/Memory.cpp
+++ b/source/Memory.cpp
@@ -1499,12 +1499,6 @@ void MemInitialize()
 
 	CreateLanguageCard();
 
-	// Reinit memmode on a restart (eg. h/w config changes)
-	if (IsApple2PlusOrClone(GetApple2Type()) && g_CardMgr.QuerySlot(SLOT0) == CT_Empty)
-		SetMemMode(0);
-	else
-		SetMemMode(LanguageCardUnit::kMemModeInitialState);
-
 	MemInitializeROM();
 	MemInitializeCustomROM();
 	MemInitializeCustomF8ROM();
@@ -1769,19 +1763,17 @@ void MemInitializeIO(void)
 
 	if (g_CardMgr.QuerySlot(SLOT7) == CT_GenericHDD)
 		HD_Load_Rom(pCxRomPeripheral, SLOT7);			// $C700 : HDD f/w
-
-	//
-
-	// Finally remove the cards' ROMs at $Csnn if internal ROM is enabled
-	// . required when restoring saved-state
-	if (IsAppleIIeOrAbove(GetApple2Type()) && SW_INTCXROM)
-		IoHandlerCardsOut();
 }
 
 // Called by:
 // . Snapshot_LoadState_v2()
-void MemInitializeCardExpansionRomFromSnapshot(void)
+void MemInitializeCardSlotAndExpansionRomFromSnapshot(void)
 {
+	// Remove all the cards' ROMs at $Csnn if internal ROM is enabled
+	if (IsAppleIIeOrAbove(GetApple2Type()) && SW_INTCXROM)
+		IoHandlerCardsOut();
+
+	// Potentially init a card's expansion ROM
 	const UINT uSlot = g_uPeripheralRomSlot;
 
 	if (ExpansionRom[uSlot] == NULL)
@@ -1948,7 +1940,7 @@ void MemReset()
 	mem = memimage;
 
 	// INITIALIZE PAGING, FILLING IN THE 64K MEMORY IMAGE
-	ResetPaging(1);		// Initialize=1
+	ResetPaging(TRUE);		// Initialize=1, init memmode
 
 	// INITIALIZE & RESET THE CPU
 	// . Do this after ROM has been copied back to mem[], so that PC is correctly init'ed from 6502's reset vector
diff --git a/source/Memory.h b/source/Memory.h
index 5f91647f..6226b996 100644
--- a/source/Memory.h
+++ b/source/Memory.h
@@ -74,7 +74,7 @@ void    MemInitializeROM(void);
 void    MemInitializeCustomROM(void);
 void    MemInitializeCustomF8ROM(void);
 void    MemInitializeIO(void);
-void    MemInitializeCardExpansionRomFromSnapshot(void);
+void    MemInitializeCardSlotAndExpansionRomFromSnapshot(void);
 BYTE    MemReadFloatingBus(const ULONG uExecutedCycles);
 BYTE    MemReadFloatingBus(const BYTE highbit, const ULONG uExecutedCycles);
 void    MemReset ();
diff --git a/source/SaveState.cpp b/source/SaveState.cpp
index 0e1dc5f3..8a070e60 100644
--- a/source/SaveState.cpp
+++ b/source/SaveState.cpp
@@ -400,7 +400,7 @@ static void Snapshot_LoadState_v2(void)
 		m_ConfigNew.m_bEnableHDD = false;
 		//m_ConfigNew.m_bEnableTheFreezesF8Rom = ?;	// todo: when support saving config
 
-		MemReset();							// Also calls CpuInitialize()
+		MemReset();							// Also calls CpuInitialize(), CNoSlotClock.Reset()
 		PravetsReset();
 
 		if (g_CardMgr.IsSSCInstalled())
@@ -455,7 +455,7 @@ static void Snapshot_LoadState_v2(void)
 		MemInitializeCustomROM();
 		MemInitializeCustomF8ROM();
 		MemInitializeIO();
-		MemInitializeCardExpansionRomFromSnapshot();
+		MemInitializeCardSlotAndExpansionRomFromSnapshot();
 
 		MemUpdatePaging(TRUE);