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.11 by cebix, 2000-04-10T18:53:02Z vs.
Revision 1.12 by cebix, 2000-07-13T13:47:10Z

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines