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 |
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; |
174 |
|
QuitEmulator(); |
175 |
|
GadToolsBase = OpenLibrary((UBYTE *)"gadtools.library", 39); |
176 |
|
if (GadToolsBase == NULL) { |
177 |
< |
ErrorAlert(GetString(STR_NO_GADTOOLS_LIB_ERR)); |
177 |
> |
ErrorAlert(STR_NO_GADTOOLS_LIB_ERR); |
178 |
|
QuitEmulator(); |
179 |
|
} |
180 |
|
IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library", 39); |
181 |
|
if (IFFParseBase == NULL) { |
182 |
< |
ErrorAlert(GetString(STR_NO_IFFPARSE_LIB_ERR)); |
182 |
> |
ErrorAlert(STR_NO_IFFPARSE_LIB_ERR); |
183 |
|
QuitEmulator(); |
184 |
|
} |
185 |
|
AslBase = OpenLibrary((UBYTE *)"asl.library", 36); |
186 |
|
if (AslBase == NULL) { |
187 |
< |
ErrorAlert(GetString(STR_NO_ASL_LIB_ERR)); |
187 |
> |
ErrorAlert(STR_NO_ASL_LIB_ERR); |
188 |
|
QuitEmulator(); |
189 |
|
} |
190 |
|
|
191 |
+ |
if (FindTask((UBYTE *) "« Enforcer »")) |
192 |
+ |
{ |
193 |
+ |
ErrorAlert(STR_ENFORCER_RUNNING_ERR); |
194 |
+ |
QuitEmulator(); |
195 |
+ |
} |
196 |
+ |
|
197 |
|
// These two can fail (the respective gfx support won't be available, then) |
198 |
|
P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2); |
199 |
|
CyberGfxBase = OpenLibrary((UBYTE *)"cybergraphics.library", 2); |
200 |
|
|
201 |
|
// Read preferences |
202 |
< |
PrefsInit(); |
202 |
> |
PrefsInit(argc, argv); |
203 |
|
|
204 |
|
// Open AHI |
205 |
|
ahi_port = CreateMsgPort(); |
223 |
|
|
224 |
|
// Check start of Chip memory (because we need access to 0x0000..0x2000) |
225 |
|
if ((uint32)FindName(&SysBase->MemList, (UBYTE *)"chip memory") < 0x2000) { |
226 |
< |
ErrorAlert(GetString(STR_NO_PREPARE_EMUL_ERR)); |
226 |
> |
ErrorAlert(STR_NO_PREPARE_EMUL_ERR); |
227 |
|
QuitEmulator(); |
228 |
|
} |
229 |
|
|
230 |
|
// Open timer.device |
231 |
|
timereq = (struct timerequest *)AllocVec(sizeof(timerequest), MEMF_PUBLIC | MEMF_CLEAR); |
232 |
|
if (timereq == NULL) { |
233 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
233 |
> |
ErrorAlert(STR_NO_MEM_ERR); |
234 |
|
QuitEmulator(); |
235 |
|
} |
236 |
|
if (OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timereq, 0)) { |
237 |
< |
ErrorAlert(GetString(STR_NO_TIMER_DEV_ERR)); |
237 |
> |
ErrorAlert(STR_NO_TIMER_DEV_ERR); |
238 |
|
QuitEmulator(); |
239 |
|
} |
240 |
|
TimerBase = (struct Library *)timereq->tr_node.io_Device; |
241 |
|
|
242 |
|
// Allocate scratch memory |
243 |
< |
ScratchMem = (uint32)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); |
243 |
> |
ScratchMem = (uint8 *)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); |
244 |
|
if (ScratchMem == NULL) { |
245 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
245 |
> |
ErrorAlert(STR_NO_MEM_ERR); |
246 |
|
QuitEmulator(); |
247 |
|
} |
248 |
|
ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block |
254 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
255 |
|
RAMSize = 1024*1024; |
256 |
|
} |
257 |
< |
RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC); |
257 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
258 |
|
if (RAMBaseHost == NULL) { |
259 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
260 |
< |
QuitEmulator(); |
259 |
> |
uint32 newRAMSize = AvailMem(MEMF_LARGEST) - 0x100000; |
260 |
> |
char xText[120]; |
261 |
> |
|
262 |
> |
sprintf(xText, GetString(STR_NOT_ENOUGH_MEM_WARN), RAMSize, newRAMSize); |
263 |
> |
|
264 |
> |
if (ChoiceAlert(xText, "Use", "Quit") != 1) |
265 |
> |
QuitEmulator(); |
266 |
> |
|
267 |
> |
RAMSize = newRAMSize; |
268 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
269 |
> |
if (RAMBaseHost == NULL) { |
270 |
> |
ErrorAlert(STR_NO_MEM_ERR); |
271 |
> |
QuitEmulator(); |
272 |
> |
} |
273 |
|
} |
274 |
|
RAMBaseMac = (uint32)RAMBaseHost; |
275 |
|
D(bug("Mac RAM starts at %08lx\n", RAMBaseHost)); |
282 |
|
|
283 |
|
// Load Mac ROM |
284 |
|
BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE); |
285 |
< |
if (rom_fh == NULL) { |
286 |
< |
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
285 |
> |
if (rom_fh == 0) { |
286 |
> |
ErrorAlert(STR_NO_ROM_FILE_ERR); |
287 |
|
QuitEmulator(); |
288 |
|
} |
289 |
|
printf(GetString(STR_READING_ROM_FILE)); |
290 |
|
Seek(rom_fh, 0, OFFSET_END); |
291 |
|
ROMSize = Seek(rom_fh, 0, OFFSET_CURRENT); |
292 |
|
if (ROMSize != 512*1024 && ROMSize != 1024*1024) { |
293 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
293 |
> |
ErrorAlert(STR_ROM_SIZE_ERR); |
294 |
|
Close(rom_fh); |
295 |
|
QuitEmulator(); |
296 |
|
} |
297 |
|
Seek(rom_fh, 0, OFFSET_BEGINNING); |
298 |
|
if (Read(rom_fh, ROMBaseHost, ROMSize) != ROMSize) { |
299 |
< |
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
299 |
> |
ErrorAlert(STR_ROM_FILE_READ_ERR); |
300 |
|
Close(rom_fh); |
301 |
|
QuitEmulator(); |
302 |
|
} |
314 |
|
// Move VBR away from 0 if neccessary |
315 |
|
MoveVBR(); |
316 |
|
|
317 |
+ |
// On 68060, disable Super Bypass mode because of a CPU bug that is triggered by MacOS 8 |
318 |
+ |
if (CPUIs68060) |
319 |
+ |
DisableSuperBypass(); |
320 |
+ |
|
321 |
|
// Install trap handler |
322 |
|
EmulatedSR = 0x2700; |
323 |
|
OldTrapHandler = MainTask->tc_TrapCode; |
330 |
|
MainTask->tc_ExceptCode = (APTR)ExceptionHandlerAsm; |
331 |
|
SetExcept(SIGBREAKF_CTRL_C | IRQSigMask, SIGBREAKF_CTRL_C | IRQSigMask); |
332 |
|
|
333 |
+ |
// Start XPRAM watchdog process |
334 |
+ |
xpram_proc = CreateNewProcTags( |
335 |
+ |
NP_Entry, (ULONG)xpram_func, |
336 |
+ |
NP_Name, (ULONG)"Basilisk II XPRAM Watchdog", |
337 |
+ |
NP_Priority, 0, |
338 |
+ |
TAG_END |
339 |
+ |
); |
340 |
+ |
|
341 |
|
// Start 60Hz process |
342 |
|
tick_proc = CreateNewProcTags( |
343 |
|
NP_Entry, (ULONG)tick_func, |
349 |
|
// Set task priority to -1 so we don't use all processing time |
350 |
|
SetTaskPri(MainTask, -1); |
351 |
|
|
352 |
+ |
WriteMacInt32(0xbff, 0); // MacsBugFlags |
353 |
+ |
|
354 |
|
// Swap stack to Mac RAM area |
355 |
|
stack_swap.stk_Lower = RAMBaseHost; |
356 |
|
stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize; |
385 |
|
|
386 |
|
void QuitEmulator(void) |
387 |
|
{ |
388 |
+ |
// Stop 60Hz process |
389 |
+ |
if (tick_proc) { |
390 |
+ |
SetSignal(0, SIGF_SINGLE); |
391 |
+ |
tick_proc_active = false; |
392 |
+ |
Wait(SIGF_SINGLE); |
393 |
+ |
} |
394 |
+ |
|
395 |
+ |
// Stop XPRAM watchdog process |
396 |
+ |
if (xpram_proc) { |
397 |
+ |
SetSignal(0, SIGF_SINGLE); |
398 |
+ |
xpram_proc_active = false; |
399 |
+ |
Wait(SIGF_SINGLE); |
400 |
+ |
} |
401 |
+ |
|
402 |
|
// Restore stack |
403 |
|
if (stack_swapped) { |
404 |
|
stack_swapped = false; |
412 |
|
FreeSignal(IRQSig); |
413 |
|
} |
414 |
|
|
363 |
– |
// Stop 60Hz thread |
364 |
– |
if (tick_proc) { |
365 |
– |
SetSignal(0, SIGF_SINGLE); |
366 |
– |
tick_proc_active = false; |
367 |
– |
Wait(SIGF_SINGLE); |
368 |
– |
} |
369 |
– |
|
415 |
|
// Remove trap handler |
416 |
|
MainTask->tc_TrapCode = OldTrapHandler; |
417 |
|
|
420 |
|
|
421 |
|
// Delete RAM/ROM area |
422 |
|
if (RAMBaseHost) |
423 |
< |
FreeMem(RAMBaseHost, RAMSize + 0x100000); |
423 |
> |
FreeVec(RAMBaseHost); |
424 |
|
|
425 |
|
// Delete scratch memory area |
426 |
|
if (ScratchMem) |
478 |
|
|
479 |
|
|
480 |
|
/* |
481 |
+ |
* Mutexes |
482 |
+ |
*/ |
483 |
+ |
|
484 |
+ |
struct B2_mutex { |
485 |
+ |
int dummy; //!! |
486 |
+ |
}; |
487 |
+ |
|
488 |
+ |
B2_mutex *B2_create_mutex(void) |
489 |
+ |
{ |
490 |
+ |
return new B2_mutex; |
491 |
+ |
} |
492 |
+ |
|
493 |
+ |
void B2_lock_mutex(B2_mutex *mutex) |
494 |
+ |
{ |
495 |
+ |
} |
496 |
+ |
|
497 |
+ |
void B2_unlock_mutex(B2_mutex *mutex) |
498 |
+ |
{ |
499 |
+ |
} |
500 |
+ |
|
501 |
+ |
void B2_delete_mutex(B2_mutex *mutex) |
502 |
+ |
{ |
503 |
+ |
delete mutex; |
504 |
+ |
} |
505 |
+ |
|
506 |
+ |
|
507 |
+ |
/* |
508 |
|
* Interrupt flags (must be handled atomically!) |
509 |
|
*/ |
510 |
|
|
525 |
|
Signal(MainTask, IRQSigMask); |
526 |
|
} |
527 |
|
|
528 |
+ |
void TriggerNMI(void) |
529 |
+ |
{ |
530 |
+ |
AsmTriggerNMI(); |
531 |
+ |
} |
532 |
+ |
|
533 |
|
|
534 |
|
/* |
535 |
< |
* 60Hz thread |
535 |
> |
* 60Hz thread (really 60.15Hz) |
536 |
|
*/ |
537 |
|
|
538 |
|
static __saveds void tick_func(void) |
572 |
|
if (++tick_counter > 60) { |
573 |
|
tick_counter = 0; |
574 |
|
WriteMacInt32(0x20c, TimerDateTime()); |
575 |
+ |
SetInterruptFlag(INTFLAG_1HZ); |
576 |
+ |
TriggerInterrupt(); |
577 |
|
} |
578 |
|
|
579 |
|
// Trigger 60Hz interrupt |
599 |
|
|
600 |
|
|
601 |
|
/* |
602 |
+ |
* XPRAM watchdog thread (saves XPRAM every minute) |
603 |
+ |
*/ |
604 |
+ |
|
605 |
+ |
static __saveds void xpram_func(void) |
606 |
+ |
{ |
607 |
+ |
uint8 last_xpram[XPRAM_SIZE]; |
608 |
+ |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
609 |
+ |
|
610 |
+ |
while (xpram_proc_active) { |
611 |
+ |
for (int i=0; i<60 && xpram_proc_active; i++) |
612 |
+ |
Delay(50); // Only wait 1 second so we quit promptly when xpram_proc_active becomes false |
613 |
+ |
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) { |
614 |
+ |
memcpy(last_xpram, XPRAM, XPRAM_SIZE); |
615 |
+ |
SaveXPRAM(); |
616 |
+ |
} |
617 |
+ |
} |
618 |
+ |
|
619 |
+ |
// Main task asked for termination, send signal |
620 |
+ |
Forbid(); |
621 |
+ |
Signal(MainTask, SIGF_SINGLE); |
622 |
+ |
} |
623 |
+ |
|
624 |
+ |
|
625 |
+ |
/* |
626 |
|
* Display error alert |
627 |
|
*/ |
628 |
|
|
711 |
|
EmulatedSR |= 0x0700; |
712 |
|
|
713 |
|
// Call opcode routine |
714 |
< |
EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r); |
714 |
> |
EmulOp(opcode, (M68kRegisters *)r); |
715 |
|
r->pc += 2; |
716 |
|
|
717 |
|
// Restore interrupts |