ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.49
Committed: 2002-09-16T15:06:24Z (21 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.48: +2 -0 lines
Log Message:
Conditionalize use of pthread_mutexattr_setpshared() as this is an optional
POSIX feature from _POSIX_THREAD_PROCESS_SHARED group. Besides, Ulrich Drepper
mentions "the kernel support for this option wasn't available until 2.5.7.
Future versions of the thread library will support this option."
<http://people.redhat.com/drepper/posix-option-groups.html>

BTW, this nukes a warning when build on FreeBSD 4.6

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