ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.50
Committed: 2002-09-17T16:08:07Z (22 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.49: +13 -1 lines
Log Message:
Make FlushCodeRange aware of the JIT compiler's flush_icache()

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