ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/main_amiga.cpp
Revision: 1.8
Committed: 2000-07-22T16:00:34Z (24 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
- small fixes for NetBSD and AmigaOS

File Contents

# Content
1 /*
2 * main_amiga.cpp - Startup code for AmigaOS
3 *
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
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <exec/types.h>
22 #include <exec/execbase.h>
23 #include <exec/memory.h>
24 #include <exec/tasks.h>
25 #include <dos/dostags.h>
26 #include <intuition/intuition.h>
27 #include <devices/timer.h>
28 #include <devices/ahi.h>
29 #include <proto/exec.h>
30 #include <proto/dos.h>
31 #include <proto/intuition.h>
32
33 #include "sysdeps.h"
34 #include "cpu_emulation.h"
35 #include "main.h"
36 #include "xpram.h"
37 #include "timer.h"
38 #include "sony.h"
39 #include "disk.h"
40 #include "cdrom.h"
41 #include "scsi.h"
42 #include "audio.h"
43 #include "video.h"
44 #include "serial.h"
45 #include "ether.h"
46 #include "clip.h"
47 #include "emul_op.h"
48 #include "rom_patches.h"
49 #include "prefs.h"
50 #include "prefs_editor.h"
51 #include "sys.h"
52 #include "user_strings.h"
53 #include "version.h"
54
55 #define DEBUG 0
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 " " __DATE__;
67 static const int SCRATCH_MEM_SIZE = 65536;
68
69
70 // RAM and ROM pointers
71 uint32 RAMBaseMac; // RAM base (Mac address space)
72 uint8 *RAMBaseHost; // RAM base (host address space)
73 uint32 RAMSize; // Size of RAM
74 uint32 ROMBaseMac; // ROM base (Mac address space)
75 uint8 *ROMBaseHost; // ROM base (host address space)
76 uint32 ROMSize; // Size of ROM
77
78
79 // CPU and FPU type, addressing mode
80 int CPUType;
81 bool CPUIs68060;
82 int FPUType;
83 bool TwentyFourBitAddressing;
84
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
101 APTR OldTrapHandler = NULL; // Old trap handler
102 APTR OldExceptionHandler = NULL; // Old exception handler
103 BYTE IRQSig = -1; // "Interrupt" signal number
104 ULONG IRQSigMask = 0; // "Interrupt" signal mask
105
106 static struct timerequest *timereq = NULL; // IORequest for timer
107
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 *tick_proc = NULL; // 60Hz process
112 static volatile bool tick_proc_active = true; // Flag for quitting the 60Hz process
113
114 static bool stack_swapped = false; // Stack swapping
115 static StackSwapStruct stack_swap;
116
117
118 // Assembly functions
119 struct trap_regs;
120 extern "C" void AtomicAnd(uint32 *p, uint32 val);
121 extern "C" void AtomicOr(uint32 *p, uint32 val);
122 extern "C" void MoveVBR(void);
123 extern "C" void TrapHandlerAsm(void);
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 */
139
140 int main(void)
141 {
142 // Initialize variables
143 RAMBaseHost = NULL;
144 ROMBaseHost = NULL;
145 MainTask = FindTask(NULL);
146 struct DateStamp ds;
147 DateStamp(&ds);
148 srand(ds.ds_Tick);
149
150 // Print some info
151 printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
152 printf(" %s\n", GetString(STR_ABOUT_TEXT2));
153
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();
169 GadToolsBase = OpenLibrary((UBYTE *)"gadtools.library", 39);
170 if (GadToolsBase == NULL) {
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();
194 if (ahi_port) {
195 ahi_io = (struct AHIRequest *)CreateIORequest(ahi_port, sizeof(struct AHIRequest));
196 if (ahi_io) {
197 ahi_io->ahir_Version = 2;
198 if (OpenDevice((UBYTE *)AHINAME, AHI_NO_UNIT, (struct IORequest *)ahi_io, 0) == 0) {
199 AHIBase = (struct Library *)ahi_io->ahir_Std.io_Device;
200 }
201 }
202 }
203
204 // Init system routines
205 SysInit();
206
207 // Show preferences editor
208 if (!PrefsFindBool("nogui"))
209 if (!PrefsEditor())
210 QuitEmulator();
211
212 // Check start of Chip memory (because we need access to 0x0000..0x2000)
213 if ((uint32)FindName(&SysBase->MemList, (UBYTE *)"chip memory") < 0x2000) {
214 ErrorAlert(GetString(STR_NO_PREPARE_EMUL_ERR));
215 QuitEmulator();
216 }
217
218 // Open timer.device
219 timereq = (struct timerequest *)AllocVec(sizeof(timerequest), MEMF_PUBLIC | MEMF_CLEAR);
220 if (timereq == NULL) {
221 ErrorAlert(GetString(STR_NO_MEM_ERR));
222 QuitEmulator();
223 }
224 if (OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timereq, 0)) {
225 ErrorAlert(GetString(STR_NO_TIMER_DEV_ERR));
226 QuitEmulator();
227 }
228 TimerBase = (struct Library *)timereq->tr_node.io_Device;
229
230 // Allocate scratch memory
231 ScratchMem = (uint32)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC);
232 if (ScratchMem == NULL) {
233 ErrorAlert(GetString(STR_NO_MEM_ERR));
234 QuitEmulator();
235 }
236 ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
237
238 // Create area for Mac RAM and ROM (ROM must be higher in memory,
239 // so we allocate one big chunk and put the ROM at the top of it)
240 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
241 if (RAMSize < 1024*1024) {
242 WarningAlert(GetString(STR_SMALL_RAM_WARN));
243 RAMSize = 1024*1024;
244 }
245 RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC);
246 if (RAMBaseHost == NULL) {
247 ErrorAlert(GetString(STR_NO_MEM_ERR));
248 QuitEmulator();
249 }
250 RAMBaseMac = (uint32)RAMBaseHost;
251 D(bug("Mac RAM starts at %08lx\n", RAMBaseHost));
252 ROMBaseHost = RAMBaseHost + RAMSize;
253 ROMBaseMac = (uint32)ROMBaseHost;
254 D(bug("Mac ROM starts at %08lx\n", ROMBaseHost));
255
256 // Get rom file path from preferences
257 const char *rom_path = PrefsFindString("rom");
258
259 // Load Mac ROM
260 BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE);
261 if (rom_fh == NULL) {
262 ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
263 QuitEmulator();
264 }
265 printf(GetString(STR_READING_ROM_FILE));
266 Seek(rom_fh, 0, OFFSET_END);
267 ROMSize = Seek(rom_fh, 0, OFFSET_CURRENT);
268 if (ROMSize != 512*1024 && ROMSize != 1024*1024) {
269 ErrorAlert(GetString(STR_ROM_SIZE_ERR));
270 Close(rom_fh);
271 QuitEmulator();
272 }
273 Seek(rom_fh, 0, OFFSET_BEGINNING);
274 if (Read(rom_fh, ROMBaseHost, ROMSize) != ROMSize) {
275 ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
276 Close(rom_fh);
277 QuitEmulator();
278 }
279
280 // Set CPU and FPU type
281 UWORD attn = SysBase->AttnFlags;
282 CPUType = attn & AFF_68040 ? 4 : (attn & AFF_68030 ? 3 : 2);
283 CPUIs68060 = attn & AFF_68060;
284 FPUType = attn & AFF_68881 ? 1 : 0;
285
286 // Initialize everything
287 if (!InitAll())
288 QuitEmulator();
289
290 // Move VBR away from 0 if neccessary
291 MoveVBR();
292
293 // Install trap handler
294 EmulatedSR = 0x2700;
295 OldTrapHandler = MainTask->tc_TrapCode;
296 MainTask->tc_TrapCode = (APTR)TrapHandlerAsm;
297
298 // Allocate signal for interrupt emulation and install exception handler
299 IRQSig = AllocSignal(-1);
300 IRQSigMask = 1 << IRQSig;
301 OldExceptionHandler = MainTask->tc_ExceptCode;
302 MainTask->tc_ExceptCode = (APTR)ExceptionHandlerAsm;
303 SetExcept(SIGBREAKF_CTRL_C | IRQSigMask, SIGBREAKF_CTRL_C | IRQSigMask);
304
305 // Start 60Hz process
306 tick_proc = CreateNewProcTags(
307 NP_Entry, (ULONG)tick_func,
308 NP_Name, (ULONG)"Basilisk II 60Hz",
309 NP_Priority, 5,
310 TAG_END
311 );
312
313 // Set task priority to -1 so we don't use all processing time
314 SetTaskPri(MainTask, -1);
315
316 // Swap stack to Mac RAM area
317 stack_swap.stk_Lower = RAMBaseHost;
318 stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize;
319 stack_swap.stk_Pointer = RAMBaseHost + 0x8000;
320 StackSwap(&stack_swap);
321 stack_swapped = true;
322
323 // Jump to ROM boot routine
324 Start680x0();
325
326 QuitEmulator();
327 return 0;
328 }
329
330 void Start680x0(void)
331 {
332 typedef void (*rom_func)(void);
333 rom_func fp = (rom_func)(ROMBaseHost + 0x2a);
334 fp();
335 }
336
337
338 /*
339 * Quit emulator (__saveds because it might be called from an exception)
340 */
341
342 // Assembly entry point
343 void __saveds quit_emulator(void)
344 {
345 QuitEmulator();
346 }
347
348 void QuitEmulator(void)
349 {
350 // Stop 60Hz thread
351 if (tick_proc) {
352 SetSignal(0, SIGF_SINGLE);
353 tick_proc_active = false;
354 Wait(SIGF_SINGLE);
355 }
356
357 // Restore stack
358 if (stack_swapped) {
359 stack_swapped = false;
360 StackSwap(&stack_swap);
361 }
362
363 // Remove exception handler
364 if (IRQSig >= 0) {
365 SetExcept(0, SIGBREAKF_CTRL_C | IRQSigMask);
366 MainTask->tc_ExceptCode = OldExceptionHandler;
367 FreeSignal(IRQSig);
368 }
369
370 // Remove trap handler
371 MainTask->tc_TrapCode = OldTrapHandler;
372
373 // Deinitialize everything
374 ExitAll();
375
376 // Delete RAM/ROM area
377 if (RAMBaseHost)
378 FreeMem(RAMBaseHost, RAMSize + 0x100000);
379
380 // Delete scratch memory area
381 if (ScratchMem)
382 FreeMem((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
383
384 // Close timer.device
385 if (TimerBase)
386 CloseDevice((struct IORequest *)timereq);
387 if (timereq)
388 FreeVec(timereq);
389
390 // Exit system routines
391 SysExit();
392
393 // Close AHI
394 if (AHIBase)
395 CloseDevice((struct IORequest *)ahi_io);
396 if (ahi_io)
397 DeleteIORequest((struct IORequest *)ahi_io);
398 if (ahi_port)
399 DeleteMsgPort(ahi_port);
400
401 // Exit preferences
402 PrefsExit();
403
404 // Close libraries
405 if (CyberGfxBase)
406 CloseLibrary(CyberGfxBase);
407 if (P96Base)
408 CloseLibrary(P96Base);
409 if (AslBase)
410 CloseLibrary(AslBase);
411 if (IFFParseBase)
412 CloseLibrary(IFFParseBase);
413 if (GadToolsBase)
414 CloseLibrary(GadToolsBase);
415 if (IntuitionBase)
416 CloseLibrary((struct Library *)IntuitionBase);
417 if (GfxBase)
418 CloseLibrary(GfxBase);
419
420 exit(0);
421 }
422
423
424 /*
425 * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
426 * or a dynamically recompiling emulator)
427 */
428
429 void FlushCodeCache(void *start, uint32 size)
430 {
431 CacheClearE(start, size, CACRF_ClearI | CACRF_ClearD);
432 }
433
434
435 /*
436 * Interrupt flags (must be handled atomically!)
437 */
438
439 uint32 InterruptFlags;
440
441 void SetInterruptFlag(uint32 flag)
442 {
443 AtomicOr(&InterruptFlags, flag);
444 }
445
446 void ClearInterruptFlag(uint32 flag)
447 {
448 AtomicAnd(&InterruptFlags, ~flag);
449 }
450
451 void TriggerInterrupt(void)
452 {
453 Signal(MainTask, IRQSigMask);
454 }
455
456
457 /*
458 * 60Hz thread
459 */
460
461 static __saveds void tick_func(void)
462 {
463 int tick_counter = 0;
464 struct MsgPort *timer_port = NULL;
465 struct timerequest *timer_io = NULL;
466 ULONG timer_mask = 0;
467
468 // Start 60Hz timer
469 timer_port = CreateMsgPort();
470 if (timer_port) {
471 timer_io = (struct timerequest *)CreateIORequest(timer_port, sizeof(struct timerequest));
472 if (timer_io) {
473 if (!OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) {
474 timer_mask = 1 << timer_port->mp_SigBit;
475 timer_io->tr_node.io_Command = TR_ADDREQUEST;
476 timer_io->tr_time.tv_secs = 0;
477 timer_io->tr_time.tv_micro = 16625;
478 SendIO((struct IORequest *)timer_io);
479 }
480 }
481 }
482
483 while (tick_proc_active) {
484
485 // Wait for timer tick
486 Wait(timer_mask);
487
488 // Restart timer
489 timer_io->tr_node.io_Command = TR_ADDREQUEST;
490 timer_io->tr_time.tv_secs = 0;
491 timer_io->tr_time.tv_micro = 16625;
492 SendIO((struct IORequest *)timer_io);
493
494 // Pseudo Mac 1Hz interrupt, update local time
495 if (++tick_counter > 60) {
496 tick_counter = 0;
497 WriteMacInt32(0x20c, TimerDateTime());
498 SetInterruptFlag(INTFLAG_1HZ);
499 TriggerInterrupt();
500 }
501
502 // Trigger 60Hz interrupt
503 SetInterruptFlag(INTFLAG_60HZ);
504 TriggerInterrupt();
505 }
506
507 // Stop timer
508 if (timer_io) {
509 if (!CheckIO((struct IORequest *)timer_io))
510 AbortIO((struct IORequest *)timer_io);
511 WaitIO((struct IORequest *)timer_io);
512 CloseDevice((struct IORequest *)timer_io);
513 DeleteIORequest(timer_io);
514 }
515 if (timer_port)
516 DeleteMsgPort(timer_port);
517
518 // Main task asked for termination, send signal
519 Forbid();
520 Signal(MainTask, SIGF_SINGLE);
521 }
522
523
524 /*
525 * Display error alert
526 */
527
528 void ErrorAlert(const char *text)
529 {
530 if (PrefsFindBool("nogui")) {
531 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
532 return;
533 }
534 EasyStruct req;
535 req.es_StructSize = sizeof(EasyStruct);
536 req.es_Flags = 0;
537 req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE);
538 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX);
539 req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON);
540 EasyRequest(NULL, &req, NULL, (ULONG)text);
541 }
542
543
544 /*
545 * Display warning alert
546 */
547
548 void WarningAlert(const char *text)
549 {
550 if (PrefsFindBool("nogui")) {
551 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
552 return;
553 }
554 EasyStruct req;
555 req.es_StructSize = sizeof(EasyStruct);
556 req.es_Flags = 0;
557 req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
558 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
559 req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON);
560 EasyRequest(NULL, &req, NULL, (ULONG)text);
561 }
562
563
564 /*
565 * Display choice alert
566 */
567
568 bool ChoiceAlert(const char *text, const char *pos, const char *neg)
569 {
570 char str[256];
571 sprintf(str, "%s|%s", pos, neg);
572 EasyStruct req;
573 req.es_StructSize = sizeof(EasyStruct);
574 req.es_Flags = 0;
575 req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
576 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
577 req.es_GadgetFormat = (UBYTE *)str;
578 return EasyRequest(NULL, &req, NULL, (ULONG)text);
579 }
580
581
582 /*
583 * Illegal Instruction and Privilege Violation trap handlers
584 */
585
586 struct trap_regs { // This must match the layout of M68kRegisters
587 uint32 d[8];
588 uint32 a[8];
589 uint16 sr;
590 uint32 pc;
591 };
592
593 void __saveds IllInstrHandler(trap_regs *r)
594 {
595 uint16 opcode = *(uint16 *)(r->pc);
596 if ((opcode & 0xff00) != 0x7100) {
597 printf("Illegal Instruction %04x at %08lx\n", *(uint16 *)(r->pc), r->pc);
598 printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
599 "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
600 "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
601 "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
602 "sr %04x\n",
603 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
604 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
605 r->sr);
606 QuitEmulator();
607 } else {
608 // Disable interrupts
609 uint16 sr = EmulatedSR;
610 EmulatedSR |= 0x0700;
611
612 // Call opcode routine
613 EmulOp(opcode, (M68kRegisters *)r);
614 r->pc += 2;
615
616 // Restore interrupts
617 EmulatedSR = sr;
618 if ((EmulatedSR & 0x0700) == 0 && InterruptFlags)
619 Signal(MainTask, IRQSigMask);
620 }
621 }
622
623 void __saveds PrivViolHandler(trap_regs *r)
624 {
625 printf("Privileged instruction %04x %04x at %08lx\n", *(uint16 *)(r->pc), *(uint16 *)(r->pc + 2), r->pc);
626 printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
627 "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
628 "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
629 "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
630 "sr %04x\n",
631 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
632 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
633 r->sr);
634 QuitEmulator();
635 }