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.6 by cebix, 1999-10-27T16:59:46Z vs.
Revision 1.16 by cebix, 2000-07-22T18:12:34Z

# 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
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"
# Line 31 | Line 63
63   #include "xpram.h"
64   #include "timer.h"
65   #include "video.h"
66 + #include "emul_op.h"
67   #include "prefs.h"
68   #include "prefs_editor.h"
69   #include "macos_util.h"
# Line 38 | Line 71
71   #include "version.h"
72   #include "main.h"
73  
74 + #ifdef ENABLE_MON
75 + # include "mon.h"
76 + #endif
77 +
78   #define DEBUG 0
79   #include "debug.h"
80  
81  
82 < #include <X11/Xlib.h>
83 <
84 < #if ENABLE_GTK
85 < #include <gtk/gtk.h>
49 < #endif
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  
51 #if ENABLE_XF86_DGA
52 #include <X11/Xlib.h>
53 #include <X11/Xutil.h>
54 #include <X11/extensions/xf86dga.h>
55 #endif
87  
88 < #if ENABLE_MON
89 < #include "mon.h"
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  
62 // Constants
63 const char ROM_FILE_NAME[] = "ROM";
64
65
99   // CPU and FPU type, addressing mode
100   int CPUType;
101   bool CPUIs68060;
# Line 71 | 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 83 | 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 + uint32 ScratchMem = NULL;                       // Scratch memory for Mac ROM writes
137 + #endif
138 +
139 + static struct sigaction timer_sa;       // sigaction used for timer
140  
141   #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
142   #define SIG_TIMER SIGRTMIN
143 < static struct sigaction timer_sa;                                       // sigaction used for timer
91 < static timer_t timer;                                                           // 60Hz timer
143 > static timer_t timer;                           // 60Hz timer
144   #endif
145  
146 < #if ENABLE_MON
147 < static struct sigaction sigint_sa;                                      // sigaction for SIGINT handler
146 > #ifdef ENABLE_MON
147 > static struct sigaction sigint_sa;      // sigaction for SIGINT handler
148   static void sigint_handler(...);
149   #endif
150  
151 + #if REAL_ADDRESSING
152 + static bool lm_area_mapped = false;     // Flag: Low Memory area mmap()ped
153 + #endif
154 +
155 + #ifdef USE_MAPPED_MEMORY
156 + extern char *address_space, *good_address_map;
157 + #endif
158 +
159  
160   // Prototypes
161   static void *xpram_func(void *arg);
162   static void *tick_func(void *arg);
163   static void one_tick(...);
164 + #if !EMULATED_68K
165 + static void sigirq_handler(int sig, int code, struct sigcontext *scp);
166 + static void sigill_handler(int sig, int code, struct sigcontext *scp);
167 + extern "C" void EmulOpTrampoline(void);
168 + #endif
169  
170  
171   /*
# Line 127 | Line 192 | char *strdup(const char *s)
192  
193   int main(int argc, char **argv)
194   {
195 +        char str[256];
196 +
197          // Initialize variables
198          RAMBaseHost = NULL;
199          ROMBaseHost = NULL;
# Line 156 | Line 223 | int main(int argc, char **argv)
223                  QuitEmulator();
224          }
225  
226 < #if ENABLE_XF86_DGA
226 > #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
227          // Fork out, so we can return from fullscreen mode when things get ugly
228          XF86DGAForkApp(DefaultScreen(x_display));
229   #endif
230  
231 < #if ENABLE_GTK
231 > #ifdef ENABLE_GTK
232          // Init GTK
233          gtk_set_locale();
234          gtk_init(&argc, &argv);
# Line 178 | Line 245 | int main(int argc, char **argv)
245                  if (!PrefsEditor())
246                          QuitEmulator();
247  
248 <        // Create area for Mac RAM
248 >        // Open /dev/zero
249 >        zero_fd = open("/dev/zero", O_RDWR);
250 >        if (zero_fd < 0) {
251 >                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
252 >                ErrorAlert(str);
253 >                QuitEmulator();
254 >        }
255 >
256 >        // Read RAM size
257          RAMSize = PrefsFindInt32("ramsize") & 0xfff00000;       // Round down to 1MB boundary
258          if (RAMSize < 1024*1024) {
259                  WarningAlert(GetString(STR_SMALL_RAM_WARN));
260                  RAMSize = 1024*1024;
261          }
187        RAMBaseHost = new uint8[RAMSize];
262  
263 <        // Create area for Mac ROM
263 > #if REAL_ADDRESSING
264 >        // Create Low Memory area (0x0000..0x2000)
265 >        if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) {
266 >                sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
267 >                ErrorAlert(str);
268 >                QuitEmulator();
269 >        }
270 >        lm_area_mapped = true;
271 > #endif
272 >
273 > #if !EMULATED_68K
274 >        // Allocate scratch memory
275 >        ScratchMem = (uint32)malloc(SCRATCH_MEM_SIZE);
276 >        if (ScratchMem == NULL) {
277 >                ErrorAlert(GetString(STR_NO_MEM_ERR));
278 >                QuitEmulator();
279 >        }
280 >        ScratchMem += SCRATCH_MEM_SIZE/2;       // ScratchMem points to middle of block
281 > #endif
282 >
283 >        // Create areas for Mac RAM and ROM
284 > #if defined(USE_MAPPED_MEMORY)
285 >    good_address_map = (char *)mmap(NULL, 1<<24, PROT_READ, MAP_PRIVATE, zero_fd, 0);
286 >    address_space = (char *)mmap(NULL, 1<<24, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
287 >    if ((int)address_space < 0 || (int)good_address_map < 0) {
288 >                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
289 >                QuitEmulator();
290 >    }
291 >    RAMBaseHost = (uint8 *)mmap(address_space, RAMSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0);
292 >    ROMBaseHost = (uint8 *)mmap(address_space + 0x00400000, 0x80000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0);
293 >        char *nam = tmpnam(NULL);
294 >    int good_address_fd = open(nam, O_CREAT | O_RDWR, 0600);
295 >        char buffer[4096];
296 >    memset(buffer, 1, sizeof(buffer));
297 >    write(good_address_fd, buffer, sizeof(buffer));
298 >    unlink(nam);
299 >    for (int i=0; i<RAMSize; i+=4096)
300 >        mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
301 >    for (int i=0; i<0x80000; i+=4096)
302 >        mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
303 > #else
304 >        RAMBaseHost = new uint8[RAMSize];
305          ROMBaseHost = new uint8[0x100000];
306 + #endif
307 + #if REAL_ADDRESSING && !EMULATED_68K
308 +        RAMBaseMac = (uint32)RAMBaseHost;
309 +        ROMBaseMac = (uint32)ROMBaseHost;
310 + #endif
311 +        D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
312 +        D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
313  
314          // Get rom file path from preferences
315          const char *rom_path = PrefsFindString("rom");
# Line 212 | Line 334 | int main(int argc, char **argv)
334                  QuitEmulator();
335          }
336  
337 + #if !EMULATED_68K
338 +        // Get CPU model
339 +        int mib[2] = {CTL_HW, HW_MODEL};
340 +        char *model;
341 +        size_t model_len;
342 +        sysctl(mib, 2, NULL, &model_len, NULL, 0);
343 +        model = (char *)malloc(model_len);
344 +        sysctl(mib, 2, model, &model_len, NULL, 0);
345 +        D(bug("Model: %s\n", model));
346 +
347 +        // Set CPU and FPU type
348 +        CPUIs68060 = false;
349 +        if (strstr(model, "020"))
350 +                CPUType = 2;
351 +        else if (strstr(model, "030"))
352 +                CPUType = 3;
353 +        else if (strstr(model, "040"))
354 +                CPUType = 4;
355 +        else if (strstr(model, "060")) {
356 +                CPUType = 4;
357 +                CPUIs68060 = true;
358 +        } else {
359 +                printf("WARNING: Cannot detect CPU type, assuming 68020\n");
360 +                CPUType = 2;
361 +        }
362 +        FPUType = 0;    //!!
363 +        TwentyFourBitAddressing = false;
364 + #endif
365 +
366          // Initialize everything
367          if (!InitAll())
368                  QuitEmulator();
369 +        D(bug("Initialization complete\n"));
370  
371 <        // Start XPRAM watchdog thread
372 <        xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
371 > #ifdef HAVE_PTHREADS
372 >        // Get handle of main thread
373 >        emul_thread = pthread_self();
374 > #endif
375 >
376 > #if !EMULATED_68K
377 >        // (Virtual) supervisor mode, disable interrupts
378 >        EmulatedSR = 0x2700;
379 >
380 >        // Create and install stack for signal handlers
381 >        sig_stack = malloc(SIG_STACK_SIZE);
382 >        D(bug("Signal stack at %p\n", sig_stack));
383 >        if (sig_stack == NULL) {
384 >                ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
385 >                QuitEmulator();
386 >        }
387 >        stack_t new_stack;
388 >        new_stack.ss_sp = sig_stack;
389 >        new_stack.ss_flags = 0;
390 >        new_stack.ss_size = SIG_STACK_SIZE;
391 >        if (sigaltstack(&new_stack, NULL) < 0) {
392 >                sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
393 >                ErrorAlert(str);
394 >                QuitEmulator();
395 >        }
396 >
397 >        // Install SIGILL handler for emulating privileged instructions and
398 >        // executing A-Trap and EMUL_OP opcodes
399 >        sigemptyset(&sigill_sa.sa_mask);        // Block virtual 68k interrupts during SIGILL handling
400 >        sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
401 >        sigaddset(&sigill_sa.sa_mask, SIGALRM);
402 >        sigill_sa.sa_handler = (void (*)(int))sigill_handler;
403 >        sigill_sa.sa_flags = SA_ONSTACK;
404 >        if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
405 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
406 >                ErrorAlert(str);
407 >                QuitEmulator();
408 >        }
409 >
410 >        // Install virtual 68k interrupt signal handler
411 >        sigemptyset(&sigirq_sa.sa_mask);
412 >        sigaddset(&sigirq_sa.sa_mask, SIGALRM);
413 >        sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
414 >        sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
415 >        if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
416 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
417 >                ErrorAlert(str);
418 >                QuitEmulator();
419 >        }
420 > #endif
421 >
422 > #ifdef ENABLE_MON
423 >        // Setup SIGINT handler to enter mon
424 >        sigemptyset(&sigint_sa.sa_mask);
425 >        sigint_sa.sa_handler = sigint_handler;
426 >        sigint_sa.sa_flags = 0;
427 >        sigaction(SIGINT, &sigint_sa, NULL);
428 > #endif
429  
430   #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
431 <        // Start 60Hz timer
431 >
432 >        // POSIX.4 timers and real-time signals available, start 60Hz timer
433          sigemptyset(&timer_sa.sa_mask);
225        timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
434          timer_sa.sa_sigaction = one_tick;
435 +        timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
436          if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
437 <                printf("FATAL: cannot set up timer signal handler\n");
437 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
438 >                ErrorAlert(str);
439                  QuitEmulator();
440          }
441          struct sigevent timer_event;
442          timer_event.sigev_notify = SIGEV_SIGNAL;
443          timer_event.sigev_signo = SIG_TIMER;
444          if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
445 <                printf("FATAL: cannot create timer\n");
445 >                sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
446 >                ErrorAlert(str);
447                  QuitEmulator();
448          }
449          struct itimerspec req;
# Line 240 | Line 451 | int main(int argc, char **argv)
451          req.it_value.tv_nsec = 16625000;
452          req.it_interval.tv_sec = 0;
453          req.it_interval.tv_nsec = 16625000;
454 <        if (timer_settime(timer, TIMER_RELTIME, &req, NULL) < 0) {
455 <                printf("FATAL: cannot start timer\n");
454 >        if (timer_settime(timer, 0, &req, NULL) < 0) {
455 >                sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
456 >                ErrorAlert(str);
457                  QuitEmulator();
458          }
459 +        D(bug("60Hz timer started\n"));
460  
461 < #else
461 > #elif defined(HAVE_PTHREADS)
462  
463 <        // Start 60Hz thread
463 >        // POSIX threads available, start 60Hz thread
464          pthread_attr_init(&tick_thread_attr);
465   #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
466          if (geteuid() == 0) {
# Line 260 | Line 473 | int main(int argc, char **argv)
473   #endif
474          tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
475          if (!tick_thread_active) {
476 <                printf("FATAL: cannot create tick thread\n");
476 >                sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
477 >                ErrorAlert(str);
478 >                QuitEmulator();
479 >        }
480 >        D(bug("60Hz thread started\n"));
481 >
482 > #else
483 >
484 >        // Start 60Hz timer
485 >        sigemptyset(&timer_sa.sa_mask);         // Block virtual 68k interrupts during SIGARLM handling
486 >        sigaddset(&timer_sa.sa_mask, SIG_IRQ);
487 >        timer_sa.sa_handler = one_tick;
488 >        timer_sa.sa_flags = SA_ONSTACK | SA_RESTART;
489 >        if (sigaction(SIGALRM, &timer_sa, NULL) < 0) {
490 >                sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno));
491 >                ErrorAlert(str);
492                  QuitEmulator();
493          }
494 +        struct itimerval req;
495 +        req.it_interval.tv_sec = req.it_value.tv_sec = 0;
496 +        req.it_interval.tv_usec = req.it_value.tv_usec = 16625;
497 +        setitimer(ITIMER_REAL, &req, NULL);
498 +
499   #endif
500  
501 < #if ENABLE_MON
502 <        // Setup SIGINT handler to enter mon
503 <        sigemptyset(&sigint_sa.sa_mask);
504 <        sigint_sa.sa_flags = 0;
505 <        sigint_sa.sa_handler = sigint_handler;
273 <        sigaction(SIGINT, &sigint_sa, NULL);
501 > #ifdef HAVE_PTHREADS
502 >        // Start XPRAM watchdog thread
503 >        memcpy(last_xpram, XPRAM, 256);
504 >        xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
505 >        D(bug("XPRAM thread started\n"));
506   #endif
507  
508          // Start 68k and jump to ROM boot routine
509 +        D(bug("Starting emulation...\n"));
510          Start680x0();
511  
512          QuitEmulator();
# Line 287 | Line 520 | int main(int argc, char **argv)
520  
521   void QuitEmulator(void)
522   {
523 +        D(bug("QuitEmulator\n"));
524 +
525 + #if EMULATED_68K
526          // Exit 680x0 emulation
527          Exit680x0();
528 + #endif
529  
530   #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
531          // Stop 60Hz timer
532          timer_delete(timer);
533 < #else
533 > #elif defined(HAVE_PTHREADS)
534          // Stop 60Hz thread
535          if (tick_thread_active) {
536                  tick_thread_cancel = true;
# Line 302 | Line 539 | void QuitEmulator(void)
539   #endif
540                  pthread_join(tick_thread, NULL);
541          }
542 + #else
543 +        struct itimerval req;
544 +        req.it_interval.tv_sec = req.it_value.tv_sec = 0;
545 +        req.it_interval.tv_usec = req.it_value.tv_usec = 0;
546 +        setitimer(ITIMER_REAL, &req, NULL);
547   #endif
548  
549 + #ifdef HAVE_PTHREADS
550          // Stop XPRAM watchdog thread
551          if (xpram_thread_active) {
552                  xpram_thread_cancel = true;
# Line 312 | Line 555 | void QuitEmulator(void)
555   #endif
556                  pthread_join(xpram_thread, NULL);
557          }
558 + #endif
559  
560          // Deinitialize everything
561          ExitAll();
# Line 322 | Line 566 | void QuitEmulator(void)
566          // Delete RAM area
567          delete[] RAMBaseHost;
568  
569 + #if !EMULATED_68K
570 +        // Delete scratch memory area
571 +        if (ScratchMem)
572 +                free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2));
573 + #endif
574 +
575 + #if REAL_ADDRESSING
576 +        // Delete Low Memory area
577 +        if (lm_area_mapped)
578 +                munmap((char *)0x0000, 0x2000);
579 + #endif
580 +
581 +        // Close /dev/zero
582 +        if (zero_fd > 0)
583 +                close(zero_fd);
584 +
585          // Exit system routines
586          SysExit();
587  
# Line 341 | Line 601 | void QuitEmulator(void)
601   *  or a dynamically recompiling emulator)
602   */
603  
344 #if EMULATED_68K
604   void FlushCodeCache(void *start, uint32 size)
605   {
606 < }
606 > #if !EMULATED_68K && defined(__NetBSD__)
607 >        m68k_sync_icache(start, size);
608   #endif
609 + }
610  
611  
612   /*
613   *  SIGINT handler, enters mon
614   */
615  
616 < #if ENABLE_MON
616 > #ifdef ENABLE_MON
617   static void sigint_handler(...)
618   {
619 + #if EMULATED_68K
620 +        uaecptr nextpc;
621 +        extern void m68k_dumpstate(uaecptr *nextpc);
622 +        m68k_dumpstate(&nextpc);
623 + #else
624          char *arg[2] = {"rmon", NULL};
625          mon(1, arg);
626          QuitEmulator();
627 + #endif
628   }
629   #endif
630  
# Line 368 | Line 635 | static void sigint_handler(...)
635  
636   uint32 InterruptFlags = 0;
637  
638 + #if EMULATED_68K
639   void SetInterruptFlag(uint32 flag)
640   {
641 + #ifdef HAVE_PTHREADS
642          pthread_mutex_lock(&intflag_lock);
643          InterruptFlags |= flag;
644          pthread_mutex_unlock(&intflag_lock);
645 + #else
646 +        InterruptFlags |= flag;         // Pray that this is an atomic operation...
647 + #endif
648   }
649  
650   void ClearInterruptFlag(uint32 flag)
651   {
652 + #ifdef HAVE_PTHREADS
653          pthread_mutex_lock(&intflag_lock);
654          InterruptFlags &= ~flag;
655          pthread_mutex_unlock(&intflag_lock);
656 + #else
657 +        InterruptFlags &= ~flag;
658 + #endif
659 + }
660 + #endif
661 +
662 + #if !EMULATED_68K
663 + void TriggerInterrupt(void)
664 + {
665 + #if defined(HAVE_PTHREADS)
666 +        pthread_kill(emul_thread, SIG_IRQ);
667 + #else
668 +        raise(SIG_IRQ);
669 + #endif
670 + }
671 + #endif
672 +
673 +
674 + /*
675 + *  XPRAM watchdog thread (saves XPRAM every minute)
676 + */
677 +
678 + static void xpram_watchdog(void)
679 + {
680 +        if (memcmp(last_xpram, XPRAM, 256)) {
681 +                memcpy(last_xpram, XPRAM, 256);
682 +                SaveXPRAM();
683 +        }
684   }
685  
686 + #ifdef HAVE_PTHREADS
687 + static void *xpram_func(void *arg)
688 + {
689 +        while (!xpram_thread_cancel) {
690 +                for (int i=0; i<60 && !xpram_thread_cancel; i++)
691 +                        Delay_usec(1000000);
692 +                xpram_watchdog();
693 +        }
694 +        return NULL;
695 + }
696 + #endif
697 +
698  
699   /*
700   *  60Hz thread (really 60.15Hz)
701   */
702  
703 + static void one_second(void)
704 + {
705 +        // Pseudo Mac 1Hz interrupt, update local time
706 +        WriteMacInt32(0x20c, TimerDateTime());
707 +
708 +        SetInterruptFlag(INTFLAG_60HZ);
709 +        TriggerInterrupt();
710 +
711 + #ifndef HAVE_PTHREADS
712 +        static int second_counter = 0;
713 +        if (++second_counter > 60) {
714 +                second_counter = 0;
715 +                xpram_watchdog();
716 +        }
717 + #endif
718 + }
719 +
720   static void one_tick(...)
721   {
722          static int tick_counter = 0;
393
394        // Pseudo Mac 1Hz interrupt, update local time
723          if (++tick_counter > 60) {
724                  tick_counter = 0;
725 <                WriteMacInt32(0x20c, TimerDateTime());
725 >                one_second();
726          }
727  
728 + #ifndef HAVE_PTHREADS
729 +        // No threads available, perform video refresh from here
730 +        VideoRefresh();
731 + #endif
732 +
733          // Trigger 60Hz interrupt
734          if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
735                  SetInterruptFlag(INTFLAG_60HZ);
# Line 404 | Line 737 | static void one_tick(...)
737          }
738   }
739  
740 + #ifdef HAVE_PTHREADS
741   static void *tick_func(void *arg)
742   {
743 +        uint64 next = GetTicks_usec();
744          while (!tick_thread_cancel) {
410
411                // Wait
412 #ifdef HAVE_NANOSLEEP
413                struct timespec req = {0, 16625000};
414                nanosleep(&req, NULL);
415 #else
416                usleep(16625);
417 #endif
418
419                // Action
745                  one_tick();
746 +                next += 16625;
747 +                int64 delay = next - GetTicks_usec();
748 +                if (delay > 0)
749 +                        Delay_usec(delay);
750 +                else if (delay < -16625)
751 +                        next = GetTicks_usec();
752          }
753          return NULL;
754   }
755 + #endif
756 +
757 +
758 + /*
759 + *  Get current value of microsecond timer
760 + */
761 +
762 + uint64 GetTicks_usec(void)
763 + {
764 + #ifdef HAVE_CLOCK_GETTIME
765 +        struct timespec t;
766 +        clock_gettime(CLOCK_REALTIME, &t);
767 +        return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000;
768 + #else
769 +        struct timeval t;
770 +        gettimeofday(&t, NULL);
771 +        return (uint64)t.tv_sec * 1000000 + t.tv_usec;
772 + #endif
773 + }
774  
775  
776   /*
777 < *  XPRAM watchdog thread (saves XPRAM every minute)
777 > *  Delay by specified number of microseconds (<1 second)
778 > *  (adapted from SDL_Delay() source)
779   */
780  
781 < void *xpram_func(void *arg)
781 > void Delay_usec(uint32 usec)
782   {
783 <        uint8 last_xpram[256];
784 <        memcpy(last_xpram, XPRAM, 256);
783 >        int was_error;
784 > #ifndef __linux__       // Non-Linux implementations need to calculate time left
785 >        uint64 then, now, elapsed;
786 > #endif
787 >        struct timeval tv;
788  
789 <        while (!xpram_thread_cancel) {
790 <                for (int i=0; i<60 && !xpram_thread_cancel; i++) {
791 < #ifdef HAVE_NANOSLEEP
792 <                        struct timespec req = {1, 0};
439 <                        nanosleep(&req, NULL);
789 >        // Set the timeout interval - Linux only needs to do this once
790 > #ifdef __linux__
791 >        tv.tv_sec = 0;
792 >        tv.tv_usec = usec;
793   #else
794 <                        usleep(1000000);
794 >        then = GetTicks_usec();
795 > #endif
796 >        do {
797 >                errno = 0;
798 > #ifndef __linux__
799 >                /* Calculate the time interval left (in case of interrupt) */
800 >                now = GetTicks_usec();
801 >                elapsed = now - then;
802 >                then = now;
803 >                if (elapsed >= usec)
804 >                        break;
805 >                usec -= elapsed;
806 >                tv.tv_sec = 0;
807 >                tv.tv_usec = usec;
808   #endif
809 +                was_error = select(0, NULL, NULL, NULL, &tv);
810 +        } while (was_error && (errno == EINTR));
811 + }
812 +
813 +
814 + #if !EMULATED_68K
815 + /*
816 + *  Virtual 68k interrupt handler
817 + */
818 +
819 + static void sigirq_handler(int sig, int code, struct sigcontext *scp)
820 + {
821 +        // Interrupts disabled? Then do nothing
822 +        if (EmulatedSR & 0x0700)
823 +                return;
824 +
825 +        struct sigstate *state = (struct sigstate *)scp->sc_ap;
826 +        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
827 +
828 +        // Set up interrupt frame on stack
829 +        uint32 a7 = regs->a[7];
830 +        a7 -= 2;
831 +        WriteMacInt16(a7, 0x64);
832 +        a7 -= 4;
833 +        WriteMacInt32(a7, scp->sc_pc);
834 +        a7 -= 2;
835 +        WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
836 +        scp->sc_sp = regs->a[7] = a7;
837 +
838 +        // Set interrupt level
839 +        EmulatedSR |= 0x2100;
840 +
841 +        // Jump to MacOS interrupt handler on return
842 +        scp->sc_pc = ReadMacInt32(0x64);
843 + }
844 +
845 +
846 + /*
847 + *  SIGILL handler, for emulation of privileged instructions and executing
848 + *  A-Trap and EMUL_OP opcodes
849 + */
850 +
851 + static void sigill_handler(int sig, int code, struct sigcontext *scp)
852 + {
853 +        struct sigstate *state = (struct sigstate *)scp->sc_ap;
854 +        uint16 *pc = (uint16 *)scp->sc_pc;
855 +        uint16 opcode = *pc;
856 +        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
857 +
858 + #define INC_PC(n) scp->sc_pc += (n)
859 +
860 + #define GET_SR (scp->sc_ps | EmulatedSR)
861 +
862 + #define STORE_SR(v) \
863 +        scp->sc_ps = (v) & 0xff; \
864 +        EmulatedSR = (v) & 0x2700; \
865 +        if (((v) & 0x0700) == 0 && InterruptFlags) \
866 +                TriggerInterrupt();
867 +
868 + //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
869 +
870 +        if ((opcode & 0xf000) == 0xa000) {
871 +
872 +                // A-Line instruction, set up A-Line trap frame on stack
873 +                uint32 a7 = regs->a[7];
874 +                a7 -= 2;
875 +                WriteMacInt16(a7, 0x28);
876 +                a7 -= 4;
877 +                WriteMacInt32(a7, (uint32)pc);
878 +                a7 -= 2;
879 +                WriteMacInt16(a7, GET_SR);
880 +                scp->sc_sp = regs->a[7] = a7;
881 +
882 +                // Jump to MacOS A-Line handler on return
883 +                scp->sc_pc = ReadMacInt32(0x28);
884 +
885 +        } else if ((opcode & 0xff00) == 0x7100) {
886 +
887 +                // Extended opcode, push registers on user stack
888 +                uint32 a7 = regs->a[7];
889 +                a7 -= 4;
890 +                WriteMacInt32(a7, (uint32)pc);
891 +                a7 -= 2;
892 +                WriteMacInt16(a7, scp->sc_ps);
893 +                for (int i=7; i>=0; i--) {
894 +                        a7 -= 4;
895 +                        WriteMacInt32(a7, regs->a[i]);
896                  }
897 <                if (memcmp(last_xpram, XPRAM, 256)) {
898 <                        memcpy(last_xpram, XPRAM, 256);
899 <                        SaveXPRAM();
897 >                for (int i=7; i>=0; i--) {
898 >                        a7 -= 4;
899 >                        WriteMacInt32(a7, regs->d[i]);
900                  }
901 +                scp->sc_sp = regs->a[7] = a7;
902 +
903 +                // Jump to EmulOp trampoline code on return
904 +                scp->sc_pc = (uint32)EmulOpTrampoline;
905 +                
906 +        } else switch (opcode) {        // Emulate privileged instructions
907 +
908 +                case 0x40e7:    // move sr,-(sp)
909 +                        regs->a[7] -= 2;
910 +                        WriteMacInt16(regs->a[7], GET_SR);
911 +                        scp->sc_sp = regs->a[7];
912 +                        INC_PC(2);
913 +                        break;
914 +
915 +                case 0x46df: {  // move (sp)+,sr
916 +                        uint16 sr = ReadMacInt16(regs->a[7]);
917 +                        STORE_SR(sr);
918 +                        regs->a[7] += 2;
919 +                        scp->sc_sp = regs->a[7];
920 +                        INC_PC(2);
921 +                        break;
922 +                }
923 +
924 +                case 0x007c: {  // ori #xxxx,sr
925 +                        uint16 sr = GET_SR | pc[1];
926 +                        scp->sc_ps = sr & 0xff;         // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
927 +                        EmulatedSR = sr & 0x2700;
928 +                        INC_PC(4);
929 +                        break;
930 +                }
931 +
932 +                case 0x027c: {  // andi #xxxx,sr
933 +                        uint16 sr = GET_SR & pc[1];
934 +                        STORE_SR(sr);
935 +                        INC_PC(4);
936 +                        break;
937 +                }
938 +
939 +                case 0x46fc:    // move #xxxx,sr
940 +                        STORE_SR(pc[1]);
941 +                        INC_PC(4);
942 +                        break;
943 +
944 +                case 0x46ef: {  // move (xxxx,sp),sr
945 +                        uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
946 +                        STORE_SR(sr);
947 +                        INC_PC(4);
948 +                        break;
949 +                }
950 +
951 +                case 0x46d8:    // move (a0)+,sr
952 +                case 0x46d9: {  // move (a1)+,sr
953 +                        uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
954 +                        STORE_SR(sr);
955 +                        regs->a[opcode & 7] += 2;
956 +                        INC_PC(2);
957 +                        break;
958 +                }
959 +
960 +                case 0x40f8:    // move sr,xxxx.w
961 +                        WriteMacInt16(pc[1], GET_SR);
962 +                        INC_PC(4);
963 +                        break;
964 +
965 +                case 0x40d0:    // move sr,(a0)
966 +                case 0x40d1:    // move sr,(a1)
967 +                case 0x40d2:    // move sr,(a2)
968 +                case 0x40d3:    // move sr,(a3)
969 +                case 0x40d4:    // move sr,(a4)
970 +                case 0x40d5:    // move sr,(a5)
971 +                case 0x40d6:    // move sr,(a6)
972 +                case 0x40d7:    // move sr,(sp)
973 +                        WriteMacInt16(regs->a[opcode & 7], GET_SR);
974 +                        INC_PC(2);
975 +                        break;
976 +
977 +                case 0x40c0:    // move sr,d0
978 +                case 0x40c1:    // move sr,d1
979 +                case 0x40c2:    // move sr,d2
980 +                case 0x40c3:    // move sr,d3
981 +                case 0x40c4:    // move sr,d4
982 +                case 0x40c5:    // move sr,d5
983 +                case 0x40c6:    // move sr,d6
984 +                case 0x40c7:    // move sr,d7
985 +                        regs->d[opcode & 7] = GET_SR;
986 +                        INC_PC(2);
987 +                        break;
988 +
989 +                case 0x46c0:    // move d0,sr
990 +                case 0x46c1:    // move d1,sr
991 +                case 0x46c2:    // move d2,sr
992 +                case 0x46c3:    // move d3,sr
993 +                case 0x46c4:    // move d4,sr
994 +                case 0x46c5:    // move d5,sr
995 +                case 0x46c6:    // move d6,sr
996 +                case 0x46c7: {  // move d7,sr
997 +                        uint16 sr = regs->d[opcode & 7];
998 +                        STORE_SR(sr);
999 +                        INC_PC(2);
1000 +                        break;
1001 +                }
1002 +
1003 +                case 0xf327:    // fsave -(sp)
1004 +                        goto ill;       //!!
1005 +
1006 +                case 0xf35f:    // frestore (sp)+
1007 +                        goto ill;       //!!
1008 +
1009 +                case 0x4e73: {  // rte (only handles format 0)
1010 +                        uint32 a7 = regs->a[7];
1011 +                        uint16 sr = ReadMacInt16(a7);
1012 +                        a7 += 2;
1013 +                        scp->sc_ps = sr & 0xff;
1014 +                        EmulatedSR = sr & 0x2700;
1015 +                        scp->sc_pc = ReadMacInt32(a7);
1016 +                        a7 += 6;
1017 +                        scp->sc_sp = regs->a[7] = a7;
1018 +                        break;
1019 +                }
1020 +
1021 +                case 0x4e7a:    // movec cr,x
1022 +                        switch (pc[1]) {
1023 +                                case 0x8801:    // movec vbr,a0
1024 +                                        regs->a[0] = 0;
1025 +                                        break;
1026 +                                case 0x9801:    // movec vbr,a1
1027 +                                        regs->a[1] = 0;
1028 +                                        break;
1029 +                                case 0x0002:    // movec cacr,d0
1030 +                                        regs->d[0] = 0x3111;
1031 +                                        break;
1032 +                                case 0x1002:    // movec cacr,d1
1033 +                                        regs->d[1] = 0x3111;
1034 +                                        break;
1035 +                                case 0x0003:    // movec tc,d0
1036 +                                        regs->d[0] = 0;
1037 +                                        break;
1038 +                                case 0x1003:    // movec tc,d1
1039 +                                        regs->d[1] = 0;
1040 +                                        break;
1041 +                                default:
1042 +                                        goto ill;
1043 +                        }
1044 +                        INC_PC(4);
1045 +                        break;
1046 +
1047 +                case 0x4e7b:    // movec x,cr
1048 +                        switch (pc[1]) {
1049 +                                case 0x0801:    // movec d0,vbr
1050 +                                        break;
1051 +                                case 0x0002:    // movec d0,cacr
1052 +                                case 0x1002:    // movec d1,cacr
1053 +                                        FlushCodeCache(NULL, 0);
1054 +                                        break;
1055 +                                default:
1056 +                                        goto ill;
1057 +                        }
1058 +                        INC_PC(4);
1059 +                        break;
1060 +
1061 +                case 0xf478:    // cpusha dc
1062 +                case 0xf4f8:    // cpusha dc/ic
1063 +                        FlushCodeCache(NULL, 0);
1064 +                        INC_PC(2);
1065 +                        break;
1066 +
1067 +                default:
1068 + ill:            printf("SIGILL num %d, code %d\n", sig, code);
1069 +                        printf(" context %p:\n", scp);
1070 +                        printf("  onstack %08x\n", scp->sc_onstack);
1071 +                        printf("  sp %08x\n", scp->sc_sp);
1072 +                        printf("  fp %08x\n", scp->sc_fp);
1073 +                        printf("  pc %08x\n", scp->sc_pc);
1074 +                        printf("   opcode %04x\n", opcode);
1075 +                        printf("  sr %08x\n", scp->sc_ps);
1076 +                        printf(" state %p:\n", state);
1077 +                        printf("  flags %d\n", state->ss_flags);
1078 +                        for (int i=0; i<8; i++)
1079 +                                printf("  d%d %08x\n", i, state->ss_frame.f_regs[i]);
1080 +                        for (int i=0; i<8; i++)
1081 +                                printf("  a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1082 +
1083 + #ifdef ENABLE_MON
1084 +                        char *arg[2] = {"rmon", NULL};
1085 +                        mon(1, arg);
1086 + #endif
1087 +                        QuitEmulator();
1088 +                        break;
1089          }
449        return NULL;
1090   }
1091 + #endif
1092  
1093  
1094   /*
1095   *  Display alert
1096   */
1097  
1098 < #if ENABLE_GTK
1098 > #ifdef ENABLE_GTK
1099   static void dl_destroyed(void)
1100   {
1101          gtk_main_quit();
# Line 499 | Line 1140 | void display_alert(int title_id, int pre
1140  
1141   void ErrorAlert(const char *text)
1142   {
1143 < #if ENABLE_GTK
1143 > #ifdef ENABLE_GTK
1144          if (PrefsFindBool("nogui") || x_display == NULL) {
1145                  printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1146                  return;
# Line 518 | Line 1159 | void ErrorAlert(const char *text)
1159  
1160   void WarningAlert(const char *text)
1161   {
1162 < #if ENABLE_GTK
1162 > #ifdef ENABLE_GTK
1163          if (PrefsFindBool("nogui") || x_display == NULL) {
1164                  printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1165                  return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines