ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.61
Committed: 2004-07-19T19:25:07Z (20 years, 1 month ago) by gbeauche
Branch: MAIN
Changes since 1.60: +3 -0 lines
Log Message:
SDL audio support

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 gbeauche 1.61 #ifdef USE_SDL_AUDIO
398     sdl_flags |= SDL_INIT_AUDIO;
399     #endif
400 gbeauche 1.59 assert(sdl_flags != 0);
401     if (SDL_Init(sdl_flags) == -1) {
402     char str[256];
403     sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError());
404     ErrorAlert(str);
405     QuitEmulator();
406     }
407 gbeauche 1.60 atexit(SDL_Quit);
408 gbeauche 1.59 #endif
409 cebix 1.1
410     // Init system routines
411     SysInit();
412    
413     // Show preferences editor
414     if (!PrefsFindBool("nogui"))
415     if (!PrefsEditor())
416     QuitEmulator();
417 gbeauche 1.46
418 gbeauche 1.54 // Install the handler for SIGSEGV
419 gbeauche 1.55 if (!sigsegv_install_handler(sigsegv_handler)) {
420     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno));
421     ErrorAlert(str);
422     QuitEmulator();
423     }
424 gbeauche 1.54
425 gbeauche 1.47 // Register dump state function when we got mad after a segfault
426     sigsegv_set_dump_state(sigsegv_dump_state);
427 cebix 1.1
428 cebix 1.9 // Read RAM size
429 cebix 1.1 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
430     if (RAMSize < 1024*1024) {
431     WarningAlert(GetString(STR_SMALL_RAM_WARN));
432     RAMSize = 1024*1024;
433     }
434 cebix 1.9
435 gbeauche 1.20 #if REAL_ADDRESSING || DIRECT_ADDRESSING
436 gbeauche 1.33 RAMSize = RAMSize & -getpagesize(); // Round down to page boundary
437 gbeauche 1.20 #endif
438 gbeauche 1.33
439     // Initialize VM system
440     vm_init();
441 gbeauche 1.20
442 cebix 1.12 #if REAL_ADDRESSING
443 gbeauche 1.33 // Flag: RAM and ROM are contigously allocated from address 0
444     bool memory_mapped_from_zero = false;
445    
446     // Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash
447     // when trying to map a too big chunk of memory starting at address 0
448 gbeauche 1.56 #if defined(OS_solaris) || defined(OS_netbsd) || defined(PAGEZERO_HACK)
449 gbeauche 1.33 const bool can_map_all_memory = false;
450 gbeauche 1.20 #else
451 gbeauche 1.33 const bool can_map_all_memory = true;
452 gbeauche 1.20 #endif
453 gbeauche 1.33
454     // Try to allocate all memory from 0x0000, if it is not known to crash
455     if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) {
456 gbeauche 1.20 D(bug("Could allocate RAM and ROM from 0x0000\n"));
457     memory_mapped_from_zero = true;
458     }
459 gbeauche 1.33
460 gbeauche 1.56 #ifndef PAGEZERO_HACK
461 gbeauche 1.33 // Otherwise, just create the Low Memory area (0x0000..0x2000)
462     else if (vm_acquire_fixed(0, 0x2000) == 0) {
463 gbeauche 1.20 D(bug("Could allocate the Low Memory globals\n"));
464     lm_area_mapped = true;
465     }
466 gbeauche 1.33
467     // Exit on failure
468 gbeauche 1.20 else {
469 cebix 1.12 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
470     ErrorAlert(str);
471     QuitEmulator();
472     }
473     #endif
474 gbeauche 1.56 #endif /* REAL_ADDRESSING */
475 cebix 1.12
476 cebix 1.9 // Create areas for Mac RAM and ROM
477 gbeauche 1.20 #if REAL_ADDRESSING
478     if (memory_mapped_from_zero) {
479     RAMBaseHost = (uint8 *)0;
480 gbeauche 1.33 ROMBaseHost = RAMBaseHost + RAMSize;
481 gbeauche 1.20 }
482     else
483     #endif
484     {
485 gbeauche 1.33 RAMBaseHost = (uint8 *)vm_acquire(RAMSize);
486     ROMBaseHost = (uint8 *)vm_acquire(0x100000);
487     if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) {
488 cebix 1.36 ErrorAlert(STR_NO_MEM_ERR);
489 gbeauche 1.20 QuitEmulator();
490     }
491     }
492 gbeauche 1.38
493     #if USE_SCRATCHMEM_SUBTERFUGE
494     // Allocate scratch memory
495     ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE);
496     if (ScratchMem == VM_MAP_FAILED) {
497     ErrorAlert(STR_NO_MEM_ERR);
498     QuitEmulator();
499     }
500     ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
501     #endif
502 cebix 1.22
503 gbeauche 1.20 #if DIRECT_ADDRESSING
504 gbeauche 1.33 // RAMBaseMac shall always be zero
505     MEMBaseDiff = (uintptr)RAMBaseHost;
506 gbeauche 1.20 RAMBaseMac = 0;
507 gbeauche 1.33 ROMBaseMac = Host2MacAddr(ROMBaseHost);
508 gbeauche 1.20 #endif
509 gbeauche 1.33 #if REAL_ADDRESSING
510 cebix 1.12 RAMBaseMac = (uint32)RAMBaseHost;
511     ROMBaseMac = (uint32)ROMBaseHost;
512     #endif
513     D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
514     D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
515 gbeauche 1.20
516 cebix 1.1 // Get rom file path from preferences
517     const char *rom_path = PrefsFindString("rom");
518    
519     // Load Mac ROM
520     int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
521     if (rom_fd < 0) {
522 cebix 1.36 ErrorAlert(STR_NO_ROM_FILE_ERR);
523 cebix 1.1 QuitEmulator();
524     }
525     printf(GetString(STR_READING_ROM_FILE));
526     ROMSize = lseek(rom_fd, 0, SEEK_END);
527     if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
528 cebix 1.36 ErrorAlert(STR_ROM_SIZE_ERR);
529 cebix 1.1 close(rom_fd);
530     QuitEmulator();
531     }
532     lseek(rom_fd, 0, SEEK_SET);
533     if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
534 cebix 1.36 ErrorAlert(STR_ROM_FILE_READ_ERR);
535 cebix 1.1 close(rom_fd);
536     QuitEmulator();
537     }
538    
539 cebix 1.12 #if !EMULATED_68K
540     // Get CPU model
541     int mib[2] = {CTL_HW, HW_MODEL};
542     char *model;
543     size_t model_len;
544     sysctl(mib, 2, NULL, &model_len, NULL, 0);
545     model = (char *)malloc(model_len);
546     sysctl(mib, 2, model, &model_len, NULL, 0);
547     D(bug("Model: %s\n", model));
548    
549     // Set CPU and FPU type
550     CPUIs68060 = false;
551     if (strstr(model, "020"))
552     CPUType = 2;
553     else if (strstr(model, "030"))
554     CPUType = 3;
555     else if (strstr(model, "040"))
556     CPUType = 4;
557     else if (strstr(model, "060")) {
558     CPUType = 4;
559     CPUIs68060 = true;
560     } else {
561     printf("WARNING: Cannot detect CPU type, assuming 68020\n");
562     CPUType = 2;
563     }
564 cebix 1.24 FPUType = 1; // NetBSD has an FPU emulation, so the FPU ought to be available at all times
565 cebix 1.12 TwentyFourBitAddressing = false;
566     #endif
567    
568 cebix 1.3 // Initialize everything
569     if (!InitAll())
570 cebix 1.1 QuitEmulator();
571 cebix 1.12 D(bug("Initialization complete\n"));
572    
573 gbeauche 1.51 #if !EMULATED_68K
574     // (Virtual) supervisor mode, disable interrupts
575     EmulatedSR = 0x2700;
576    
577 cebix 1.12 #ifdef HAVE_PTHREADS
578     // Get handle of main thread
579     emul_thread = pthread_self();
580     #endif
581    
582     // Create and install stack for signal handlers
583     sig_stack = malloc(SIG_STACK_SIZE);
584     D(bug("Signal stack at %p\n", sig_stack));
585     if (sig_stack == NULL) {
586 cebix 1.36 ErrorAlert(STR_NOT_ENOUGH_MEMORY_ERR);
587 cebix 1.12 QuitEmulator();
588     }
589     stack_t new_stack;
590     new_stack.ss_sp = sig_stack;
591     new_stack.ss_flags = 0;
592     new_stack.ss_size = SIG_STACK_SIZE;
593     if (sigaltstack(&new_stack, NULL) < 0) {
594     sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
595     ErrorAlert(str);
596     QuitEmulator();
597     }
598    
599     // Install SIGILL handler for emulating privileged instructions and
600     // executing A-Trap and EMUL_OP opcodes
601     sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling
602     sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
603     sigaddset(&sigill_sa.sa_mask, SIGALRM);
604     sigill_sa.sa_handler = (void (*)(int))sigill_handler;
605     sigill_sa.sa_flags = SA_ONSTACK;
606     if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
607     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
608     ErrorAlert(str);
609     QuitEmulator();
610     }
611    
612     // Install virtual 68k interrupt signal handler
613     sigemptyset(&sigirq_sa.sa_mask);
614     sigaddset(&sigirq_sa.sa_mask, SIGALRM);
615     sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
616     sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
617     if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
618     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
619     ErrorAlert(str);
620     QuitEmulator();
621     }
622     #endif
623 cebix 1.1
624 cebix 1.12 #ifdef ENABLE_MON
625     // Setup SIGINT handler to enter mon
626     sigemptyset(&sigint_sa.sa_mask);
627 cebix 1.21 sigint_sa.sa_handler = (void (*)(int))sigint_handler;
628 cebix 1.12 sigint_sa.sa_flags = 0;
629     sigaction(SIGINT, &sigint_sa, NULL);
630     #endif
631 cebix 1.1
632 cebix 1.39 #if defined(HAVE_PTHREADS)
633    
634     // POSIX threads available, start 60Hz thread
635 cebix 1.44 Set_pthread_attr(&tick_thread_attr, 0);
636 cebix 1.39 tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
637     if (!tick_thread_active) {
638     sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
639     ErrorAlert(str);
640     QuitEmulator();
641     }
642     D(bug("60Hz thread started\n"));
643    
644     #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
645 cebix 1.12
646     // POSIX.4 timers and real-time signals available, start 60Hz timer
647 cebix 1.1 sigemptyset(&timer_sa.sa_mask);
648 cebix 1.19 timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick;
649 cebix 1.1 timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
650     if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
651 cebix 1.12 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
652     ErrorAlert(str);
653 cebix 1.1 QuitEmulator();
654     }
655     struct sigevent timer_event;
656     timer_event.sigev_notify = SIGEV_SIGNAL;
657     timer_event.sigev_signo = SIG_TIMER;
658     if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
659 cebix 1.12 sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
660     ErrorAlert(str);
661 cebix 1.1 QuitEmulator();
662     }
663     struct itimerspec req;
664     req.it_value.tv_sec = 0;
665     req.it_value.tv_nsec = 16625000;
666     req.it_interval.tv_sec = 0;
667     req.it_interval.tv_nsec = 16625000;
668 cebix 1.10 if (timer_settime(timer, 0, &req, NULL) < 0) {
669 cebix 1.12 sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
670     ErrorAlert(str);
671 cebix 1.1 QuitEmulator();
672     }
673 cebix 1.12 D(bug("60Hz timer started\n"));
674 cebix 1.1
675 cebix 1.12 #else
676    
677     // Start 60Hz timer
678     sigemptyset(&timer_sa.sa_mask); // Block virtual 68k interrupts during SIGARLM handling
679 cebix 1.53 #if !EMULATED_68K
680 cebix 1.12 sigaddset(&timer_sa.sa_mask, SIG_IRQ);
681 cebix 1.53 #endif
682 cebix 1.12 timer_sa.sa_handler = one_tick;
683     timer_sa.sa_flags = SA_ONSTACK | SA_RESTART;
684     if (sigaction(SIGALRM, &timer_sa, NULL) < 0) {
685     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno));
686     ErrorAlert(str);
687 cebix 1.1 QuitEmulator();
688     }
689 cebix 1.12 struct itimerval req;
690     req.it_interval.tv_sec = req.it_value.tv_sec = 0;
691     req.it_interval.tv_usec = req.it_value.tv_usec = 16625;
692     setitimer(ITIMER_REAL, &req, NULL);
693    
694 cebix 1.1 #endif
695    
696 cebix 1.12 #ifdef HAVE_PTHREADS
697     // Start XPRAM watchdog thread
698 cebix 1.41 memcpy(last_xpram, XPRAM, XPRAM_SIZE);
699 cebix 1.12 xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
700     D(bug("XPRAM thread started\n"));
701 cebix 1.4 #endif
702    
703 cebix 1.1 // Start 68k and jump to ROM boot routine
704 cebix 1.12 D(bug("Starting emulation...\n"));
705 cebix 1.1 Start680x0();
706    
707     QuitEmulator();
708     return 0;
709     }
710    
711    
712     /*
713     * Quit emulator
714     */
715    
716     void QuitEmulator(void)
717     {
718 cebix 1.12 D(bug("QuitEmulator\n"));
719    
720     #if EMULATED_68K
721 cebix 1.1 // Exit 680x0 emulation
722     Exit680x0();
723 cebix 1.12 #endif
724 cebix 1.1
725 cebix 1.39 #if defined(HAVE_PTHREADS)
726 cebix 1.1 // Stop 60Hz thread
727     if (tick_thread_active) {
728     tick_thread_cancel = true;
729     #ifdef HAVE_PTHREAD_CANCEL
730     pthread_cancel(tick_thread);
731     #endif
732     pthread_join(tick_thread, NULL);
733     }
734 cebix 1.39 #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
735     // Stop 60Hz timer
736     timer_delete(timer);
737 cebix 1.12 #else
738     struct itimerval req;
739     req.it_interval.tv_sec = req.it_value.tv_sec = 0;
740     req.it_interval.tv_usec = req.it_value.tv_usec = 0;
741     setitimer(ITIMER_REAL, &req, NULL);
742 cebix 1.1 #endif
743    
744 cebix 1.12 #ifdef HAVE_PTHREADS
745 cebix 1.1 // Stop XPRAM watchdog thread
746     if (xpram_thread_active) {
747     xpram_thread_cancel = true;
748     #ifdef HAVE_PTHREAD_CANCEL
749     pthread_cancel(xpram_thread);
750     #endif
751     pthread_join(xpram_thread, NULL);
752     }
753 cebix 1.12 #endif
754 cebix 1.1
755 cebix 1.3 // Deinitialize everything
756     ExitAll();
757 cebix 1.1
758 cebix 1.22 // Free ROM/RAM areas
759 gbeauche 1.33 if (RAMBaseHost != VM_MAP_FAILED) {
760     vm_release(RAMBaseHost, RAMSize);
761 cebix 1.22 RAMBaseHost = NULL;
762 gbeauche 1.20 }
763 gbeauche 1.33 if (ROMBaseHost != VM_MAP_FAILED) {
764     vm_release(ROMBaseHost, 0x100000);
765 cebix 1.17 ROMBaseHost = NULL;
766     }
767 cebix 1.1
768 cebix 1.22 #if USE_SCRATCHMEM_SUBTERFUGE
769 cebix 1.12 // Delete scratch memory area
770 gbeauche 1.33 if (ScratchMem != (uint8 *)VM_MAP_FAILED) {
771     vm_release((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
772 cebix 1.17 ScratchMem = NULL;
773     }
774 cebix 1.12 #endif
775    
776     #if REAL_ADDRESSING
777     // Delete Low Memory area
778     if (lm_area_mapped)
779 gbeauche 1.33 vm_release(0, 0x2000);
780 cebix 1.12 #endif
781 gbeauche 1.33
782     // Exit VM wrappers
783     vm_exit();
784 cebix 1.12
785 cebix 1.1 // Exit system routines
786     SysExit();
787    
788     // Exit preferences
789     PrefsExit();
790    
791     // Close X11 server connection
792 gbeauche 1.60 #ifndef USE_SDL_VIDEO
793 cebix 1.1 if (x_display)
794     XCloseDisplay(x_display);
795 gbeauche 1.59 #endif
796 cebix 1.1
797     exit(0);
798     }
799    
800    
801     /*
802     * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
803     * or a dynamically recompiling emulator)
804     */
805    
806     void FlushCodeCache(void *start, uint32 size)
807     {
808 gbeauche 1.50 #if USE_JIT
809     if (UseJIT)
810 gbeauche 1.57 flush_icache_range((uintptr)start, size);
811 gbeauche 1.50 #endif
812 cebix 1.12 #if !EMULATED_68K && defined(__NetBSD__)
813     m68k_sync_icache(start, size);
814     #endif
815 cebix 1.4 }
816    
817    
818     /*
819     * SIGINT handler, enters mon
820     */
821    
822 cebix 1.12 #ifdef ENABLE_MON
823 cebix 1.4 static void sigint_handler(...)
824     {
825 cebix 1.12 #if EMULATED_68K
826 cebix 1.8 uaecptr nextpc;
827 cebix 1.12 extern void m68k_dumpstate(uaecptr *nextpc);
828 cebix 1.8 m68k_dumpstate(&nextpc);
829 cebix 1.34 #endif
830 cebix 1.37 VideoQuitFullScreen();
831 cebix 1.21 char *arg[4] = {"mon", "-m", "-r", NULL};
832     mon(3, arg);
833 cebix 1.4 QuitEmulator();
834 cebix 1.1 }
835     #endif
836    
837    
838 cebix 1.44 #ifdef HAVE_PTHREADS
839     /*
840 cebix 1.45 * Pthread configuration
841 cebix 1.44 */
842 cebix 1.45
843     void Set_pthread_attr(pthread_attr_t *attr, int priority)
844 cebix 1.44 {
845     pthread_attr_init(attr);
846     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
847     // Some of these only work for superuser
848     if (geteuid() == 0) {
849     pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
850     pthread_attr_setschedpolicy(attr, SCHED_FIFO);
851     struct sched_param fifo_param;
852     fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) +
853     sched_get_priority_max(SCHED_FIFO)) / 2 +
854     priority);
855     pthread_attr_setschedparam(attr, &fifo_param);
856     }
857     if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) {
858     #ifdef PTHREAD_SCOPE_BOUND_NP
859     // If system scope is not available (eg. we're not running
860     // with CAP_SCHED_MGT capability on an SGI box), try bound
861     // scope. It exposes pthread scheduling to the kernel,
862     // without setting realtime priority.
863     pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP);
864     #endif
865     }
866     #endif
867     }
868     #endif // HAVE_PTHREADS
869    
870    
871 cebix 1.1 /*
872 cebix 1.37 * Mutexes
873     */
874    
875     #ifdef HAVE_PTHREADS
876    
877     struct B2_mutex {
878 cebix 1.44 B2_mutex() {
879     pthread_mutexattr_t attr;
880     pthread_mutexattr_init(&attr);
881     // Initialize the mutex for priority inheritance --
882     // required for accurate timing.
883     #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
884     pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
885     #endif
886     #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
887     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
888     #endif
889 gbeauche 1.49 #ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED
890 cebix 1.44 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
891 gbeauche 1.49 #endif
892 cebix 1.44 pthread_mutex_init(&m, &attr);
893     pthread_mutexattr_destroy(&attr);
894     }
895 gbeauche 1.51 ~B2_mutex() {
896     pthread_mutex_trylock(&m); // Make sure it's locked before
897     pthread_mutex_unlock(&m); // unlocking it.
898     pthread_mutex_destroy(&m);
899     }
900 cebix 1.37 pthread_mutex_t m;
901     };
902    
903     B2_mutex *B2_create_mutex(void)
904     {
905     return new B2_mutex;
906     }
907    
908     void B2_lock_mutex(B2_mutex *mutex)
909     {
910     pthread_mutex_lock(&mutex->m);
911     }
912    
913     void B2_unlock_mutex(B2_mutex *mutex)
914     {
915     pthread_mutex_unlock(&mutex->m);
916     }
917    
918     void B2_delete_mutex(B2_mutex *mutex)
919     {
920     delete mutex;
921     }
922    
923     #else
924    
925     struct B2_mutex {
926     int dummy;
927     };
928    
929     B2_mutex *B2_create_mutex(void)
930     {
931     return new B2_mutex;
932     }
933    
934     void B2_lock_mutex(B2_mutex *mutex)
935     {
936     }
937    
938     void B2_unlock_mutex(B2_mutex *mutex)
939     {
940     }
941    
942     void B2_delete_mutex(B2_mutex *mutex)
943     {
944     delete mutex;
945     }
946    
947     #endif
948    
949    
950     /*
951 cebix 1.1 * Interrupt flags (must be handled atomically!)
952     */
953    
954     uint32 InterruptFlags = 0;
955    
956 cebix 1.12 #if EMULATED_68K
957 cebix 1.1 void SetInterruptFlag(uint32 flag)
958     {
959 cebix 1.37 LOCK_INTFLAGS;
960 cebix 1.1 InterruptFlags |= flag;
961 cebix 1.37 UNLOCK_INTFLAGS;
962 cebix 1.1 }
963    
964     void ClearInterruptFlag(uint32 flag)
965     {
966 cebix 1.37 LOCK_INTFLAGS;
967 cebix 1.1 InterruptFlags &= ~flag;
968 cebix 1.37 UNLOCK_INTFLAGS;
969 cebix 1.12 }
970     #endif
971    
972     #if !EMULATED_68K
973     void TriggerInterrupt(void)
974     {
975     #if defined(HAVE_PTHREADS)
976     pthread_kill(emul_thread, SIG_IRQ);
977     #else
978     raise(SIG_IRQ);
979     #endif
980 cebix 1.22 }
981    
982     void TriggerNMI(void)
983     {
984     // not yet supported
985 cebix 1.12 }
986     #endif
987    
988    
989     /*
990     * XPRAM watchdog thread (saves XPRAM every minute)
991     */
992    
993     static void xpram_watchdog(void)
994     {
995 cebix 1.41 if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
996     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
997 cebix 1.12 SaveXPRAM();
998     }
999     }
1000    
1001     #ifdef HAVE_PTHREADS
1002     static void *xpram_func(void *arg)
1003     {
1004     while (!xpram_thread_cancel) {
1005 cebix 1.16 for (int i=0; i<60 && !xpram_thread_cancel; i++)
1006 cebix 1.29 Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true
1007 cebix 1.12 xpram_watchdog();
1008     }
1009     return NULL;
1010 cebix 1.1 }
1011 cebix 1.12 #endif
1012 cebix 1.1
1013    
1014     /*
1015     * 60Hz thread (really 60.15Hz)
1016     */
1017    
1018 cebix 1.12 static void one_second(void)
1019     {
1020     // Pseudo Mac 1Hz interrupt, update local time
1021     WriteMacInt32(0x20c, TimerDateTime());
1022    
1023 cebix 1.18 SetInterruptFlag(INTFLAG_1HZ);
1024 cebix 1.14 TriggerInterrupt();
1025    
1026 cebix 1.12 #ifndef HAVE_PTHREADS
1027     static int second_counter = 0;
1028     if (++second_counter > 60) {
1029     second_counter = 0;
1030     xpram_watchdog();
1031     }
1032     #endif
1033     }
1034    
1035 cebix 1.1 static void one_tick(...)
1036     {
1037     static int tick_counter = 0;
1038     if (++tick_counter > 60) {
1039     tick_counter = 0;
1040 cebix 1.12 one_second();
1041 cebix 1.1 }
1042    
1043 gbeauche 1.59 #if !defined(HAVE_PTHREADS) && !defined(USE_SDL_VIDEO)
1044 cebix 1.40 // No threads available, perform video refresh and networking from here
1045 cebix 1.12 VideoRefresh();
1046 cebix 1.40 SetInterruptFlag(INTFLAG_ETHER);
1047 cebix 1.12 #endif
1048    
1049 cebix 1.1 // Trigger 60Hz interrupt
1050     if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
1051     SetInterruptFlag(INTFLAG_60HZ);
1052     TriggerInterrupt();
1053     }
1054     }
1055    
1056 cebix 1.12 #ifdef HAVE_PTHREADS
1057 cebix 1.1 static void *tick_func(void *arg)
1058     {
1059 cebix 1.39 uint64 start = GetTicks_usec();
1060     int64 ticks = 0;
1061 cebix 1.16 uint64 next = GetTicks_usec();
1062 cebix 1.1 while (!tick_thread_cancel) {
1063 cebix 1.16 one_tick();
1064     next += 16625;
1065     int64 delay = next - GetTicks_usec();
1066     if (delay > 0)
1067     Delay_usec(delay);
1068     else if (delay < -16625)
1069     next = GetTicks_usec();
1070 cebix 1.39 ticks++;
1071 cebix 1.16 }
1072 cebix 1.39 uint64 end = GetTicks_usec();
1073     D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1074 cebix 1.16 return NULL;
1075     }
1076     #endif
1077 cebix 1.12
1078    
1079     #if !EMULATED_68K
1080     /*
1081     * Virtual 68k interrupt handler
1082     */
1083    
1084     static void sigirq_handler(int sig, int code, struct sigcontext *scp)
1085     {
1086     // Interrupts disabled? Then do nothing
1087     if (EmulatedSR & 0x0700)
1088     return;
1089    
1090     struct sigstate *state = (struct sigstate *)scp->sc_ap;
1091     M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
1092    
1093     // Set up interrupt frame on stack
1094     uint32 a7 = regs->a[7];
1095     a7 -= 2;
1096     WriteMacInt16(a7, 0x64);
1097     a7 -= 4;
1098     WriteMacInt32(a7, scp->sc_pc);
1099     a7 -= 2;
1100     WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
1101     scp->sc_sp = regs->a[7] = a7;
1102    
1103     // Set interrupt level
1104     EmulatedSR |= 0x2100;
1105    
1106     // Jump to MacOS interrupt handler on return
1107     scp->sc_pc = ReadMacInt32(0x64);
1108     }
1109 cebix 1.1
1110    
1111     /*
1112 cebix 1.12 * SIGILL handler, for emulation of privileged instructions and executing
1113     * A-Trap and EMUL_OP opcodes
1114 cebix 1.1 */
1115    
1116 cebix 1.12 static void sigill_handler(int sig, int code, struct sigcontext *scp)
1117 cebix 1.1 {
1118 cebix 1.12 struct sigstate *state = (struct sigstate *)scp->sc_ap;
1119     uint16 *pc = (uint16 *)scp->sc_pc;
1120     uint16 opcode = *pc;
1121     M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
1122    
1123     #define INC_PC(n) scp->sc_pc += (n)
1124    
1125     #define GET_SR (scp->sc_ps | EmulatedSR)
1126    
1127     #define STORE_SR(v) \
1128     scp->sc_ps = (v) & 0xff; \
1129 cebix 1.24 EmulatedSR = (v) & 0xe700; \
1130 cebix 1.12 if (((v) & 0x0700) == 0 && InterruptFlags) \
1131     TriggerInterrupt();
1132    
1133     //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
1134    
1135     if ((opcode & 0xf000) == 0xa000) {
1136    
1137     // A-Line instruction, set up A-Line trap frame on stack
1138     uint32 a7 = regs->a[7];
1139     a7 -= 2;
1140     WriteMacInt16(a7, 0x28);
1141     a7 -= 4;
1142     WriteMacInt32(a7, (uint32)pc);
1143     a7 -= 2;
1144     WriteMacInt16(a7, GET_SR);
1145     scp->sc_sp = regs->a[7] = a7;
1146    
1147     // Jump to MacOS A-Line handler on return
1148     scp->sc_pc = ReadMacInt32(0x28);
1149    
1150     } else if ((opcode & 0xff00) == 0x7100) {
1151    
1152     // Extended opcode, push registers on user stack
1153     uint32 a7 = regs->a[7];
1154     a7 -= 4;
1155     WriteMacInt32(a7, (uint32)pc);
1156     a7 -= 2;
1157     WriteMacInt16(a7, scp->sc_ps);
1158     for (int i=7; i>=0; i--) {
1159     a7 -= 4;
1160     WriteMacInt32(a7, regs->a[i]);
1161     }
1162     for (int i=7; i>=0; i--) {
1163     a7 -= 4;
1164     WriteMacInt32(a7, regs->d[i]);
1165     }
1166     scp->sc_sp = regs->a[7] = a7;
1167    
1168     // Jump to EmulOp trampoline code on return
1169     scp->sc_pc = (uint32)EmulOpTrampoline;
1170    
1171     } else switch (opcode) { // Emulate privileged instructions
1172    
1173     case 0x40e7: // move sr,-(sp)
1174     regs->a[7] -= 2;
1175     WriteMacInt16(regs->a[7], GET_SR);
1176     scp->sc_sp = regs->a[7];
1177     INC_PC(2);
1178     break;
1179    
1180     case 0x46df: { // move (sp)+,sr
1181     uint16 sr = ReadMacInt16(regs->a[7]);
1182     STORE_SR(sr);
1183     regs->a[7] += 2;
1184     scp->sc_sp = regs->a[7];
1185     INC_PC(2);
1186     break;
1187     }
1188    
1189     case 0x007c: { // ori #xxxx,sr
1190     uint16 sr = GET_SR | pc[1];
1191     scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
1192 cebix 1.24 EmulatedSR = sr & 0xe700;
1193 cebix 1.12 INC_PC(4);
1194     break;
1195     }
1196    
1197     case 0x027c: { // andi #xxxx,sr
1198     uint16 sr = GET_SR & pc[1];
1199     STORE_SR(sr);
1200     INC_PC(4);
1201     break;
1202     }
1203    
1204     case 0x46fc: // move #xxxx,sr
1205     STORE_SR(pc[1]);
1206     INC_PC(4);
1207     break;
1208    
1209     case 0x46ef: { // move (xxxx,sp),sr
1210     uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
1211     STORE_SR(sr);
1212     INC_PC(4);
1213     break;
1214     }
1215    
1216     case 0x46d8: // move (a0)+,sr
1217     case 0x46d9: { // move (a1)+,sr
1218     uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
1219     STORE_SR(sr);
1220     regs->a[opcode & 7] += 2;
1221     INC_PC(2);
1222     break;
1223     }
1224 cebix 1.1
1225 cebix 1.12 case 0x40f8: // move sr,xxxx.w
1226     WriteMacInt16(pc[1], GET_SR);
1227     INC_PC(4);
1228     break;
1229    
1230     case 0x40d0: // move sr,(a0)
1231     case 0x40d1: // move sr,(a1)
1232     case 0x40d2: // move sr,(a2)
1233     case 0x40d3: // move sr,(a3)
1234     case 0x40d4: // move sr,(a4)
1235     case 0x40d5: // move sr,(a5)
1236     case 0x40d6: // move sr,(a6)
1237     case 0x40d7: // move sr,(sp)
1238     WriteMacInt16(regs->a[opcode & 7], GET_SR);
1239     INC_PC(2);
1240     break;
1241    
1242     case 0x40c0: // move sr,d0
1243     case 0x40c1: // move sr,d1
1244     case 0x40c2: // move sr,d2
1245     case 0x40c3: // move sr,d3
1246     case 0x40c4: // move sr,d4
1247     case 0x40c5: // move sr,d5
1248     case 0x40c6: // move sr,d6
1249     case 0x40c7: // move sr,d7
1250     regs->d[opcode & 7] = GET_SR;
1251     INC_PC(2);
1252     break;
1253    
1254     case 0x46c0: // move d0,sr
1255     case 0x46c1: // move d1,sr
1256     case 0x46c2: // move d2,sr
1257     case 0x46c3: // move d3,sr
1258     case 0x46c4: // move d4,sr
1259     case 0x46c5: // move d5,sr
1260     case 0x46c6: // move d6,sr
1261     case 0x46c7: { // move d7,sr
1262     uint16 sr = regs->d[opcode & 7];
1263     STORE_SR(sr);
1264     INC_PC(2);
1265     break;
1266 cebix 1.1 }
1267 cebix 1.12
1268     case 0xf327: // fsave -(sp)
1269 cebix 1.35 regs->a[7] -= 4;
1270     WriteMacInt32(regs->a[7], 0x41000000); // Idle frame
1271 cebix 1.24 scp->sc_sp = regs->a[7];
1272     INC_PC(2);
1273     break;
1274 cebix 1.12
1275     case 0xf35f: // frestore (sp)+
1276 cebix 1.35 regs->a[7] += 4;
1277 cebix 1.24 scp->sc_sp = regs->a[7];
1278     INC_PC(2);
1279     break;
1280 cebix 1.12
1281 cebix 1.24 case 0x4e73: { // rte
1282 cebix 1.12 uint32 a7 = regs->a[7];
1283     uint16 sr = ReadMacInt16(a7);
1284     a7 += 2;
1285     scp->sc_ps = sr & 0xff;
1286 cebix 1.24 EmulatedSR = sr & 0xe700;
1287 cebix 1.12 scp->sc_pc = ReadMacInt32(a7);
1288 cebix 1.24 a7 += 4;
1289     uint16 format = ReadMacInt16(a7) >> 12;
1290     a7 += 2;
1291     static const int frame_adj[16] = {
1292     0, 0, 4, 4, 8, 0, 0, 52, 50, 12, 24, 84, 16, 0, 0, 0
1293     };
1294     scp->sc_sp = regs->a[7] = a7 + frame_adj[format];
1295 cebix 1.12 break;
1296 cebix 1.1 }
1297 cebix 1.12
1298     case 0x4e7a: // movec cr,x
1299     switch (pc[1]) {
1300     case 0x0002: // movec cacr,d0
1301     regs->d[0] = 0x3111;
1302     break;
1303     case 0x1002: // movec cacr,d1
1304     regs->d[1] = 0x3111;
1305     break;
1306     case 0x0003: // movec tc,d0
1307 cebix 1.24 case 0x0004: // movec itt0,d0
1308     case 0x0005: // movec itt1,d0
1309     case 0x0006: // movec dtt0,d0
1310     case 0x0007: // movec dtt1,d0
1311     case 0x0806: // movec urp,d0
1312     case 0x0807: // movec srp,d0
1313 cebix 1.12 regs->d[0] = 0;
1314     break;
1315 cebix 1.24 case 0x1000: // movec sfc,d1
1316     case 0x1001: // movec dfc,d1
1317 cebix 1.12 case 0x1003: // movec tc,d1
1318 cebix 1.24 case 0x1801: // movec vbr,d1
1319 cebix 1.12 regs->d[1] = 0;
1320     break;
1321 cebix 1.24 case 0x8801: // movec vbr,a0
1322     regs->a[0] = 0;
1323     break;
1324     case 0x9801: // movec vbr,a1
1325     regs->a[1] = 0;
1326     break;
1327 cebix 1.12 default:
1328     goto ill;
1329     }
1330     INC_PC(4);
1331     break;
1332    
1333     case 0x4e7b: // movec x,cr
1334     switch (pc[1]) {
1335 cebix 1.24 case 0x1000: // movec d1,sfc
1336     case 0x1001: // movec d1,dfc
1337 cebix 1.12 case 0x0801: // movec d0,vbr
1338 cebix 1.24 case 0x1801: // movec d1,vbr
1339 cebix 1.12 break;
1340     case 0x0002: // movec d0,cacr
1341     case 0x1002: // movec d1,cacr
1342     FlushCodeCache(NULL, 0);
1343     break;
1344     default:
1345     goto ill;
1346     }
1347     INC_PC(4);
1348     break;
1349    
1350     case 0xf478: // cpusha dc
1351     case 0xf4f8: // cpusha dc/ic
1352     FlushCodeCache(NULL, 0);
1353     INC_PC(2);
1354     break;
1355    
1356     default:
1357     ill: printf("SIGILL num %d, code %d\n", sig, code);
1358     printf(" context %p:\n", scp);
1359     printf(" onstack %08x\n", scp->sc_onstack);
1360     printf(" sp %08x\n", scp->sc_sp);
1361     printf(" fp %08x\n", scp->sc_fp);
1362     printf(" pc %08x\n", scp->sc_pc);
1363     printf(" opcode %04x\n", opcode);
1364     printf(" sr %08x\n", scp->sc_ps);
1365     printf(" state %p:\n", state);
1366     printf(" flags %d\n", state->ss_flags);
1367     for (int i=0; i<8; i++)
1368     printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]);
1369     for (int i=0; i<8; i++)
1370     printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1371    
1372 cebix 1.37 VideoQuitFullScreen();
1373 cebix 1.12 #ifdef ENABLE_MON
1374 cebix 1.21 char *arg[4] = {"mon", "-m", "-r", NULL};
1375     mon(3, arg);
1376 cebix 1.12 #endif
1377     QuitEmulator();
1378     break;
1379 cebix 1.1 }
1380     }
1381 cebix 1.12 #endif
1382 cebix 1.1
1383    
1384     /*
1385     * Display alert
1386     */
1387    
1388 cebix 1.12 #ifdef ENABLE_GTK
1389 cebix 1.1 static void dl_destroyed(void)
1390     {
1391     gtk_main_quit();
1392     }
1393    
1394     static void dl_quit(GtkWidget *dialog)
1395     {
1396     gtk_widget_destroy(dialog);
1397     }
1398    
1399     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1400     {
1401     char str[256];
1402     sprintf(str, GetString(prefix_id), text);
1403    
1404     GtkWidget *dialog = gtk_dialog_new();
1405     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1406     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1407     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1408     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1409    
1410     GtkWidget *label = gtk_label_new(str);
1411     gtk_widget_show(label);
1412     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1413    
1414     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1415     gtk_widget_show(button);
1416     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1417     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1418     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1419     gtk_widget_grab_default(button);
1420     gtk_widget_show(dialog);
1421    
1422     gtk_main();
1423     }
1424     #endif
1425    
1426    
1427     /*
1428     * Display error alert
1429     */
1430    
1431     void ErrorAlert(const char *text)
1432     {
1433 gbeauche 1.59 #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
1434 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1435     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1436     return;
1437     }
1438     VideoQuitFullScreen();
1439     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1440     #else
1441     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1442     #endif
1443     }
1444    
1445    
1446     /*
1447     * Display warning alert
1448     */
1449    
1450     void WarningAlert(const char *text)
1451     {
1452 gbeauche 1.59 #if defined(ENABLE_GTK) && !defined(USE_SDL_VIDEO)
1453 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1454     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1455     return;
1456     }
1457     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1458     #else
1459     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1460     #endif
1461     }
1462    
1463    
1464     /*
1465     * Display choice alert
1466     */
1467    
1468     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1469     {
1470     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1471     return false; //!!
1472     }