--- BasiliskII/src/AmigaOS/main_amiga.cpp 2000/07/06 16:04:24 1.6 +++ BasiliskII/src/AmigaOS/main_amiga.cpp 2002/09/01 12:01:46 1.23 @@ -1,7 +1,7 @@ /* * main_amiga.cpp - Startup code for AmigaOS * - * Basilisk II (C) 1997-2000 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 @@ -26,9 +26,13 @@ #include #include #include +#define __USE_SYSBASE #include #include #include +#include +#include +#include #include "sysdeps.h" #include "cpu_emulation.h" @@ -97,7 +101,7 @@ 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 @@ -108,6 +112,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 @@ -120,16 +127,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); @@ -137,7 +147,7 @@ static void tick_func(void); * Main program */ -int main(void) +int main(int argc, char **argv) { // Initialize variables RAMBaseHost = NULL; @@ -152,42 +162,47 @@ int main(void) printf(" %s\n", GetString(STR_ABOUT_TEXT2)); // Open libraries - GfxBase = OpenLibrary((UBYTE *)"graphics.library", 39); + GfxBase = OpenLibrary((UBYTE *) "graphics.library", 39); if (GfxBase == NULL) { printf("Cannot open graphics.library V39.\n"); exit(1); } - IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39); + IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *) "intuition.library", 39); if (IntuitionBase == NULL) { printf("Cannot open intuition.library V39.\n"); CloseLibrary(GfxBase); exit(1); } - DiskBase = (struct Library *)OpenResource((UBYTE *)"disk.resource"); + DiskBase = (struct Library *)OpenResource((UBYTE *) "disk.resource"); if (DiskBase == NULL) QuitEmulator(); - GadToolsBase = OpenLibrary((UBYTE *)"gadtools.library", 39); + GadToolsBase = OpenLibrary((UBYTE *) "gadtools.library", 39); if (GadToolsBase == NULL) { - ErrorAlert(GetString(STR_NO_GADTOOLS_LIB_ERR)); + ErrorAlert(STR_NO_GADTOOLS_LIB_ERR); QuitEmulator(); } - IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library", 39); + IFFParseBase = OpenLibrary((UBYTE *) "iffparse.library", 39); if (IFFParseBase == NULL) { - ErrorAlert(GetString(STR_NO_IFFPARSE_LIB_ERR)); + ErrorAlert(STR_NO_IFFPARSE_LIB_ERR); QuitEmulator(); } - AslBase = OpenLibrary((UBYTE *)"asl.library", 36); + AslBase = OpenLibrary((UBYTE *) "asl.library", 36); if (AslBase == NULL) { - ErrorAlert(GetString(STR_NO_ASL_LIB_ERR)); + ErrorAlert(STR_NO_ASL_LIB_ERR); + QuitEmulator(); + } + + if (FindTask((UBYTE *) "« Enforcer »")) { + ErrorAlert(STR_ENFORCER_RUNNING_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); + P96Base = OpenLibrary((UBYTE *) "Picasso96API.library", 2); + CyberGfxBase = OpenLibrary((UBYTE *) "cybergraphics.library", 2); // Read preferences - PrefsInit(); + PrefsInit(argc, argv); // Open AHI ahi_port = CreateMsgPort(); @@ -195,7 +210,7 @@ int main(void) ahi_io = (struct AHIRequest *)CreateIORequest(ahi_port, sizeof(struct AHIRequest)); if (ahi_io) { ahi_io->ahir_Version = 2; - if (OpenDevice((UBYTE *)AHINAME, AHI_NO_UNIT, (struct IORequest *)ahi_io, 0) == 0) { + if (OpenDevice((UBYTE *) AHINAME, AHI_NO_UNIT, (struct IORequest *)ahi_io, 0) == 0) { AHIBase = (struct Library *)ahi_io->ahir_Std.io_Device; } } @@ -210,27 +225,27 @@ int main(void) QuitEmulator(); // Check start of Chip memory (because we need access to 0x0000..0x2000) - if ((uint32)FindName(&SysBase->MemList, (UBYTE *)"chip memory") < 0x2000) { - ErrorAlert(GetString(STR_NO_PREPARE_EMUL_ERR)); + if ((uint32)FindName(&SysBase->MemList, (UBYTE *) "chip memory") < 0x2000) { + ErrorAlert(STR_NO_PREPARE_EMUL_ERR); QuitEmulator(); } // Open timer.device timereq = (struct timerequest *)AllocVec(sizeof(timerequest), MEMF_PUBLIC | MEMF_CLEAR); if (timereq == NULL) { - ErrorAlert(GetString(STR_NO_MEM_ERR)); + ErrorAlert(STR_NO_MEM_ERR); QuitEmulator(); } - if (OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timereq, 0)) { - ErrorAlert(GetString(STR_NO_TIMER_DEV_ERR)); + if (OpenDevice((UBYTE *) TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timereq, 0)) { + ErrorAlert(STR_NO_TIMER_DEV_ERR); QuitEmulator(); } 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)); + ErrorAlert(STR_NO_MEM_ERR); QuitEmulator(); } ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block @@ -242,10 +257,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(STR_NO_MEM_ERR); + QuitEmulator(); + } } RAMBaseMac = (uint32)RAMBaseHost; D(bug("Mac RAM starts at %08lx\n", RAMBaseHost)); @@ -258,21 +285,21 @@ 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) { - ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); + if (rom_fh == 0) { + ErrorAlert(STR_NO_ROM_FILE_ERR); QuitEmulator(); } printf(GetString(STR_READING_ROM_FILE)); Seek(rom_fh, 0, OFFSET_END); ROMSize = Seek(rom_fh, 0, OFFSET_CURRENT); if (ROMSize != 512*1024 && ROMSize != 1024*1024) { - ErrorAlert(GetString(STR_ROM_SIZE_ERR)); + ErrorAlert(STR_ROM_SIZE_ERR); Close(rom_fh); QuitEmulator(); } Seek(rom_fh, 0, OFFSET_BEGINNING); if (Read(rom_fh, ROMBaseHost, ROMSize) != ROMSize) { - ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); + ErrorAlert(STR_ROM_FILE_READ_ERR); Close(rom_fh); QuitEmulator(); } @@ -290,6 +317,12 @@ 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(); + + memset((UBYTE *) 8, 0, 0x2000-8); + // Install trap handler EmulatedSR = 0x2700; OldTrapHandler = MainTask->tc_TrapCode; @@ -302,6 +335,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, @@ -313,6 +354,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; @@ -347,6 +390,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; @@ -360,13 +417,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; @@ -375,7 +425,7 @@ void QuitEmulator(void) // Delete RAM/ROM area if (RAMBaseHost) - FreeMem(RAMBaseHost, RAMSize + 0x100000); + FreeVec(RAMBaseHost); // Delete scratch memory area if (ScratchMem) @@ -433,6 +483,33 @@ void FlushCodeCache(void *start, uint32 /* + * Mutexes + */ + +struct B2_mutex { + int dummy; //!! +}; + +B2_mutex *B2_create_mutex(void) +{ + return new B2_mutex; +} + +void B2_lock_mutex(B2_mutex *mutex) +{ +} + +void B2_unlock_mutex(B2_mutex *mutex) +{ +} + +void B2_delete_mutex(B2_mutex *mutex) +{ + delete mutex; +} + + +/* * Interrupt flags (must be handled atomically!) */ @@ -453,9 +530,14 @@ void TriggerInterrupt(void) Signal(MainTask, IRQSigMask); } +void TriggerNMI(void) +{ + AsmTriggerNMI(); +} + /* - * 60Hz thread + * 60Hz thread (really 60.15Hz) */ static __saveds void tick_func(void) @@ -470,7 +552,7 @@ static __saveds void tick_func(void) if (timer_port) { timer_io = (struct timerequest *)CreateIORequest(timer_port, sizeof(struct timerequest)); if (timer_io) { - if (!OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) { + if (!OpenDevice((UBYTE *) TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) { timer_mask = 1 << timer_port->mp_SigBit; timer_io->tr_node.io_Command = TR_ADDREQUEST; timer_io->tr_time.tv_secs = 0; @@ -495,6 +577,8 @@ static __saveds void tick_func(void) if (++tick_counter > 60) { tick_counter = 0; WriteMacInt32(0x20c, TimerDateTime()); + SetInterruptFlag(INTFLAG_1HZ); + TriggerInterrupt(); } // Trigger 60Hz interrupt @@ -520,6 +604,30 @@ static __saveds void tick_func(void) /* + * XPRAM watchdog thread (saves XPRAM every minute) + */ + +static __saveds void xpram_func(void) +{ + uint8 last_xpram[XPRAM_SIZE]; + memcpy(last_xpram, XPRAM, XPRAM_SIZE); + + 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, XPRAM_SIZE)) { + memcpy(last_xpram, XPRAM, XPRAM_SIZE); + SaveXPRAM(); + } + } + + // Main task asked for termination, send signal + Forbid(); + Signal(MainTask, SIGF_SINGLE); +} + + +/* * Display error alert */ @@ -590,6 +698,8 @@ struct trap_regs { // This must match th void __saveds IllInstrHandler(trap_regs *r) { +// D(bug("IllInstrHandler/%ld\n", __LINE__)); + uint16 opcode = *(uint16 *)(r->pc); if ((opcode & 0xff00) != 0x7100) { printf("Illegal Instruction %04x at %08lx\n", *(uint16 *)(r->pc), r->pc); @@ -608,7 +718,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