ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.54
Committed: 2003-09-29T07:02:58Z (20 years, 10 months ago) by gbeauche
Branch: MAIN
Changes since 1.53: +30 -6 lines
Log Message:
New SIGSEGV API so that skip-instruction requests are more explicit. Yes,
that's api change, but that's cooler now for SheepShaver. ;-)

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