From 3659603228f4b364657a25c09cdde52512c4a5b0 Mon Sep 17 00:00:00 2001 From: tomcw Date: Wed, 25 Mar 2020 23:05:02 +0000 Subject: [PATCH] Added cmd line arg -rom for 12KB & 16KB roms (#771) --- source/Applewin.cpp | 28 +++++++++++++++++++++---- source/Applewin.h | 3 ++- source/Memory.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++ source/Memory.h | 1 + source/SaveState.cpp | 1 + 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/source/Applewin.cpp b/source/Applewin.cpp index 480d72ef..9863081e 100644 --- a/source/Applewin.cpp +++ b/source/Applewin.cpp @@ -109,8 +109,10 @@ int g_nMemoryClearType = MIP_FF_FF_00_00; // Note: -1 = random MIP in Memory.c CardManager g_CardMgr; IPropertySheet& sg_PropertySheet = * new CPropertySheet; -HANDLE g_hCustomRomF8 = INVALID_HANDLE_VALUE; // Cmd-line specified custom ROM at $F800..$FFFF -static bool g_bCustomRomF8Failed = false; // Set if custom ROM file failed +HANDLE g_hCustomRomF8 = INVALID_HANDLE_VALUE; // Cmd-line specified custom F8 ROM at $F800..$FFFF +static bool g_bCustomRomF8Failed = false; // Set if custom F8 ROM file failed +HANDLE g_hCustomRom = INVALID_HANDLE_VALUE; // Cmd-line specified custom ROM at $C000..$FFFF(16KiB) or $D000..$FFFF(12KiB) +static bool g_bCustomRomFailed = false; // Set if custom ROM file failed static bool g_bEnableSpeech = false; #ifdef USE_SPEECH_API @@ -1541,6 +1543,18 @@ static bool ProcessCmdLine(LPSTR lpCmdLine) if ((g_hCustomRomF8 == INVALID_HANDLE_VALUE) || (GetFileSize(g_hCustomRomF8, NULL) != 0x800)) g_bCustomRomF8Failed = true; } + else if (strcmp(lpCmdLine, "-rom") == 0) // Use custom 16K at [$C000..$FFFF] or 12K ROM at [$D000..$FFFF] + { + lpCmdLine = GetCurrArg(lpNextArg); + lpNextArg = GetNextArg(lpNextArg); + + if (g_hCustomRom != INVALID_HANDLE_VALUE) // Stop resource leak if -rom is specified twice! + CloseHandle(g_hCustomRom); + + g_hCustomRom = CreateFile(lpCmdLine, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); + if ((g_hCustomRom == INVALID_HANDLE_VALUE) || ((GetFileSize(g_hCustomRom, NULL) != 0x4000) && (GetFileSize(g_hCustomRom, NULL) != 0x3000))) + g_bCustomRomFailed = true; + } else if (strcmp(lpCmdLine, "-videorom") == 0) // Use 2K (for II/II+). Use 4K,8K or 16K video ROM (for Enhanced //e) { lpCmdLine = GetCurrArg(lpNextArg); @@ -1957,9 +1971,12 @@ static void RepeatInitialization(void) g_cmdLine.bShutdown = true; } - if (g_bCustomRomF8Failed) + if (g_bCustomRomF8Failed || g_bCustomRomFailed || (g_hCustomRomF8 != INVALID_HANDLE_VALUE && g_hCustomRom != INVALID_HANDLE_VALUE)) { - std::string msg = "Failed to load custom F8 rom (not found or not exactly 2KiB)\n"; + std::string msg = g_bCustomRomF8Failed ? "Failed to load custom F8 rom (not found or not exactly 2KiB)\n" + : g_bCustomRomFailed ? "Failed to load custom rom (not found or not exactly 12KiB or 16KiB)\n" + : "Unsupported -rom and -f8rom being used at the same time\n"; + LogFileOutput("%s", msg.c_str()); MessageBox(g_hFrameWindow, msg.c_str(), TEXT("AppleWin Error"), MB_OK); g_cmdLine.bShutdown = true; @@ -2064,6 +2081,9 @@ static void Shutdown(void) if (g_hCustomRomF8 != INVALID_HANDLE_VALUE) CloseHandle(g_hCustomRomF8); + if (g_hCustomRom != INVALID_HANDLE_VALUE) + CloseHandle(g_hCustomRom); + if (g_cmdLine.bSlot7EmptyOnExit) UnplugHardDiskControllerCard(); } diff --git a/source/Applewin.h b/source/Applewin.h index a340f416..36a12974 100644 --- a/source/Applewin.h +++ b/source/Applewin.h @@ -54,7 +54,8 @@ extern int g_nMemoryClearType; // Cmd line switch: use specific MIP ( extern class CardManager g_CardMgr; -extern HANDLE g_hCustomRomF8; // NULL if no custom rom +extern HANDLE g_hCustomRomF8; // INVALID_HANDLE_VALUE if no custom F8 rom +extern HANDLE g_hCustomRom; // INVALID_HANDLE_VALUE if no custom rom #ifdef USE_SPEECH_API class CSpeech; diff --git a/source/Memory.cpp b/source/Memory.cpp index 4d04cc29..aad2aafa 100644 --- a/source/Memory.cpp +++ b/source/Memory.cpp @@ -1491,6 +1491,7 @@ void MemInitialize() CreateLanguageCard(); MemInitializeROM(); + MemInitializeCustomROM(); MemInitializeCustomF8ROM(); MemInitializeIO(); MemReset(); @@ -1645,6 +1646,54 @@ void MemInitializeCustomF8ROM(void) } } +void MemInitializeCustomROM(void) +{ + if (g_hCustomRom == INVALID_HANDLE_VALUE) + return; + + std::vector oldRom; + oldRom.resize(Apple2RomSize); + memcpy(&oldRom[0], memrom, Apple2RomSize); + + std::vector oldRomC0; + oldRomC0.resize(CxRomSize); + memcpy(&oldRomC0[0], pCxRomInternal, CxRomSize); + + SetFilePointer(g_hCustomRom, 0, NULL, FILE_BEGIN); + DWORD uNumBytesRead; + BOOL bRes = TRUE; + + if (GetFileSize(g_hCustomRom, NULL) == Apple2eRomSize) + { + bRes = ReadFile(g_hCustomRom, pCxRomInternal, CxRomSize, &uNumBytesRead, NULL); + if (uNumBytesRead != CxRomSize) + { + memcpy(pCxRomInternal, &oldRomC0[0], CxRomSize); // ROM at $C000...$CFFF + bRes = FALSE; + } + } + + if (bRes) + { + bRes = ReadFile(g_hCustomRom, memrom, Apple2RomSize, &uNumBytesRead, NULL); + if (uNumBytesRead != Apple2RomSize) + { + memcpy(memrom, &oldRom[0], Apple2RomSize); // ROM at $D000...$FFFF + bRes = FALSE; + } + } + + // NB. If succeeded, then keep g_hCustomRom handle open - so that any next restart can load it again + + if (!bRes) + { + MessageBox( g_hFrameWindow, "Failed to read custom rom", TEXT("AppleWin Error"), MB_OK ); + CloseHandle(g_hCustomRom); + g_hCustomRom = INVALID_HANDLE_VALUE; + // Failed, so use default rom... + } +} + // Called by: // . MemInitialize() // . Snapshot_LoadState_v2() diff --git a/source/Memory.h b/source/Memory.h index 5de37bb5..5f91647f 100644 --- a/source/Memory.h +++ b/source/Memory.h @@ -71,6 +71,7 @@ bool MemOptimizeForModeChanging(WORD programcounter, WORD address); bool MemIsAddrCodeMemory(const USHORT addr); void MemInitialize (); void MemInitializeROM(void); +void MemInitializeCustomROM(void); void MemInitializeCustomF8ROM(void); void MemInitializeIO(void); void MemInitializeCardExpansionRomFromSnapshot(void); diff --git a/source/SaveState.cpp b/source/SaveState.cpp index b83bfd14..33c57d2c 100644 --- a/source/SaveState.cpp +++ b/source/SaveState.cpp @@ -448,6 +448,7 @@ static void Snapshot_LoadState_v2(void) sg_PropertySheet.ApplyNewConfig(m_ConfigNew, ConfigOld); // Mainly just saves (some) new state to Registry MemInitializeROM(); + MemInitializeCustomROM(); MemInitializeCustomF8ROM(); MemInitializeIO(); MemInitializeCardExpansionRomFromSnapshot();