ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.60
Committed: 2004-06-24T15:18:42Z (20 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.59: +2 -4 lines
Log Message:
Use atexit(SDL_Quit) idiom. XCloseDisplay() only when using X11 graphics.

File Contents

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