ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/main_amiga.cpp
Revision: 1.13
Committed: 2001-01-04T19:50:22Z (23 years, 10 months ago) by cebix
Branch: MAIN
Changes since 1.12: +2 -2 lines
Log Message:
- removed the INT16 prefs item type; use INT32 instead
- AmigaOS/Unix: it's now possible to specify preferences items on the
  command line
- Unix: command line options now take "--"-prefix, e.g. "--rominfo"

File Contents

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