ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/main_amiga.cpp
Revision: 1.21
Committed: 2002-01-15T14:58:34Z (22 years, 5 months ago) by cebix
Branch: MAIN
CVS Tags: snapshot-15012002
Changes since 1.20: +1 -1 lines
Log Message:
- documentation updates
- 2001 -> 2002
- version 0.9 -> 1.0

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_amiga.cpp - Startup code for AmigaOS
3     *
4 cebix 1.21 * Basilisk II (C) 1997-2002 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 cebix 1.14 static struct Process *xpram_proc = NULL; // XPRAM watchdog
112     static volatile bool xpram_proc_active = true; // Flag for quitting the XPRAM watchdog
113    
114 cebix 1.1 static struct Process *tick_proc = NULL; // 60Hz process
115     static volatile bool tick_proc_active = true; // Flag for quitting the 60Hz process
116    
117     static bool stack_swapped = false; // Stack swapping
118     static StackSwapStruct stack_swap;
119    
120    
121     // Assembly functions
122     struct trap_regs;
123     extern "C" void AtomicAnd(uint32 *p, uint32 val);
124     extern "C" void AtomicOr(uint32 *p, uint32 val);
125     extern "C" void MoveVBR(void);
126 cebix 1.14 extern "C" void DisableSuperBypass(void);
127 cebix 1.1 extern "C" void TrapHandlerAsm(void);
128     extern "C" void ExceptionHandlerAsm(void);
129     extern "C" void IllInstrHandler(trap_regs *regs);
130     extern "C" void PrivViolHandler(trap_regs *regs);
131 cebix 1.3 extern "C" void quit_emulator(void);
132 cebix 1.12 extern "C" void AsmTriggerNMI(void);
133 cebix 1.1 uint16 EmulatedSR; // Emulated SR (supervisor bit and interrupt mask)
134    
135    
136 cebix 1.3 // Prototypes
137     static void jump_to_rom(void);
138 cebix 1.14 static void xpram_func(void);
139 cebix 1.3 static void tick_func(void);
140    
141    
142 cebix 1.1 /*
143     * Main program
144     */
145    
146 cebix 1.13 int main(int argc, char **argv)
147 cebix 1.1 {
148     // Initialize variables
149     RAMBaseHost = NULL;
150     ROMBaseHost = NULL;
151     MainTask = FindTask(NULL);
152     struct DateStamp ds;
153     DateStamp(&ds);
154     srand(ds.ds_Tick);
155    
156     // Print some info
157     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
158     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
159    
160     // Open libraries
161 cebix 1.3 GfxBase = OpenLibrary((UBYTE *)"graphics.library", 39);
162     if (GfxBase == NULL) {
163     printf("Cannot open graphics.library V39.\n");
164     exit(1);
165     }
166     IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"intuition.library", 39);
167     if (IntuitionBase == NULL) {
168     printf("Cannot open intuition.library V39.\n");
169     CloseLibrary(GfxBase);
170     exit(1);
171     }
172 cebix 1.1 DiskBase = (struct Library *)OpenResource((UBYTE *)"disk.resource");
173     if (DiskBase == NULL)
174     QuitEmulator();
175     GadToolsBase = OpenLibrary((UBYTE *)"gadtools.library", 39);
176     if (GadToolsBase == NULL) {
177 cebix 1.17 ErrorAlert(STR_NO_GADTOOLS_LIB_ERR);
178 cebix 1.1 QuitEmulator();
179     }
180 cebix 1.3 IFFParseBase = OpenLibrary((UBYTE *)"iffparse.library", 39);
181     if (IFFParseBase == NULL) {
182 cebix 1.17 ErrorAlert(STR_NO_IFFPARSE_LIB_ERR);
183 cebix 1.3 QuitEmulator();
184     }
185 cebix 1.1 AslBase = OpenLibrary((UBYTE *)"asl.library", 36);
186     if (AslBase == NULL) {
187 cebix 1.17 ErrorAlert(STR_NO_ASL_LIB_ERR);
188 cebix 1.1 QuitEmulator();
189     }
190 cebix 1.6
191 jlachmann 1.20 if (FindTask((UBYTE *) "« Enforcer »"))
192     {
193     ErrorAlert(STR_ENFORCER_RUNNING_ERR);
194     QuitEmulator();
195     }
196    
197 cebix 1.6 // These two can fail (the respective gfx support won't be available, then)
198 cebix 1.1 P96Base = OpenLibrary((UBYTE *)"Picasso96API.library", 2);
199 cebix 1.6 CyberGfxBase = OpenLibrary((UBYTE *)"cybergraphics.library", 2);
200 cebix 1.1
201 cebix 1.3 // Read preferences
202 cebix 1.13 PrefsInit(argc, argv);
203 cebix 1.3
204 cebix 1.1 // Open AHI
205     ahi_port = CreateMsgPort();
206     if (ahi_port) {
207     ahi_io = (struct AHIRequest *)CreateIORequest(ahi_port, sizeof(struct AHIRequest));
208     if (ahi_io) {
209     ahi_io->ahir_Version = 2;
210     if (OpenDevice((UBYTE *)AHINAME, AHI_NO_UNIT, (struct IORequest *)ahi_io, 0) == 0) {
211     AHIBase = (struct Library *)ahi_io->ahir_Std.io_Device;
212     }
213     }
214     }
215    
216     // Init system routines
217     SysInit();
218    
219     // Show preferences editor
220     if (!PrefsFindBool("nogui"))
221     if (!PrefsEditor())
222     QuitEmulator();
223    
224     // Check start of Chip memory (because we need access to 0x0000..0x2000)
225     if ((uint32)FindName(&SysBase->MemList, (UBYTE *)"chip memory") < 0x2000) {
226 cebix 1.17 ErrorAlert(STR_NO_PREPARE_EMUL_ERR);
227 cebix 1.1 QuitEmulator();
228     }
229    
230     // Open timer.device
231     timereq = (struct timerequest *)AllocVec(sizeof(timerequest), MEMF_PUBLIC | MEMF_CLEAR);
232     if (timereq == NULL) {
233 cebix 1.17 ErrorAlert(STR_NO_MEM_ERR);
234 cebix 1.1 QuitEmulator();
235     }
236     if (OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timereq, 0)) {
237 cebix 1.17 ErrorAlert(STR_NO_TIMER_DEV_ERR);
238 cebix 1.1 QuitEmulator();
239     }
240     TimerBase = (struct Library *)timereq->tr_node.io_Device;
241    
242     // Allocate scratch memory
243 gbeauche 1.11 ScratchMem = (uint8 *)AllocMem(SCRATCH_MEM_SIZE, MEMF_PUBLIC);
244 cebix 1.1 if (ScratchMem == NULL) {
245 cebix 1.17 ErrorAlert(STR_NO_MEM_ERR);
246 cebix 1.1 QuitEmulator();
247     }
248     ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
249    
250     // Create area for Mac RAM and ROM (ROM must be higher in memory,
251     // so we allocate one big chunk and put the ROM at the top of it)
252     RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
253     if (RAMSize < 1024*1024) {
254     WarningAlert(GetString(STR_SMALL_RAM_WARN));
255     RAMSize = 1024*1024;
256     }
257 jlachmann 1.9 RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC);
258 cebix 1.10 if (RAMBaseHost == NULL) {
259 jlachmann 1.9 uint32 newRAMSize = AvailMem(MEMF_LARGEST) - 0x100000;
260     char xText[120];
261    
262     sprintf(xText, GetString(STR_NOT_ENOUGH_MEM_WARN), RAMSize, newRAMSize);
263    
264 cebix 1.10 if (ChoiceAlert(xText, "Use", "Quit") != 1)
265 jlachmann 1.9 QuitEmulator();
266    
267     RAMSize = newRAMSize;
268     RAMBaseHost = (uint8 *)AllocVec(RAMSize + 0x100000, MEMF_PUBLIC);
269     if (RAMBaseHost == NULL) {
270 cebix 1.17 ErrorAlert(STR_NO_MEM_ERR);
271 jlachmann 1.9 QuitEmulator();
272     }
273 cebix 1.10 }
274 cebix 1.1 RAMBaseMac = (uint32)RAMBaseHost;
275     D(bug("Mac RAM starts at %08lx\n", RAMBaseHost));
276     ROMBaseHost = RAMBaseHost + RAMSize;
277     ROMBaseMac = (uint32)ROMBaseHost;
278     D(bug("Mac ROM starts at %08lx\n", ROMBaseHost));
279    
280     // Get rom file path from preferences
281     const char *rom_path = PrefsFindString("rom");
282    
283     // Load Mac ROM
284 cebix 1.3 BPTR rom_fh = Open(rom_path ? (char *)rom_path : (char *)ROM_FILE_NAME, MODE_OLDFILE);
285 cebix 1.16 if (rom_fh == 0) {
286 cebix 1.17 ErrorAlert(STR_NO_ROM_FILE_ERR);
287 cebix 1.1 QuitEmulator();
288     }
289     printf(GetString(STR_READING_ROM_FILE));
290     Seek(rom_fh, 0, OFFSET_END);
291     ROMSize = Seek(rom_fh, 0, OFFSET_CURRENT);
292     if (ROMSize != 512*1024 && ROMSize != 1024*1024) {
293 cebix 1.17 ErrorAlert(STR_ROM_SIZE_ERR);
294 cebix 1.1 Close(rom_fh);
295     QuitEmulator();
296     }
297     Seek(rom_fh, 0, OFFSET_BEGINNING);
298     if (Read(rom_fh, ROMBaseHost, ROMSize) != ROMSize) {
299 cebix 1.17 ErrorAlert(STR_ROM_FILE_READ_ERR);
300 cebix 1.1 Close(rom_fh);
301     QuitEmulator();
302     }
303    
304     // Set CPU and FPU type
305     UWORD attn = SysBase->AttnFlags;
306     CPUType = attn & AFF_68040 ? 4 : (attn & AFF_68030 ? 3 : 2);
307     CPUIs68060 = attn & AFF_68060;
308     FPUType = attn & AFF_68881 ? 1 : 0;
309    
310 cebix 1.2 // Initialize everything
311     if (!InitAll())
312 cebix 1.1 QuitEmulator();
313    
314     // Move VBR away from 0 if neccessary
315     MoveVBR();
316    
317 cebix 1.14 // On 68060, disable Super Bypass mode because of a CPU bug that is triggered by MacOS 8
318     if (CPUIs68060)
319     DisableSuperBypass();
320    
321 cebix 1.1 // Install trap handler
322     EmulatedSR = 0x2700;
323     OldTrapHandler = MainTask->tc_TrapCode;
324     MainTask->tc_TrapCode = (APTR)TrapHandlerAsm;
325    
326     // Allocate signal for interrupt emulation and install exception handler
327     IRQSig = AllocSignal(-1);
328     IRQSigMask = 1 << IRQSig;
329     OldExceptionHandler = MainTask->tc_ExceptCode;
330     MainTask->tc_ExceptCode = (APTR)ExceptionHandlerAsm;
331     SetExcept(SIGBREAKF_CTRL_C | IRQSigMask, SIGBREAKF_CTRL_C | IRQSigMask);
332    
333 cebix 1.14 // Start XPRAM watchdog process
334     xpram_proc = CreateNewProcTags(
335     NP_Entry, (ULONG)xpram_func,
336     NP_Name, (ULONG)"Basilisk II XPRAM Watchdog",
337     NP_Priority, 0,
338     TAG_END
339     );
340    
341 cebix 1.1 // Start 60Hz process
342     tick_proc = CreateNewProcTags(
343 cebix 1.3 NP_Entry, (ULONG)tick_func,
344     NP_Name, (ULONG)"Basilisk II 60Hz",
345 cebix 1.1 NP_Priority, 5,
346     TAG_END
347     );
348    
349     // Set task priority to -1 so we don't use all processing time
350     SetTaskPri(MainTask, -1);
351    
352 cebix 1.10 WriteMacInt32(0xbff, 0); // MacsBugFlags
353 jlachmann 1.9
354 cebix 1.1 // Swap stack to Mac RAM area
355     stack_swap.stk_Lower = RAMBaseHost;
356     stack_swap.stk_Upper = (ULONG)RAMBaseHost + RAMSize;
357     stack_swap.stk_Pointer = RAMBaseHost + 0x8000;
358     StackSwap(&stack_swap);
359     stack_swapped = true;
360    
361     // Jump to ROM boot routine
362     Start680x0();
363    
364     QuitEmulator();
365     return 0;
366     }
367    
368     void Start680x0(void)
369     {
370     typedef void (*rom_func)(void);
371     rom_func fp = (rom_func)(ROMBaseHost + 0x2a);
372     fp();
373     }
374    
375    
376     /*
377     * Quit emulator (__saveds because it might be called from an exception)
378     */
379    
380 cebix 1.3 // Assembly entry point
381     void __saveds quit_emulator(void)
382     {
383     QuitEmulator();
384     }
385    
386     void QuitEmulator(void)
387 cebix 1.1 {
388 cebix 1.14 // Stop 60Hz process
389 cebix 1.7 if (tick_proc) {
390     SetSignal(0, SIGF_SINGLE);
391     tick_proc_active = false;
392     Wait(SIGF_SINGLE);
393     }
394    
395 cebix 1.14 // Stop XPRAM watchdog process
396     if (xpram_proc) {
397     SetSignal(0, SIGF_SINGLE);
398     xpram_proc_active = false;
399     Wait(SIGF_SINGLE);
400     }
401    
402 cebix 1.1 // Restore stack
403     if (stack_swapped) {
404     stack_swapped = false;
405     StackSwap(&stack_swap);
406     }
407    
408     // Remove exception handler
409     if (IRQSig >= 0) {
410     SetExcept(0, SIGBREAKF_CTRL_C | IRQSigMask);
411     MainTask->tc_ExceptCode = OldExceptionHandler;
412     FreeSignal(IRQSig);
413     }
414    
415     // Remove trap handler
416     MainTask->tc_TrapCode = OldTrapHandler;
417    
418 cebix 1.2 // Deinitialize everything
419     ExitAll();
420 cebix 1.1
421     // Delete RAM/ROM area
422     if (RAMBaseHost)
423 jlachmann 1.9 FreeVec(RAMBaseHost);
424 cebix 1.1
425     // Delete scratch memory area
426     if (ScratchMem)
427     FreeMem((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
428    
429     // Close timer.device
430     if (TimerBase)
431     CloseDevice((struct IORequest *)timereq);
432     if (timereq)
433     FreeVec(timereq);
434    
435     // Exit system routines
436     SysExit();
437    
438     // Close AHI
439     if (AHIBase)
440     CloseDevice((struct IORequest *)ahi_io);
441     if (ahi_io)
442     DeleteIORequest((struct IORequest *)ahi_io);
443     if (ahi_port)
444     DeleteMsgPort(ahi_port);
445    
446     // Exit preferences
447     PrefsExit();
448    
449     // Close libraries
450 cebix 1.6 if (CyberGfxBase)
451     CloseLibrary(CyberGfxBase);
452 cebix 1.1 if (P96Base)
453     CloseLibrary(P96Base);
454     if (AslBase)
455     CloseLibrary(AslBase);
456 cebix 1.3 if (IFFParseBase)
457     CloseLibrary(IFFParseBase);
458 cebix 1.1 if (GadToolsBase)
459     CloseLibrary(GadToolsBase);
460 cebix 1.3 if (IntuitionBase)
461     CloseLibrary((struct Library *)IntuitionBase);
462     if (GfxBase)
463     CloseLibrary(GfxBase);
464 cebix 1.1
465     exit(0);
466     }
467    
468    
469     /*
470     * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
471     * or a dynamically recompiling emulator)
472     */
473    
474     void FlushCodeCache(void *start, uint32 size)
475     {
476     CacheClearE(start, size, CACRF_ClearI | CACRF_ClearD);
477     }
478    
479    
480     /*
481 cebix 1.18 * Mutexes
482     */
483    
484     struct B2_mutex {
485     int dummy; //!!
486     };
487    
488     B2_mutex *B2_create_mutex(void)
489     {
490     return new B2_mutex;
491     }
492    
493     void B2_lock_mutex(B2_mutex *mutex)
494     {
495     }
496    
497     void B2_unlock_mutex(B2_mutex *mutex)
498     {
499     }
500    
501     void B2_delete_mutex(B2_mutex *mutex)
502     {
503     delete mutex;
504     }
505    
506    
507     /*
508 cebix 1.1 * Interrupt flags (must be handled atomically!)
509     */
510    
511     uint32 InterruptFlags;
512    
513     void SetInterruptFlag(uint32 flag)
514     {
515     AtomicOr(&InterruptFlags, flag);
516     }
517    
518     void ClearInterruptFlag(uint32 flag)
519     {
520     AtomicAnd(&InterruptFlags, ~flag);
521     }
522    
523     void TriggerInterrupt(void)
524     {
525     Signal(MainTask, IRQSigMask);
526 jlachmann 1.9 }
527    
528     void TriggerNMI(void)
529     {
530     AsmTriggerNMI();
531 cebix 1.1 }
532    
533    
534     /*
535 cebix 1.14 * 60Hz thread (really 60.15Hz)
536 cebix 1.1 */
537    
538     static __saveds void tick_func(void)
539     {
540     int tick_counter = 0;
541     struct MsgPort *timer_port = NULL;
542     struct timerequest *timer_io = NULL;
543     ULONG timer_mask = 0;
544    
545     // Start 60Hz timer
546     timer_port = CreateMsgPort();
547     if (timer_port) {
548     timer_io = (struct timerequest *)CreateIORequest(timer_port, sizeof(struct timerequest));
549     if (timer_io) {
550     if (!OpenDevice((UBYTE *)TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timer_io, 0)) {
551     timer_mask = 1 << timer_port->mp_SigBit;
552     timer_io->tr_node.io_Command = TR_ADDREQUEST;
553     timer_io->tr_time.tv_secs = 0;
554     timer_io->tr_time.tv_micro = 16625;
555     SendIO((struct IORequest *)timer_io);
556     }
557     }
558     }
559    
560     while (tick_proc_active) {
561    
562     // Wait for timer tick
563     Wait(timer_mask);
564    
565     // Restart timer
566     timer_io->tr_node.io_Command = TR_ADDREQUEST;
567     timer_io->tr_time.tv_secs = 0;
568     timer_io->tr_time.tv_micro = 16625;
569     SendIO((struct IORequest *)timer_io);
570    
571     // Pseudo Mac 1Hz interrupt, update local time
572     if (++tick_counter > 60) {
573     tick_counter = 0;
574     WriteMacInt32(0x20c, TimerDateTime());
575 cebix 1.7 SetInterruptFlag(INTFLAG_1HZ);
576     TriggerInterrupt();
577 cebix 1.1 }
578    
579     // Trigger 60Hz interrupt
580     SetInterruptFlag(INTFLAG_60HZ);
581     TriggerInterrupt();
582     }
583    
584     // Stop timer
585     if (timer_io) {
586     if (!CheckIO((struct IORequest *)timer_io))
587     AbortIO((struct IORequest *)timer_io);
588     WaitIO((struct IORequest *)timer_io);
589     CloseDevice((struct IORequest *)timer_io);
590     DeleteIORequest(timer_io);
591     }
592     if (timer_port)
593     DeleteMsgPort(timer_port);
594 cebix 1.14
595     // Main task asked for termination, send signal
596     Forbid();
597     Signal(MainTask, SIGF_SINGLE);
598     }
599    
600    
601     /*
602     * XPRAM watchdog thread (saves XPRAM every minute)
603     */
604    
605     static __saveds void xpram_func(void)
606     {
607 cebix 1.19 uint8 last_xpram[XPRAM_SIZE];
608     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
609 cebix 1.14
610     while (xpram_proc_active) {
611     for (int i=0; i<60 && xpram_proc_active; i++)
612     Delay(50); // Only wait 1 second so we quit promptly when xpram_proc_active becomes false
613 cebix 1.19 if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
614     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
615 cebix 1.14 SaveXPRAM();
616     }
617     }
618 cebix 1.1
619     // Main task asked for termination, send signal
620     Forbid();
621     Signal(MainTask, SIGF_SINGLE);
622     }
623    
624    
625     /*
626     * Display error alert
627     */
628    
629     void ErrorAlert(const char *text)
630     {
631     if (PrefsFindBool("nogui")) {
632     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
633     return;
634     }
635     EasyStruct req;
636     req.es_StructSize = sizeof(EasyStruct);
637     req.es_Flags = 0;
638     req.es_Title = (UBYTE *)GetString(STR_ERROR_ALERT_TITLE);
639     req.es_TextFormat = (UBYTE *)GetString(STR_GUI_ERROR_PREFIX);
640     req.es_GadgetFormat = (UBYTE *)GetString(STR_QUIT_BUTTON);
641 cebix 1.3 EasyRequest(NULL, &req, NULL, (ULONG)text);
642 cebix 1.1 }
643    
644    
645     /*
646     * Display warning alert
647     */
648    
649     void WarningAlert(const char *text)
650     {
651     if (PrefsFindBool("nogui")) {
652     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
653     return;
654     }
655     EasyStruct req;
656     req.es_StructSize = sizeof(EasyStruct);
657     req.es_Flags = 0;
658     req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
659     req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
660     req.es_GadgetFormat = (UBYTE *)GetString(STR_OK_BUTTON);
661 cebix 1.3 EasyRequest(NULL, &req, NULL, (ULONG)text);
662 cebix 1.1 }
663    
664    
665     /*
666     * Display choice alert
667     */
668    
669     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
670     {
671     char str[256];
672     sprintf(str, "%s|%s", pos, neg);
673     EasyStruct req;
674     req.es_StructSize = sizeof(EasyStruct);
675     req.es_Flags = 0;
676     req.es_Title = (UBYTE *)GetString(STR_WARNING_ALERT_TITLE);
677     req.es_TextFormat = (UBYTE *)GetString(STR_GUI_WARNING_PREFIX);
678     req.es_GadgetFormat = (UBYTE *)str;
679 cebix 1.3 return EasyRequest(NULL, &req, NULL, (ULONG)text);
680 cebix 1.1 }
681    
682    
683     /*
684     * Illegal Instruction and Privilege Violation trap handlers
685     */
686    
687     struct trap_regs { // This must match the layout of M68kRegisters
688     uint32 d[8];
689     uint32 a[8];
690     uint16 sr;
691     uint32 pc;
692     };
693    
694     void __saveds IllInstrHandler(trap_regs *r)
695     {
696     uint16 opcode = *(uint16 *)(r->pc);
697     if ((opcode & 0xff00) != 0x7100) {
698     printf("Illegal Instruction %04x at %08lx\n", *(uint16 *)(r->pc), r->pc);
699     printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
700     "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
701     "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
702     "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
703     "sr %04x\n",
704     r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
705     r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
706     r->sr);
707     QuitEmulator();
708     } else {
709     // Disable interrupts
710     uint16 sr = EmulatedSR;
711     EmulatedSR |= 0x0700;
712    
713     // Call opcode routine
714 cebix 1.8 EmulOp(opcode, (M68kRegisters *)r);
715 cebix 1.1 r->pc += 2;
716    
717     // Restore interrupts
718     EmulatedSR = sr;
719     if ((EmulatedSR & 0x0700) == 0 && InterruptFlags)
720     Signal(MainTask, IRQSigMask);
721     }
722     }
723    
724     void __saveds PrivViolHandler(trap_regs *r)
725     {
726     printf("Privileged instruction %04x %04x at %08lx\n", *(uint16 *)(r->pc), *(uint16 *)(r->pc + 2), r->pc);
727     printf("d0 %08lx d1 %08lx d2 %08lx d3 %08lx\n"
728     "d4 %08lx d5 %08lx d6 %08lx d7 %08lx\n"
729     "a0 %08lx a1 %08lx a2 %08lx a3 %08lx\n"
730     "a4 %08lx a5 %08lx a6 %08lx a7 %08lx\n"
731     "sr %04x\n",
732     r->d[0], r->d[1], r->d[2], r->d[3], r->d[4], r->d[5], r->d[6], r->d[7],
733     r->a[0], r->a[1], r->a[2], r->a[3], r->a[4], r->a[5], r->a[6], r->a[7],
734     r->sr);
735     QuitEmulator();
736     }