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; |
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 |
|
uint16 EmulatedSR; // Emulated SR (supervisor bit and interrupt mask) |
129 |
|
|
130 |
|
|
131 |
+ |
// Prototypes |
132 |
+ |
static void jump_to_rom(void); |
133 |
+ |
static void tick_func(void); |
134 |
+ |
|
135 |
+ |
|
136 |
|
/* |
137 |
|
* Main program |
138 |
|
*/ |
151 |
|
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
152 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
153 |
|
|
144 |
– |
// Read preferences |
145 |
– |
PrefsInit(); |
146 |
– |
|
154 |
|
// Open libraries |
155 |
+ |
GfxBase = OpenLibrary((UBYTE *)"graphics.library", 39); |
156 |
+ |
if (GfxBase == NULL) { |
157 |
+ |
printf("Cannot open graphics.library V39.\n"); |
158 |
+ |
exit(1); |
159 |
+ |
} |
160 |
+ |
IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39); |
161 |
+ |
if (IntuitionBase == NULL) { |
162 |
+ |
printf("Cannot open intuition.library V39.\n"); |
163 |
+ |
CloseLibrary(GfxBase); |
164 |
+ |
exit(1); |
165 |
+ |
} |
166 |
|
DiskBase = (struct Library *)OpenResource((UBYTE *)"disk.resource"); |
167 |
|
if (DiskBase == NULL) |
168 |
|
QuitEmulator(); |
171 |
|
ErrorAlert(GetString(STR_NO_GADTOOLS_LIB_ERR)); |
172 |
|
QuitEmulator(); |
173 |
|
} |
174 |
+ |
IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library", 39); |
175 |
+ |
if (IFFParseBase == NULL) { |
176 |
+ |
ErrorAlert(GetString(STR_NO_IFFPARSE_LIB_ERR)); |
177 |
+ |
QuitEmulator(); |
178 |
+ |
} |
179 |
|
AslBase = OpenLibrary((UBYTE *)"asl.library", 36); |
180 |
|
if (AslBase == NULL) { |
181 |
|
ErrorAlert(GetString(STR_NO_ASL_LIB_ERR)); |
182 |
|
QuitEmulator(); |
183 |
|
} |
184 |
+ |
|
185 |
+ |
// These two can fail (the respective gfx support won't be available, then) |
186 |
|
P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2); |
187 |
+ |
CyberGfxBase = OpenLibrary((UBYTE *)"cybergraphics.library", 2); |
188 |
+ |
|
189 |
+ |
// Read preferences |
190 |
+ |
PrefsInit(); |
191 |
|
|
192 |
|
// Open AHI |
193 |
|
ahi_port = CreateMsgPort(); |
242 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
243 |
|
RAMSize = 1024*1024; |
244 |
|
} |
245 |
< |
RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC); |
245 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
246 |
|
if (RAMBaseHost == NULL) { |
247 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
248 |
< |
QuitEmulator(); |
247 |
> |
uint32 newRAMSize = AvailMem(MEMF_LARGEST) - 0x100000; |
248 |
> |
char xText[120]; |
249 |
> |
|
250 |
> |
sprintf(xText, GetString(STR_NOT_ENOUGH_MEM_WARN), RAMSize, newRAMSize); |
251 |
> |
|
252 |
> |
if (ChoiceAlert(xText, "Use", "Quit") != 1) |
253 |
> |
QuitEmulator(); |
254 |
> |
|
255 |
> |
RAMSize = newRAMSize; |
256 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
257 |
> |
if (RAMBaseHost == NULL) { |
258 |
> |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
259 |
> |
QuitEmulator(); |
260 |
> |
} |
261 |
|
} |
262 |
|
RAMBaseMac = (uint32)RAMBaseHost; |
263 |
|
D(bug("Mac RAM starts at %08lx\n", RAMBaseHost)); |
269 |
|
const char *rom_path = PrefsFindString("rom"); |
270 |
|
|
271 |
|
// Load Mac ROM |
272 |
< |
BPTR rom_fh = Open(rom_path ? (char *)rom_path : ROM_FILE_NAME, MODE_OLDFILE); |
272 |
> |
BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE); |
273 |
|
if (rom_fh == NULL) { |
274 |
|
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
275 |
|
QuitEmulator(); |
289 |
|
QuitEmulator(); |
290 |
|
} |
291 |
|
|
251 |
– |
// Check ROM version |
252 |
– |
if (!CheckROM()) { |
253 |
– |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
254 |
– |
QuitEmulator(); |
255 |
– |
} |
256 |
– |
|
292 |
|
// Set CPU and FPU type |
293 |
|
UWORD attn = SysBase->AttnFlags; |
294 |
|
CPUType = attn & AFF_68040 ? 4 : (attn & AFF_68030 ? 3 : 2); |
295 |
|
CPUIs68060 = attn & AFF_68060; |
296 |
|
FPUType = attn & AFF_68881 ? 1 : 0; |
297 |
|
|
298 |
< |
// Load XPRAM |
299 |
< |
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)) |
298 |
> |
// Initialize everything |
299 |
> |
if (!InitAll()) |
300 |
|
QuitEmulator(); |
301 |
|
|
299 |
– |
// Install ROM patches |
300 |
– |
if (!PatchROM()) { |
301 |
– |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
302 |
– |
QuitEmulator(); |
303 |
– |
} |
304 |
– |
|
302 |
|
// Move VBR away from 0 if neccessary |
303 |
|
MoveVBR(); |
304 |
|
|
316 |
|
|
317 |
|
// Start 60Hz process |
318 |
|
tick_proc = CreateNewProcTags( |
319 |
< |
NP_Entry, tick_func, |
320 |
< |
NP_Name, "Basilisk II 60Hz", |
319 |
> |
NP_Entry, (ULONG)tick_func, |
320 |
> |
NP_Name, (ULONG)"Basilisk II 60Hz", |
321 |
|
NP_Priority, 5, |
322 |
|
TAG_END |
323 |
|
); |
325 |
|
// Set task priority to -1 so we don't use all processing time |
326 |
|
SetTaskPri(MainTask, -1); |
327 |
|
|
328 |
+ |
WriteMacInt32(0xbff, 0); // MacsBugFlags |
329 |
+ |
|
330 |
|
// Swap stack to Mac RAM area |
331 |
|
stack_swap.stk_Lower = RAMBaseHost; |
332 |
|
stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize; |
353 |
|
* Quit emulator (__saveds because it might be called from an exception) |
354 |
|
*/ |
355 |
|
|
356 |
< |
void __saveds QuitEmulator(void) |
356 |
> |
// Assembly entry point |
357 |
> |
void __saveds quit_emulator(void) |
358 |
> |
{ |
359 |
> |
QuitEmulator(); |
360 |
> |
} |
361 |
> |
|
362 |
> |
void QuitEmulator(void) |
363 |
|
{ |
364 |
+ |
// Stop 60Hz thread |
365 |
+ |
if (tick_proc) { |
366 |
+ |
SetSignal(0, SIGF_SINGLE); |
367 |
+ |
tick_proc_active = false; |
368 |
+ |
Wait(SIGF_SINGLE); |
369 |
+ |
} |
370 |
+ |
|
371 |
|
// Restore stack |
372 |
|
if (stack_swapped) { |
373 |
|
stack_swapped = false; |
381 |
|
FreeSignal(IRQSig); |
382 |
|
} |
383 |
|
|
372 |
– |
// Stop 60Hz thread |
373 |
– |
if (tick_proc) { |
374 |
– |
SetSignal(0, SIGF_SINGLE); |
375 |
– |
tick_proc_active = false; |
376 |
– |
Wait(SIGF_SINGLE); |
377 |
– |
} |
378 |
– |
|
384 |
|
// Remove trap handler |
385 |
|
MainTask->tc_TrapCode = OldTrapHandler; |
386 |
|
|
387 |
< |
// Save XPRAM |
388 |
< |
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(); |
387 |
> |
// Deinitialize everything |
388 |
> |
ExitAll(); |
389 |
|
|
390 |
|
// Delete RAM/ROM area |
391 |
|
if (RAMBaseHost) |
392 |
< |
FreeMem(RAMBaseHost, RAMSize + 0x100000); |
392 |
> |
FreeVec(RAMBaseHost); |
393 |
|
|
394 |
|
// Delete scratch memory area |
395 |
|
if (ScratchMem) |
416 |
|
PrefsExit(); |
417 |
|
|
418 |
|
// Close libraries |
419 |
+ |
if (CyberGfxBase) |
420 |
+ |
CloseLibrary(CyberGfxBase); |
421 |
|
if (P96Base) |
422 |
|
CloseLibrary(P96Base); |
423 |
|
if (AslBase) |
424 |
|
CloseLibrary(AslBase); |
425 |
+ |
if (IFFParseBase) |
426 |
+ |
CloseLibrary(IFFParseBase); |
427 |
|
if (GadToolsBase) |
428 |
|
CloseLibrary(GadToolsBase); |
429 |
+ |
if (IntuitionBase) |
430 |
+ |
CloseLibrary((struct Library *)IntuitionBase); |
431 |
+ |
if (GfxBase) |
432 |
+ |
CloseLibrary(GfxBase); |
433 |
|
|
434 |
|
exit(0); |
435 |
|
} |
467 |
|
Signal(MainTask, IRQSigMask); |
468 |
|
} |
469 |
|
|
470 |
+ |
void TriggerNMI(void) |
471 |
+ |
{ |
472 |
+ |
AsmTriggerNMI(); |
473 |
+ |
} |
474 |
+ |
|
475 |
|
|
476 |
|
/* |
477 |
|
* 60Hz thread |
514 |
|
if (++tick_counter > 60) { |
515 |
|
tick_counter = 0; |
516 |
|
WriteMacInt32(0x20c, TimerDateTime()); |
517 |
+ |
SetInterruptFlag(INTFLAG_1HZ); |
518 |
+ |
TriggerInterrupt(); |
519 |
|
} |
520 |
|
|
521 |
|
// Trigger 60Hz interrupt |
556 |
|
req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE); |
557 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX); |
558 |
|
req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON); |
559 |
< |
EasyRequest(NULL, &req, NULL, text); |
559 |
> |
EasyRequest(NULL, &req, NULL, (ULONG)text); |
560 |
|
} |
561 |
|
|
562 |
|
|
576 |
|
req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE); |
577 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX); |
578 |
|
req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON); |
579 |
< |
EasyRequest(NULL, &req, NULL, text); |
579 |
> |
EasyRequest(NULL, &req, NULL, (ULONG)text); |
580 |
|
} |
581 |
|
|
582 |
|
|
594 |
|
req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE); |
595 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX); |
596 |
|
req.es_GadgetFormat = (UBYTE *)str; |
597 |
< |
return EasyRequest(NULL, &req, NULL, text); |
597 |
> |
return EasyRequest(NULL, &req, NULL, (ULONG)text); |
598 |
|
} |
599 |
|
|
600 |
|
|
629 |
|
EmulatedSR |= 0x0700; |
630 |
|
|
631 |
|
// Call opcode routine |
632 |
< |
EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r); |
632 |
> |
EmulOp(opcode, (M68kRegisters *)r); |
633 |
|
r->pc += 2; |
634 |
|
|
635 |
|
// Restore interrupts |