1 |
|
/* |
2 |
|
* main_amiga.cpp - Startup code for AmigaOS |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2000 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2001 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
91 |
|
struct Library *IFFParseBase = NULL; |
92 |
|
struct Library *AslBase = NULL; |
93 |
|
struct Library *P96Base = NULL; |
94 |
+ |
struct Library *CyberGfxBase = NULL; |
95 |
|
struct Library *TimerBase = NULL; |
96 |
|
struct Library *AHIBase = NULL; |
97 |
|
struct Library *DiskBase = NULL; |
98 |
|
|
99 |
|
struct Task *MainTask; // Our task |
100 |
< |
uint32 ScratchMem = NULL; // Scratch memory for Mac ROM writes |
100 |
> |
uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes |
101 |
|
APTR OldTrapHandler = NULL; // Old trap handler |
102 |
|
APTR OldExceptionHandler = NULL; // Old exception handler |
103 |
|
BYTE IRQSig = -1; // "Interrupt" signal number |
108 |
|
static struct MsgPort *ahi_port = NULL; // Port for AHI |
109 |
|
static struct AHIRequest *ahi_io = NULL; // IORequest for AHI |
110 |
|
|
111 |
+ |
static struct Process *xpram_proc = NULL; // XPRAM watchdog |
112 |
+ |
static volatile bool xpram_proc_active = true; // Flag for quitting the XPRAM watchdog |
113 |
+ |
|
114 |
|
static struct Process *tick_proc = NULL; // 60Hz process |
115 |
|
static volatile bool tick_proc_active = true; // Flag for quitting the 60Hz process |
116 |
|
|
123 |
|
extern "C" void AtomicAnd(uint32 *p, uint32 val); |
124 |
|
extern "C" void AtomicOr(uint32 *p, uint32 val); |
125 |
|
extern "C" void MoveVBR(void); |
126 |
+ |
extern "C" void DisableSuperBypass(void); |
127 |
|
extern "C" void TrapHandlerAsm(void); |
128 |
|
extern "C" void ExceptionHandlerAsm(void); |
129 |
|
extern "C" void IllInstrHandler(trap_regs *regs); |
130 |
|
extern "C" void PrivViolHandler(trap_regs *regs); |
131 |
|
extern "C" void quit_emulator(void); |
132 |
+ |
extern "C" void AsmTriggerNMI(void); |
133 |
|
uint16 EmulatedSR; // Emulated SR (supervisor bit and interrupt mask) |
134 |
|
|
135 |
|
|
136 |
|
// Prototypes |
137 |
|
static void jump_to_rom(void); |
138 |
+ |
static void xpram_func(void); |
139 |
|
static void tick_func(void); |
140 |
|
|
141 |
|
|
143 |
|
* Main program |
144 |
|
*/ |
145 |
|
|
146 |
< |
int main(void) |
146 |
> |
int main(int argc, char **argv) |
147 |
|
{ |
148 |
|
// Initialize variables |
149 |
|
RAMBaseHost = NULL; |
187 |
|
ErrorAlert(GetString(STR_NO_ASL_LIB_ERR)); |
188 |
|
QuitEmulator(); |
189 |
|
} |
190 |
+ |
|
191 |
+ |
// These two can fail (the respective gfx support won't be available, then) |
192 |
|
P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2); |
193 |
+ |
CyberGfxBase = OpenLibrary((UBYTE *)"cybergraphics.library", 2); |
194 |
|
|
195 |
|
// Read preferences |
196 |
< |
PrefsInit(); |
196 |
> |
PrefsInit(argc, argv); |
197 |
|
|
198 |
|
// Open AHI |
199 |
|
ahi_port = CreateMsgPort(); |
234 |
|
TimerBase = (struct Library *)timereq->tr_node.io_Device; |
235 |
|
|
236 |
|
// Allocate scratch memory |
237 |
< |
ScratchMem = (uint32)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); |
237 |
> |
ScratchMem = (uint8 *)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); |
238 |
|
if (ScratchMem == NULL) { |
239 |
|
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
240 |
|
QuitEmulator(); |
248 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
249 |
|
RAMSize = 1024*1024; |
250 |
|
} |
251 |
< |
RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC); |
251 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
252 |
|
if (RAMBaseHost == NULL) { |
253 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
254 |
< |
QuitEmulator(); |
253 |
> |
uint32 newRAMSize = AvailMem(MEMF_LARGEST) - 0x100000; |
254 |
> |
char xText[120]; |
255 |
> |
|
256 |
> |
sprintf(xText, GetString(STR_NOT_ENOUGH_MEM_WARN), RAMSize, newRAMSize); |
257 |
> |
|
258 |
> |
if (ChoiceAlert(xText, "Use", "Quit") != 1) |
259 |
> |
QuitEmulator(); |
260 |
> |
|
261 |
> |
RAMSize = newRAMSize; |
262 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
263 |
> |
if (RAMBaseHost == NULL) { |
264 |
> |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
265 |
> |
QuitEmulator(); |
266 |
> |
} |
267 |
|
} |
268 |
|
RAMBaseMac = (uint32)RAMBaseHost; |
269 |
|
D(bug("Mac RAM starts at %08lx\n", RAMBaseHost)); |
308 |
|
// Move VBR away from 0 if neccessary |
309 |
|
MoveVBR(); |
310 |
|
|
311 |
+ |
// On 68060, disable Super Bypass mode because of a CPU bug that is triggered by MacOS 8 |
312 |
+ |
if (CPUIs68060) |
313 |
+ |
DisableSuperBypass(); |
314 |
+ |
|
315 |
|
// Install trap handler |
316 |
|
EmulatedSR = 0x2700; |
317 |
|
OldTrapHandler = MainTask->tc_TrapCode; |
324 |
|
MainTask->tc_ExceptCode = (APTR)ExceptionHandlerAsm; |
325 |
|
SetExcept(SIGBREAKF_CTRL_C | IRQSigMask, SIGBREAKF_CTRL_C | IRQSigMask); |
326 |
|
|
327 |
+ |
// Start XPRAM watchdog process |
328 |
+ |
xpram_proc = CreateNewProcTags( |
329 |
+ |
NP_Entry, (ULONG)xpram_func, |
330 |
+ |
NP_Name, (ULONG)"Basilisk II XPRAM Watchdog", |
331 |
+ |
NP_Priority, 0, |
332 |
+ |
TAG_END |
333 |
+ |
); |
334 |
+ |
|
335 |
|
// Start 60Hz process |
336 |
|
tick_proc = CreateNewProcTags( |
337 |
|
NP_Entry, (ULONG)tick_func, |
343 |
|
// Set task priority to -1 so we don't use all processing time |
344 |
|
SetTaskPri(MainTask, -1); |
345 |
|
|
346 |
+ |
WriteMacInt32(0xbff, 0); // MacsBugFlags |
347 |
+ |
|
348 |
|
// Swap stack to Mac RAM area |
349 |
|
stack_swap.stk_Lower = RAMBaseHost; |
350 |
|
stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize; |
379 |
|
|
380 |
|
void QuitEmulator(void) |
381 |
|
{ |
382 |
+ |
// Stop 60Hz process |
383 |
+ |
if (tick_proc) { |
384 |
+ |
SetSignal(0, SIGF_SINGLE); |
385 |
+ |
tick_proc_active = false; |
386 |
+ |
Wait(SIGF_SINGLE); |
387 |
+ |
} |
388 |
+ |
|
389 |
+ |
// Stop XPRAM watchdog process |
390 |
+ |
if (xpram_proc) { |
391 |
+ |
SetSignal(0, SIGF_SINGLE); |
392 |
+ |
xpram_proc_active = false; |
393 |
+ |
Wait(SIGF_SINGLE); |
394 |
+ |
} |
395 |
+ |
|
396 |
|
// Restore stack |
397 |
|
if (stack_swapped) { |
398 |
|
stack_swapped = false; |
406 |
|
FreeSignal(IRQSig); |
407 |
|
} |
408 |
|
|
359 |
– |
// Stop 60Hz thread |
360 |
– |
if (tick_proc) { |
361 |
– |
SetSignal(0, SIGF_SINGLE); |
362 |
– |
tick_proc_active = false; |
363 |
– |
Wait(SIGF_SINGLE); |
364 |
– |
} |
365 |
– |
|
409 |
|
// Remove trap handler |
410 |
|
MainTask->tc_TrapCode = OldTrapHandler; |
411 |
|
|
414 |
|
|
415 |
|
// Delete RAM/ROM area |
416 |
|
if (RAMBaseHost) |
417 |
< |
FreeMem(RAMBaseHost, RAMSize + 0x100000); |
417 |
> |
FreeVec(RAMBaseHost); |
418 |
|
|
419 |
|
// Delete scratch memory area |
420 |
|
if (ScratchMem) |
441 |
|
PrefsExit(); |
442 |
|
|
443 |
|
// Close libraries |
444 |
+ |
if (CyberGfxBase) |
445 |
+ |
CloseLibrary(CyberGfxBase); |
446 |
|
if (P96Base) |
447 |
|
CloseLibrary(P96Base); |
448 |
|
if (AslBase) |
492 |
|
Signal(MainTask, IRQSigMask); |
493 |
|
} |
494 |
|
|
495 |
+ |
void TriggerNMI(void) |
496 |
+ |
{ |
497 |
+ |
AsmTriggerNMI(); |
498 |
+ |
} |
499 |
+ |
|
500 |
|
|
501 |
|
/* |
502 |
< |
* 60Hz thread |
502 |
> |
* 60Hz thread (really 60.15Hz) |
503 |
|
*/ |
504 |
|
|
505 |
|
static __saveds void tick_func(void) |
539 |
|
if (++tick_counter > 60) { |
540 |
|
tick_counter = 0; |
541 |
|
WriteMacInt32(0x20c, TimerDateTime()); |
542 |
+ |
SetInterruptFlag(INTFLAG_1HZ); |
543 |
+ |
TriggerInterrupt(); |
544 |
|
} |
545 |
|
|
546 |
|
// Trigger 60Hz interrupt |
566 |
|
|
567 |
|
|
568 |
|
/* |
569 |
+ |
* XPRAM watchdog thread (saves XPRAM every minute) |
570 |
+ |
*/ |
571 |
+ |
|
572 |
+ |
static __saveds void xpram_func(void) |
573 |
+ |
{ |
574 |
+ |
uint8 last_xpram[256]; |
575 |
+ |
memcpy(last_xpram, XPRAM, 256); |
576 |
+ |
|
577 |
+ |
while (xpram_proc_active) { |
578 |
+ |
for (int i=0; i<60 && xpram_proc_active; i++) |
579 |
+ |
Delay(50); // Only wait 1 second so we quit promptly when xpram_proc_active becomes false |
580 |
+ |
if (memcmp(last_xpram, XPRAM, 256)) { |
581 |
+ |
memcpy(last_xpram, XPRAM, 256); |
582 |
+ |
SaveXPRAM(); |
583 |
+ |
} |
584 |
+ |
} |
585 |
+ |
|
586 |
+ |
// Main task asked for termination, send signal |
587 |
+ |
Forbid(); |
588 |
+ |
Signal(MainTask, SIGF_SINGLE); |
589 |
+ |
} |
590 |
+ |
|
591 |
+ |
|
592 |
+ |
/* |
593 |
|
* Display error alert |
594 |
|
*/ |
595 |
|
|
678 |
|
EmulatedSR |= 0x0700; |
679 |
|
|
680 |
|
// Call opcode routine |
681 |
< |
EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r); |
681 |
> |
EmulOp(opcode, (M68kRegisters *)r); |
682 |
|
r->pc += 2; |
683 |
|
|
684 |
|
// Restore interrupts |