ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.46
Committed: 2002-05-12T11:10:50Z (22 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.45: +7 -0 lines
Log Message:
Implement the "ignoresegv" feature from SheepShaver. This is Unix-specific
so far. Target platform is currently Linux/x86.

File Contents

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