ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/main_amiga.cpp
Revision: 1.3
Committed: 1999-10-19T19:28:15Z (24 years, 7 months ago) by cebix
Branch: MAIN
CVS Tags: snapshot-21101999
Changes since 1.2: +52 -16 lines
Log Message:
- now compiles with GCC under AmigaOS

File Contents

# Content
1 /*
2 * main_amiga.cpp - Startup code for AmigaOS
3 *
4 * Basilisk II (C) 1997-1999 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 // Our minimum stack requirement
60 unsigned long __stack = 0x4000;
61
62
63 // Constants
64 static const char ROM_FILE_NAME[] = "ROM";
65 static const char __ver[] = "$VER: " VERSION_STRING " " __DATE__;
66 static const int SCRATCH_MEM_SIZE = 65536;
67
68
69 // RAM and ROM pointers
70 uint32 RAMBaseMac; // RAM base (Mac address space)
71 uint8 *RAMBaseHost; // RAM base (host address space)
72 uint32 RAMSize; // Size of RAM
73 uint32 ROMBaseMac; // ROM base (Mac address space)
74 uint8 *ROMBaseHost; // ROM base (host address space)
75 uint32 ROMSize; // Size of ROM
76
77
78 // CPU and FPU type, addressing mode
79 int CPUType;
80 bool CPUIs68060;
81 int FPUType;
82 bool TwentyFourBitAddressing;
83
84
85 // Global variables
86 extern ExecBase *SysBase;
87 struct Library *GfxBase = NULL;
88 struct IntuitionBase *IntuitionBase = NULL;
89 struct Library *GadToolsBase = NULL;
90 struct Library *IFFParseBase = NULL;
91 struct Library *AslBase = NULL;
92 struct Library *P96Base = NULL;
93 struct Library *TimerBase = NULL;
94 struct Library *AHIBase = NULL;
95 struct Library *DiskBase = NULL;
96
97 struct Task *MainTask; // Our task
98 uint32 ScratchMem = NULL; // Scratch memory for Mac ROM writes
99 APTR OldTrapHandler = NULL; // Old trap handler
100 APTR OldExceptionHandler = NULL; // Old exception handler
101 BYTE IRQSig = -1; // "Interrupt" signal number
102 ULONG IRQSigMask = 0; // "Interrupt" signal mask
103
104 static struct timerequest *timereq = NULL; // IORequest for timer
105
106 static struct MsgPort *ahi_port = NULL; // Port for AHI
107 static struct AHIRequest *ahi_io = NULL; // IORequest for AHI
108
109 static struct Process *tick_proc = NULL; // 60Hz process
110 static volatile bool tick_proc_active = true; // Flag for quitting the 60Hz process
111
112 static bool stack_swapped = false; // Stack swapping
113 static StackSwapStruct stack_swap;
114
115
116 // Assembly functions
117 struct trap_regs;
118 extern "C" void AtomicAnd(uint32 *p, uint32 val);
119 extern "C" void AtomicOr(uint32 *p, uint32 val);
120 extern "C" void MoveVBR(void);
121 extern "C" void TrapHandlerAsm(void);
122 extern "C" void ExceptionHandlerAsm(void);
123 extern "C" void IllInstrHandler(trap_regs *regs);
124 extern "C" void PrivViolHandler(trap_regs *regs);
125 extern "C" void quit_emulator(void);
126 uint16 EmulatedSR; // Emulated SR (supervisor bit and interrupt mask)
127
128
129 // Prototypes
130 static void jump_to_rom(void);
131 static void tick_func(void);
132
133
134 /*
135 * Main program
136 */
137
138 int main(void)
139 {
140 // Initialize variables
141 RAMBaseHost = NULL;
142 ROMBaseHost = NULL;
143 MainTask = FindTask(NULL);
144 struct DateStamp ds;
145 DateStamp(&ds);
146 srand(ds.ds_Tick);
147
148 // Print some info
149 printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
150 printf(" %s\n", GetString(STR_ABOUT_TEXT2));
151
152 // Open libraries
153 GfxBase = OpenLibrary((UBYTE *)"graphics.library", 39);
154 if (GfxBase == NULL) {
155 printf("Cannot open graphics.library V39.\n");
156 exit(1);
157 }
158 IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39);
159 if (IntuitionBase == NULL) {
160 printf("Cannot open intuition.library V39.\n");
161 CloseLibrary(GfxBase);
162 exit(1);
163 }
164 DiskBase = (struct Library *)OpenResource((UBYTE *)"disk.resource");
165 if (DiskBase == NULL)
166 QuitEmulator();
167 GadToolsBase = OpenLibrary((UBYTE *)"gadtools.library", 39);
168 if (GadToolsBase == NULL) {
169 ErrorAlert(GetString(STR_NO_GADTOOLS_LIB_ERR));
170 QuitEmulator();
171 }
172 IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library", 39);
173 if (IFFParseBase == NULL) {
174 ErrorAlert(GetString(STR_NO_IFFPARSE_LIB_ERR));
175 QuitEmulator();
176 }
177 AslBase = OpenLibrary((UBYTE *)"asl.library", 36);
178 if (AslBase == NULL) {
179 ErrorAlert(GetString(STR_NO_ASL_LIB_ERR));
180 QuitEmulator();
181 }
182 P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2);
183
184 // Read preferences
185 PrefsInit();
186
187 // Open AHI
188 ahi_port = CreateMsgPort();
189 if (ahi_port) {
190 ahi_io = (struct AHIRequest *)CreateIORequest(ahi_port, sizeof(struct AHIRequest));
191 if (ahi_io) {
192 ahi_io->ahir_Version = 2;
193 if (OpenDevice((UBYTE *)AHINAME, AHI_NO_UNIT, (struct IORequest *)ahi_io, 0) == 0) {
194 AHIBase = (struct Library *)ahi_io->ahir_Std.io_Device;
195 }
196 }
197 }
198
199 // Init system routines
200 SysInit();
201
202 // Show preferences editor
203 if (!PrefsFindBool("nogui"))
204 if (!PrefsEditor())
205 QuitEmulator();
206
207 // Check start of Chip memory (because we need access to 0x0000..0x2000)
208 if ((uint32)FindName(&SysBase->MemList, (UBYTE *)"chip memory") < 0x2000) {
209 ErrorAlert(GetString(STR_NO_PREPARE_EMUL_ERR));
210 QuitEmulator();
211 }
212
213 // Open timer.device
214 timereq = (struct timerequest *)AllocVec(sizeof(timerequest), MEMF_PUBLIC | MEMF_CLEAR);
215 if (timereq == NULL) {
216 ErrorAlert(GetString(STR_NO_MEM_ERR));
217 QuitEmulator();
218 }
219 if (OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timereq, 0)) {
220 ErrorAlert(GetString(STR_NO_TIMER_DEV_ERR));
221 QuitEmulator();
222 }
223 TimerBase = (struct Library *)timereq->tr_node.io_Device;
224
225 // Allocate scratch memory
226 ScratchMem = (uint32)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC);
227 if (ScratchMem == NULL) {
228 ErrorAlert(GetString(STR_NO_MEM_ERR));
229 QuitEmulator();
230 }
231 ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
232
233 // Create area for Mac RAM and ROM (ROM must be higher in memory,
234 // so we allocate one big chunk and put the ROM at the top of it)
235 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
236 if (RAMSize < 1024*1024) {
237 WarningAlert(GetString(STR_SMALL_RAM_WARN));
238 RAMSize = 1024*1024;
239 }
240 RAMBaseHost = (uint8 *)AllocMem(RAMSize + 0x100000, MEMF_PUBLIC);
241 if (RAMBaseHost == NULL) {
242 ErrorAlert(GetString(STR_NO_MEM_ERR));
243 QuitEmulator();
244 }
245 RAMBaseMac = (uint32)RAMBaseHost;
246 D(bug("Mac RAM starts at %08lx\n", RAMBaseHost));
247 ROMBaseHost = RAMBaseHost + RAMSize;
248 ROMBaseMac = (uint32)ROMBaseHost;
249 D(bug("Mac ROM starts at %08lx\n", ROMBaseHost));
250
251 // Get rom file path from preferences
252 const char *rom_path = PrefsFindString("rom");
253
254 // Load Mac ROM
255 BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE);
256 if (rom_fh == NULL) {
257 ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
258 QuitEmulator();
259 }
260 printf(GetString(STR_READING_ROM_FILE));
261 Seek(rom_fh, 0, OFFSET_END);
262 ROMSize = Seek(rom_fh, 0, OFFSET_CURRENT);
263 if (ROMSize != 512*1024 && ROMSize != 1024*1024) {
264 ErrorAlert(GetString(STR_ROM_SIZE_ERR));
265 Close(rom_fh);
266 QuitEmulator();
267 }
268 Seek(rom_fh, 0, OFFSET_BEGINNING);
269 if (Read(rom_fh, ROMBaseHost, ROMSize) != ROMSize) {
270 ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
271 Close(rom_fh);
272 QuitEmulator();
273 }
274
275 // Set CPU and FPU type
276 UWORD attn = SysBase->AttnFlags;
277 CPUType = attn & AFF_68040 ? 4 : (attn & AFF_68030 ? 3 : 2);
278 CPUIs68060 = attn & AFF_68060;
279 FPUType = attn & AFF_68881 ? 1 : 0;
280
281 // Initialize everything
282 if (!InitAll())
283 QuitEmulator();
284
285 // Move VBR away from 0 if neccessary
286 MoveVBR();
287
288 // Install trap handler
289 EmulatedSR = 0x2700;
290 OldTrapHandler = MainTask->tc_TrapCode;
291 MainTask->tc_TrapCode = (APTR)TrapHandlerAsm;
292
293 // Allocate signal for interrupt emulation and install exception handler
294 IRQSig = AllocSignal(-1);
295 IRQSigMask = 1 << IRQSig;
296 OldExceptionHandler = MainTask->tc_ExceptCode;
297 MainTask->tc_ExceptCode = (APTR)ExceptionHandlerAsm;
298 SetExcept(SIGBREAKF_CTRL_C | IRQSigMask, SIGBREAKF_CTRL_C | IRQSigMask);
299
300 // Start 60Hz process
301 tick_proc = CreateNewProcTags(
302 NP_Entry, (ULONG)tick_func,
303 NP_Name, (ULONG)"Basilisk II 60Hz",
304 NP_Priority, 5,
305 TAG_END
306 );
307
308 // Set task priority to -1 so we don't use all processing time
309 SetTaskPri(MainTask, -1);
310
311 // Swap stack to Mac RAM area
312 stack_swap.stk_Lower = RAMBaseHost;
313 stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize;
314 stack_swap.stk_Pointer = RAMBaseHost + 0x8000;
315 StackSwap(&stack_swap);
316 stack_swapped = true;
317
318 // Jump to ROM boot routine
319 Start680x0();
320
321 QuitEmulator();
322 return 0;
323 }
324
325 void Start680x0(void)
326 {
327 typedef void (*rom_func)(void);
328 rom_func fp = (rom_func)(ROMBaseHost + 0x2a);
329 fp();
330 }
331
332
333 /*
334 * Quit emulator (__saveds because it might be called from an exception)
335 */
336
337 // Assembly entry point
338 void __saveds quit_emulator(void)
339 {
340 QuitEmulator();
341 }
342
343 void QuitEmulator(void)
344 {
345 // Restore stack
346 if (stack_swapped) {
347 stack_swapped = false;
348 StackSwap(&stack_swap);
349 }
350
351 // Remove exception handler
352 if (IRQSig >= 0) {
353 SetExcept(0, SIGBREAKF_CTRL_C | IRQSigMask);
354 MainTask->tc_ExceptCode = OldExceptionHandler;
355 FreeSignal(IRQSig);
356 }
357
358 // Stop 60Hz thread
359 if (tick_proc) {
360 SetSignal(0, SIGF_SINGLE);
361 tick_proc_active = false;
362 Wait(SIGF_SINGLE);
363 }
364
365 // Remove trap handler
366 MainTask->tc_TrapCode = OldTrapHandler;
367
368 // Deinitialize everything
369 ExitAll();
370
371 // Delete RAM/ROM area
372 if (RAMBaseHost)
373 FreeMem(RAMBaseHost, RAMSize + 0x100000);
374
375 // Delete scratch memory area
376 if (ScratchMem)
377 FreeMem((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
378
379 // Close timer.device
380 if (TimerBase)
381 CloseDevice((struct IORequest *)timereq);
382 if (timereq)
383 FreeVec(timereq);
384
385 // Exit system routines
386 SysExit();
387
388 // Close AHI
389 if (AHIBase)
390 CloseDevice((struct IORequest *)ahi_io);
391 if (ahi_io)
392 DeleteIORequest((struct IORequest *)ahi_io);
393 if (ahi_port)
394 DeleteMsgPort(ahi_port);
395
396 // Exit preferences
397 PrefsExit();
398
399 // Close libraries
400 if (P96Base)
401 CloseLibrary(P96Base);
402 if (AslBase)
403 CloseLibrary(AslBase);
404 if (IFFParseBase)
405 CloseLibrary(IFFParseBase);
406 if (GadToolsBase)
407 CloseLibrary(GadToolsBase);
408 if (IntuitionBase)
409 CloseLibrary((struct Library *)IntuitionBase);
410 if (GfxBase)
411 CloseLibrary(GfxBase);
412
413 exit(0);
414 }
415
416
417 /*
418 * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
419 * or a dynamically recompiling emulator)
420 */
421
422 void FlushCodeCache(void *start, uint32 size)
423 {
424 CacheClearE(start, size, CACRF_ClearI | CACRF_ClearD);
425 }
426
427
428 /*
429 * Interrupt flags (must be handled atomically!)
430 */
431
432 uint32 InterruptFlags;
433
434 void SetInterruptFlag(uint32 flag)
435 {
436 AtomicOr(&InterruptFlags, flag);
437 }
438
439 void ClearInterruptFlag(uint32 flag)
440 {
441 AtomicAnd(&InterruptFlags, ~flag);
442 }
443
444 void TriggerInterrupt(void)
445 {
446 Signal(MainTask, IRQSigMask);
447 }
448
449
450 /*
451 * 60Hz thread
452 */
453
454 static __saveds void tick_func(void)
455 {
456 int tick_counter = 0;
457 struct MsgPort *timer_port = NULL;
458 struct timerequest *timer_io = NULL;
459 ULONG timer_mask = 0;
460
461 // Start 60Hz timer
462 timer_port = CreateMsgPort();
463 if (timer_port) {
464 timer_io = (struct timerequest *)CreateIORequest(timer_port, sizeof(struct timerequest));
465 if (timer_io) {
466 if (!OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) {
467 timer_mask = 1 << timer_port->mp_SigBit;
468 timer_io->tr_node.io_Command = TR_ADDREQUEST;
469 timer_io->tr_time.tv_secs = 0;
470 timer_io->tr_time.tv_micro = 16625;
471 SendIO((struct IORequest *)timer_io);
472 }
473 }
474 }
475
476 while (tick_proc_active) {
477
478 // Wait for timer tick
479 Wait(timer_mask);
480
481 // Restart timer
482 timer_io->tr_node.io_Command = TR_ADDREQUEST;
483 timer_io->tr_time.tv_secs = 0;
484 timer_io->tr_time.tv_micro = 16625;
485 SendIO((struct IORequest *)timer_io);
486
487 // Pseudo Mac 1Hz interrupt, update local time
488 if (++tick_counter > 60) {
489 tick_counter = 0;
490 WriteMacInt32(0x20c, TimerDateTime());
491 }
492
493 // Trigger 60Hz interrupt
494 SetInterruptFlag(INTFLAG_60HZ);
495 TriggerInterrupt();
496 }
497
498 // Stop timer
499 if (timer_io) {
500 if (!CheckIO((struct IORequest *)timer_io))
501 AbortIO((struct IORequest *)timer_io);
502 WaitIO((struct IORequest *)timer_io);
503 CloseDevice((struct IORequest *)timer_io);
504 DeleteIORequest(timer_io);
505 }
506 if (timer_port)
507 DeleteMsgPort(timer_port);
508
509 // Main task asked for termination, send signal
510 Forbid();
511 Signal(MainTask, SIGF_SINGLE);
512 }
513
514
515 /*
516 * Display error alert
517 */
518
519 void ErrorAlert(const char *text)
520 {
521 if (PrefsFindBool("nogui")) {
522 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
523 return;
524 }
525 EasyStruct req;
526 req.es_StructSize = sizeof(EasyStruct);
527 req.es_Flags = 0;
528 req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE);
529 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX);
530 req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON);
531 EasyRequest(NULL, &req, NULL, (ULONG)text);
532 }
533
534
535 /*
536 * Display warning alert
537 */
538
539 void WarningAlert(const char *text)
540 {
541 if (PrefsFindBool("nogui")) {
542 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
543 return;
544 }
545 EasyStruct req;
546 req.es_StructSize = sizeof(EasyStruct);
547 req.es_Flags = 0;
548 req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
549 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
550 req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON);
551 EasyRequest(NULL, &req, NULL, (ULONG)text);
552 }
553
554
555 /*
556 * Display choice alert
557 */
558
559 bool ChoiceAlert(const char *text, const char *pos, const char *neg)
560 {
561 char str[256];
562 sprintf(str, "%s|%s", pos, neg);
563 EasyStruct req;
564 req.es_StructSize = sizeof(EasyStruct);
565 req.es_Flags = 0;
566 req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
567 req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
568 req.es_GadgetFormat = (UBYTE *)str;
569 return EasyRequest(NULL, &req, NULL, (ULONG)text);
570 }
571
572
573 /*
574 * Illegal Instruction and Privilege Violation trap handlers
575 */
576
577 struct trap_regs { // This must match the layout of M68kRegisters
578 uint32 d[8];
579 uint32 a[8];
580 uint16 sr;
581 uint32 pc;
582 };
583
584 void __saveds IllInstrHandler(trap_regs *r)
585 {
586 uint16 opcode = *(uint16 *)(r->pc);
587 if ((opcode & 0xff00) != 0x7100) {
588 printf("Illegal Instruction %04x at %08lx\n", *(uint16 *)(r->pc), r->pc);
589 printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
590 "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
591 "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
592 "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
593 "sr %04x\n",
594 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
595 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
596 r->sr);
597 QuitEmulator();
598 } else {
599 // Disable interrupts
600 uint16 sr = EmulatedSR;
601 EmulatedSR |= 0x0700;
602
603 // Call opcode routine
604 EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r);
605 r->pc += 2;
606
607 // Restore interrupts
608 EmulatedSR = sr;
609 if ((EmulatedSR & 0x0700) == 0 && InterruptFlags)
610 Signal(MainTask, IRQSigMask);
611 }
612 }
613
614 void __saveds PrivViolHandler(trap_regs *r)
615 {
616 printf("Privileged instruction %04x %04x at %08lx\n", *(uint16 *)(r->pc), *(uint16 *)(r->pc + 2), r->pc);
617 printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
618 "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
619 "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
620 "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
621 "sr %04x\n",
622 r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
623 r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
624 r->sr);
625 QuitEmulator();
626 }