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.15 by cebix, 2000-07-22T16:07:20Z

# 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
50 <
51 < #if ENABLE_XF86_DGA
52 < #include <X11/Xlib.h>
53 < #include <X11/Xutil.h>
54 < #include <X11/extensions/xf86dga.h>
55 < #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  
57 #if ENABLE_MON
58 #include "mon.h"
59 #endif
87  
88 < #ifdef USE_MAPPED_MEMORY
89 < #include <sys/mman.h>
90 < extern char *address_space, *good_address_map;
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  
67 // Constants
68 const char ROM_FILE_NAME[] = "ROM";
69
70
99   // CPU and FPU type, addressing mode
100   int CPUType;
101   bool CPUIs68060;
# Line 76 | 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 88 | 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
96 < 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 132 | 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 161 | Line 223 | int main(int argc, char **argv)
223                  QuitEmulator();
224          }
225  
226 < #if ENABLE_XF86_DGA && !ENABLE_MON
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 183 | Line 245 | int main(int argc, char **argv)
245                  if (!PrefsEditor())
246                          QuitEmulator();
247  
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) {
# Line 190 | Line 260 | int main(int argc, char **argv)
260                  RAMSize = 1024*1024;
261          }
262  
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 < #ifdef USE_MAPPED_MEMORY
285 <    int fd = open("/dev/zero", O_RDWR);
286 <    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);
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, fd, 0);
292 <    ROMBaseHost = (uint8 *)mmap(address_space + 0x00400000, 0x80000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd, 0);
204 <    close(fd);
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];
# Line 216 | Line 304 | int main(int argc, char **argv)
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 240 | 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);
253        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 269 | Line 452 | int main(int argc, char **argv)
452          req.it_interval.tv_sec = 0;
453          req.it_interval.tv_nsec = 16625000;
454          if (timer_settime(timer, 0, &req, NULL) < 0) {
455 <                printf("FATAL: cannot start timer\n");
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 288 | 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;
301 <        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 315 | 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 330 | 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 340 | Line 555 | void QuitEmulator(void)
555   #endif
556                  pthread_join(xpram_thread, NULL);
557          }
558 + #endif
559  
560          // Deinitialize everything
561          ExitAll();
# Line 350 | 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 369 | Line 601 | void QuitEmulator(void)
601   *  or a dynamically recompiling emulator)
602   */
603  
372 #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
384 < extern void m68k_dumpstate(uaecptr *nextpc);
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 399 | 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 + #ifdef HAVE_NANOSLEEP
692 +                        struct timespec req = {1, 0};
693 +                        nanosleep(&req, NULL);
694 + #else
695 +                        usleep(1000000);
696 + #endif
697 +                }
698 +                xpram_watchdog();
699 +        }
700 +        return NULL;
701 + }
702 + #endif
703  
704  
705   /*
706   *  60Hz thread (really 60.15Hz)
707   */
708  
709 + static void one_second(void)
710 + {
711 +        // Pseudo Mac 1Hz interrupt, update local time
712 +        WriteMacInt32(0x20c, TimerDateTime());
713 +
714 +        SetInterruptFlag(INTFLAG_60HZ);
715 +        TriggerInterrupt();
716 +
717 + #ifndef HAVE_PTHREADS
718 +        static int second_counter = 0;
719 +        if (++second_counter > 60) {
720 +                second_counter = 0;
721 +                xpram_watchdog();
722 +        }
723 + #endif
724 + }
725 +
726   static void one_tick(...)
727   {
728          static int tick_counter = 0;
424
425        // Pseudo Mac 1Hz interrupt, update local time
729          if (++tick_counter > 60) {
730                  tick_counter = 0;
731 <                WriteMacInt32(0x20c, TimerDateTime());
731 >                one_second();
732          }
733  
734 + #ifndef HAVE_PTHREADS
735 +        // No threads available, perform video refresh from here
736 +        VideoRefresh();
737 + #endif
738 +
739          // Trigger 60Hz interrupt
740          if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
741                  SetInterruptFlag(INTFLAG_60HZ);
# Line 435 | Line 743 | static void one_tick(...)
743          }
744   }
745  
746 + #ifdef HAVE_PTHREADS
747   static void *tick_func(void *arg)
748   {
749          while (!tick_thread_cancel) {
# Line 452 | Line 761 | static void *tick_func(void *arg)
761          }
762          return NULL;
763   }
764 + #endif
765  
766  
767 + #if !EMULATED_68K
768   /*
769 < *  XPRAM watchdog thread (saves XPRAM every minute)
769 > *  Virtual 68k interrupt handler
770   */
771  
772 < void *xpram_func(void *arg)
772 > static void sigirq_handler(int sig, int code, struct sigcontext *scp)
773   {
774 <        uint8 last_xpram[256];
775 <        memcpy(last_xpram, XPRAM, 256);
774 >        // Interrupts disabled? Then do nothing
775 >        if (EmulatedSR & 0x0700)
776 >                return;
777  
778 <        while (!xpram_thread_cancel) {
779 <                for (int i=0; i<60 && !xpram_thread_cancel; i++) {
780 < #ifdef HAVE_NANOSLEEP
781 <                        struct timespec req = {1, 0};
782 <                        nanosleep(&req, NULL);
783 < #else
784 <                        usleep(1000000);
785 < #endif
778 >        struct sigstate *state = (struct sigstate *)scp->sc_ap;
779 >        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
780 >
781 >        // Set up interrupt frame on stack
782 >        uint32 a7 = regs->a[7];
783 >        a7 -= 2;
784 >        WriteMacInt16(a7, 0x64);
785 >        a7 -= 4;
786 >        WriteMacInt32(a7, scp->sc_pc);
787 >        a7 -= 2;
788 >        WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
789 >        scp->sc_sp = regs->a[7] = a7;
790 >
791 >        // Set interrupt level
792 >        EmulatedSR |= 0x2100;
793 >
794 >        // Jump to MacOS interrupt handler on return
795 >        scp->sc_pc = ReadMacInt32(0x64);
796 > }
797 >
798 >
799 > /*
800 > *  SIGILL handler, for emulation of privileged instructions and executing
801 > *  A-Trap and EMUL_OP opcodes
802 > */
803 >
804 > static void sigill_handler(int sig, int code, struct sigcontext *scp)
805 > {
806 >        struct sigstate *state = (struct sigstate *)scp->sc_ap;
807 >        uint16 *pc = (uint16 *)scp->sc_pc;
808 >        uint16 opcode = *pc;
809 >        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
810 >
811 > #define INC_PC(n) scp->sc_pc += (n)
812 >
813 > #define GET_SR (scp->sc_ps | EmulatedSR)
814 >
815 > #define STORE_SR(v) \
816 >        scp->sc_ps = (v) & 0xff; \
817 >        EmulatedSR = (v) & 0x2700; \
818 >        if (((v) & 0x0700) == 0 && InterruptFlags) \
819 >                TriggerInterrupt();
820 >
821 > //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
822 >
823 >        if ((opcode & 0xf000) == 0xa000) {
824 >
825 >                // A-Line instruction, set up A-Line trap frame on stack
826 >                uint32 a7 = regs->a[7];
827 >                a7 -= 2;
828 >                WriteMacInt16(a7, 0x28);
829 >                a7 -= 4;
830 >                WriteMacInt32(a7, (uint32)pc);
831 >                a7 -= 2;
832 >                WriteMacInt16(a7, GET_SR);
833 >                scp->sc_sp = regs->a[7] = a7;
834 >
835 >                // Jump to MacOS A-Line handler on return
836 >                scp->sc_pc = ReadMacInt32(0x28);
837 >
838 >        } else if ((opcode & 0xff00) == 0x7100) {
839 >
840 >                // Extended opcode, push registers on user stack
841 >                uint32 a7 = regs->a[7];
842 >                a7 -= 4;
843 >                WriteMacInt32(a7, (uint32)pc);
844 >                a7 -= 2;
845 >                WriteMacInt16(a7, scp->sc_ps);
846 >                for (int i=7; i>=0; i--) {
847 >                        a7 -= 4;
848 >                        WriteMacInt32(a7, regs->a[i]);
849 >                }
850 >                for (int i=7; i>=0; i--) {
851 >                        a7 -= 4;
852 >                        WriteMacInt32(a7, regs->d[i]);
853 >                }
854 >                scp->sc_sp = regs->a[7] = a7;
855 >
856 >                // Jump to EmulOp trampoline code on return
857 >                scp->sc_pc = (uint32)EmulOpTrampoline;
858 >                
859 >        } else switch (opcode) {        // Emulate privileged instructions
860 >
861 >                case 0x40e7:    // move sr,-(sp)
862 >                        regs->a[7] -= 2;
863 >                        WriteMacInt16(regs->a[7], GET_SR);
864 >                        scp->sc_sp = regs->a[7];
865 >                        INC_PC(2);
866 >                        break;
867 >
868 >                case 0x46df: {  // move (sp)+,sr
869 >                        uint16 sr = ReadMacInt16(regs->a[7]);
870 >                        STORE_SR(sr);
871 >                        regs->a[7] += 2;
872 >                        scp->sc_sp = regs->a[7];
873 >                        INC_PC(2);
874 >                        break;
875 >                }
876 >
877 >                case 0x007c: {  // ori #xxxx,sr
878 >                        uint16 sr = GET_SR | pc[1];
879 >                        scp->sc_ps = sr & 0xff;         // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
880 >                        EmulatedSR = sr & 0x2700;
881 >                        INC_PC(4);
882 >                        break;
883 >                }
884 >
885 >                case 0x027c: {  // andi #xxxx,sr
886 >                        uint16 sr = GET_SR & pc[1];
887 >                        STORE_SR(sr);
888 >                        INC_PC(4);
889 >                        break;
890                  }
891 <                if (memcmp(last_xpram, XPRAM, 256)) {
892 <                        memcpy(last_xpram, XPRAM, 256);
893 <                        SaveXPRAM();
891 >
892 >                case 0x46fc:    // move #xxxx,sr
893 >                        STORE_SR(pc[1]);
894 >                        INC_PC(4);
895 >                        break;
896 >
897 >                case 0x46ef: {  // move (xxxx,sp),sr
898 >                        uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
899 >                        STORE_SR(sr);
900 >                        INC_PC(4);
901 >                        break;
902 >                }
903 >
904 >                case 0x46d8:    // move (a0)+,sr
905 >                case 0x46d9: {  // move (a1)+,sr
906 >                        uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
907 >                        STORE_SR(sr);
908 >                        regs->a[opcode & 7] += 2;
909 >                        INC_PC(2);
910 >                        break;
911 >                }
912 >
913 >                case 0x40f8:    // move sr,xxxx.w
914 >                        WriteMacInt16(pc[1], GET_SR);
915 >                        INC_PC(4);
916 >                        break;
917 >
918 >                case 0x40d0:    // move sr,(a0)
919 >                case 0x40d1:    // move sr,(a1)
920 >                case 0x40d2:    // move sr,(a2)
921 >                case 0x40d3:    // move sr,(a3)
922 >                case 0x40d4:    // move sr,(a4)
923 >                case 0x40d5:    // move sr,(a5)
924 >                case 0x40d6:    // move sr,(a6)
925 >                case 0x40d7:    // move sr,(sp)
926 >                        WriteMacInt16(regs->a[opcode & 7], GET_SR);
927 >                        INC_PC(2);
928 >                        break;
929 >
930 >                case 0x40c0:    // move sr,d0
931 >                case 0x40c1:    // move sr,d1
932 >                case 0x40c2:    // move sr,d2
933 >                case 0x40c3:    // move sr,d3
934 >                case 0x40c4:    // move sr,d4
935 >                case 0x40c5:    // move sr,d5
936 >                case 0x40c6:    // move sr,d6
937 >                case 0x40c7:    // move sr,d7
938 >                        regs->d[opcode & 7] = GET_SR;
939 >                        INC_PC(2);
940 >                        break;
941 >
942 >                case 0x46c0:    // move d0,sr
943 >                case 0x46c1:    // move d1,sr
944 >                case 0x46c2:    // move d2,sr
945 >                case 0x46c3:    // move d3,sr
946 >                case 0x46c4:    // move d4,sr
947 >                case 0x46c5:    // move d5,sr
948 >                case 0x46c6:    // move d6,sr
949 >                case 0x46c7: {  // move d7,sr
950 >                        uint16 sr = regs->d[opcode & 7];
951 >                        STORE_SR(sr);
952 >                        INC_PC(2);
953 >                        break;
954 >                }
955 >
956 >                case 0xf327:    // fsave -(sp)
957 >                        goto ill;       //!!
958 >
959 >                case 0xf35f:    // frestore (sp)+
960 >                        goto ill;       //!!
961 >
962 >                case 0x4e73: {  // rte (only handles format 0)
963 >                        uint32 a7 = regs->a[7];
964 >                        uint16 sr = ReadMacInt16(a7);
965 >                        a7 += 2;
966 >                        scp->sc_ps = sr & 0xff;
967 >                        EmulatedSR = sr & 0x2700;
968 >                        scp->sc_pc = ReadMacInt32(a7);
969 >                        a7 += 6;
970 >                        scp->sc_sp = regs->a[7] = a7;
971 >                        break;
972                  }
973 +
974 +                case 0x4e7a:    // movec cr,x
975 +                        switch (pc[1]) {
976 +                                case 0x8801:    // movec vbr,a0
977 +                                        regs->a[0] = 0;
978 +                                        break;
979 +                                case 0x9801:    // movec vbr,a1
980 +                                        regs->a[1] = 0;
981 +                                        break;
982 +                                case 0x0002:    // movec cacr,d0
983 +                                        regs->d[0] = 0x3111;
984 +                                        break;
985 +                                case 0x1002:    // movec cacr,d1
986 +                                        regs->d[1] = 0x3111;
987 +                                        break;
988 +                                case 0x0003:    // movec tc,d0
989 +                                        regs->d[0] = 0;
990 +                                        break;
991 +                                case 0x1003:    // movec tc,d1
992 +                                        regs->d[1] = 0;
993 +                                        break;
994 +                                default:
995 +                                        goto ill;
996 +                        }
997 +                        INC_PC(4);
998 +                        break;
999 +
1000 +                case 0x4e7b:    // movec x,cr
1001 +                        switch (pc[1]) {
1002 +                                case 0x0801:    // movec d0,vbr
1003 +                                        break;
1004 +                                case 0x0002:    // movec d0,cacr
1005 +                                case 0x1002:    // movec d1,cacr
1006 +                                        FlushCodeCache(NULL, 0);
1007 +                                        break;
1008 +                                default:
1009 +                                        goto ill;
1010 +                        }
1011 +                        INC_PC(4);
1012 +                        break;
1013 +
1014 +                case 0xf478:    // cpusha dc
1015 +                case 0xf4f8:    // cpusha dc/ic
1016 +                        FlushCodeCache(NULL, 0);
1017 +                        INC_PC(2);
1018 +                        break;
1019 +
1020 +                default:
1021 + ill:            printf("SIGILL num %d, code %d\n", sig, code);
1022 +                        printf(" context %p:\n", scp);
1023 +                        printf("  onstack %08x\n", scp->sc_onstack);
1024 +                        printf("  sp %08x\n", scp->sc_sp);
1025 +                        printf("  fp %08x\n", scp->sc_fp);
1026 +                        printf("  pc %08x\n", scp->sc_pc);
1027 +                        printf("   opcode %04x\n", opcode);
1028 +                        printf("  sr %08x\n", scp->sc_ps);
1029 +                        printf(" state %p:\n", state);
1030 +                        printf("  flags %d\n", state->ss_flags);
1031 +                        for (int i=0; i<8; i++)
1032 +                                printf("  d%d %08x\n", i, state->ss_frame.f_regs[i]);
1033 +                        for (int i=0; i<8; i++)
1034 +                                printf("  a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1035 +
1036 + #ifdef ENABLE_MON
1037 +                        char *arg[2] = {"rmon", NULL};
1038 +                        mon(1, arg);
1039 + #endif
1040 +                        QuitEmulator();
1041 +                        break;
1042          }
480        return NULL;
1043   }
1044 + #endif
1045  
1046  
1047   /*
1048   *  Display alert
1049   */
1050  
1051 < #if ENABLE_GTK
1051 > #ifdef ENABLE_GTK
1052   static void dl_destroyed(void)
1053   {
1054          gtk_main_quit();
# Line 530 | Line 1093 | void display_alert(int title_id, int pre
1093  
1094   void ErrorAlert(const char *text)
1095   {
1096 < #if ENABLE_GTK
1096 > #ifdef ENABLE_GTK
1097          if (PrefsFindBool("nogui") || x_display == NULL) {
1098                  printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1099                  return;
# Line 549 | Line 1112 | void ErrorAlert(const char *text)
1112  
1113   void WarningAlert(const char *text)
1114   {
1115 < #if ENABLE_GTK
1115 > #ifdef ENABLE_GTK
1116          if (PrefsFindBool("nogui") || x_display == NULL) {
1117                  printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1118                  return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines