ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/main_amiga.cpp
Revision: 1.1
Committed: 1999-10-03T14:16:25Z (24 years, 8 months ago) by cebix
Branch: MAIN
Branch point for: cebix
Log Message:
Initial revision

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