ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/main_amiga.cpp
Revision: 1.2
Committed: 1999-10-19T17:41:21Z (24 years, 7 months ago) by cebix
Branch: MAIN
Changes since 1.1: +4 -72 lines
Log Message:
- added external file system
- moved most init/deinit code to InitAll()/ExitAll() in main.cpp

File Contents

# User Rev Content
1 cebix 1.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     // Set CPU and FPU type
252     UWORD attn = SysBase->AttnFlags;
253     CPUType = attn & AFF_68040 ? 4 : (attn & AFF_68030 ? 3 : 2);
254     CPUIs68060 = attn & AFF_68060;
255     FPUType = attn & AFF_68881 ? 1 : 0;
256    
257 cebix 1.2 // Initialize everything
258     if (!InitAll())
259 cebix 1.1 QuitEmulator();
260    
261     // Move VBR away from 0 if neccessary
262     MoveVBR();
263    
264     // Install trap handler
265     EmulatedSR = 0x2700;
266     OldTrapHandler = MainTask->tc_TrapCode;
267     MainTask->tc_TrapCode = (APTR)TrapHandlerAsm;
268    
269     // Allocate signal for interrupt emulation and install exception handler
270     IRQSig = AllocSignal(-1);
271     IRQSigMask = 1 << IRQSig;
272     OldExceptionHandler = MainTask->tc_ExceptCode;
273     MainTask->tc_ExceptCode = (APTR)ExceptionHandlerAsm;
274     SetExcept(SIGBREAKF_CTRL_C | IRQSigMask, SIGBREAKF_CTRL_C | IRQSigMask);
275    
276     // Start 60Hz process
277     tick_proc = CreateNewProcTags(
278     NP_Entry, tick_func,
279     NP_Name, "Basilisk II 60Hz",
280     NP_Priority, 5,
281     TAG_END
282     );
283    
284     // Set task priority to -1 so we don't use all processing time
285     SetTaskPri(MainTask, -1);
286    
287     // Swap stack to Mac RAM area
288     stack_swap.stk_Lower = RAMBaseHost;
289     stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize;
290     stack_swap.stk_Pointer = RAMBaseHost + 0x8000;
291     StackSwap(&stack_swap);
292     stack_swapped = true;
293    
294     // Jump to ROM boot routine
295     Start680x0();
296    
297     QuitEmulator();
298     return 0;
299     }
300    
301     void Start680x0(void)
302     {
303     typedef void (*rom_func)(void);
304     rom_func fp = (rom_func)(ROMBaseHost + 0x2a);
305     fp();
306     }
307    
308    
309     /*
310     * Quit emulator (__saveds because it might be called from an exception)
311     */
312    
313     void __saveds QuitEmulator(void)
314     {
315     // Restore stack
316     if (stack_swapped) {
317     stack_swapped = false;
318     StackSwap(&stack_swap);
319     }
320    
321     // Remove exception handler
322     if (IRQSig >= 0) {
323     SetExcept(0, SIGBREAKF_CTRL_C | IRQSigMask);
324     MainTask->tc_ExceptCode = OldExceptionHandler;
325     FreeSignal(IRQSig);
326     }
327    
328     // Stop 60Hz thread
329     if (tick_proc) {
330     SetSignal(0, SIGF_SINGLE);
331     tick_proc_active = false;
332     Wait(SIGF_SINGLE);
333     }
334    
335     // Remove trap handler
336     MainTask->tc_TrapCode = OldTrapHandler;
337    
338 cebix 1.2 // Deinitialize everything
339     ExitAll();
340 cebix 1.1
341     // Delete RAM/ROM area
342     if (RAMBaseHost)
343     FreeMem(RAMBaseHost, RAMSize + 0x100000);
344    
345     // Delete scratch memory area
346     if (ScratchMem)
347     FreeMem((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
348    
349     // Close timer.device
350     if (TimerBase)
351     CloseDevice((struct IORequest *)timereq);
352     if (timereq)
353     FreeVec(timereq);
354    
355     // Exit system routines
356     SysExit();
357    
358     // Close AHI
359     if (AHIBase)
360     CloseDevice((struct IORequest *)ahi_io);
361     if (ahi_io)
362     DeleteIORequest((struct IORequest *)ahi_io);
363     if (ahi_port)
364     DeleteMsgPort(ahi_port);
365    
366     // Exit preferences
367     PrefsExit();
368    
369     // Close libraries
370     if (P96Base)
371     CloseLibrary(P96Base);
372     if (AslBase)
373     CloseLibrary(AslBase);
374     if (GadToolsBase)
375     CloseLibrary(GadToolsBase);
376    
377     exit(0);
378     }
379    
380    
381     /*
382     * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
383     * or a dynamically recompiling emulator)
384     */
385    
386     void FlushCodeCache(void *start, uint32 size)
387     {
388     CacheClearE(start, size, CACRF_ClearI | CACRF_ClearD);
389     }
390    
391    
392     /*
393     * Interrupt flags (must be handled atomically!)
394     */
395    
396     uint32 InterruptFlags;
397    
398     void SetInterruptFlag(uint32 flag)
399     {
400     AtomicOr(&InterruptFlags, flag);
401     }
402    
403     void ClearInterruptFlag(uint32 flag)
404     {
405     AtomicAnd(&InterruptFlags, ~flag);
406     }
407    
408     void TriggerInterrupt(void)
409     {
410     Signal(MainTask, IRQSigMask);
411     }
412    
413    
414     /*
415     * 60Hz thread
416     */
417    
418     static __saveds void tick_func(void)
419     {
420     int tick_counter = 0;
421     struct MsgPort *timer_port = NULL;
422     struct timerequest *timer_io = NULL;
423     ULONG timer_mask = 0;
424    
425     // Start 60Hz timer
426     timer_port = CreateMsgPort();
427     if (timer_port) {
428     timer_io = (struct timerequest *)CreateIORequest(timer_port, sizeof(struct timerequest));
429     if (timer_io) {
430     if (!OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) {
431     timer_mask = 1 << timer_port->mp_SigBit;
432     timer_io->tr_node.io_Command = TR_ADDREQUEST;
433     timer_io->tr_time.tv_secs = 0;
434     timer_io->tr_time.tv_micro = 16625;
435     SendIO((struct IORequest *)timer_io);
436     }
437     }
438     }
439    
440     while (tick_proc_active) {
441    
442     // Wait for timer tick
443     Wait(timer_mask);
444    
445     // Restart timer
446     timer_io->tr_node.io_Command = TR_ADDREQUEST;
447     timer_io->tr_time.tv_secs = 0;
448     timer_io->tr_time.tv_micro = 16625;
449     SendIO((struct IORequest *)timer_io);
450    
451     // Pseudo Mac 1Hz interrupt, update local time
452     if (++tick_counter > 60) {
453     tick_counter = 0;
454     WriteMacInt32(0x20c, TimerDateTime());
455     }
456    
457     // Trigger 60Hz interrupt
458     SetInterruptFlag(INTFLAG_60HZ);
459     TriggerInterrupt();
460     }
461    
462     // Stop timer
463     if (timer_io) {
464     if (!CheckIO((struct IORequest *)timer_io))
465     AbortIO((struct IORequest *)timer_io);
466     WaitIO((struct IORequest *)timer_io);
467     CloseDevice((struct IORequest *)timer_io);
468     DeleteIORequest(timer_io);
469     }
470     if (timer_port)
471     DeleteMsgPort(timer_port);
472    
473     // Main task asked for termination, send signal
474     Forbid();
475     Signal(MainTask, SIGF_SINGLE);
476     }
477    
478    
479     /*
480     * Display error alert
481     */
482    
483     void ErrorAlert(const char *text)
484     {
485     if (PrefsFindBool("nogui")) {
486     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
487     return;
488     }
489     EasyStruct req;
490     req.es_StructSize = sizeof(EasyStruct);
491     req.es_Flags = 0;
492     req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE);
493     req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX);
494     req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON);
495     EasyRequest(NULL, &req, NULL, text);
496     }
497    
498    
499     /*
500     * Display warning alert
501     */
502    
503     void WarningAlert(const char *text)
504     {
505     if (PrefsFindBool("nogui")) {
506     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
507     return;
508     }
509     EasyStruct req;
510     req.es_StructSize = sizeof(EasyStruct);
511     req.es_Flags = 0;
512     req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
513     req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
514     req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON);
515     EasyRequest(NULL, &req, NULL, text);
516     }
517    
518    
519     /*
520     * Display choice alert
521     */
522    
523     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
524     {
525     char str[256];
526     sprintf(str, "%s|%s", pos, neg);
527     EasyStruct req;
528     req.es_StructSize = sizeof(EasyStruct);
529     req.es_Flags = 0;
530     req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
531     req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
532     req.es_GadgetFormat = (UBYTE *)str;
533     return EasyRequest(NULL, &req, NULL, text);
534     }
535    
536    
537     /*
538     * Illegal Instruction and Privilege Violation trap handlers
539     */
540    
541     struct trap_regs { // This must match the layout of M68kRegisters
542     uint32 d[8];
543     uint32 a[8];
544     uint16 sr;
545     uint32 pc;
546     };
547    
548     void __saveds IllInstrHandler(trap_regs *r)
549     {
550     uint16 opcode = *(uint16 *)(r->pc);
551     if ((opcode & 0xff00) != 0x7100) {
552     printf("Illegal Instruction %04x at %08lx\n", *(uint16 *)(r->pc), r->pc);
553     printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
554     "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
555     "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
556     "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
557     "sr %04x\n",
558     r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
559     r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
560     r->sr);
561     QuitEmulator();
562     } else {
563     // Disable interrupts
564     uint16 sr = EmulatedSR;
565     EmulatedSR |= 0x0700;
566    
567     // Call opcode routine
568     EmulOp(*(uint16 *)(r->pc), (M68kRegisters *)r);
569     r->pc += 2;
570    
571     // Restore interrupts
572     EmulatedSR = sr;
573     if ((EmulatedSR & 0x0700) == 0 && InterruptFlags)
574     Signal(MainTask, IRQSigMask);
575     }
576     }
577    
578     void __saveds PrivViolHandler(trap_regs *r)
579     {
580     printf("Privileged instruction %04x %04x at %08lx\n", *(uint16 *)(r->pc), *(uint16 *)(r->pc + 2), r->pc);
581     printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
582     "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
583     "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
584     "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
585     "sr %04x\n",
586     r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
587     r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
588     r->sr);
589     QuitEmulator();
590     }