ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.33
Committed: 2001-06-26T22:35:41Z (23 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.32: +40 -73 lines
Log Message:
- added SIGSEGV support for Linux/Alpha (to be checked), Darwin/PPC
- added uniform virtual memory allocation
  (supports mmap(), vm_allocate(), or fallbacks to malloc()/free())
- cleaned up memory allocation in main_unix.cpp

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_unix.cpp - Startup code for Unix
3     *
4 cebix 1.30 * Basilisk II (C) 1997-2001 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 "sysdeps.h"
22    
23     #include <stdio.h>
24     #include <stdlib.h>
25     #include <signal.h>
26 cebix 1.12 #include <errno.h>
27     #include <X11/Xlib.h>
28    
29     #ifdef HAVE_PTHREADS
30     # include <pthread.h>
31     #endif
32    
33 cebix 1.27 #if REAL_ADDRESSING || DIRECT_ADDRESSING
34 cebix 1.12 # include <sys/mman.h>
35     #endif
36    
37     #if !EMULATED_68K && defined(__NetBSD__)
38     # include <m68k/sync_icache.h>
39     # include <m68k/frame.h>
40     # include <sys/param.h>
41     # include <sys/sysctl.h>
42     struct sigstate {
43     int ss_flags;
44     struct frame ss_frame;
45     struct fpframe ss_fpstate;
46     };
47     # define SS_FPSTATE 0x02
48     # define SS_USERREGS 0x04
49     #endif
50    
51     #ifdef ENABLE_GTK
52     # include <gtk/gtk.h>
53 cebix 1.28 # include <gdk/gdk.h>
54 cebix 1.12 #endif
55    
56     #ifdef ENABLE_XF86_DGA
57     # include <X11/Xutil.h>
58     # include <X11/extensions/xf86dga.h>
59     #endif
60 cebix 1.1
61     #include "cpu_emulation.h"
62     #include "sys.h"
63 cebix 1.3 #include "rom_patches.h"
64 cebix 1.1 #include "xpram.h"
65     #include "timer.h"
66     #include "video.h"
67 cebix 1.12 #include "emul_op.h"
68 cebix 1.1 #include "prefs.h"
69     #include "prefs_editor.h"
70     #include "macos_util.h"
71     #include "user_strings.h"
72     #include "version.h"
73     #include "main.h"
74 gbeauche 1.33 #include "vm_alloc.h"
75 cebix 1.1
76 cebix 1.12 #ifdef ENABLE_MON
77     # include "mon.h"
78     #endif
79    
80 cebix 1.13 #define DEBUG 0
81 cebix 1.1 #include "debug.h"
82    
83    
84 cebix 1.12 // Constants
85     const char ROM_FILE_NAME[] = "ROM";
86     const int SIG_STACK_SIZE = SIGSTKSZ; // Size of signal stack
87     const int SCRATCH_MEM_SIZE = 0x10000; // Size of scratch memory area
88 cebix 1.1
89 cebix 1.4
90 cebix 1.12 #if !EMULATED_68K
91     // RAM and ROM pointers
92     uint32 RAMBaseMac; // RAM base (Mac address space)
93     uint8 *RAMBaseHost; // RAM base (host address space)
94     uint32 RAMSize; // Size of RAM
95     uint32 ROMBaseMac; // ROM base (Mac address space)
96     uint8 *ROMBaseHost; // ROM base (host address space)
97     uint32 ROMSize; // Size of ROM
98 cebix 1.9 #endif
99    
100 cebix 1.1
101     // CPU and FPU type, addressing mode
102     int CPUType;
103     bool CPUIs68060;
104     int FPUType;
105     bool TwentyFourBitAddressing;
106    
107    
108     // Global variables
109 cebix 1.13 char *x_display_name = NULL; // X11 display name
110 cebix 1.1 Display *x_display = NULL; // X11 display handle
111    
112 cebix 1.12 static uint8 last_xpram[256]; // Buffer for monitoring XPRAM changes
113    
114     #ifdef HAVE_PTHREADS
115     static pthread_t emul_thread; // Handle of MacOS emulation thread (main thread)
116    
117 cebix 1.1 static bool xpram_thread_active = false; // Flag: XPRAM watchdog installed
118     static volatile bool xpram_thread_cancel = false; // Flag: Cancel XPRAM thread
119     static pthread_t xpram_thread; // XPRAM watchdog
120    
121     static bool tick_thread_active = false; // Flag: 60Hz thread installed
122     static volatile bool tick_thread_cancel = false; // Flag: Cancel 60Hz thread
123     static pthread_t tick_thread; // 60Hz thread
124     static pthread_attr_t tick_thread_attr; // 60Hz thread attributes
125    
126 cebix 1.12 #if EMULATED_68K
127 cebix 1.1 static pthread_mutex_t intflag_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect InterruptFlags
128 cebix 1.12 #endif
129     #endif
130    
131     #if !EMULATED_68K
132     #define SIG_IRQ SIGUSR1
133     static struct sigaction sigirq_sa; // Virtual 68k interrupt signal
134     static struct sigaction sigill_sa; // Illegal instruction
135     static void *sig_stack = NULL; // Stack for signal handlers
136     uint16 EmulatedSR; // Emulated bits of SR (supervisor bit and interrupt mask)
137 gbeauche 1.20 #endif
138    
139     #if USE_SCRATCHMEM_SUBTERFUGE
140 cebix 1.22 uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes
141 cebix 1.12 #endif
142    
143     static struct sigaction timer_sa; // sigaction used for timer
144 cebix 1.1
145     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
146     #define SIG_TIMER SIGRTMIN
147 cebix 1.12 static timer_t timer; // 60Hz timer
148 cebix 1.1 #endif
149    
150 cebix 1.12 #ifdef ENABLE_MON
151     static struct sigaction sigint_sa; // sigaction for SIGINT handler
152 cebix 1.4 static void sigint_handler(...);
153 cebix 1.15 #endif
154    
155     #if REAL_ADDRESSING
156     static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
157 cebix 1.22 #endif
158    
159 cebix 1.1
160     // Prototypes
161     static void *xpram_func(void *arg);
162     static void *tick_func(void *arg);
163     static void one_tick(...);
164 cebix 1.12 #if !EMULATED_68K
165     static void sigirq_handler(int sig, int code, struct sigcontext *scp);
166     static void sigill_handler(int sig, int code, struct sigcontext *scp);
167     extern "C" void EmulOpTrampoline(void);
168     #endif
169 cebix 1.1
170    
171     /*
172     * Ersatz functions
173     */
174    
175     extern "C" {
176    
177     #ifndef HAVE_STRDUP
178     char *strdup(const char *s)
179     {
180     char *n = (char *)malloc(strlen(s) + 1);
181     strcpy(n, s);
182     return n;
183     }
184     #endif
185    
186     }
187    
188    
189     /*
190     * Main program
191     */
192    
193 cebix 1.32 static void usage(const char *prg_name)
194     {
195     printf("Usage: %s [OPTION...]\n", prg_name);
196     printf("\nUnix options:\n");
197     printf(" --display STRING\n X display to use\n");
198     printf(" --break ADDRESS\n set ROM breakpoint\n");
199     printf(" --rominfo\n dump ROM information\n");
200     PrefsPrintUsage();
201     exit(0);
202     }
203    
204 cebix 1.1 int main(int argc, char **argv)
205     {
206 cebix 1.12 char str[256];
207    
208 cebix 1.1 // Initialize variables
209     RAMBaseHost = NULL;
210     ROMBaseHost = NULL;
211     srand(time(NULL));
212     tzset();
213    
214     // Print some info
215     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
216     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
217    
218 cebix 1.28 #ifdef ENABLE_GTK
219     // Init GTK
220     gtk_set_locale();
221     gtk_init(&argc, &argv);
222     x_display_name = gdk_get_display(); // gtk_init() handles and removes the "--display" argument
223     #endif
224    
225 cebix 1.32 // Read preferences
226     PrefsInit(argc, argv);
227    
228     // Parse command line arguments
229 cebix 1.1 for (int i=1; i<argc; i++) {
230 cebix 1.32 if (strcmp(argv[i], "--help") == 0) {
231     usage(argv[0]);
232     } else if (strcmp(argv[i], "--display") == 0) {
233     i++;
234     if (i < argc)
235 cebix 1.28 x_display_name = strdup(argv[i]);
236     } else if (strcmp(argv[i], "--break") == 0) {
237 cebix 1.32 i++;
238     if (i < argc)
239 cebix 1.28 ROMBreakpoint = strtol(argv[i], NULL, 0);
240     } else if (strcmp(argv[i], "--rominfo") == 0) {
241 cebix 1.6 PrintROMInfo = true;
242 cebix 1.32 } else if (argv[i][0] == '-') {
243     fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
244     usage(argv[0]);
245 cebix 1.28 }
246 cebix 1.1 }
247    
248     // Open display
249     x_display = XOpenDisplay(x_display_name);
250     if (x_display == NULL) {
251     char str[256];
252     sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
253     ErrorAlert(str);
254     QuitEmulator();
255     }
256    
257 cebix 1.12 #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
258 cebix 1.1 // Fork out, so we can return from fullscreen mode when things get ugly
259 cebix 1.2 XF86DGAForkApp(DefaultScreen(x_display));
260 cebix 1.1 #endif
261    
262     // Init system routines
263     SysInit();
264    
265     // Show preferences editor
266     if (!PrefsFindBool("nogui"))
267     if (!PrefsEditor())
268     QuitEmulator();
269    
270 cebix 1.9 // Read RAM size
271 cebix 1.1 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
272     if (RAMSize < 1024*1024) {
273     WarningAlert(GetString(STR_SMALL_RAM_WARN));
274     RAMSize = 1024*1024;
275     }
276 cebix 1.9
277 gbeauche 1.20 #if REAL_ADDRESSING || DIRECT_ADDRESSING
278 gbeauche 1.33 RAMSize = RAMSize & -getpagesize(); // Round down to page boundary
279 gbeauche 1.20 #endif
280 gbeauche 1.33
281     // Initialize VM system
282     vm_init();
283 gbeauche 1.20
284 cebix 1.12 #if REAL_ADDRESSING
285 gbeauche 1.33 // Flag: RAM and ROM are contigously allocated from address 0
286     bool memory_mapped_from_zero = false;
287    
288     // Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash
289     // when trying to map a too big chunk of memory starting at address 0
290 cebix 1.22 #if defined(OS_solaris) || defined(OS_netbsd)
291 gbeauche 1.33 const bool can_map_all_memory = false;
292 gbeauche 1.20 #else
293 gbeauche 1.33 const bool can_map_all_memory = true;
294 gbeauche 1.20 #endif
295 gbeauche 1.33
296     // Try to allocate all memory from 0x0000, if it is not known to crash
297     if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) {
298 gbeauche 1.20 D(bug("Could allocate RAM and ROM from 0x0000\n"));
299     memory_mapped_from_zero = true;
300     }
301 gbeauche 1.33
302     // Otherwise, just create the Low Memory area (0x0000..0x2000)
303     else if (vm_acquire_fixed(0, 0x2000) == 0) {
304 gbeauche 1.20 D(bug("Could allocate the Low Memory globals\n"));
305     lm_area_mapped = true;
306     }
307 gbeauche 1.33
308     // Exit on failure
309 gbeauche 1.20 else {
310 cebix 1.12 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
311     ErrorAlert(str);
312     QuitEmulator();
313     }
314     #endif
315    
316 cebix 1.22 #if USE_SCRATCHMEM_SUBTERFUGE
317 cebix 1.12 // Allocate scratch memory
318 gbeauche 1.33 ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE);
319     if (ScratchMem == VM_MAP_FAILED) {
320 cebix 1.12 ErrorAlert(GetString(STR_NO_MEM_ERR));
321     QuitEmulator();
322     }
323     ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
324     #endif
325    
326 cebix 1.9 // Create areas for Mac RAM and ROM
327 gbeauche 1.20 #if REAL_ADDRESSING
328     if (memory_mapped_from_zero) {
329     RAMBaseHost = (uint8 *)0;
330 gbeauche 1.33 ROMBaseHost = RAMBaseHost + RAMSize;
331 gbeauche 1.20 }
332     else
333     #endif
334     {
335 gbeauche 1.33 RAMBaseHost = (uint8 *)vm_acquire(RAMSize);
336     ROMBaseHost = (uint8 *)vm_acquire(0x100000);
337     if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) {
338 gbeauche 1.20 ErrorAlert(GetString(STR_NO_MEM_ERR));
339     QuitEmulator();
340     }
341     }
342 cebix 1.22
343 gbeauche 1.20 #if DIRECT_ADDRESSING
344 gbeauche 1.33 // RAMBaseMac shall always be zero
345     MEMBaseDiff = (uintptr)RAMBaseHost;
346 gbeauche 1.20 RAMBaseMac = 0;
347 gbeauche 1.33 ROMBaseMac = Host2MacAddr(ROMBaseHost);
348 gbeauche 1.20 #endif
349 gbeauche 1.33 #if REAL_ADDRESSING
350 cebix 1.12 RAMBaseMac = (uint32)RAMBaseHost;
351     ROMBaseMac = (uint32)ROMBaseHost;
352     #endif
353     D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
354     D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
355 gbeauche 1.20
356 cebix 1.1 // Get rom file path from preferences
357     const char *rom_path = PrefsFindString("rom");
358    
359     // Load Mac ROM
360     int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
361     if (rom_fd < 0) {
362     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
363     QuitEmulator();
364     }
365     printf(GetString(STR_READING_ROM_FILE));
366     ROMSize = lseek(rom_fd, 0, SEEK_END);
367     if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
368     ErrorAlert(GetString(STR_ROM_SIZE_ERR));
369     close(rom_fd);
370     QuitEmulator();
371     }
372     lseek(rom_fd, 0, SEEK_SET);
373     if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
374     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
375     close(rom_fd);
376     QuitEmulator();
377     }
378    
379 cebix 1.12 #if !EMULATED_68K
380     // Get CPU model
381     int mib[2] = {CTL_HW, HW_MODEL};
382     char *model;
383     size_t model_len;
384     sysctl(mib, 2, NULL, &model_len, NULL, 0);
385     model = (char *)malloc(model_len);
386     sysctl(mib, 2, model, &model_len, NULL, 0);
387     D(bug("Model: %s\n", model));
388    
389     // Set CPU and FPU type
390     CPUIs68060 = false;
391     if (strstr(model, "020"))
392     CPUType = 2;
393     else if (strstr(model, "030"))
394     CPUType = 3;
395     else if (strstr(model, "040"))
396     CPUType = 4;
397     else if (strstr(model, "060")) {
398     CPUType = 4;
399     CPUIs68060 = true;
400     } else {
401     printf("WARNING: Cannot detect CPU type, assuming 68020\n");
402     CPUType = 2;
403     }
404 cebix 1.24 FPUType = 1; // NetBSD has an FPU emulation, so the FPU ought to be available at all times
405 cebix 1.12 TwentyFourBitAddressing = false;
406     #endif
407    
408 cebix 1.3 // Initialize everything
409     if (!InitAll())
410 cebix 1.1 QuitEmulator();
411 cebix 1.12 D(bug("Initialization complete\n"));
412    
413     #ifdef HAVE_PTHREADS
414     // Get handle of main thread
415     emul_thread = pthread_self();
416     #endif
417    
418     #if !EMULATED_68K
419     // (Virtual) supervisor mode, disable interrupts
420     EmulatedSR = 0x2700;
421    
422     // Create and install stack for signal handlers
423     sig_stack = malloc(SIG_STACK_SIZE);
424     D(bug("Signal stack at %p\n", sig_stack));
425     if (sig_stack == NULL) {
426     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
427     QuitEmulator();
428     }
429     stack_t new_stack;
430     new_stack.ss_sp = sig_stack;
431     new_stack.ss_flags = 0;
432     new_stack.ss_size = SIG_STACK_SIZE;
433     if (sigaltstack(&new_stack, NULL) < 0) {
434     sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
435     ErrorAlert(str);
436     QuitEmulator();
437     }
438    
439     // Install SIGILL handler for emulating privileged instructions and
440     // executing A-Trap and EMUL_OP opcodes
441     sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling
442     sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
443     sigaddset(&sigill_sa.sa_mask, SIGALRM);
444     sigill_sa.sa_handler = (void (*)(int))sigill_handler;
445     sigill_sa.sa_flags = SA_ONSTACK;
446     if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
447     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
448     ErrorAlert(str);
449     QuitEmulator();
450     }
451    
452     // Install virtual 68k interrupt signal handler
453     sigemptyset(&sigirq_sa.sa_mask);
454     sigaddset(&sigirq_sa.sa_mask, SIGALRM);
455     sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
456     sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
457     if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
458     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
459     ErrorAlert(str);
460     QuitEmulator();
461     }
462     #endif
463 cebix 1.1
464 cebix 1.12 #ifdef ENABLE_MON
465     // Setup SIGINT handler to enter mon
466     sigemptyset(&sigint_sa.sa_mask);
467 cebix 1.21 sigint_sa.sa_handler = (void (*)(int))sigint_handler;
468 cebix 1.12 sigint_sa.sa_flags = 0;
469     sigaction(SIGINT, &sigint_sa, NULL);
470     #endif
471 cebix 1.1
472     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
473 cebix 1.12
474     // POSIX.4 timers and real-time signals available, start 60Hz timer
475 cebix 1.1 sigemptyset(&timer_sa.sa_mask);
476 cebix 1.19 timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick;
477 cebix 1.1 timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
478     if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
479 cebix 1.12 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
480     ErrorAlert(str);
481 cebix 1.1 QuitEmulator();
482     }
483     struct sigevent timer_event;
484     timer_event.sigev_notify = SIGEV_SIGNAL;
485     timer_event.sigev_signo = SIG_TIMER;
486     if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
487 cebix 1.12 sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
488     ErrorAlert(str);
489 cebix 1.1 QuitEmulator();
490     }
491     struct itimerspec req;
492     req.it_value.tv_sec = 0;
493     req.it_value.tv_nsec = 16625000;
494     req.it_interval.tv_sec = 0;
495     req.it_interval.tv_nsec = 16625000;
496 cebix 1.10 if (timer_settime(timer, 0, &req, NULL) < 0) {
497 cebix 1.12 sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
498     ErrorAlert(str);
499 cebix 1.1 QuitEmulator();
500     }
501 cebix 1.12 D(bug("60Hz timer started\n"));
502 cebix 1.1
503 cebix 1.12 #elif defined(HAVE_PTHREADS)
504 cebix 1.1
505 cebix 1.12 // POSIX threads available, start 60Hz thread
506 cebix 1.1 pthread_attr_init(&tick_thread_attr);
507     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
508     if (geteuid() == 0) {
509     pthread_attr_setinheritsched(&tick_thread_attr, PTHREAD_EXPLICIT_SCHED);
510     pthread_attr_setschedpolicy(&tick_thread_attr, SCHED_FIFO);
511     struct sched_param fifo_param;
512     fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2;
513     pthread_attr_setschedparam(&tick_thread_attr, &fifo_param);
514     }
515     #endif
516     tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
517     if (!tick_thread_active) {
518 cebix 1.12 sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
519     ErrorAlert(str);
520     QuitEmulator();
521     }
522     D(bug("60Hz thread started\n"));
523    
524     #else
525    
526     // Start 60Hz timer
527     sigemptyset(&timer_sa.sa_mask); // Block virtual 68k interrupts during SIGARLM handling
528     sigaddset(&timer_sa.sa_mask, SIG_IRQ);
529     timer_sa.sa_handler = one_tick;
530     timer_sa.sa_flags = SA_ONSTACK | SA_RESTART;
531     if (sigaction(SIGALRM, &timer_sa, NULL) < 0) {
532     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno));
533     ErrorAlert(str);
534 cebix 1.1 QuitEmulator();
535     }
536 cebix 1.12 struct itimerval req;
537     req.it_interval.tv_sec = req.it_value.tv_sec = 0;
538     req.it_interval.tv_usec = req.it_value.tv_usec = 16625;
539     setitimer(ITIMER_REAL, &req, NULL);
540    
541 cebix 1.1 #endif
542    
543 cebix 1.12 #ifdef HAVE_PTHREADS
544     // Start XPRAM watchdog thread
545     memcpy(last_xpram, XPRAM, 256);
546     xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
547     D(bug("XPRAM thread started\n"));
548 cebix 1.4 #endif
549    
550 cebix 1.1 // Start 68k and jump to ROM boot routine
551 cebix 1.12 D(bug("Starting emulation...\n"));
552 cebix 1.1 Start680x0();
553    
554     QuitEmulator();
555     return 0;
556     }
557    
558    
559     /*
560     * Quit emulator
561     */
562    
563     void QuitEmulator(void)
564     {
565 cebix 1.12 D(bug("QuitEmulator\n"));
566    
567     #if EMULATED_68K
568 cebix 1.1 // Exit 680x0 emulation
569     Exit680x0();
570 cebix 1.12 #endif
571 cebix 1.1
572     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
573     // Stop 60Hz timer
574     timer_delete(timer);
575 cebix 1.12 #elif defined(HAVE_PTHREADS)
576 cebix 1.1 // Stop 60Hz thread
577     if (tick_thread_active) {
578     tick_thread_cancel = true;
579     #ifdef HAVE_PTHREAD_CANCEL
580     pthread_cancel(tick_thread);
581     #endif
582     pthread_join(tick_thread, NULL);
583     }
584 cebix 1.12 #else
585     struct itimerval req;
586     req.it_interval.tv_sec = req.it_value.tv_sec = 0;
587     req.it_interval.tv_usec = req.it_value.tv_usec = 0;
588     setitimer(ITIMER_REAL, &req, NULL);
589 cebix 1.1 #endif
590    
591 cebix 1.12 #ifdef HAVE_PTHREADS
592 cebix 1.1 // Stop XPRAM watchdog thread
593     if (xpram_thread_active) {
594     xpram_thread_cancel = true;
595     #ifdef HAVE_PTHREAD_CANCEL
596     pthread_cancel(xpram_thread);
597     #endif
598     pthread_join(xpram_thread, NULL);
599     }
600 cebix 1.12 #endif
601 cebix 1.1
602 cebix 1.3 // Deinitialize everything
603     ExitAll();
604 cebix 1.1
605 cebix 1.22 // Free ROM/RAM areas
606 gbeauche 1.33 if (RAMBaseHost != VM_MAP_FAILED) {
607     vm_release(RAMBaseHost, RAMSize);
608 cebix 1.22 RAMBaseHost = NULL;
609 gbeauche 1.20 }
610 gbeauche 1.33 if (ROMBaseHost != VM_MAP_FAILED) {
611     vm_release(ROMBaseHost, 0x100000);
612 cebix 1.17 ROMBaseHost = NULL;
613     }
614 cebix 1.1
615 cebix 1.22 #if USE_SCRATCHMEM_SUBTERFUGE
616 cebix 1.12 // Delete scratch memory area
617 gbeauche 1.33 if (ScratchMem != (uint8 *)VM_MAP_FAILED) {
618     vm_release((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
619 cebix 1.17 ScratchMem = NULL;
620     }
621 cebix 1.12 #endif
622    
623     #if REAL_ADDRESSING
624     // Delete Low Memory area
625     if (lm_area_mapped)
626 gbeauche 1.33 vm_release(0, 0x2000);
627 cebix 1.12 #endif
628 gbeauche 1.33
629     // Exit VM wrappers
630     vm_exit();
631 cebix 1.12
632 cebix 1.1 // Exit system routines
633     SysExit();
634    
635     // Exit preferences
636     PrefsExit();
637    
638     // Close X11 server connection
639     if (x_display)
640     XCloseDisplay(x_display);
641    
642     exit(0);
643     }
644    
645    
646     /*
647     * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
648     * or a dynamically recompiling emulator)
649     */
650    
651     void FlushCodeCache(void *start, uint32 size)
652     {
653 cebix 1.12 #if !EMULATED_68K && defined(__NetBSD__)
654     m68k_sync_icache(start, size);
655     #endif
656 cebix 1.4 }
657    
658    
659     /*
660     * SIGINT handler, enters mon
661     */
662    
663 cebix 1.12 #ifdef ENABLE_MON
664 cebix 1.4 static void sigint_handler(...)
665     {
666 cebix 1.12 #if EMULATED_68K
667 cebix 1.8 uaecptr nextpc;
668 cebix 1.12 extern void m68k_dumpstate(uaecptr *nextpc);
669 cebix 1.8 m68k_dumpstate(&nextpc);
670 cebix 1.12 #else
671 cebix 1.21 char *arg[4] = {"mon", "-m", "-r", NULL};
672     mon(3, arg);
673 cebix 1.4 QuitEmulator();
674 cebix 1.12 #endif
675 cebix 1.1 }
676     #endif
677    
678    
679     /*
680     * Interrupt flags (must be handled atomically!)
681     */
682    
683     uint32 InterruptFlags = 0;
684    
685 cebix 1.12 #if EMULATED_68K
686 cebix 1.1 void SetInterruptFlag(uint32 flag)
687     {
688 cebix 1.12 #ifdef HAVE_PTHREADS
689 cebix 1.1 pthread_mutex_lock(&intflag_lock);
690     InterruptFlags |= flag;
691     pthread_mutex_unlock(&intflag_lock);
692 cebix 1.12 #else
693     InterruptFlags |= flag; // Pray that this is an atomic operation...
694     #endif
695 cebix 1.1 }
696    
697     void ClearInterruptFlag(uint32 flag)
698     {
699 cebix 1.12 #ifdef HAVE_PTHREADS
700 cebix 1.1 pthread_mutex_lock(&intflag_lock);
701     InterruptFlags &= ~flag;
702     pthread_mutex_unlock(&intflag_lock);
703 cebix 1.12 #else
704     InterruptFlags &= ~flag;
705     #endif
706     }
707     #endif
708    
709     #if !EMULATED_68K
710     void TriggerInterrupt(void)
711     {
712     #if defined(HAVE_PTHREADS)
713     pthread_kill(emul_thread, SIG_IRQ);
714     #else
715     raise(SIG_IRQ);
716     #endif
717 cebix 1.22 }
718    
719     void TriggerNMI(void)
720     {
721     // not yet supported
722 cebix 1.12 }
723     #endif
724    
725    
726     /*
727     * XPRAM watchdog thread (saves XPRAM every minute)
728     */
729    
730     static void xpram_watchdog(void)
731     {
732     if (memcmp(last_xpram, XPRAM, 256)) {
733     memcpy(last_xpram, XPRAM, 256);
734     SaveXPRAM();
735     }
736     }
737    
738     #ifdef HAVE_PTHREADS
739     static void *xpram_func(void *arg)
740     {
741     while (!xpram_thread_cancel) {
742 cebix 1.16 for (int i=0; i<60 && !xpram_thread_cancel; i++)
743 cebix 1.29 Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true
744 cebix 1.12 xpram_watchdog();
745     }
746     return NULL;
747 cebix 1.1 }
748 cebix 1.12 #endif
749 cebix 1.1
750    
751     /*
752     * 60Hz thread (really 60.15Hz)
753     */
754    
755 cebix 1.12 static void one_second(void)
756     {
757     // Pseudo Mac 1Hz interrupt, update local time
758     WriteMacInt32(0x20c, TimerDateTime());
759    
760 cebix 1.18 SetInterruptFlag(INTFLAG_1HZ);
761 cebix 1.14 TriggerInterrupt();
762    
763 cebix 1.12 #ifndef HAVE_PTHREADS
764     static int second_counter = 0;
765     if (++second_counter > 60) {
766     second_counter = 0;
767     xpram_watchdog();
768     }
769     #endif
770     }
771    
772 cebix 1.1 static void one_tick(...)
773     {
774     static int tick_counter = 0;
775     if (++tick_counter > 60) {
776     tick_counter = 0;
777 cebix 1.12 one_second();
778 cebix 1.1 }
779    
780 cebix 1.12 #ifndef HAVE_PTHREADS
781     // No threads available, perform video refresh from here
782     VideoRefresh();
783     #endif
784    
785 cebix 1.1 // Trigger 60Hz interrupt
786     if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
787     SetInterruptFlag(INTFLAG_60HZ);
788     TriggerInterrupt();
789     }
790     }
791    
792 cebix 1.12 #ifdef HAVE_PTHREADS
793 cebix 1.1 static void *tick_func(void *arg)
794     {
795 cebix 1.16 uint64 next = GetTicks_usec();
796 cebix 1.1 while (!tick_thread_cancel) {
797 cebix 1.16 one_tick();
798     next += 16625;
799     int64 delay = next - GetTicks_usec();
800     if (delay > 0)
801     Delay_usec(delay);
802     else if (delay < -16625)
803     next = GetTicks_usec();
804     }
805     return NULL;
806     }
807     #endif
808 cebix 1.1
809 cebix 1.16
810     /*
811     * Get current value of microsecond timer
812     */
813    
814     uint64 GetTicks_usec(void)
815     {
816     #ifdef HAVE_CLOCK_GETTIME
817     struct timespec t;
818     clock_gettime(CLOCK_REALTIME, &t);
819     return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000;
820 cebix 1.1 #else
821 cebix 1.16 struct timeval t;
822     gettimeofday(&t, NULL);
823     return (uint64)t.tv_sec * 1000000 + t.tv_usec;
824 cebix 1.1 #endif
825 cebix 1.16 }
826 cebix 1.1
827 cebix 1.16
828     /*
829     * Delay by specified number of microseconds (<1 second)
830 cebix 1.25 * (adapted from SDL_Delay() source; this function is designed to provide
831     * the highest accuracy possible)
832 cebix 1.16 */
833    
834 cebix 1.27 #if defined(linux)
835     // Linux select() changes its timeout parameter upon return to contain
836     // the remaining time. Most other unixen leave it unchanged or undefined.
837     #define SELECT_SETS_REMAINING
838 cebix 1.31 #elif defined(__FreeBSD__) || defined(__sun__)
839 cebix 1.27 #define USE_NANOSLEEP
840 cebix 1.31 #elif defined(HAVE_PTHREADS) && defined(sgi)
841     // SGI pthreads has a bug when using pthreads+signals+nanosleep,
842     // so instead of using nanosleep, wait on a CV which is never signalled.
843     #define USE_COND_TIMEDWAIT
844 cebix 1.27 #endif
845    
846 cebix 1.16 void Delay_usec(uint32 usec)
847     {
848     int was_error;
849 cebix 1.25
850 cebix 1.31 #if defined(USE_NANOSLEEP)
851 cebix 1.27 struct timespec elapsed, tv;
852 cebix 1.31 #elif defined(USE_COND_TIMEDWAIT)
853     // Use a local mutex and cv, so threads remain independent
854     pthread_cond_t delay_cond = PTHREAD_COND_INITIALIZER;
855     pthread_mutex_t delay_mutex = PTHREAD_MUTEX_INITIALIZER;
856     struct timespec elapsed;
857     uint64 future;
858 cebix 1.27 #else
859 cebix 1.25 struct timeval tv;
860 cebix 1.27 #ifndef SELECT_SETS_REMAINING
861 cebix 1.16 uint64 then, now, elapsed;
862     #endif
863 cebix 1.27 #endif
864 cebix 1.16
865     // Set the timeout interval - Linux only needs to do this once
866 cebix 1.31 #if defined(SELECT_SETS_REMAINING)
867 cebix 1.27 tv.tv_sec = 0;
868     tv.tv_usec = usec;
869     #elif defined(USE_NANOSLEEP)
870     elapsed.tv_sec = 0;
871     elapsed.tv_nsec = usec * 1000;
872 cebix 1.31 #elif defined(USE_COND_TIMEDWAIT)
873     future = GetTicks_usec() + usec;
874     elapsed.tv_sec = future / 1000000;
875     elapsed.tv_nsec = (future % 1000000) * 1000;
876 cebix 1.16 #else
877 cebix 1.27 then = GetTicks_usec();
878 cebix 1.16 #endif
879 cebix 1.27
880 cebix 1.16 do {
881     errno = 0;
882 cebix 1.31 #if defined(USE_NANOSLEEP)
883 cebix 1.27 tv.tv_sec = elapsed.tv_sec;
884     tv.tv_nsec = elapsed.tv_nsec;
885     was_error = nanosleep(&tv, &elapsed);
886 cebix 1.31 #elif defined(USE_COND_TIMEDWAIT)
887     was_error = pthread_mutex_lock(&delay_mutex);
888     was_error = pthread_cond_timedwait(&delay_cond, &delay_mutex, &elapsed);
889     was_error = pthread_mutex_unlock(&delay_mutex);
890 cebix 1.27 #else
891     #ifndef SELECT_SETS_REMAINING
892     // Calculate the time interval left (in case of interrupt)
893 cebix 1.16 now = GetTicks_usec();
894     elapsed = now - then;
895     then = now;
896     if (elapsed >= usec)
897     break;
898     usec -= elapsed;
899     tv.tv_sec = 0;
900     tv.tv_usec = usec;
901     #endif
902     was_error = select(0, NULL, NULL, NULL, &tv);
903 cebix 1.25 #endif
904 cebix 1.16 } while (was_error && (errno == EINTR));
905 cebix 1.1 }
906 cebix 1.12
907    
908     #if !EMULATED_68K
909     /*
910     * Virtual 68k interrupt handler
911     */
912    
913     static void sigirq_handler(int sig, int code, struct sigcontext *scp)
914     {
915     // Interrupts disabled? Then do nothing
916     if (EmulatedSR & 0x0700)
917     return;
918    
919     struct sigstate *state = (struct sigstate *)scp->sc_ap;
920     M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
921    
922     // Set up interrupt frame on stack
923     uint32 a7 = regs->a[7];
924     a7 -= 2;
925     WriteMacInt16(a7, 0x64);
926     a7 -= 4;
927     WriteMacInt32(a7, scp->sc_pc);
928     a7 -= 2;
929     WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
930     scp->sc_sp = regs->a[7] = a7;
931    
932     // Set interrupt level
933     EmulatedSR |= 0x2100;
934    
935     // Jump to MacOS interrupt handler on return
936     scp->sc_pc = ReadMacInt32(0x64);
937     }
938 cebix 1.1
939    
940     /*
941 cebix 1.12 * SIGILL handler, for emulation of privileged instructions and executing
942     * A-Trap and EMUL_OP opcodes
943 cebix 1.1 */
944    
945 cebix 1.12 static void sigill_handler(int sig, int code, struct sigcontext *scp)
946 cebix 1.1 {
947 cebix 1.12 struct sigstate *state = (struct sigstate *)scp->sc_ap;
948     uint16 *pc = (uint16 *)scp->sc_pc;
949     uint16 opcode = *pc;
950     M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
951    
952     #define INC_PC(n) scp->sc_pc += (n)
953    
954     #define GET_SR (scp->sc_ps | EmulatedSR)
955    
956     #define STORE_SR(v) \
957     scp->sc_ps = (v) & 0xff; \
958 cebix 1.24 EmulatedSR = (v) & 0xe700; \
959 cebix 1.12 if (((v) & 0x0700) == 0 && InterruptFlags) \
960     TriggerInterrupt();
961    
962     //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
963    
964     if ((opcode & 0xf000) == 0xa000) {
965    
966     // A-Line instruction, set up A-Line trap frame on stack
967     uint32 a7 = regs->a[7];
968     a7 -= 2;
969     WriteMacInt16(a7, 0x28);
970     a7 -= 4;
971     WriteMacInt32(a7, (uint32)pc);
972     a7 -= 2;
973     WriteMacInt16(a7, GET_SR);
974     scp->sc_sp = regs->a[7] = a7;
975    
976     // Jump to MacOS A-Line handler on return
977     scp->sc_pc = ReadMacInt32(0x28);
978    
979     } else if ((opcode & 0xff00) == 0x7100) {
980    
981     // Extended opcode, push registers on user stack
982     uint32 a7 = regs->a[7];
983     a7 -= 4;
984     WriteMacInt32(a7, (uint32)pc);
985     a7 -= 2;
986     WriteMacInt16(a7, scp->sc_ps);
987     for (int i=7; i>=0; i--) {
988     a7 -= 4;
989     WriteMacInt32(a7, regs->a[i]);
990     }
991     for (int i=7; i>=0; i--) {
992     a7 -= 4;
993     WriteMacInt32(a7, regs->d[i]);
994     }
995     scp->sc_sp = regs->a[7] = a7;
996    
997     // Jump to EmulOp trampoline code on return
998     scp->sc_pc = (uint32)EmulOpTrampoline;
999    
1000     } else switch (opcode) { // Emulate privileged instructions
1001    
1002     case 0x40e7: // move sr,-(sp)
1003     regs->a[7] -= 2;
1004     WriteMacInt16(regs->a[7], GET_SR);
1005     scp->sc_sp = regs->a[7];
1006     INC_PC(2);
1007     break;
1008    
1009     case 0x46df: { // move (sp)+,sr
1010     uint16 sr = ReadMacInt16(regs->a[7]);
1011     STORE_SR(sr);
1012     regs->a[7] += 2;
1013     scp->sc_sp = regs->a[7];
1014     INC_PC(2);
1015     break;
1016     }
1017    
1018     case 0x007c: { // ori #xxxx,sr
1019     uint16 sr = GET_SR | pc[1];
1020     scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
1021 cebix 1.24 EmulatedSR = sr & 0xe700;
1022 cebix 1.12 INC_PC(4);
1023     break;
1024     }
1025    
1026     case 0x027c: { // andi #xxxx,sr
1027     uint16 sr = GET_SR & pc[1];
1028     STORE_SR(sr);
1029     INC_PC(4);
1030     break;
1031     }
1032    
1033     case 0x46fc: // move #xxxx,sr
1034     STORE_SR(pc[1]);
1035     INC_PC(4);
1036     break;
1037    
1038     case 0x46ef: { // move (xxxx,sp),sr
1039     uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
1040     STORE_SR(sr);
1041     INC_PC(4);
1042     break;
1043     }
1044    
1045     case 0x46d8: // move (a0)+,sr
1046     case 0x46d9: { // move (a1)+,sr
1047     uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
1048     STORE_SR(sr);
1049     regs->a[opcode & 7] += 2;
1050     INC_PC(2);
1051     break;
1052     }
1053 cebix 1.1
1054 cebix 1.12 case 0x40f8: // move sr,xxxx.w
1055     WriteMacInt16(pc[1], GET_SR);
1056     INC_PC(4);
1057     break;
1058    
1059     case 0x40d0: // move sr,(a0)
1060     case 0x40d1: // move sr,(a1)
1061     case 0x40d2: // move sr,(a2)
1062     case 0x40d3: // move sr,(a3)
1063     case 0x40d4: // move sr,(a4)
1064     case 0x40d5: // move sr,(a5)
1065     case 0x40d6: // move sr,(a6)
1066     case 0x40d7: // move sr,(sp)
1067     WriteMacInt16(regs->a[opcode & 7], GET_SR);
1068     INC_PC(2);
1069     break;
1070    
1071     case 0x40c0: // move sr,d0
1072     case 0x40c1: // move sr,d1
1073     case 0x40c2: // move sr,d2
1074     case 0x40c3: // move sr,d3
1075     case 0x40c4: // move sr,d4
1076     case 0x40c5: // move sr,d5
1077     case 0x40c6: // move sr,d6
1078     case 0x40c7: // move sr,d7
1079     regs->d[opcode & 7] = GET_SR;
1080     INC_PC(2);
1081     break;
1082    
1083     case 0x46c0: // move d0,sr
1084     case 0x46c1: // move d1,sr
1085     case 0x46c2: // move d2,sr
1086     case 0x46c3: // move d3,sr
1087     case 0x46c4: // move d4,sr
1088     case 0x46c5: // move d5,sr
1089     case 0x46c6: // move d6,sr
1090     case 0x46c7: { // move d7,sr
1091     uint16 sr = regs->d[opcode & 7];
1092     STORE_SR(sr);
1093     INC_PC(2);
1094     break;
1095 cebix 1.1 }
1096 cebix 1.12
1097     case 0xf327: // fsave -(sp)
1098 cebix 1.24 if (CPUIs68060) {
1099     regs->a[7] -= 4;
1100     WriteMacInt32(regs->a[7], 0x60000000); // Idle frame
1101     regs->a[7] -= 4;
1102     WriteMacInt32(regs->a[7], 0);
1103     regs->a[7] -= 4;
1104     WriteMacInt32(regs->a[7], 0);
1105     } else {
1106     regs->a[7] -= 4;
1107     WriteMacInt32(regs->a[7], 0x41000000); // Idle frame
1108     }
1109     scp->sc_sp = regs->a[7];
1110     INC_PC(2);
1111     break;
1112 cebix 1.12
1113     case 0xf35f: // frestore (sp)+
1114 cebix 1.24 if (CPUIs68060)
1115     regs->a[7] += 12;
1116     else
1117     regs->a[7] += 4;
1118     scp->sc_sp = regs->a[7];
1119     INC_PC(2);
1120     break;
1121 cebix 1.12
1122 cebix 1.24 case 0x4e73: { // rte
1123 cebix 1.12 uint32 a7 = regs->a[7];
1124     uint16 sr = ReadMacInt16(a7);
1125     a7 += 2;
1126     scp->sc_ps = sr & 0xff;
1127 cebix 1.24 EmulatedSR = sr & 0xe700;
1128 cebix 1.12 scp->sc_pc = ReadMacInt32(a7);
1129 cebix 1.24 a7 += 4;
1130     uint16 format = ReadMacInt16(a7) >> 12;
1131     a7 += 2;
1132     static const int frame_adj[16] = {
1133     0, 0, 4, 4, 8, 0, 0, 52, 50, 12, 24, 84, 16, 0, 0, 0
1134     };
1135     scp->sc_sp = regs->a[7] = a7 + frame_adj[format];
1136 cebix 1.12 break;
1137 cebix 1.1 }
1138 cebix 1.12
1139     case 0x4e7a: // movec cr,x
1140     switch (pc[1]) {
1141     case 0x0002: // movec cacr,d0
1142     regs->d[0] = 0x3111;
1143     break;
1144     case 0x1002: // movec cacr,d1
1145     regs->d[1] = 0x3111;
1146     break;
1147     case 0x0003: // movec tc,d0
1148 cebix 1.24 case 0x0004: // movec itt0,d0
1149     case 0x0005: // movec itt1,d0
1150     case 0x0006: // movec dtt0,d0
1151     case 0x0007: // movec dtt1,d0
1152     case 0x0806: // movec urp,d0
1153     case 0x0807: // movec srp,d0
1154 cebix 1.12 regs->d[0] = 0;
1155     break;
1156 cebix 1.24 case 0x1000: // movec sfc,d1
1157     case 0x1001: // movec dfc,d1
1158 cebix 1.12 case 0x1003: // movec tc,d1
1159 cebix 1.24 case 0x1801: // movec vbr,d1
1160 cebix 1.12 regs->d[1] = 0;
1161     break;
1162 cebix 1.24 case 0x8801: // movec vbr,a0
1163     regs->a[0] = 0;
1164     break;
1165     case 0x9801: // movec vbr,a1
1166     regs->a[1] = 0;
1167     break;
1168 cebix 1.12 default:
1169     goto ill;
1170     }
1171     INC_PC(4);
1172     break;
1173    
1174     case 0x4e7b: // movec x,cr
1175     switch (pc[1]) {
1176 cebix 1.24 case 0x1000: // movec d1,sfc
1177     case 0x1001: // movec d1,dfc
1178 cebix 1.12 case 0x0801: // movec d0,vbr
1179 cebix 1.24 case 0x1801: // movec d1,vbr
1180 cebix 1.12 break;
1181     case 0x0002: // movec d0,cacr
1182     case 0x1002: // movec d1,cacr
1183     FlushCodeCache(NULL, 0);
1184     break;
1185     default:
1186     goto ill;
1187     }
1188     INC_PC(4);
1189     break;
1190    
1191     case 0xf478: // cpusha dc
1192     case 0xf4f8: // cpusha dc/ic
1193     FlushCodeCache(NULL, 0);
1194     INC_PC(2);
1195     break;
1196    
1197     default:
1198     ill: printf("SIGILL num %d, code %d\n", sig, code);
1199     printf(" context %p:\n", scp);
1200     printf(" onstack %08x\n", scp->sc_onstack);
1201     printf(" sp %08x\n", scp->sc_sp);
1202     printf(" fp %08x\n", scp->sc_fp);
1203     printf(" pc %08x\n", scp->sc_pc);
1204     printf(" opcode %04x\n", opcode);
1205     printf(" sr %08x\n", scp->sc_ps);
1206     printf(" state %p:\n", state);
1207     printf(" flags %d\n", state->ss_flags);
1208     for (int i=0; i<8; i++)
1209     printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]);
1210     for (int i=0; i<8; i++)
1211     printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1212    
1213     #ifdef ENABLE_MON
1214 cebix 1.21 char *arg[4] = {"mon", "-m", "-r", NULL};
1215     mon(3, arg);
1216 cebix 1.12 #endif
1217     QuitEmulator();
1218     break;
1219 cebix 1.1 }
1220     }
1221 cebix 1.12 #endif
1222 cebix 1.1
1223    
1224     /*
1225     * Display alert
1226     */
1227    
1228 cebix 1.12 #ifdef ENABLE_GTK
1229 cebix 1.1 static void dl_destroyed(void)
1230     {
1231     gtk_main_quit();
1232     }
1233    
1234     static void dl_quit(GtkWidget *dialog)
1235     {
1236     gtk_widget_destroy(dialog);
1237     }
1238    
1239     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1240     {
1241     char str[256];
1242     sprintf(str, GetString(prefix_id), text);
1243    
1244     GtkWidget *dialog = gtk_dialog_new();
1245     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1246     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1247     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1248     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1249    
1250     GtkWidget *label = gtk_label_new(str);
1251     gtk_widget_show(label);
1252     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1253    
1254     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1255     gtk_widget_show(button);
1256     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1257     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1258     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1259     gtk_widget_grab_default(button);
1260     gtk_widget_show(dialog);
1261    
1262     gtk_main();
1263     }
1264     #endif
1265    
1266    
1267     /*
1268     * Display error alert
1269     */
1270    
1271     void ErrorAlert(const char *text)
1272     {
1273 cebix 1.12 #ifdef ENABLE_GTK
1274 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1275     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1276     return;
1277     }
1278     VideoQuitFullScreen();
1279     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1280     #else
1281     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1282     #endif
1283     }
1284    
1285    
1286     /*
1287     * Display warning alert
1288     */
1289    
1290     void WarningAlert(const char *text)
1291     {
1292 cebix 1.12 #ifdef ENABLE_GTK
1293 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1294     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1295     return;
1296     }
1297     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1298     #else
1299     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1300     #endif
1301     }
1302    
1303    
1304     /*
1305     * Display choice alert
1306     */
1307    
1308     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1309     {
1310     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1311     return false; //!!
1312     }