ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.62
Committed: 2004-11-08T21:07:07Z (19 years, 10 months ago) by gbeauche
Branch: MAIN
Changes since 1.61: +22 -2 lines
Log Message:
Enable 33-bit memory addressing on 64-bit JIT capable platforms (e.g. x86-64).
This is useful to get rid of address offset sign extensions. It uses POSIX
shared memory to create aliased regions, fallback to usual sign-extension
way if shm_open et al. don't work (e.g. no /dev/shm mounted)

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