ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.31
Committed: 2001-03-28T16:53:39Z (23 years, 3 months ago) by cebix
Branch: MAIN
Changes since 1.30: +22 -4 lines
Log Message:
- "-Ofast" option is supplied to MIPSPro compiler [Brian J. Johnson
- workaround for IRIX pthreads bug in Delay_usec() [Brian J. Johnson]

File Contents

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