1 |
|
/* |
2 |
|
* main_amiga.cpp - Startup code for AmigaOS |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-1999 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2000 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 |
56 |
|
#include "debug.h" |
57 |
|
|
58 |
|
|
59 |
+ |
// Options for libnix |
60 |
+ |
unsigned long __stack = 0x4000; // Stack requirement |
61 |
+ |
int __nocommandline = 1; // Disable command line parsing |
62 |
+ |
|
63 |
+ |
|
64 |
|
// Constants |
65 |
|
static const char ROM_FILE_NAME[] = "ROM"; |
66 |
< |
static const char __ver[] = "$VER: " VERSION_STRING " " __AMIGADATE__; |
66 |
> |
static const char __ver[] = "$VER: " VERSION_STRING " " __DATE__; |
67 |
|
static const int SCRATCH_MEM_SIZE = 65536; |
68 |
|
|
69 |
|
|
85 |
|
|
86 |
|
// Global variables |
87 |
|
extern ExecBase *SysBase; |
88 |
+ |
struct Library *GfxBase = NULL; |
89 |
+ |
struct IntuitionBase *IntuitionBase = NULL; |
90 |
|
struct Library *GadToolsBase = NULL; |
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 |
115 |
|
static StackSwapStruct stack_swap; |
116 |
|
|
117 |
|
|
109 |
– |
// Prototypes |
110 |
– |
static void jump_to_rom(void); |
111 |
– |
static void tick_func(void); |
112 |
– |
|
113 |
– |
|
118 |
|
// Assembly functions |
119 |
|
struct trap_regs; |
120 |
|
extern "C" void AtomicAnd(uint32 *p, uint32 val); |
124 |
|
extern "C" void ExceptionHandlerAsm(void); |
125 |
|
extern "C" void IllInstrHandler(trap_regs *regs); |
126 |
|
extern "C" void PrivViolHandler(trap_regs *regs); |
127 |
+ |
extern "C" void quit_emulator(void); |
128 |
+ |
extern "C" void AsmTriggerNMI(void); |
129 |
|
uint16 EmulatedSR; // Emulated SR (supervisor bit and interrupt mask) |
130 |
|
|
131 |
|
|
132 |
+ |
// Prototypes |
133 |
+ |
static void jump_to_rom(void); |
134 |
+ |
static void tick_func(void); |
135 |
+ |
|
136 |
+ |
|
137 |
|
/* |
138 |
|
* Main program |
139 |
|
*/ |
140 |
|
|
141 |
< |
int main(void) |
141 |
> |
int main(int argc, char **argv) |
142 |
|
{ |
143 |
|
// Initialize variables |
144 |
|
RAMBaseHost = NULL; |
152 |
|
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
153 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
154 |
|
|
144 |
– |
// Read preferences |
145 |
– |
PrefsInit(); |
146 |
– |
|
155 |
|
// Open libraries |
156 |
+ |
GfxBase = OpenLibrary((UBYTE *)"graphics.library", 39); |
157 |
+ |
if (GfxBase == NULL) { |
158 |
+ |
printf("Cannot open graphics.library V39.\n"); |
159 |
+ |
exit(1); |
160 |
+ |
} |
161 |
+ |
IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39); |
162 |
+ |
if (IntuitionBase == NULL) { |
163 |
+ |
printf("Cannot open intuition.library V39.\n"); |
164 |
+ |
CloseLibrary(GfxBase); |
165 |
+ |
exit(1); |
166 |
+ |
} |
167 |
|
DiskBase = (struct Library *)OpenResource((UBYTE *)"disk.resource"); |
168 |
|
if (DiskBase == NULL) |
169 |
|
QuitEmulator(); |
172 |
|
ErrorAlert(GetString(STR_NO_GADTOOLS_LIB_ERR)); |
173 |
|
QuitEmulator(); |
174 |
|
} |
175 |
+ |
IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library", 39); |
176 |
+ |
if (IFFParseBase == NULL) { |
177 |
+ |
ErrorAlert(GetString(STR_NO_IFFPARSE_LIB_ERR)); |
178 |
+ |
QuitEmulator(); |
179 |
+ |
} |
180 |
|
AslBase = OpenLibrary((UBYTE *)"asl.library", 36); |
181 |
|
if (AslBase == NULL) { |
182 |
|
ErrorAlert(GetString(STR_NO_ASL_LIB_ERR)); |
183 |
|
QuitEmulator(); |
184 |
|
} |
185 |
+ |
|
186 |
+ |
// These two can fail (the respective gfx support won't be available, then) |
187 |
|
P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2); |
188 |
+ |
CyberGfxBase = OpenLibrary((UBYTE *)"cybergraphics.library", 2); |
189 |
+ |
|
190 |
+ |
// Read preferences |
191 |
+ |
PrefsInit(argc, argv); |
192 |
|
|
193 |
|
// Open AHI |
194 |
|
ahi_port = CreateMsgPort(); |
229 |
|
TimerBase = (struct Library *)timereq->tr_node.io_Device; |
230 |
|
|
231 |
|
// Allocate scratch memory |
232 |
< |
ScratchMem = (uint32)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); |
232 |
> |
ScratchMem = (uint8 *)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC); |
233 |
|
if (ScratchMem == NULL) { |
234 |
|
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
235 |
|
QuitEmulator(); |
243 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
244 |
|
RAMSize = 1024*1024; |
245 |
|
} |
246 |
< |
RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC); |
246 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
247 |
|
if (RAMBaseHost == NULL) { |
248 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
249 |
< |
QuitEmulator(); |
248 |
> |
uint32 newRAMSize = AvailMem(MEMF_LARGEST) - 0x100000; |
249 |
> |
char xText[120]; |
250 |
> |
|
251 |
> |
sprintf(xText, GetString(STR_NOT_ENOUGH_MEM_WARN), RAMSize, newRAMSize); |
252 |
> |
|
253 |
> |
if (ChoiceAlert(xText, "Use", "Quit") != 1) |
254 |
> |
QuitEmulator(); |
255 |
> |
|
256 |
> |
RAMSize = newRAMSize; |
257 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
258 |
> |
if (RAMBaseHost == NULL) { |
259 |
> |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
260 |
> |
QuitEmulator(); |
261 |
> |
} |
262 |
|
} |
263 |
|
RAMBaseMac = (uint32)RAMBaseHost; |
264 |
|
D(bug("Mac RAM starts at %08lx\n", RAMBaseHost)); |
270 |
|
const char *rom_path = PrefsFindString("rom"); |
271 |
|
|
272 |
|
// Load Mac ROM |
273 |
< |
BPTR rom_fh = Open(rom_path ? (char *)rom_path : ROM_FILE_NAME, MODE_OLDFILE); |
273 |
> |
BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE); |
274 |
|
if (rom_fh == NULL) { |
275 |
|
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
276 |
|
QuitEmulator(); |
290 |
|
QuitEmulator(); |
291 |
|
} |
292 |
|
|
251 |
– |
// Check ROM version |
252 |
– |
if (!CheckROM()) { |
253 |
– |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
254 |
– |
QuitEmulator(); |
255 |
– |
} |
256 |
– |
|
293 |
|
// Set CPU and FPU type |
294 |
|
UWORD attn = SysBase->AttnFlags; |
295 |
|
CPUType = attn & AFF_68040 ? 4 : (attn & AFF_68030 ? 3 : 2); |
296 |
|
CPUIs68060 = attn & AFF_68060; |
297 |
|
FPUType = attn & AFF_68881 ? 1 : 0; |
298 |
|
|
299 |
< |
// Load XPRAM |
300 |
< |
XPRAMInit(); |
265 |
< |
|
266 |
< |
// Set boot volume |
267 |
< |
int16 i16 = PrefsFindInt16("bootdrive"); |
268 |
< |
XPRAM[0x78] = i16 >> 8; |
269 |
< |
XPRAM[0x79] = i16 & 0xff; |
270 |
< |
i16 = PrefsFindInt16("bootdriver"); |
271 |
< |
XPRAM[0x7a] = i16 >> 8; |
272 |
< |
XPRAM[0x7b] = i16 & 0xff; |
273 |
< |
|
274 |
< |
// Init drivers |
275 |
< |
SonyInit(); |
276 |
< |
DiskInit(); |
277 |
< |
CDROMInit(); |
278 |
< |
SCSIInit(); |
279 |
< |
|
280 |
< |
// Init network |
281 |
< |
EtherInit(); |
282 |
< |
|
283 |
< |
// Init serial ports |
284 |
< |
SerialInit(); |
285 |
< |
|
286 |
< |
// Init Time Manager |
287 |
< |
TimerInit(); |
288 |
< |
|
289 |
< |
// Init clipboard |
290 |
< |
ClipInit(); |
291 |
< |
|
292 |
< |
// Init audio |
293 |
< |
AudioInit(); |
294 |
< |
|
295 |
< |
// Init video |
296 |
< |
if (!VideoInit(ROMVersion == ROM_VERSION_64K || ROMVersion == ROM_VERSION_PLUS || ROMVersion == ROM_VERSION_CLASSIC)) |
299 |
> |
// Initialize everything |
300 |
> |
if (!InitAll()) |
301 |
|
QuitEmulator(); |
302 |
|
|
299 |
– |
// Install ROM patches |
300 |
– |
if (!PatchROM()) { |
301 |
– |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
302 |
– |
QuitEmulator(); |
303 |
– |
} |
304 |
– |
|
303 |
|
// Move VBR away from 0 if neccessary |
304 |
|
MoveVBR(); |
305 |
|
|
317 |
|
|
318 |
|
// Start 60Hz process |
319 |
|
tick_proc = CreateNewProcTags( |
320 |
< |
NP_Entry, tick_func, |
321 |
< |
NP_Name, "Basilisk II 60Hz", |
320 |
> |
NP_Entry, (ULONG)tick_func, |
321 |
> |
NP_Name, (ULONG)"Basilisk II 60Hz", |
322 |
|
NP_Priority, 5, |
323 |
|
TAG_END |
324 |
|
); |
326 |
|
// Set task priority to -1 so we don't use all processing time |
327 |
|
SetTaskPri(MainTask, -1); |
328 |
|
|
329 |
+ |
WriteMacInt32(0xbff, 0); // MacsBugFlags |
330 |
+ |
|
331 |
|
// Swap stack to Mac RAM area |
332 |
|
stack_swap.stk_Lower = RAMBaseHost; |
333 |
|
stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize; |
354 |
|
* Quit emulator (__saveds because it might be called from an exception) |
355 |
|
*/ |
356 |
|
|
357 |
< |
void __saveds QuitEmulator(void) |
357 |
> |
// Assembly entry point |
358 |
> |
void __saveds quit_emulator(void) |
359 |
> |
{ |
360 |
> |
QuitEmulator(); |
361 |
> |
} |
362 |
> |
|
363 |
> |
void QuitEmulator(void) |
364 |
|
{ |
365 |
+ |
// Stop 60Hz thread |
366 |
+ |
if (tick_proc) { |
367 |
+ |
SetSignal(0, SIGF_SINGLE); |
368 |
+ |
tick_proc_active = false; |
369 |
+ |
Wait(SIGF_SINGLE); |
370 |
+ |
} |
371 |
+ |
|
372 |
|
// Restore stack |
373 |
|
if (stack_swapped) { |
374 |
|
stack_swapped = false; |
382 |
|
FreeSignal(IRQSig); |
383 |
|
} |
384 |
|
|
372 |
– |
// Stop 60Hz thread |
373 |
– |
if (tick_proc) { |
374 |
– |
SetSignal(0, SIGF_SINGLE); |
375 |
– |
tick_proc_active = false; |
376 |
– |
Wait(SIGF_SINGLE); |
377 |
– |
} |
378 |
– |
|
385 |
|
// Remove trap handler |
386 |
|
MainTask->tc_TrapCode = OldTrapHandler; |
387 |
|
|
388 |
< |
// Save XPRAM |
389 |
< |
XPRAMExit(); |
384 |
< |
|
385 |
< |
// Exit video |
386 |
< |
VideoExit(); |
387 |
< |
|
388 |
< |
// Exit audio |
389 |
< |
AudioExit(); |
390 |
< |
|
391 |
< |
// Exit clipboard |
392 |
< |
ClipExit(); |
393 |
< |
|
394 |
< |
// Exit Time Manager |
395 |
< |
TimerExit(); |
396 |
< |
|
397 |
< |
// Exit serial ports |
398 |
< |
SerialExit(); |
399 |
< |
|
400 |
< |
// Exit network |
401 |
< |
EtherExit(); |
402 |
< |
|
403 |
< |
// Exit drivers |
404 |
< |
SCSIExit(); |
405 |
< |
CDROMExit(); |
406 |
< |
DiskExit(); |
407 |
< |
SonyExit(); |
388 |
> |
// Deinitialize everything |
389 |
> |
ExitAll(); |
390 |
|
|
391 |
|
// Delete RAM/ROM area |
392 |
|
if (RAMBaseHost) |
393 |
< |
FreeMem(RAMBaseHost, RAMSize + 0x100000); |
393 |
> |
FreeVec(RAMBaseHost); |
394 |
|
|
395 |
|
// Delete scratch memory area |
396 |
|
if (ScratchMem) |
417 |
|
PrefsExit(); |
418 |
|
|
419 |
|
// Close libraries |
420 |
+ |
if (CyberGfxBase) |
421 |
+ |
CloseLibrary(CyberGfxBase); |
422 |
|
if (P96Base) |
423 |
|
CloseLibrary(P96Base); |
424 |
|
if (AslBase) |
425 |
|
CloseLibrary(AslBase); |
426 |
+ |
if (IFFParseBase) |
427 |
+ |
CloseLibrary(IFFParseBase); |
428 |
|
if (GadToolsBase) |
429 |
|
CloseLibrary(GadToolsBase); |
430 |
+ |
if (IntuitionBase) |
431 |
+ |
CloseLibrary((struct Library *)IntuitionBase); |
432 |
+ |
if (GfxBase) |
433 |
+ |
CloseLibrary(GfxBase); |
434 |
|
|
435 |
|
exit(0); |
436 |
|
} |
468 |
|
Signal(MainTask, IRQSigMask); |
469 |
|
} |
470 |
|
|
471 |
+ |
void TriggerNMI(void) |
472 |
+ |
{ |
473 |
+ |
AsmTriggerNMI(); |
474 |
+ |
} |
475 |
+ |
|
476 |
|
|
477 |
|
/* |
478 |
|
* 60Hz thread |
515 |
|
if (++tick_counter > 60) { |
516 |
|
tick_counter = 0; |
517 |
|
WriteMacInt32(0x20c, TimerDateTime()); |
518 |
+ |
SetInterruptFlag(INTFLAG_1HZ); |
519 |
+ |
TriggerInterrupt(); |
520 |
|
} |
521 |
|
|
522 |
|
// Trigger 60Hz interrupt |
557 |
|
req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE); |
558 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX); |
559 |
|
req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON); |
560 |
< |
EasyRequest(NULL, &req, NULL, text); |
560 |
> |
EasyRequest(NULL, &req, NULL, (ULONG)text); |
561 |
|
} |
562 |
|
|
563 |
|
|
577 |
|
req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE); |
578 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX); |
579 |
|
req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON); |
580 |
< |
EasyRequest(NULL, &req, NULL, text); |
580 |
> |
EasyRequest(NULL, &req, NULL, (ULONG)text); |
581 |
|
} |
582 |
|
|
583 |
|
|
595 |
|
req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE); |
596 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX); |
597 |
|
req.es_GadgetFormat = (UBYTE *)str; |
598 |
< |
return EasyRequest(NULL, &req, NULL, text); |
598 |
> |
return EasyRequest(NULL, &req, NULL, (ULONG)text); |
599 |
|
} |
600 |
|
|
601 |
|
|
630 |
|
EmulatedSR |= 0x0700; |
631 |
|
|
632 |
|
// Call opcode routine |
633 |
< |
EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r); |
633 |
> |
EmulOp(opcode, (M68kRegisters *)r); |
634 |
|
r->pc += 2; |
635 |
|
|
636 |
|
// Restore interrupts |