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 |
|
|
75 |
|
uint8 *ROMBaseHost; // ROM base (host address space) |
76 |
|
uint32 ROMSize; // Size of ROM |
77 |
|
|
78 |
+ |
uint32 MacsBugFlags = 0xbff; |
79 |
+ |
|
80 |
|
|
81 |
|
// CPU and FPU type, addressing mode |
82 |
|
int CPUType; |
87 |
|
|
88 |
|
// Global variables |
89 |
|
extern ExecBase *SysBase; |
90 |
+ |
struct Library *GfxBase = NULL; |
91 |
+ |
struct IntuitionBase *IntuitionBase = NULL; |
92 |
|
struct Library *GadToolsBase = NULL; |
93 |
+ |
struct Library *IFFParseBase = NULL; |
94 |
|
struct Library *AslBase = NULL; |
95 |
|
struct Library *P96Base = NULL; |
96 |
+ |
struct Library *CyberGfxBase = NULL; |
97 |
|
struct Library *TimerBase = NULL; |
98 |
|
struct Library *AHIBase = NULL; |
99 |
|
struct Library *DiskBase = NULL; |
117 |
|
static StackSwapStruct stack_swap; |
118 |
|
|
119 |
|
|
109 |
– |
// Prototypes |
110 |
– |
static void jump_to_rom(void); |
111 |
– |
static void tick_func(void); |
112 |
– |
|
113 |
– |
|
120 |
|
// Assembly functions |
121 |
|
struct trap_regs; |
122 |
|
extern "C" void AtomicAnd(uint32 *p, uint32 val); |
126 |
|
extern "C" void ExceptionHandlerAsm(void); |
127 |
|
extern "C" void IllInstrHandler(trap_regs *regs); |
128 |
|
extern "C" void PrivViolHandler(trap_regs *regs); |
129 |
+ |
extern "C" void quit_emulator(void); |
130 |
|
uint16 EmulatedSR; // Emulated SR (supervisor bit and interrupt mask) |
131 |
|
|
132 |
|
|
133 |
+ |
// Prototypes |
134 |
+ |
static void jump_to_rom(void); |
135 |
+ |
static void tick_func(void); |
136 |
+ |
|
137 |
+ |
|
138 |
|
/* |
139 |
|
* Main program |
140 |
|
*/ |
153 |
|
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
154 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
155 |
|
|
144 |
– |
// Read preferences |
145 |
– |
PrefsInit(); |
146 |
– |
|
156 |
|
// Open libraries |
157 |
+ |
GfxBase = OpenLibrary((UBYTE *)"graphics.library", 39); |
158 |
+ |
if (GfxBase == NULL) { |
159 |
+ |
printf("Cannot open graphics.library V39.\n"); |
160 |
+ |
exit(1); |
161 |
+ |
} |
162 |
+ |
IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39); |
163 |
+ |
if (IntuitionBase == NULL) { |
164 |
+ |
printf("Cannot open intuition.library V39.\n"); |
165 |
+ |
CloseLibrary(GfxBase); |
166 |
+ |
exit(1); |
167 |
+ |
} |
168 |
|
DiskBase = (struct Library *)OpenResource((UBYTE *)"disk.resource"); |
169 |
|
if (DiskBase == NULL) |
170 |
|
QuitEmulator(); |
173 |
|
ErrorAlert(GetString(STR_NO_GADTOOLS_LIB_ERR)); |
174 |
|
QuitEmulator(); |
175 |
|
} |
176 |
+ |
IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library", 39); |
177 |
+ |
if (IFFParseBase == NULL) { |
178 |
+ |
ErrorAlert(GetString(STR_NO_IFFPARSE_LIB_ERR)); |
179 |
+ |
QuitEmulator(); |
180 |
+ |
} |
181 |
|
AslBase = OpenLibrary((UBYTE *)"asl.library", 36); |
182 |
|
if (AslBase == NULL) { |
183 |
|
ErrorAlert(GetString(STR_NO_ASL_LIB_ERR)); |
184 |
|
QuitEmulator(); |
185 |
|
} |
186 |
+ |
|
187 |
+ |
// These two can fail (the respective gfx support won't be available, then) |
188 |
|
P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2); |
189 |
+ |
CyberGfxBase = OpenLibrary((UBYTE *)"cybergraphics.library", 2); |
190 |
+ |
|
191 |
+ |
// Read preferences |
192 |
+ |
PrefsInit(); |
193 |
|
|
194 |
|
// Open AHI |
195 |
|
ahi_port = CreateMsgPort(); |
244 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
245 |
|
RAMSize = 1024*1024; |
246 |
|
} |
247 |
< |
RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC); |
248 |
< |
if (RAMBaseHost == NULL) { |
249 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
250 |
< |
QuitEmulator(); |
251 |
< |
} |
247 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
248 |
> |
// if (RAMBaseHost == NULL) { |
249 |
> |
// ErrorAlert(GetString(STR_NO_MEM_ERR)); |
250 |
> |
// QuitEmulator(); |
251 |
> |
// } |
252 |
> |
if (RAMBaseHost == NULL) |
253 |
> |
{ |
254 |
> |
uint32 newRAMSize = AvailMem(MEMF_LARGEST) - 0x100000; |
255 |
> |
char xText[120]; |
256 |
> |
|
257 |
> |
sprintf(xText, GetString(STR_NOT_ENOUGH_MEM_WARN), RAMSize, newRAMSize); |
258 |
> |
|
259 |
> |
if (1 != ChoiceAlert(xText, "Use", "Quit")) |
260 |
> |
QuitEmulator(); |
261 |
> |
|
262 |
> |
RAMSize = newRAMSize; |
263 |
> |
RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC); |
264 |
> |
if (RAMBaseHost == NULL) { |
265 |
> |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
266 |
> |
QuitEmulator(); |
267 |
> |
} |
268 |
> |
} |
269 |
|
RAMBaseMac = (uint32)RAMBaseHost; |
270 |
|
D(bug("Mac RAM starts at %08lx\n", RAMBaseHost)); |
271 |
|
ROMBaseHost = RAMBaseHost + RAMSize; |
276 |
|
const char *rom_path = PrefsFindString("rom"); |
277 |
|
|
278 |
|
// Load Mac ROM |
279 |
< |
BPTR rom_fh = Open(rom_path ? (char *)rom_path : ROM_FILE_NAME, MODE_OLDFILE); |
279 |
> |
BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE); |
280 |
|
if (rom_fh == NULL) { |
281 |
|
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
282 |
|
QuitEmulator(); |
296 |
|
QuitEmulator(); |
297 |
|
} |
298 |
|
|
251 |
– |
// Check ROM version |
252 |
– |
if (!CheckROM()) { |
253 |
– |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
254 |
– |
QuitEmulator(); |
255 |
– |
} |
256 |
– |
|
299 |
|
// Set CPU and FPU type |
300 |
|
UWORD attn = SysBase->AttnFlags; |
301 |
|
CPUType = attn & AFF_68040 ? 4 : (attn & AFF_68030 ? 3 : 2); |
302 |
|
CPUIs68060 = attn & AFF_68060; |
303 |
|
FPUType = attn & AFF_68881 ? 1 : 0; |
304 |
|
|
305 |
< |
// Load XPRAM |
306 |
< |
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)) |
305 |
> |
// Initialize everything |
306 |
> |
if (!InitAll()) |
307 |
|
QuitEmulator(); |
308 |
|
|
299 |
– |
// Install ROM patches |
300 |
– |
if (!PatchROM()) { |
301 |
– |
ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR)); |
302 |
– |
QuitEmulator(); |
303 |
– |
} |
304 |
– |
|
309 |
|
// Move VBR away from 0 if neccessary |
310 |
|
MoveVBR(); |
311 |
|
|
323 |
|
|
324 |
|
// Start 60Hz process |
325 |
|
tick_proc = CreateNewProcTags( |
326 |
< |
NP_Entry, tick_func, |
327 |
< |
NP_Name, "Basilisk II 60Hz", |
326 |
> |
NP_Entry, (ULONG)tick_func, |
327 |
> |
NP_Name, (ULONG)"Basilisk II 60Hz", |
328 |
|
NP_Priority, 5, |
329 |
|
TAG_END |
330 |
|
); |
332 |
|
// Set task priority to -1 so we don't use all processing time |
333 |
|
SetTaskPri(MainTask, -1); |
334 |
|
|
335 |
+ |
WriteMacInt32(MacsBugFlags, 0); |
336 |
+ |
|
337 |
|
// Swap stack to Mac RAM area |
338 |
|
stack_swap.stk_Lower = RAMBaseHost; |
339 |
|
stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize; |
360 |
|
* Quit emulator (__saveds because it might be called from an exception) |
361 |
|
*/ |
362 |
|
|
363 |
< |
void __saveds QuitEmulator(void) |
363 |
> |
// Assembly entry point |
364 |
> |
void __saveds quit_emulator(void) |
365 |
|
{ |
366 |
+ |
QuitEmulator(); |
367 |
+ |
} |
368 |
+ |
|
369 |
+ |
void QuitEmulator(void) |
370 |
+ |
{ |
371 |
+ |
// Stop 60Hz thread |
372 |
+ |
if (tick_proc) { |
373 |
+ |
SetSignal(0, SIGF_SINGLE); |
374 |
+ |
tick_proc_active = false; |
375 |
+ |
Wait(SIGF_SINGLE); |
376 |
+ |
} |
377 |
+ |
|
378 |
|
// Restore stack |
379 |
|
if (stack_swapped) { |
380 |
|
stack_swapped = false; |
388 |
|
FreeSignal(IRQSig); |
389 |
|
} |
390 |
|
|
372 |
– |
// Stop 60Hz thread |
373 |
– |
if (tick_proc) { |
374 |
– |
SetSignal(0, SIGF_SINGLE); |
375 |
– |
tick_proc_active = false; |
376 |
– |
Wait(SIGF_SINGLE); |
377 |
– |
} |
378 |
– |
|
391 |
|
// Remove trap handler |
392 |
|
MainTask->tc_TrapCode = OldTrapHandler; |
393 |
|
|
394 |
< |
// Save XPRAM |
395 |
< |
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(); |
394 |
> |
// Deinitialize everything |
395 |
> |
ExitAll(); |
396 |
|
|
397 |
|
// Delete RAM/ROM area |
398 |
|
if (RAMBaseHost) |
399 |
< |
FreeMem(RAMBaseHost, RAMSize + 0x100000); |
399 |
> |
FreeVec(RAMBaseHost); |
400 |
|
|
401 |
|
// Delete scratch memory area |
402 |
|
if (ScratchMem) |
423 |
|
PrefsExit(); |
424 |
|
|
425 |
|
// Close libraries |
426 |
+ |
if (CyberGfxBase) |
427 |
+ |
CloseLibrary(CyberGfxBase); |
428 |
|
if (P96Base) |
429 |
|
CloseLibrary(P96Base); |
430 |
|
if (AslBase) |
431 |
|
CloseLibrary(AslBase); |
432 |
+ |
if (IFFParseBase) |
433 |
+ |
CloseLibrary(IFFParseBase); |
434 |
|
if (GadToolsBase) |
435 |
|
CloseLibrary(GadToolsBase); |
436 |
+ |
if (IntuitionBase) |
437 |
+ |
CloseLibrary((struct Library *)IntuitionBase); |
438 |
+ |
if (GfxBase) |
439 |
+ |
CloseLibrary(GfxBase); |
440 |
|
|
441 |
|
exit(0); |
442 |
|
} |
474 |
|
Signal(MainTask, IRQSigMask); |
475 |
|
} |
476 |
|
|
477 |
+ |
void TriggerNMI(void) |
478 |
+ |
{ |
479 |
+ |
AsmTriggerNMI(); |
480 |
+ |
} |
481 |
+ |
|
482 |
|
|
483 |
|
/* |
484 |
|
* 60Hz thread |
521 |
|
if (++tick_counter > 60) { |
522 |
|
tick_counter = 0; |
523 |
|
WriteMacInt32(0x20c, TimerDateTime()); |
524 |
+ |
SetInterruptFlag(INTFLAG_1HZ); |
525 |
+ |
TriggerInterrupt(); |
526 |
|
} |
527 |
|
|
528 |
|
// Trigger 60Hz interrupt |
563 |
|
req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE); |
564 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX); |
565 |
|
req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON); |
566 |
< |
EasyRequest(NULL, &req, NULL, text); |
566 |
> |
EasyRequest(NULL, &req, NULL, (ULONG)text); |
567 |
|
} |
568 |
|
|
569 |
|
|
583 |
|
req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE); |
584 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX); |
585 |
|
req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON); |
586 |
< |
EasyRequest(NULL, &req, NULL, text); |
586 |
> |
EasyRequest(NULL, &req, NULL, (ULONG)text); |
587 |
|
} |
588 |
|
|
589 |
|
|
601 |
|
req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE); |
602 |
|
req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX); |
603 |
|
req.es_GadgetFormat = (UBYTE *)str; |
604 |
< |
return EasyRequest(NULL, &req, NULL, text); |
604 |
> |
return EasyRequest(NULL, &req, NULL, (ULONG)text); |
605 |
|
} |
606 |
|
|
607 |
|
|
636 |
|
EmulatedSR |= 0x0700; |
637 |
|
|
638 |
|
// Call opcode routine |
639 |
< |
EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r); |
639 |
> |
EmulOp(opcode, (M68kRegisters *)r); |
640 |
|
r->pc += 2; |
641 |
|
|
642 |
|
// Restore interrupts |