--- BasiliskII/src/AmigaOS/main_amiga.cpp 1999/10/21 22:39:56 1.4 +++ BasiliskII/src/AmigaOS/main_amiga.cpp 2001/06/30 12:58:07 1.16 @@ -1,7 +1,7 @@ /* * main_amiga.cpp - Startup code for AmigaOS * - * Basilisk II (C) 1997-1999 Christian Bauer + * Basilisk II (C) 1997-2001 Christian Bauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -91,12 +91,13 @@ struct Library *GadToolsBase = NULL; struct Library *IFFParseBase = NULL; struct Library *AslBase = NULL; struct Library *P96Base = NULL; +struct Library *CyberGfxBase = NULL; struct Library *TimerBase = NULL; struct Library *AHIBase = NULL; struct Library *DiskBase = NULL; struct Task *MainTask; // Our task -uint32 ScratchMem = NULL; // Scratch memory for Mac ROM writes +uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes APTR OldTrapHandler = NULL; // Old trap handler APTR OldExceptionHandler = NULL; // Old exception handler BYTE IRQSig = -1; // "Interrupt" signal number @@ -107,6 +108,9 @@ static struct timerequest *timereq = NUL static struct MsgPort *ahi_port = NULL; // Port for AHI static struct AHIRequest *ahi_io = NULL; // IORequest for AHI +static struct Process *xpram_proc = NULL; // XPRAM watchdog +static volatile bool xpram_proc_active = true; // Flag for quitting the XPRAM watchdog + static struct Process *tick_proc = NULL; // 60Hz process static volatile bool tick_proc_active = true; // Flag for quitting the 60Hz process @@ -119,16 +123,19 @@ struct trap_regs; extern "C" void AtomicAnd(uint32 *p, uint32 val); extern "C" void AtomicOr(uint32 *p, uint32 val); extern "C" void MoveVBR(void); +extern "C" void DisableSuperBypass(void); extern "C" void TrapHandlerAsm(void); extern "C" void ExceptionHandlerAsm(void); extern "C" void IllInstrHandler(trap_regs *regs); extern "C" void PrivViolHandler(trap_regs *regs); extern "C" void quit_emulator(void); +extern "C" void AsmTriggerNMI(void); uint16 EmulatedSR; // Emulated SR (supervisor bit and interrupt mask) // Prototypes static void jump_to_rom(void); +static void xpram_func(void); static void tick_func(void); @@ -136,7 +143,7 @@ static void tick_func(void); * Main program */ -int main(void) +int main(int argc, char **argv) { // Initialize variables RAMBaseHost = NULL; @@ -180,10 +187,13 @@ int main(void) ErrorAlert(GetString(STR_NO_ASL_LIB_ERR)); QuitEmulator(); } + + // These two can fail (the respective gfx support won't be available, then) P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2); + CyberGfxBase = OpenLibrary((UBYTE *)"cybergraphics.library", 2); // Read preferences - PrefsInit(); + PrefsInit(argc, argv); // Open AHI ahi_port = CreateMsgPort(); @@ -224,7 +234,7 @@ int main(void) TimerBase = (struct Library *)timereq->tr_node.io_Device; // Allocate scratch memory - ScratchMem = (uint32)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); + ScratchMem = (uint8 *)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); if (ScratchMem == NULL) { ErrorAlert(GetString(STR_NO_MEM_ERR)); QuitEmulator(); @@ -238,10 +248,22 @@ int main(void) WarningAlert(GetString(STR_SMALL_RAM_WARN)); RAMSize = 1024*1024; } - RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC); + RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); if (RAMBaseHost == NULL) { - ErrorAlert(GetString(STR_NO_MEM_ERR)); - QuitEmulator(); + uint32 newRAMSize = AvailMem(MEMF_LARGEST) - 0x100000; + char xText[120]; + + sprintf(xText, GetString(STR_NOT_ENOUGH_MEM_WARN), RAMSize, newRAMSize); + + if (ChoiceAlert(xText, "Use", "Quit") != 1) + QuitEmulator(); + + RAMSize = newRAMSize; + RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); + if (RAMBaseHost == NULL) { + ErrorAlert(GetString(STR_NO_MEM_ERR)); + QuitEmulator(); + } } RAMBaseMac = (uint32)RAMBaseHost; D(bug("Mac RAM starts at %08lx\n", RAMBaseHost)); @@ -254,7 +276,7 @@ int main(void) // Load Mac ROM BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE); - if (rom_fh == NULL) { + if (rom_fh == 0) { ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); QuitEmulator(); } @@ -286,6 +308,10 @@ int main(void) // Move VBR away from 0 if neccessary MoveVBR(); + // On 68060, disable Super Bypass mode because of a CPU bug that is triggered by MacOS 8 + if (CPUIs68060) + DisableSuperBypass(); + // Install trap handler EmulatedSR = 0x2700; OldTrapHandler = MainTask->tc_TrapCode; @@ -298,6 +324,14 @@ int main(void) MainTask->tc_ExceptCode = (APTR)ExceptionHandlerAsm; SetExcept(SIGBREAKF_CTRL_C | IRQSigMask, SIGBREAKF_CTRL_C | IRQSigMask); + // Start XPRAM watchdog process + xpram_proc = CreateNewProcTags( + NP_Entry, (ULONG)xpram_func, + NP_Name, (ULONG)"Basilisk II XPRAM Watchdog", + NP_Priority, 0, + TAG_END + ); + // Start 60Hz process tick_proc = CreateNewProcTags( NP_Entry, (ULONG)tick_func, @@ -309,6 +343,8 @@ int main(void) // Set task priority to -1 so we don't use all processing time SetTaskPri(MainTask, -1); + WriteMacInt32(0xbff, 0); // MacsBugFlags + // Swap stack to Mac RAM area stack_swap.stk_Lower = RAMBaseHost; stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize; @@ -343,6 +379,20 @@ void __saveds quit_emulator(void) void QuitEmulator(void) { + // Stop 60Hz process + if (tick_proc) { + SetSignal(0, SIGF_SINGLE); + tick_proc_active = false; + Wait(SIGF_SINGLE); + } + + // Stop XPRAM watchdog process + if (xpram_proc) { + SetSignal(0, SIGF_SINGLE); + xpram_proc_active = false; + Wait(SIGF_SINGLE); + } + // Restore stack if (stack_swapped) { stack_swapped = false; @@ -356,13 +406,6 @@ void QuitEmulator(void) FreeSignal(IRQSig); } - // Stop 60Hz thread - if (tick_proc) { - SetSignal(0, SIGF_SINGLE); - tick_proc_active = false; - Wait(SIGF_SINGLE); - } - // Remove trap handler MainTask->tc_TrapCode = OldTrapHandler; @@ -371,7 +414,7 @@ void QuitEmulator(void) // Delete RAM/ROM area if (RAMBaseHost) - FreeMem(RAMBaseHost, RAMSize + 0x100000); + FreeVec(RAMBaseHost); // Delete scratch memory area if (ScratchMem) @@ -398,6 +441,8 @@ void QuitEmulator(void) PrefsExit(); // Close libraries + if (CyberGfxBase) + CloseLibrary(CyberGfxBase); if (P96Base) CloseLibrary(P96Base); if (AslBase) @@ -447,9 +492,14 @@ void TriggerInterrupt(void) Signal(MainTask, IRQSigMask); } +void TriggerNMI(void) +{ + AsmTriggerNMI(); +} + /* - * 60Hz thread + * 60Hz thread (really 60.15Hz) */ static __saveds void tick_func(void) @@ -489,6 +539,8 @@ static __saveds void tick_func(void) if (++tick_counter > 60) { tick_counter = 0; WriteMacInt32(0x20c, TimerDateTime()); + SetInterruptFlag(INTFLAG_1HZ); + TriggerInterrupt(); } // Trigger 60Hz interrupt @@ -514,6 +566,30 @@ static __saveds void tick_func(void) /* + * XPRAM watchdog thread (saves XPRAM every minute) + */ + +static __saveds void xpram_func(void) +{ + uint8 last_xpram[256]; + memcpy(last_xpram, XPRAM, 256); + + while (xpram_proc_active) { + for (int i=0; i<60 && xpram_proc_active; i++) + Delay(50); // Only wait 1 second so we quit promptly when xpram_proc_active becomes false + if (memcmp(last_xpram, XPRAM, 256)) { + memcpy(last_xpram, XPRAM, 256); + SaveXPRAM(); + } + } + + // Main task asked for termination, send signal + Forbid(); + Signal(MainTask, SIGF_SINGLE); +} + + +/* * Display error alert */ @@ -602,7 +678,7 @@ void __saveds IllInstrHandler(trap_regs EmulatedSR |= 0x0700; // Call opcode routine - EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r); + EmulOp(opcode, (M68kRegisters *)r); r->pc += 2; // Restore interrupts