ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/main_unix.cpp (file contents):
Revision 1.1 by cebix, 1999-10-03T14:16:25Z vs.
Revision 1.20 by gbeauche, 2000-09-22T17:14:28Z

# Line 1 | Line 1
1   /*
2   *  main_unix.cpp - Startup code for Unix
3   *
4 < *  Basilisk II (C) 1997-1999 Christian Bauer
4 > *  Basilisk II (C) 1997-2000 Christian Bauer
5   *
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
# Line 22 | Line 22
22  
23   #include <stdio.h>
24   #include <stdlib.h>
25 #include <pthread.h>
25   #include <signal.h>
26 + #include <errno.h>
27 + #include <X11/Xlib.h>
28 +
29 + #ifdef HAVE_PTHREADS
30 + # include <pthread.h>
31 + #endif
32 +
33 + #if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING || DIRECT_ADDRESSING
34 + # include <sys/mman.h>
35 + #endif
36 +
37 + #if !EMULATED_68K && defined(__NetBSD__)
38 + # include <m68k/sync_icache.h>
39 + # include <m68k/frame.h>
40 + # include <sys/param.h>
41 + # include <sys/sysctl.h>
42 + struct sigstate {
43 +        int ss_flags;
44 +        struct frame ss_frame;
45 +        struct fpframe ss_fpstate;
46 + };
47 + # define SS_FPSTATE  0x02
48 + # define SS_USERREGS 0x04
49 + #endif
50 +
51 + #ifdef ENABLE_GTK
52 + # include <gtk/gtk.h>
53 + #endif
54 +
55 + #ifdef ENABLE_XF86_DGA
56 + # include <X11/Xutil.h>
57 + # include <X11/extensions/xf86dga.h>
58 + #endif
59  
60   #include "cpu_emulation.h"
61   #include "sys.h"
62 + #include "rom_patches.h"
63   #include "xpram.h"
64   #include "timer.h"
32 #include "sony.h"
33 #include "disk.h"
34 #include "cdrom.h"
35 #include "scsi.h"
36 #include "audio.h"
65   #include "video.h"
66 < #include "serial.h"
39 < #include "ether.h"
40 < #include "clip.h"
41 < #include "rom_patches.h"
66 > #include "emul_op.h"
67   #include "prefs.h"
68   #include "prefs_editor.h"
69   #include "macos_util.h"
# Line 46 | Line 71
71   #include "version.h"
72   #include "main.h"
73  
74 < #define DEBUG 1
75 < #include "debug.h"
51 <
52 <
53 < #include <X11/Xlib.h>
54 <
55 < #if ENABLE_GTK
56 < #include <gtk/gtk.h>
74 > #ifdef ENABLE_MON
75 > # include "mon.h"
76   #endif
77  
78 < #if ENABLE_DGA
79 < #include <X11/Xlib.h>
61 < #include <X11/Xutil.h>
62 < #include <X11/extensions/xf86dga.h>
63 < #endif
78 > #define DEBUG 0
79 > #include "debug.h"
80  
81  
82   // Constants
83   const char ROM_FILE_NAME[] = "ROM";
84 + const int SIG_STACK_SIZE = SIGSTKSZ;    // Size of signal stack
85 + const int SCRATCH_MEM_SIZE = 0x10000;   // Size of scratch memory area
86 +
87 +
88 + #if !EMULATED_68K
89 + // RAM and ROM pointers
90 + uint32 RAMBaseMac;              // RAM base (Mac address space)
91 + uint8 *RAMBaseHost;             // RAM base (host address space)
92 + uint32 RAMSize;                 // Size of RAM
93 + uint32 ROMBaseMac;              // ROM base (Mac address space)
94 + uint8 *ROMBaseHost;             // ROM base (host address space)
95 + uint32 ROMSize;                 // Size of ROM
96 + #endif
97  
98  
99   // CPU and FPU type, addressing mode
# Line 75 | Line 104 | bool TwentyFourBitAddressing;
104  
105  
106   // Global variables
107 < static char *x_display_name = NULL;                                     // X11 display name
107 > char *x_display_name = NULL;                                            // X11 display name
108   Display *x_display = NULL;                                                      // X11 display handle
109  
110 + static int zero_fd = -1;                                                        // FD of /dev/zero
111 + static uint8 last_xpram[256];                                           // Buffer for monitoring XPRAM changes
112 +
113 + #ifdef HAVE_PTHREADS
114 + static pthread_t emul_thread;                                           // Handle of MacOS emulation thread (main thread)
115 +
116   static bool xpram_thread_active = false;                        // Flag: XPRAM watchdog installed
117   static volatile bool xpram_thread_cancel = false;       // Flag: Cancel XPRAM thread
118   static pthread_t xpram_thread;                                          // XPRAM watchdog
# Line 87 | Line 122 | static volatile bool tick_thread_cancel
122   static pthread_t tick_thread;                                           // 60Hz thread
123   static pthread_attr_t tick_thread_attr;                         // 60Hz thread attributes
124  
125 + #if EMULATED_68K
126   static pthread_mutex_t intflag_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect InterruptFlags
127 + #endif
128 + #endif
129 +
130 + #if !EMULATED_68K
131 + #define SIG_IRQ SIGUSR1
132 + static struct sigaction sigirq_sa;      // Virtual 68k interrupt signal
133 + static struct sigaction sigill_sa;      // Illegal instruction
134 + static void *sig_stack = NULL;          // Stack for signal handlers
135 + uint16 EmulatedSR;                                      // Emulated bits of SR (supervisor bit and interrupt mask)
136 + uint8 *ScratchMem = NULL;                       // Scratch memory for Mac ROM writes
137 + #endif
138 +
139 + #if USE_SCRATCHMEM_SUBTERFUGE
140 + uint8 *ScratchMem = 0;                          // Scratch memory for Mac ROM writes
141 + #endif
142 +
143 + static struct sigaction timer_sa;       // sigaction used for timer
144  
145   #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
146   #define SIG_TIMER SIGRTMIN
147 < static struct sigaction timer_sa;                                       // sigaction used for timer
148 < static timer_t timer;                                                           // 60Hz timer
147 > static timer_t timer;                           // 60Hz timer
148 > #endif
149 >
150 > #ifdef ENABLE_MON
151 > static struct sigaction sigint_sa;      // sigaction for SIGINT handler
152 > static void sigint_handler(...);
153 > #endif
154 >
155 > #if REAL_ADDRESSING
156 > static bool lm_area_mapped = false;     // Flag: Low Memory area mmap()ped
157 > static bool memory_mapped_from_zero = false; // Flag: Could allocate RAM area from 0
158 > #endif
159 >
160 > #ifdef USE_MAPPED_MEMORY
161 > extern char *address_space, *good_address_map;
162   #endif
163  
164  
# Line 100 | Line 166 | static timer_t timer;                                                          // 60Hz tim
166   static void *xpram_func(void *arg);
167   static void *tick_func(void *arg);
168   static void one_tick(...);
169 + #if !EMULATED_68K
170 + static void sigirq_handler(int sig, int code, struct sigcontext *scp);
171 + static void sigill_handler(int sig, int code, struct sigcontext *scp);
172 + extern "C" void EmulOpTrampoline(void);
173 + #endif
174  
175  
176   /*
# Line 126 | Line 197 | char *strdup(const char *s)
197  
198   int main(int argc, char **argv)
199   {
200 +        char str[256];
201 +
202          // Initialize variables
203          RAMBaseHost = NULL;
204          ROMBaseHost = NULL;
# Line 140 | Line 213 | int main(int argc, char **argv)
213          for (int i=1; i<argc; i++) {
214                  if (strcmp(argv[i], "-display") == 0 && ++i < argc)
215                          x_display_name = argv[i];
216 +                else if (strcmp(argv[i], "-break") == 0 && ++i < argc)
217 +                        ROMBreakpoint = strtol(argv[i], NULL, 0);
218 +                else if (strcmp(argv[i], "-rominfo") == 0)
219 +                        PrintROMInfo = true;
220          }
221  
222          // Open display
# Line 151 | Line 228 | int main(int argc, char **argv)
228                  QuitEmulator();
229          }
230  
231 < #if ENABLE_DGA
231 > #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
232          // Fork out, so we can return from fullscreen mode when things get ugly
233 <        XF86DGAForkApp(XDefaultScreen(x_display));
233 >        XF86DGAForkApp(DefaultScreen(x_display));
234   #endif
235  
236 < #if ENABLE_GTK
236 > #ifdef ENABLE_GTK
237          // Init GTK
238          gtk_set_locale();
239          gtk_init(&argc, &argv);
# Line 173 | Line 250 | int main(int argc, char **argv)
250                  if (!PrefsEditor())
251                          QuitEmulator();
252  
253 <        // Create area for Mac RAM
253 >        // Open /dev/zero
254 >        zero_fd = open("/dev/zero", O_RDWR);
255 >        if (zero_fd < 0) {
256 >                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
257 >                ErrorAlert(str);
258 >                QuitEmulator();
259 >        }
260 >
261 >        // Read RAM size
262          RAMSize = PrefsFindInt32("ramsize") & 0xfff00000;       // Round down to 1MB boundary
263          if (RAMSize < 1024*1024) {
264                  WarningAlert(GetString(STR_SMALL_RAM_WARN));
265                  RAMSize = 1024*1024;
266          }
182        RAMBaseHost = new uint8[RAMSize];
267  
268 <        // Create area for Mac ROM
269 <        ROMBaseHost = new uint8[0x100000];
268 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
269 >        const uint32 page_size = getpagesize();
270 >        const uint32 page_mask = page_size - 1;
271 >        const uint32 aligned_ram_size = (RAMSize + page_mask) & ~page_mask;
272 >        const uint32 ram_rom_size = aligned_ram_size + 0x100000;
273 > #endif
274  
275 + #if REAL_ADDRESSING
276 +        // Try to allocate the complete address space from zero
277 +        // gb-- the Solaris manpage about mmap(2) states that using MAP_FIXED
278 +        // implies undefined behaviour for further use of sbrk(), malloc(), etc.
279 + #if defined(OS_solaris)
280 +        // Anyway, it doesn't work...
281 +        if (0) {
282 + #else
283 +        if (mmap((caddr_t)0x0000, ram_rom_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) {
284 + #endif
285 +                D(bug("Could allocate RAM and ROM from 0x0000\n"));
286 +                memory_mapped_from_zero = true;
287 +        }
288 +        // Create Low Memory area (0x0000..0x2000)
289 +        else if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) {
290 +                D(bug("Could allocate the Low Memory globals\n"));
291 +                lm_area_mapped = true;
292 +        }
293 +        // Exit on error
294 +        else {
295 +                sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
296 +                ErrorAlert(str);
297 +                QuitEmulator();
298 +        }
299 + #endif
300 +
301 + #if !EMULATED_68K || USE_SCRATCHMEM_SUBTERFUGE
302 +        // Allocate scratch memory
303 +        ScratchMem = (uint8 *)malloc(SCRATCH_MEM_SIZE);
304 +        if (ScratchMem == NULL) {
305 +                ErrorAlert(GetString(STR_NO_MEM_ERR));
306 +                QuitEmulator();
307 +        }
308 +        ScratchMem += SCRATCH_MEM_SIZE/2;       // ScratchMem points to middle of block
309 + #endif
310 +
311 +        // Create areas for Mac RAM and ROM
312 + #if defined(USE_MAPPED_MEMORY)
313 +    good_address_map = (char *)mmap(NULL, 1<<24, PROT_READ, MAP_PRIVATE, zero_fd, 0);
314 +    address_space = (char *)mmap(NULL, 1<<24, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
315 +    if ((int)address_space < 0 || (int)good_address_map < 0) {
316 +                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
317 +                QuitEmulator();
318 +    }
319 +    RAMBaseHost = (uint8 *)mmap(address_space, RAMSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0);
320 +    ROMBaseHost = (uint8 *)mmap(address_space + 0x00400000, 0x80000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0);
321 +        char *nam = tmpnam(NULL);
322 +    int good_address_fd = open(nam, O_CREAT | O_RDWR, 0600);
323 +        char buffer[4096];
324 +    memset(buffer, 1, sizeof(buffer));
325 +    write(good_address_fd, buffer, sizeof(buffer));
326 +    unlink(nam);
327 +    for (int i=0; i<RAMSize; i+=4096)
328 +        mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
329 +    for (int i=0; i<0x80000; i+=4096)
330 +        mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
331 + #elif REAL_ADDRESSING || DIRECT_ADDRESSING
332 +        // gb-- Overkill, needs to be cleaned up. Probably explode it for either
333 +        // real or direct addressing mode.
334 + #if REAL_ADDRESSING
335 +        if (memory_mapped_from_zero) {
336 +                RAMBaseHost = (uint8 *)0;
337 +                ROMBaseHost = RAMBaseHost + aligned_ram_size;
338 +        }
339 +        else
340 + #endif
341 +        {
342 +                RAMBaseHost = (uint8 *)mmap(0, ram_rom_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
343 +                if (RAMBaseHost == (uint8 *)MAP_FAILED) {
344 +                        ErrorAlert(GetString(STR_NO_MEM_ERR));
345 +                        QuitEmulator();
346 +                }
347 +                ROMBaseHost = RAMBaseHost + aligned_ram_size;
348 +        }
349 + #else
350 +        RAMBaseHost = (uint8 *)malloc(RAMSize);
351 +        ROMBaseHost = (uint8 *)malloc(0x100000);
352 +        if (RAMBaseHost == NULL || ROMBaseHost == NULL) {
353 +                ErrorAlert(GetString(STR_NO_MEM_ERR));
354 +                QuitEmulator();
355 +        }
356 + #endif
357 + #if DIRECT_ADDRESSING
358 +        // Initialize MEMBaseDiff now so that Host2MacAddr in the Video module
359 +        // will return correct results
360 +        RAMBaseMac = 0;
361 +        ROMBaseMac = RAMBaseMac + aligned_ram_size;
362 +        InitMEMBaseDiff(RAMBaseHost, RAMBaseMac);
363 + #endif
364 + #if REAL_ADDRESSING // && !EMULATED_68K
365 +        RAMBaseMac = (uint32)RAMBaseHost;
366 +        ROMBaseMac = (uint32)ROMBaseHost;
367 + #endif
368 +        D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
369 +        D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
370 +        
371          // Get rom file path from preferences
372          const char *rom_path = PrefsFindString("rom");
373  
# Line 207 | Line 391 | int main(int argc, char **argv)
391                  QuitEmulator();
392          }
393  
394 <        // Check ROM version
395 <        if (!CheckROM()) {
396 <                ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
397 <                QuitEmulator();
398 <        }
394 > #if !EMULATED_68K
395 >        // Get CPU model
396 >        int mib[2] = {CTL_HW, HW_MODEL};
397 >        char *model;
398 >        size_t model_len;
399 >        sysctl(mib, 2, NULL, &model_len, NULL, 0);
400 >        model = (char *)malloc(model_len);
401 >        sysctl(mib, 2, model, &model_len, NULL, 0);
402 >        D(bug("Model: %s\n", model));
403  
404 <        // Set CPU and FPU type (UAE emulation)
217 <        switch (ROMVersion) {
218 <                case ROM_VERSION_64K:
219 <                case ROM_VERSION_PLUS:
220 <                case ROM_VERSION_CLASSIC:
221 <                        CPUType = 0;
222 <                        FPUType = 0;
223 <                        TwentyFourBitAddressing = true;
224 <                        break;
225 <                case ROM_VERSION_II:
226 <                        CPUType = 2;
227 <                        FPUType = PrefsFindBool("fpu") ? 1 : 0;
228 <                        TwentyFourBitAddressing = true;
229 <                        break;
230 <                case ROM_VERSION_32:
231 <                        CPUType = 3;
232 <                        FPUType = PrefsFindBool("fpu") ? 1 : 0;
233 <                        TwentyFourBitAddressing = false;
234 <                        break;
235 <        }
404 >        // Set CPU and FPU type
405          CPUIs68060 = false;
406 +        if (strstr(model, "020"))
407 +                CPUType = 2;
408 +        else if (strstr(model, "030"))
409 +                CPUType = 3;
410 +        else if (strstr(model, "040"))
411 +                CPUType = 4;
412 +        else if (strstr(model, "060")) {
413 +                CPUType = 4;
414 +                CPUIs68060 = true;
415 +        } else {
416 +                printf("WARNING: Cannot detect CPU type, assuming 68020\n");
417 +                CPUType = 2;
418 +        }
419 +        FPUType = 0;    //!!
420 +        TwentyFourBitAddressing = false;
421 + #endif
422  
423 <        // Load XPRAM
424 <        XPRAMInit();
425 <
426 <        // Set boot volume
242 <        int16 i16 = PrefsFindInt16("bootdrive");
243 <        XPRAM[0x78] = i16 >> 8;
244 <        XPRAM[0x79] = i16 & 0xff;
245 <        i16 = PrefsFindInt16("bootdriver");
246 <        XPRAM[0x7a] = i16 >> 8;
247 <        XPRAM[0x7b] = i16 & 0xff;
248 <
249 <        // Start XPRAM watchdog thread
250 <        xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
251 <
252 <        // Init drivers
253 <        SonyInit();
254 <        DiskInit();
255 <        CDROMInit();
256 <        SCSIInit();
257 <
258 <        // Init serial ports
259 <        SerialInit();
260 <
261 <        // Init network
262 <        EtherInit();
263 <
264 <        // Init Time Manager
265 <        TimerInit();
266 <
267 <        // Init clipboard
268 <        ClipInit();
423 >        // Initialize everything
424 >        if (!InitAll())
425 >                QuitEmulator();
426 >        D(bug("Initialization complete\n"));
427  
428 <        // Init audio
429 <        AudioInit();
428 > #ifdef HAVE_PTHREADS
429 >        // Get handle of main thread
430 >        emul_thread = pthread_self();
431 > #endif
432  
433 <        // Init video
434 <        if (!VideoInit(ROMVersion == ROM_VERSION_64K || ROMVersion == ROM_VERSION_PLUS || ROMVersion == ROM_VERSION_CLASSIC))
433 > #if !EMULATED_68K
434 >        // (Virtual) supervisor mode, disable interrupts
435 >        EmulatedSR = 0x2700;
436 >
437 >        // Create and install stack for signal handlers
438 >        sig_stack = malloc(SIG_STACK_SIZE);
439 >        D(bug("Signal stack at %p\n", sig_stack));
440 >        if (sig_stack == NULL) {
441 >                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
442                  QuitEmulator();
443 +        }
444 +        stack_t new_stack;
445 +        new_stack.ss_sp = sig_stack;
446 +        new_stack.ss_flags = 0;
447 +        new_stack.ss_size = SIG_STACK_SIZE;
448 +        if (sigaltstack(&new_stack, NULL) < 0) {
449 +                sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
450 +                ErrorAlert(str);
451 +                QuitEmulator();
452 +        }
453  
454 <        // Init 680x0 emulation (this also activates the memory system which is needed for PatchROM())
455 <        if (!Init680x0())
454 >        // Install SIGILL handler for emulating privileged instructions and
455 >        // executing A-Trap and EMUL_OP opcodes
456 >        sigemptyset(&sigill_sa.sa_mask);        // Block virtual 68k interrupts during SIGILL handling
457 >        sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
458 >        sigaddset(&sigill_sa.sa_mask, SIGALRM);
459 >        sigill_sa.sa_handler = (void (*)(int))sigill_handler;
460 >        sigill_sa.sa_flags = SA_ONSTACK;
461 >        if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
462 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
463 >                ErrorAlert(str);
464                  QuitEmulator();
465 +        }
466  
467 <        // Install ROM patches
468 <        if (!PatchROM()) {
469 <                ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
467 >        // Install virtual 68k interrupt signal handler
468 >        sigemptyset(&sigirq_sa.sa_mask);
469 >        sigaddset(&sigirq_sa.sa_mask, SIGALRM);
470 >        sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
471 >        sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
472 >        if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
473 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
474 >                ErrorAlert(str);
475                  QuitEmulator();
476          }
477 + #endif
478 +
479 + #ifdef ENABLE_MON
480 +        // Setup SIGINT handler to enter mon
481 +        sigemptyset(&sigint_sa.sa_mask);
482 +        sigint_sa.sa_handler = sigint_handler;
483 +        sigint_sa.sa_flags = 0;
484 +        sigaction(SIGINT, &sigint_sa, NULL);
485 + #endif
486  
487   #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
488 <        // Start 60Hz timer
488 >
489 >        // POSIX.4 timers and real-time signals available, start 60Hz timer
490          sigemptyset(&timer_sa.sa_mask);
491 +        timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick;
492          timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
291        timer_sa.sa_sigaction = one_tick;
493          if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
494 <                printf("FATAL: cannot set up timer signal handler\n");
494 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
495 >                ErrorAlert(str);
496                  QuitEmulator();
497          }
498          struct sigevent timer_event;
499          timer_event.sigev_notify = SIGEV_SIGNAL;
500          timer_event.sigev_signo = SIG_TIMER;
501          if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
502 <                printf("FATAL: cannot create timer\n");
502 >                sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
503 >                ErrorAlert(str);
504                  QuitEmulator();
505          }
506          struct itimerspec req;
# Line 305 | Line 508 | int main(int argc, char **argv)
508          req.it_value.tv_nsec = 16625000;
509          req.it_interval.tv_sec = 0;
510          req.it_interval.tv_nsec = 16625000;
511 <        if (timer_settime(timer, TIMER_RELTIME, &req, NULL) < 0) {
512 <                printf("FATAL: cannot start timer\n");
511 >        if (timer_settime(timer, 0, &req, NULL) < 0) {
512 >                sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
513 >                ErrorAlert(str);
514                  QuitEmulator();
515          }
516 +        D(bug("60Hz timer started\n"));
517  
518 < #else
518 > #elif defined(HAVE_PTHREADS)
519  
520 <        // Start 60Hz thread
520 >        // POSIX threads available, start 60Hz thread
521          pthread_attr_init(&tick_thread_attr);
522   #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
523          if (geteuid() == 0) {
# Line 325 | Line 530 | int main(int argc, char **argv)
530   #endif
531          tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
532          if (!tick_thread_active) {
533 <                printf("FATAL: cannot create tick thread\n");
533 >                sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
534 >                ErrorAlert(str);
535                  QuitEmulator();
536          }
537 +        D(bug("60Hz thread started\n"));
538 +
539 + #else
540 +
541 +        // Start 60Hz timer
542 +        sigemptyset(&timer_sa.sa_mask);         // Block virtual 68k interrupts during SIGARLM handling
543 +        sigaddset(&timer_sa.sa_mask, SIG_IRQ);
544 +        timer_sa.sa_handler = one_tick;
545 +        timer_sa.sa_flags = SA_ONSTACK | SA_RESTART;
546 +        if (sigaction(SIGALRM, &timer_sa, NULL) < 0) {
547 +                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno));
548 +                ErrorAlert(str);
549 +                QuitEmulator();
550 +        }
551 +        struct itimerval req;
552 +        req.it_interval.tv_sec = req.it_value.tv_sec = 0;
553 +        req.it_interval.tv_usec = req.it_value.tv_usec = 16625;
554 +        setitimer(ITIMER_REAL, &req, NULL);
555 +
556 + #endif
557 +
558 + #ifdef HAVE_PTHREADS
559 +        // Start XPRAM watchdog thread
560 +        memcpy(last_xpram, XPRAM, 256);
561 +        xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
562 +        D(bug("XPRAM thread started\n"));
563   #endif
564  
565          // Start 68k and jump to ROM boot routine
566 +        D(bug("Starting emulation...\n"));
567          Start680x0();
568  
569          QuitEmulator();
# Line 344 | Line 577 | int main(int argc, char **argv)
577  
578   void QuitEmulator(void)
579   {
580 +        D(bug("QuitEmulator\n"));
581 +
582 + #if EMULATED_68K
583          // Exit 680x0 emulation
584          Exit680x0();
585 + #endif
586  
587   #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
588          // Stop 60Hz timer
589          timer_delete(timer);
590 < #else
590 > #elif defined(HAVE_PTHREADS)
591          // Stop 60Hz thread
592          if (tick_thread_active) {
593                  tick_thread_cancel = true;
# Line 359 | Line 596 | void QuitEmulator(void)
596   #endif
597                  pthread_join(tick_thread, NULL);
598          }
599 + #else
600 +        struct itimerval req;
601 +        req.it_interval.tv_sec = req.it_value.tv_sec = 0;
602 +        req.it_interval.tv_usec = req.it_value.tv_usec = 0;
603 +        setitimer(ITIMER_REAL, &req, NULL);
604   #endif
605  
606 + #ifdef HAVE_PTHREADS
607          // Stop XPRAM watchdog thread
608          if (xpram_thread_active) {
609                  xpram_thread_cancel = true;
# Line 369 | Line 612 | void QuitEmulator(void)
612   #endif
613                  pthread_join(xpram_thread, NULL);
614          }
615 + #endif
616  
617 <        // Save XPRAM
618 <        XPRAMExit();
375 <
376 <        // Exit video
377 <        VideoExit();
378 <
379 <        // Exit audio
380 <        AudioExit();
381 <
382 <        // Exit clipboard
383 <        ClipExit();
384 <
385 <        // Exit Time Manager
386 <        TimerExit();
617 >        // Deinitialize everything
618 >        ExitAll();
619  
620 <        // Exit serial ports
621 <        SerialExit();
620 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
621 >        // Unmap ROM area
622 >        if (ROMBaseHost != (uint8 *)MAP_FAILED) {
623 >                munmap((caddr_t)ROMBaseHost, 0x100000);
624 >                ROMBaseHost = 0;
625 >        }
626 >        
627 >        //Unmap RAM area
628 >        if (RAMBaseHost != (uint8 *)MAP_FAILED) {
629 >                const uint32 page_size = getpagesize();
630 >                const uint32 page_mask = page_size - 1;
631 >                munmap((caddr_t)RAMBaseHost, ((RAMSize + page_mask) & ~page_mask));
632 >        }
633 > #else
634 >        // Delete ROM area
635 >        if (ROMBaseHost) {
636 >                free(ROMBaseHost);
637 >                ROMBaseHost = NULL;
638 >        }
639  
640 <        // Exit network
641 <        EtherExit();
640 >        // Delete RAM area
641 >        if (RAMBaseHost) {
642 >                free(RAMBaseHost);
643 >                RAMBaseHost = NULL;
644 >        }
645 > #endif
646  
647 <        // Exit drivers
648 <        SCSIExit();
649 <        CDROMExit();
650 <        DiskExit();
651 <        SonyExit();
647 > #if !EMULATED_68K || USE_SCRATMEM_SUBTERFUGE
648 >        // Delete scratch memory area
649 >        if (ScratchMem) {
650 >                free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2));
651 >                ScratchMem = NULL;
652 >        }
653 > #endif
654  
655 <        // Delete ROM area
656 <        delete[] ROMBaseHost;
655 > #if REAL_ADDRESSING
656 >        // Delete Low Memory area
657 >        if (lm_area_mapped)
658 >                munmap((char *)0x0000, 0x2000);
659 > #endif
660  
661 <        // Delete RAM area
662 <        delete[] RAMBaseHost;
661 >        // Close /dev/zero
662 >        if (zero_fd > 0)
663 >                close(zero_fd);
664  
665          // Exit system routines
666          SysExit();
# Line 422 | Line 681 | void QuitEmulator(void)
681   *  or a dynamically recompiling emulator)
682   */
683  
425 #if EMULATED_68K
684   void FlushCodeCache(void *start, uint32 size)
685   {
686 + #if !EMULATED_68K && defined(__NetBSD__)
687 +        m68k_sync_icache(start, size);
688 + #endif
689 + }
690 +
691 +
692 + /*
693 + *  SIGINT handler, enters mon
694 + */
695 +
696 + #ifdef ENABLE_MON
697 + static void sigint_handler(...)
698 + {
699 + #if EMULATED_68K
700 +        uaecptr nextpc;
701 +        extern void m68k_dumpstate(uaecptr *nextpc);
702 +        m68k_dumpstate(&nextpc);
703 + #else
704 +        char *arg[2] = {"rmon", NULL};
705 +        mon(1, arg);
706 +        QuitEmulator();
707 + #endif
708   }
709   #endif
710  
# Line 435 | Line 715 | void FlushCodeCache(void *start, uint32
715  
716   uint32 InterruptFlags = 0;
717  
718 + #if EMULATED_68K
719   void SetInterruptFlag(uint32 flag)
720   {
721 + #ifdef HAVE_PTHREADS
722          pthread_mutex_lock(&intflag_lock);
723          InterruptFlags |= flag;
724          pthread_mutex_unlock(&intflag_lock);
725 + #else
726 +        InterruptFlags |= flag;         // Pray that this is an atomic operation...
727 + #endif
728   }
729  
730   void ClearInterruptFlag(uint32 flag)
731   {
732 + #ifdef HAVE_PTHREADS
733          pthread_mutex_lock(&intflag_lock);
734          InterruptFlags &= ~flag;
735          pthread_mutex_unlock(&intflag_lock);
736 + #else
737 +        InterruptFlags &= ~flag;
738 + #endif
739   }
740 + #endif
741 +
742 + #if !EMULATED_68K
743 + void TriggerInterrupt(void)
744 + {
745 + #if defined(HAVE_PTHREADS)
746 +        pthread_kill(emul_thread, SIG_IRQ);
747 + #else
748 +        raise(SIG_IRQ);
749 + #endif
750 + }
751 + #endif
752 +
753 +
754 + /*
755 + *  XPRAM watchdog thread (saves XPRAM every minute)
756 + */
757 +
758 + static void xpram_watchdog(void)
759 + {
760 +        if (memcmp(last_xpram, XPRAM, 256)) {
761 +                memcpy(last_xpram, XPRAM, 256);
762 +                SaveXPRAM();
763 +        }
764 + }
765 +
766 + #ifdef HAVE_PTHREADS
767 + static void *xpram_func(void *arg)
768 + {
769 +        while (!xpram_thread_cancel) {
770 +                for (int i=0; i<60 && !xpram_thread_cancel; i++)
771 +                        Delay_usec(1000000);
772 +                xpram_watchdog();
773 +        }
774 +        return NULL;
775 + }
776 + #endif
777  
778  
779   /*
780   *  60Hz thread (really 60.15Hz)
781   */
782  
783 + static void one_second(void)
784 + {
785 +        // Pseudo Mac 1Hz interrupt, update local time
786 +        WriteMacInt32(0x20c, TimerDateTime());
787 +
788 +        SetInterruptFlag(INTFLAG_1HZ);
789 +        TriggerInterrupt();
790 +
791 + #ifndef HAVE_PTHREADS
792 +        static int second_counter = 0;
793 +        if (++second_counter > 60) {
794 +                second_counter = 0;
795 +                xpram_watchdog();
796 +        }
797 + #endif
798 + }
799 +
800   static void one_tick(...)
801   {
802          static int tick_counter = 0;
460
461        // Pseudo Mac 1Hz interrupt, update local time
803          if (++tick_counter > 60) {
804                  tick_counter = 0;
805 <                WriteMacInt32(0x20c, TimerDateTime());
805 >                one_second();
806          }
807  
808 + #ifndef HAVE_PTHREADS
809 +        // No threads available, perform video refresh from here
810 +        VideoRefresh();
811 + #endif
812 +
813          // Trigger 60Hz interrupt
814          if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
815                  SetInterruptFlag(INTFLAG_60HZ);
# Line 471 | Line 817 | static void one_tick(...)
817          }
818   }
819  
820 + #ifdef HAVE_PTHREADS
821   static void *tick_func(void *arg)
822   {
823 +        uint64 next = GetTicks_usec();
824          while (!tick_thread_cancel) {
477
478                // Wait
479 #ifdef HAVE_NANOSLEEP
480                struct timespec req = {0, 16625000};
481                nanosleep(&req, NULL);
482 #else
483                usleep(16625);
484 #endif
485
486                // Action
825                  one_tick();
826 +                next += 16625;
827 +                int64 delay = next - GetTicks_usec();
828 +                if (delay > 0)
829 +                        Delay_usec(delay);
830 +                else if (delay < -16625)
831 +                        next = GetTicks_usec();
832          }
833          return NULL;
834   }
835 + #endif
836  
837  
838   /*
839 < *  XPRAM watchdog thread (saves XPRAM every minute)
839 > *  Get current value of microsecond timer
840   */
841  
842 < void *xpram_func(void *arg)
842 > uint64 GetTicks_usec(void)
843   {
844 <        uint8 last_xpram[256];
845 <        memcpy(last_xpram, XPRAM, 256);
844 > #ifdef HAVE_CLOCK_GETTIME
845 >        struct timespec t;
846 >        clock_gettime(CLOCK_REALTIME, &t);
847 >        return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000;
848 > #else
849 >        struct timeval t;
850 >        gettimeofday(&t, NULL);
851 >        return (uint64)t.tv_sec * 1000000 + t.tv_usec;
852 > #endif
853 > }
854  
855 <        while (!xpram_thread_cancel) {
856 <                for (int i=0; i<60 && !xpram_thread_cancel; i++) {
857 < #ifdef HAVE_NANOSLEEP
858 <                        struct timespec req = {1, 0};
859 <                        nanosleep(&req, NULL);
855 >
856 > /*
857 > *  Delay by specified number of microseconds (<1 second)
858 > *  (adapted from SDL_Delay() source)
859 > */
860 >
861 > void Delay_usec(uint32 usec)
862 > {
863 >        int was_error;
864 > #ifndef __linux__       // Non-Linux implementations need to calculate time left
865 >        uint64 then, now, elapsed;
866 > #endif
867 >        struct timeval tv;
868 >
869 >        // Set the timeout interval - Linux only needs to do this once
870 > #ifdef __linux__
871 >        tv.tv_sec = 0;
872 >        tv.tv_usec = usec;
873   #else
874 <                        usleep(1000000);
874 >        then = GetTicks_usec();
875   #endif
876 +        do {
877 +                errno = 0;
878 + #ifndef __linux__
879 +                /* Calculate the time interval left (in case of interrupt) */
880 +                now = GetTicks_usec();
881 +                elapsed = now - then;
882 +                then = now;
883 +                if (elapsed >= usec)
884 +                        break;
885 +                usec -= elapsed;
886 +                tv.tv_sec = 0;
887 +                tv.tv_usec = usec;
888 + #endif
889 +                was_error = select(0, NULL, NULL, NULL, &tv);
890 +        } while (was_error && (errno == EINTR));
891 + }
892 +
893 +
894 + #if !EMULATED_68K
895 + /*
896 + *  Virtual 68k interrupt handler
897 + */
898 +
899 + static void sigirq_handler(int sig, int code, struct sigcontext *scp)
900 + {
901 +        // Interrupts disabled? Then do nothing
902 +        if (EmulatedSR & 0x0700)
903 +                return;
904 +
905 +        struct sigstate *state = (struct sigstate *)scp->sc_ap;
906 +        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
907 +
908 +        // Set up interrupt frame on stack
909 +        uint32 a7 = regs->a[7];
910 +        a7 -= 2;
911 +        WriteMacInt16(a7, 0x64);
912 +        a7 -= 4;
913 +        WriteMacInt32(a7, scp->sc_pc);
914 +        a7 -= 2;
915 +        WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
916 +        scp->sc_sp = regs->a[7] = a7;
917 +
918 +        // Set interrupt level
919 +        EmulatedSR |= 0x2100;
920 +
921 +        // Jump to MacOS interrupt handler on return
922 +        scp->sc_pc = ReadMacInt32(0x64);
923 + }
924 +
925 +
926 + /*
927 + *  SIGILL handler, for emulation of privileged instructions and executing
928 + *  A-Trap and EMUL_OP opcodes
929 + */
930 +
931 + static void sigill_handler(int sig, int code, struct sigcontext *scp)
932 + {
933 +        struct sigstate *state = (struct sigstate *)scp->sc_ap;
934 +        uint16 *pc = (uint16 *)scp->sc_pc;
935 +        uint16 opcode = *pc;
936 +        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
937 +
938 + #define INC_PC(n) scp->sc_pc += (n)
939 +
940 + #define GET_SR (scp->sc_ps | EmulatedSR)
941 +
942 + #define STORE_SR(v) \
943 +        scp->sc_ps = (v) & 0xff; \
944 +        EmulatedSR = (v) & 0x2700; \
945 +        if (((v) & 0x0700) == 0 && InterruptFlags) \
946 +                TriggerInterrupt();
947 +
948 + //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
949 +
950 +        if ((opcode & 0xf000) == 0xa000) {
951 +
952 +                // A-Line instruction, set up A-Line trap frame on stack
953 +                uint32 a7 = regs->a[7];
954 +                a7 -= 2;
955 +                WriteMacInt16(a7, 0x28);
956 +                a7 -= 4;
957 +                WriteMacInt32(a7, (uint32)pc);
958 +                a7 -= 2;
959 +                WriteMacInt16(a7, GET_SR);
960 +                scp->sc_sp = regs->a[7] = a7;
961 +
962 +                // Jump to MacOS A-Line handler on return
963 +                scp->sc_pc = ReadMacInt32(0x28);
964 +
965 +        } else if ((opcode & 0xff00) == 0x7100) {
966 +
967 +                // Extended opcode, push registers on user stack
968 +                uint32 a7 = regs->a[7];
969 +                a7 -= 4;
970 +                WriteMacInt32(a7, (uint32)pc);
971 +                a7 -= 2;
972 +                WriteMacInt16(a7, scp->sc_ps);
973 +                for (int i=7; i>=0; i--) {
974 +                        a7 -= 4;
975 +                        WriteMacInt32(a7, regs->a[i]);
976 +                }
977 +                for (int i=7; i>=0; i--) {
978 +                        a7 -= 4;
979 +                        WriteMacInt32(a7, regs->d[i]);
980 +                }
981 +                scp->sc_sp = regs->a[7] = a7;
982 +
983 +                // Jump to EmulOp trampoline code on return
984 +                scp->sc_pc = (uint32)EmulOpTrampoline;
985 +                
986 +        } else switch (opcode) {        // Emulate privileged instructions
987 +
988 +                case 0x40e7:    // move sr,-(sp)
989 +                        regs->a[7] -= 2;
990 +                        WriteMacInt16(regs->a[7], GET_SR);
991 +                        scp->sc_sp = regs->a[7];
992 +                        INC_PC(2);
993 +                        break;
994 +
995 +                case 0x46df: {  // move (sp)+,sr
996 +                        uint16 sr = ReadMacInt16(regs->a[7]);
997 +                        STORE_SR(sr);
998 +                        regs->a[7] += 2;
999 +                        scp->sc_sp = regs->a[7];
1000 +                        INC_PC(2);
1001 +                        break;
1002 +                }
1003 +
1004 +                case 0x007c: {  // ori #xxxx,sr
1005 +                        uint16 sr = GET_SR | pc[1];
1006 +                        scp->sc_ps = sr & 0xff;         // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
1007 +                        EmulatedSR = sr & 0x2700;
1008 +                        INC_PC(4);
1009 +                        break;
1010 +                }
1011 +
1012 +                case 0x027c: {  // andi #xxxx,sr
1013 +                        uint16 sr = GET_SR & pc[1];
1014 +                        STORE_SR(sr);
1015 +                        INC_PC(4);
1016 +                        break;
1017 +                }
1018 +
1019 +                case 0x46fc:    // move #xxxx,sr
1020 +                        STORE_SR(pc[1]);
1021 +                        INC_PC(4);
1022 +                        break;
1023 +
1024 +                case 0x46ef: {  // move (xxxx,sp),sr
1025 +                        uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
1026 +                        STORE_SR(sr);
1027 +                        INC_PC(4);
1028 +                        break;
1029 +                }
1030 +
1031 +                case 0x46d8:    // move (a0)+,sr
1032 +                case 0x46d9: {  // move (a1)+,sr
1033 +                        uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
1034 +                        STORE_SR(sr);
1035 +                        regs->a[opcode & 7] += 2;
1036 +                        INC_PC(2);
1037 +                        break;
1038                  }
1039 <                if (memcmp(last_xpram, XPRAM, 256)) {
1040 <                        memcpy(last_xpram, XPRAM, 256);
1041 <                        SaveXPRAM();
1039 >
1040 >                case 0x40f8:    // move sr,xxxx.w
1041 >                        WriteMacInt16(pc[1], GET_SR);
1042 >                        INC_PC(4);
1043 >                        break;
1044 >
1045 >                case 0x40d0:    // move sr,(a0)
1046 >                case 0x40d1:    // move sr,(a1)
1047 >                case 0x40d2:    // move sr,(a2)
1048 >                case 0x40d3:    // move sr,(a3)
1049 >                case 0x40d4:    // move sr,(a4)
1050 >                case 0x40d5:    // move sr,(a5)
1051 >                case 0x40d6:    // move sr,(a6)
1052 >                case 0x40d7:    // move sr,(sp)
1053 >                        WriteMacInt16(regs->a[opcode & 7], GET_SR);
1054 >                        INC_PC(2);
1055 >                        break;
1056 >
1057 >                case 0x40c0:    // move sr,d0
1058 >                case 0x40c1:    // move sr,d1
1059 >                case 0x40c2:    // move sr,d2
1060 >                case 0x40c3:    // move sr,d3
1061 >                case 0x40c4:    // move sr,d4
1062 >                case 0x40c5:    // move sr,d5
1063 >                case 0x40c6:    // move sr,d6
1064 >                case 0x40c7:    // move sr,d7
1065 >                        regs->d[opcode & 7] = GET_SR;
1066 >                        INC_PC(2);
1067 >                        break;
1068 >
1069 >                case 0x46c0:    // move d0,sr
1070 >                case 0x46c1:    // move d1,sr
1071 >                case 0x46c2:    // move d2,sr
1072 >                case 0x46c3:    // move d3,sr
1073 >                case 0x46c4:    // move d4,sr
1074 >                case 0x46c5:    // move d5,sr
1075 >                case 0x46c6:    // move d6,sr
1076 >                case 0x46c7: {  // move d7,sr
1077 >                        uint16 sr = regs->d[opcode & 7];
1078 >                        STORE_SR(sr);
1079 >                        INC_PC(2);
1080 >                        break;
1081                  }
1082 +
1083 +                case 0xf327:    // fsave -(sp)
1084 +                        goto ill;       //!!
1085 +
1086 +                case 0xf35f:    // frestore (sp)+
1087 +                        goto ill;       //!!
1088 +
1089 +                case 0x4e73: {  // rte (only handles format 0)
1090 +                        uint32 a7 = regs->a[7];
1091 +                        uint16 sr = ReadMacInt16(a7);
1092 +                        a7 += 2;
1093 +                        scp->sc_ps = sr & 0xff;
1094 +                        EmulatedSR = sr & 0x2700;
1095 +                        scp->sc_pc = ReadMacInt32(a7);
1096 +                        a7 += 6;
1097 +                        scp->sc_sp = regs->a[7] = a7;
1098 +                        break;
1099 +                }
1100 +
1101 +                case 0x4e7a:    // movec cr,x
1102 +                        switch (pc[1]) {
1103 +                                case 0x8801:    // movec vbr,a0
1104 +                                        regs->a[0] = 0;
1105 +                                        break;
1106 +                                case 0x9801:    // movec vbr,a1
1107 +                                        regs->a[1] = 0;
1108 +                                        break;
1109 +                                case 0x0002:    // movec cacr,d0
1110 +                                        regs->d[0] = 0x3111;
1111 +                                        break;
1112 +                                case 0x1002:    // movec cacr,d1
1113 +                                        regs->d[1] = 0x3111;
1114 +                                        break;
1115 +                                case 0x0003:    // movec tc,d0
1116 +                                        regs->d[0] = 0;
1117 +                                        break;
1118 +                                case 0x1003:    // movec tc,d1
1119 +                                        regs->d[1] = 0;
1120 +                                        break;
1121 +                                default:
1122 +                                        goto ill;
1123 +                        }
1124 +                        INC_PC(4);
1125 +                        break;
1126 +
1127 +                case 0x4e7b:    // movec x,cr
1128 +                        switch (pc[1]) {
1129 +                                case 0x0801:    // movec d0,vbr
1130 +                                        break;
1131 +                                case 0x0002:    // movec d0,cacr
1132 +                                case 0x1002:    // movec d1,cacr
1133 +                                        FlushCodeCache(NULL, 0);
1134 +                                        break;
1135 +                                default:
1136 +                                        goto ill;
1137 +                        }
1138 +                        INC_PC(4);
1139 +                        break;
1140 +
1141 +                case 0xf478:    // cpusha dc
1142 +                case 0xf4f8:    // cpusha dc/ic
1143 +                        FlushCodeCache(NULL, 0);
1144 +                        INC_PC(2);
1145 +                        break;
1146 +
1147 +                default:
1148 + ill:            printf("SIGILL num %d, code %d\n", sig, code);
1149 +                        printf(" context %p:\n", scp);
1150 +                        printf("  onstack %08x\n", scp->sc_onstack);
1151 +                        printf("  sp %08x\n", scp->sc_sp);
1152 +                        printf("  fp %08x\n", scp->sc_fp);
1153 +                        printf("  pc %08x\n", scp->sc_pc);
1154 +                        printf("   opcode %04x\n", opcode);
1155 +                        printf("  sr %08x\n", scp->sc_ps);
1156 +                        printf(" state %p:\n", state);
1157 +                        printf("  flags %d\n", state->ss_flags);
1158 +                        for (int i=0; i<8; i++)
1159 +                                printf("  d%d %08x\n", i, state->ss_frame.f_regs[i]);
1160 +                        for (int i=0; i<8; i++)
1161 +                                printf("  a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1162 +
1163 + #ifdef ENABLE_MON
1164 +                        char *arg[2] = {"rmon", NULL};
1165 +                        mon(1, arg);
1166 + #endif
1167 +                        QuitEmulator();
1168 +                        break;
1169          }
516        return NULL;
1170   }
1171 + #endif
1172  
1173  
1174   /*
1175   *  Display alert
1176   */
1177  
1178 < #if ENABLE_GTK
1178 > #ifdef ENABLE_GTK
1179   static void dl_destroyed(void)
1180   {
1181          gtk_main_quit();
# Line 566 | Line 1220 | void display_alert(int title_id, int pre
1220  
1221   void ErrorAlert(const char *text)
1222   {
1223 < #if ENABLE_GTK
1223 > #ifdef ENABLE_GTK
1224          if (PrefsFindBool("nogui") || x_display == NULL) {
1225                  printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1226                  return;
# Line 585 | Line 1239 | void ErrorAlert(const char *text)
1239  
1240   void WarningAlert(const char *text)
1241   {
1242 < #if ENABLE_GTK
1242 > #ifdef ENABLE_GTK
1243          if (PrefsFindBool("nogui") || x_display == NULL) {
1244                  printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1245                  return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines