ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/main_amiga.cpp
Revision: 1.6
Committed: 2000-07-06T16:04:24Z (24 years, 4 months ago) by cebix
Branch: MAIN
CVS Tags: snapshot-13072000
Changes since 1.5: +6 -0 lines
Log Message:
- AmigaOS: added CyberGraphX support

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 // Restore stack
351 if (stack_swapped) {
352 stack_swapped = false;
353 StackSwap(&stack_swap);
354 }
355
356 // Remove exception handler
357 if (IRQSig >= 0) {
358 SetExcept(0, SIGBREAKF_CTRL_C | IRQSigMask);
359 MainTask->tc_ExceptCode = OldExceptionHandler;
360 FreeSignal(IRQSig);
361 }
362
363 // Stop 60Hz thread
364 if (tick_proc) {
365 SetSignal(0, SIGF_SINGLE);
366 tick_proc_active = false;
367 Wait(SIGF_SINGLE);
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 }
499
500 // Trigger 60Hz interrupt
501 SetInterruptFlag(INTFLAG_60HZ);
502 TriggerInterrupt();
503 }
504
505 // Stop timer
506 if (timer_io) {
507 if (!CheckIO((struct IORequest *)timer_io))
508 AbortIO((struct IORequest *)timer_io);
509 WaitIO((struct IORequest *)timer_io);
510 CloseDevice((struct IORequest *)timer_io);
511 DeleteIORequest(timer_io);
512 }
513 if (timer_port)
514 DeleteMsgPort(timer_port);
515
516 // Main task asked for termination, send signal
517 Forbid();
518 Signal(MainTask, SIGF_SINGLE);
519 }
520
521
522 /*
523 * Display error alert
524 */
525
526 void ErrorAlert(const char *text)
527 {
528 if (PrefsFindBool("nogui")) {
529 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
530 return;
531 }
532 EasyStruct req;
533 req.es_StructSize = sizeof(EasyStruct);
534 req.es_Flags = 0;
535 req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE);
536 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX);
537 req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON);
538 EasyRequest(NULL, &req, NULL, (ULONG)text);
539 }
540
541
542 /*
543 * Display warning alert
544 */
545
546 void WarningAlert(const char *text)
547 {
548 if (PrefsFindBool("nogui")) {
549 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
550 return;
551 }
552 EasyStruct req;
553 req.es_StructSize = sizeof(EasyStruct);
554 req.es_Flags = 0;
555 req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
556 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
557 req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON);
558 EasyRequest(NULL, &req, NULL, (ULONG)text);
559 }
560
561
562 /*
563 * Display choice alert
564 */
565
566 bool ChoiceAlert(const char *text, const char *pos, const char *neg)
567 {
568 char str[256];
569 sprintf(str, "%s|%s", pos, neg);
570 EasyStruct req;
571 req.es_StructSize = sizeof(EasyStruct);
572 req.es_Flags = 0;
573 req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
574 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
575 req.es_GadgetFormat = (UBYTE *)str;
576 return EasyRequest(NULL, &req, NULL, (ULONG)text);
577 }
578
579
580 /*
581 * Illegal Instruction and Privilege Violation trap handlers
582 */
583
584 struct trap_regs { // This must match the layout of M68kRegisters
585 uint32 d[8];
586 uint32 a[8];
587 uint16 sr;
588 uint32 pc;
589 };
590
591 void __saveds IllInstrHandler(trap_regs *r)
592 {
593 uint16 opcode = *(uint16 *)(r->pc);
594 if ((opcode & 0xff00) != 0x7100) {
595 printf("Illegal Instruction %04x at %08lx\n", *(uint16 *)(r->pc), r->pc);
596 printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
597 "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
598 "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
599 "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
600 "sr %04x\n",
601 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
602 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
603 r->sr);
604 QuitEmulator();
605 } else {
606 // Disable interrupts
607 uint16 sr = EmulatedSR;
608 EmulatedSR |= 0x0700;
609
610 // Call opcode routine
611 EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r);
612 r->pc += 2;
613
614 // Restore interrupts
615 EmulatedSR = sr;
616 if ((EmulatedSR & 0x0700) == 0 && InterruptFlags)
617 Signal(MainTask, IRQSigMask);
618 }
619 }
620
621 void __saveds PrivViolHandler(trap_regs *r)
622 {
623 printf("Privileged instruction %04x %04x at %08lx\n", *(uint16 *)(r->pc), *(uint16 *)(r->pc + 2), r->pc);
624 printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
625 "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
626 "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
627 "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
628 "sr %04x\n",
629 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
630 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
631 r->sr);
632 QuitEmulator();
633 }