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.2 by cebix, 1999-10-07T13:15:14Z vs.
Revision 1.14 by cebix, 2000-07-14T21:29:13Z

# 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"
62 + #include "rom_patches.h"
63   #include "xpram.h"
64   #include "timer.h"
32 #include "sony.h"
33 #include "disk.h"
34 #include "cdrom.h"
35 #include "scsi.h"
36 #include "audio.h"
65   #include "video.h"
66 < #include "serial.h"
39 < #include "ether.h"
40 < #include "clip.h"
41 < #include "rom_patches.h"
66 > #include "emul_op.h"
67   #include "prefs.h"
68   #include "prefs_editor.h"
69   #include "macos_util.h"
# Line 46 | Line 71
71   #include "version.h"
72   #include "main.h"
73  
74 < #define DEBUG 1
75 < #include "debug.h"
51 <
52 <
53 < #include <X11/Xlib.h>
54 <
55 < #if ENABLE_GTK
56 < #include <gtk/gtk.h>
74 > #ifdef ENABLE_MON
75 > # include "mon.h"
76   #endif
77  
78 < #if ENABLE_DGA
79 < #include <X11/Xlib.h>
61 < #include <X11/Xutil.h>
62 < #include <X11/extensions/xf86dga.h>
63 < #endif
78 > #define DEBUG 0
79 > #include "debug.h"
80  
81  
82   // Constants
83   const char ROM_FILE_NAME[] = "ROM";
84 + const int SIG_STACK_SIZE = SIGSTKSZ;    // Size of signal stack
85 + const int SCRATCH_MEM_SIZE = 0x10000;   // Size of scratch memory area
86 +
87 +
88 + #if !EMULATED_68K
89 + // RAM and ROM pointers
90 + uint32 RAMBaseMac;              // RAM base (Mac address space)
91 + uint8 *RAMBaseHost;             // RAM base (host address space)
92 + uint32 RAMSize;                 // Size of RAM
93 + uint32 ROMBaseMac;              // ROM base (Mac address space)
94 + uint8 *ROMBaseHost;             // ROM base (host address space)
95 + uint32 ROMSize;                 // Size of ROM
96 + #endif
97  
98  
99   // CPU and FPU type, addressing mode
# Line 75 | Line 104 | bool TwentyFourBitAddressing;
104  
105  
106   // Global variables
107 < static char *x_display_name = NULL;                                     // X11 display name
107 > char *x_display_name = NULL;                                            // X11 display name
108   Display *x_display = NULL;                                                      // X11 display handle
109  
110 + static int zero_fd = -1;                                                        // FD of /dev/zero
111 + static 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 87 | 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
145 < static timer_t timer;                                                           // 60Hz timer
144 > static timer_t timer;                           // 60Hz timer
145 > #endif
146 >
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  
# Line 100 | Line 158 | static timer_t timer;                                                          // 60Hz tim
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 126 | 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 140 | Line 205 | int main(int argc, char **argv)
205          for (int i=1; i<argc; i++) {
206                  if (strcmp(argv[i], "-display") == 0 && ++i < argc)
207                          x_display_name = argv[i];
208 +                else if (strcmp(argv[i], "-break") == 0 && ++i < argc)
209 +                        ROMBreakpoint = strtol(argv[i], NULL, 0);
210 +                else if (strcmp(argv[i], "-rominfo") == 0)
211 +                        PrintROMInfo = true;
212          }
213  
214          // Open display
# Line 151 | Line 220 | int main(int argc, char **argv)
220                  QuitEmulator();
221          }
222  
223 < #if ENABLE_DGA
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 173 | Line 242 | int main(int argc, char **argv)
242                  if (!PrefsEditor())
243                          QuitEmulator();
244  
245 <        // Create area for Mac RAM
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) {
256                  WarningAlert(GetString(STR_SMALL_RAM_WARN));
257                  RAMSize = 1024*1024;
258          }
182        RAMBaseHost = new uint8[RAMSize];
259  
260 <        // Create area for Mac ROM
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 > #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, 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];
293 >    memset(buffer, 1, sizeof(buffer));
294 >    write(good_address_fd, buffer, sizeof(buffer));
295 >    unlink(nam);
296 >    for (int i=0; i<RAMSize; i+=4096)
297 >        mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
298 >    for (int i=0; i<0x80000; i+=4096)
299 >        mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
300 > #else
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 207 | Line 331 | int main(int argc, char **argv)
331                  QuitEmulator();
332          }
333  
334 <        // Check ROM version
335 <        if (!CheckROM()) {
336 <                ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
337 <                QuitEmulator();
338 <        }
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 (UAE emulation)
217 <        switch (ROMVersion) {
218 <                case ROM_VERSION_64K:
219 <                case ROM_VERSION_PLUS:
220 <                case ROM_VERSION_CLASSIC:
221 <                        CPUType = 0;
222 <                        FPUType = 0;
223 <                        TwentyFourBitAddressing = true;
224 <                        break;
225 <                case ROM_VERSION_II:
226 <                        CPUType = 2;
227 <                        FPUType = PrefsFindBool("fpu") ? 1 : 0;
228 <                        TwentyFourBitAddressing = true;
229 <                        break;
230 <                case ROM_VERSION_32:
231 <                        CPUType = 3;
232 <                        FPUType = PrefsFindBool("fpu") ? 1 : 0;
233 <                        TwentyFourBitAddressing = false;
234 <                        break;
235 <        }
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 <        // Load XPRAM
364 <        XPRAMInit();
365 <
366 <        // Set boot volume
242 <        int16 i16 = PrefsFindInt16("bootdrive");
243 <        XPRAM[0x78] = i16 >> 8;
244 <        XPRAM[0x79] = i16 & 0xff;
245 <        i16 = PrefsFindInt16("bootdriver");
246 <        XPRAM[0x7a] = i16 >> 8;
247 <        XPRAM[0x7b] = i16 & 0xff;
248 <
249 <        // Start XPRAM watchdog thread
250 <        xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
251 <
252 <        // Init drivers
253 <        SonyInit();
254 <        DiskInit();
255 <        CDROMInit();
256 <        SCSIInit();
257 <
258 <        // Init serial ports
259 <        SerialInit();
260 <
261 <        // Init network
262 <        EtherInit();
263 <
264 <        // Init Time Manager
265 <        TimerInit();
266 <
267 <        // Init clipboard
268 <        ClipInit();
363 >        // Initialize everything
364 >        if (!InitAll())
365 >                QuitEmulator();
366 >        D(bug("Initialization complete\n"));
367  
368 <        // Init audio
369 <        AudioInit();
368 > #ifdef HAVE_PTHREADS
369 >        // Get handle of main thread
370 >        emul_thread = pthread_self();
371 > #endif
372  
373 <        // Init video
374 <        if (!VideoInit(ROMVersion == ROM_VERSION_64K || ROMVersion == ROM_VERSION_PLUS || ROMVersion == ROM_VERSION_CLASSIC))
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 <        // Init 680x0 emulation (this also activates the memory system which is needed for PatchROM())
395 <        if (!Init680x0())
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 ROM patches
408 <        if (!PatchROM()) {
409 <                ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
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);
290        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 305 | Line 448 | int main(int argc, char **argv)
448          req.it_value.tv_nsec = 16625000;
449          req.it_interval.tv_sec = 0;
450          req.it_interval.tv_nsec = 16625000;
451 <        if (timer_settime(timer, TIMER_RELTIME, &req, NULL) < 0) {
452 <                printf("FATAL: cannot start timer\n");
451 >        if (timer_settime(timer, 0, &req, NULL) < 0) {
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 325 | 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 + #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 344 | 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 359 | 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 369 | Line 552 | void QuitEmulator(void)
552   #endif
553                  pthread_join(xpram_thread, NULL);
554          }
555 + #endif
556  
557 <        // Save XPRAM
558 <        XPRAMExit();
375 <
376 <        // Exit video
377 <        VideoExit();
378 <
379 <        // Exit audio
380 <        AudioExit();
381 <
382 <        // Exit clipboard
383 <        ClipExit();
384 <
385 <        // Exit Time Manager
386 <        TimerExit();
387 <
388 <        // Exit serial ports
389 <        SerialExit();
390 <
391 <        // Exit network
392 <        EtherExit();
393 <
394 <        // Exit drivers
395 <        SCSIExit();
396 <        CDROMExit();
397 <        DiskExit();
398 <        SonyExit();
557 >        // Deinitialize everything
558 >        ExitAll();
559  
560          // Delete ROM area
561          delete[] ROMBaseHost;
# Line 403 | 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 422 | Line 598 | void QuitEmulator(void)
598   *  or a dynamically recompiling emulator)
599   */
600  
425 #if EMULATED_68K
601   void FlushCodeCache(void *start, uint32 size)
602   {
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 + #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 435 | Line 632 | void FlushCodeCache(void *start, uint32
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 +        SetInterruptFlag(INTFLAG_60HZ);
712 +        TriggerInterrupt();
713 +
714 + #ifndef HAVE_PTHREADS
715 +        static int second_counter = 0;
716 +        if (++second_counter > 60) {
717 +                second_counter = 0;
718 +                xpram_watchdog();
719 +        }
720 + #endif
721 + }
722 +
723   static void one_tick(...)
724   {
725          static int tick_counter = 0;
460
461        // Pseudo Mac 1Hz interrupt, update local time
726          if (++tick_counter > 60) {
727                  tick_counter = 0;
728 <                WriteMacInt32(0x20c, TimerDateTime());
728 >                one_second();
729          }
730  
731 + #ifndef HAVE_PTHREADS
732 +        // No threads available, perform video refresh from here
733 +        VideoRefresh();
734 + #endif
735 +
736          // Trigger 60Hz interrupt
737          if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
738                  SetInterruptFlag(INTFLAG_60HZ);
# Line 471 | Line 740 | static void one_tick(...)
740          }
741   }
742  
743 + #ifdef HAVE_PTHREADS
744   static void *tick_func(void *arg)
745   {
746          while (!tick_thread_cancel) {
# Line 488 | Line 758 | static void *tick_func(void *arg)
758          }
759          return NULL;
760   }
761 + #endif
762  
763  
764 + #if !EMULATED_68K
765   /*
766 < *  XPRAM watchdog thread (saves XPRAM every minute)
766 > *  Virtual 68k interrupt handler
767   */
768  
769 < void *xpram_func(void *arg)
769 > static void sigirq_handler(int sig, int code, struct sigcontext *scp)
770   {
771 <        uint8 last_xpram[256];
772 <        memcpy(last_xpram, XPRAM, 256);
771 >        // Interrupts disabled? Then do nothing
772 >        if (EmulatedSR & 0x0700)
773 >                return;
774  
775 <        while (!xpram_thread_cancel) {
776 <                for (int i=0; i<60 && !xpram_thread_cancel; i++) {
777 < #ifdef HAVE_NANOSLEEP
778 <                        struct timespec req = {1, 0};
779 <                        nanosleep(&req, NULL);
780 < #else
781 <                        usleep(1000000);
782 < #endif
775 >        struct sigstate *state = (struct sigstate *)scp->sc_ap;
776 >        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
777 >
778 >        // Set up interrupt frame on stack
779 >        uint32 a7 = regs->a[7];
780 >        a7 -= 2;
781 >        WriteMacInt16(a7, 0x64);
782 >        a7 -= 4;
783 >        WriteMacInt32(a7, scp->sc_pc);
784 >        a7 -= 2;
785 >        WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
786 >        scp->sc_sp = regs->a[7] = a7;
787 >
788 >        // Set interrupt level
789 >        EmulatedSR |= 0x2100;
790 >
791 >        // Jump to MacOS interrupt handler on return
792 >        scp->sc_pc = ReadMacInt32(0x64);
793 > }
794 >
795 >
796 > /*
797 > *  SIGILL handler, for emulation of privileged instructions and executing
798 > *  A-Trap and EMUL_OP opcodes
799 > */
800 >
801 > static void sigill_handler(int sig, int code, struct sigcontext *scp)
802 > {
803 >        struct sigstate *state = (struct sigstate *)scp->sc_ap;
804 >        uint16 *pc = (uint16 *)scp->sc_pc;
805 >        uint16 opcode = *pc;
806 >        M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
807 >
808 > #define INC_PC(n) scp->sc_pc += (n)
809 >
810 > #define GET_SR (scp->sc_ps | EmulatedSR)
811 >
812 > #define STORE_SR(v) \
813 >        scp->sc_ps = (v) & 0xff; \
814 >        EmulatedSR = (v) & 0x2700; \
815 >        if (((v) & 0x0700) == 0 && InterruptFlags) \
816 >                TriggerInterrupt();
817 >
818 > //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
819 >
820 >        if ((opcode & 0xf000) == 0xa000) {
821 >
822 >                // A-Line instruction, set up A-Line trap frame on stack
823 >                uint32 a7 = regs->a[7];
824 >                a7 -= 2;
825 >                WriteMacInt16(a7, 0x28);
826 >                a7 -= 4;
827 >                WriteMacInt32(a7, (uint32)pc);
828 >                a7 -= 2;
829 >                WriteMacInt16(a7, GET_SR);
830 >                scp->sc_sp = regs->a[7] = a7;
831 >
832 >                // Jump to MacOS A-Line handler on return
833 >                scp->sc_pc = ReadMacInt32(0x28);
834 >
835 >        } else if ((opcode & 0xff00) == 0x7100) {
836 >
837 >                // Extended opcode, push registers on user stack
838 >                uint32 a7 = regs->a[7];
839 >                a7 -= 4;
840 >                WriteMacInt32(a7, (uint32)pc);
841 >                a7 -= 2;
842 >                WriteMacInt16(a7, scp->sc_ps);
843 >                for (int i=7; i>=0; i--) {
844 >                        a7 -= 4;
845 >                        WriteMacInt32(a7, regs->a[i]);
846 >                }
847 >                for (int i=7; i>=0; i--) {
848 >                        a7 -= 4;
849 >                        WriteMacInt32(a7, regs->d[i]);
850 >                }
851 >                scp->sc_sp = regs->a[7] = a7;
852 >
853 >                // Jump to EmulOp trampoline code on return
854 >                scp->sc_pc = (uint32)EmulOpTrampoline;
855 >                
856 >        } else switch (opcode) {        // Emulate privileged instructions
857 >
858 >                case 0x40e7:    // move sr,-(sp)
859 >                        regs->a[7] -= 2;
860 >                        WriteMacInt16(regs->a[7], GET_SR);
861 >                        scp->sc_sp = regs->a[7];
862 >                        INC_PC(2);
863 >                        break;
864 >
865 >                case 0x46df: {  // move (sp)+,sr
866 >                        uint16 sr = ReadMacInt16(regs->a[7]);
867 >                        STORE_SR(sr);
868 >                        regs->a[7] += 2;
869 >                        scp->sc_sp = regs->a[7];
870 >                        INC_PC(2);
871 >                        break;
872 >                }
873 >
874 >                case 0x007c: {  // ori #xxxx,sr
875 >                        uint16 sr = GET_SR | pc[1];
876 >                        scp->sc_ps = sr & 0xff;         // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
877 >                        EmulatedSR = sr & 0x2700;
878 >                        INC_PC(4);
879 >                        break;
880 >                }
881 >
882 >                case 0x027c: {  // andi #xxxx,sr
883 >                        uint16 sr = GET_SR & pc[1];
884 >                        STORE_SR(sr);
885 >                        INC_PC(4);
886 >                        break;
887 >                }
888 >
889 >                case 0x46fc:    // move #xxxx,sr
890 >                        STORE_SR(pc[1]);
891 >                        INC_PC(4);
892 >                        break;
893 >
894 >                case 0x46ef: {  // move (xxxx,sp),sr
895 >                        uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
896 >                        STORE_SR(sr);
897 >                        INC_PC(4);
898 >                        break;
899 >                }
900 >
901 >                case 0x46d8:    // move (a0)+,sr
902 >                case 0x46d9: {  // move (a1)+,sr
903 >                        uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
904 >                        STORE_SR(sr);
905 >                        regs->a[opcode & 7] += 2;
906 >                        INC_PC(2);
907 >                        break;
908 >                }
909 >
910 >                case 0x40f8:    // move sr,xxxx.w
911 >                        WriteMacInt16(pc[1], GET_SR);
912 >                        INC_PC(4);
913 >                        break;
914 >
915 >                case 0x40d0:    // move sr,(a0)
916 >                case 0x40d1:    // move sr,(a1)
917 >                case 0x40d2:    // move sr,(a2)
918 >                case 0x40d3:    // move sr,(a3)
919 >                case 0x40d4:    // move sr,(a4)
920 >                case 0x40d5:    // move sr,(a5)
921 >                case 0x40d6:    // move sr,(a6)
922 >                case 0x40d7:    // move sr,(sp)
923 >                        WriteMacInt16(regs->a[opcode & 7], GET_SR);
924 >                        INC_PC(2);
925 >                        break;
926 >
927 >                case 0x40c0:    // move sr,d0
928 >                case 0x40c1:    // move sr,d1
929 >                case 0x40c2:    // move sr,d2
930 >                case 0x40c3:    // move sr,d3
931 >                case 0x40c4:    // move sr,d4
932 >                case 0x40c5:    // move sr,d5
933 >                case 0x40c6:    // move sr,d6
934 >                case 0x40c7:    // move sr,d7
935 >                        regs->d[opcode & 7] = GET_SR;
936 >                        INC_PC(2);
937 >                        break;
938 >
939 >                case 0x46c0:    // move d0,sr
940 >                case 0x46c1:    // move d1,sr
941 >                case 0x46c2:    // move d2,sr
942 >                case 0x46c3:    // move d3,sr
943 >                case 0x46c4:    // move d4,sr
944 >                case 0x46c5:    // move d5,sr
945 >                case 0x46c6:    // move d6,sr
946 >                case 0x46c7: {  // move d7,sr
947 >                        uint16 sr = regs->d[opcode & 7];
948 >                        STORE_SR(sr);
949 >                        INC_PC(2);
950 >                        break;
951                  }
952 <                if (memcmp(last_xpram, XPRAM, 256)) {
953 <                        memcpy(last_xpram, XPRAM, 256);
954 <                        SaveXPRAM();
952 >
953 >                case 0xf327:    // fsave -(sp)
954 >                        goto ill;       //!!
955 >
956 >                case 0xf35f:    // frestore (sp)+
957 >                        goto ill;       //!!
958 >
959 >                case 0x4e73: {  // rte (only handles format 0)
960 >                        uint32 a7 = regs->a[7];
961 >                        uint16 sr = ReadMacInt16(a7);
962 >                        a7 += 2;
963 >                        scp->sc_ps = sr & 0xff;
964 >                        EmulatedSR = sr & 0x2700;
965 >                        scp->sc_pc = ReadMacInt32(a7);
966 >                        a7 += 6;
967 >                        scp->sc_sp = regs->a[7] = a7;
968 >                        break;
969                  }
970 +
971 +                case 0x4e7a:    // movec cr,x
972 +                        switch (pc[1]) {
973 +                                case 0x8801:    // movec vbr,a0
974 +                                        regs->a[0] = 0;
975 +                                        break;
976 +                                case 0x9801:    // movec vbr,a1
977 +                                        regs->a[1] = 0;
978 +                                        break;
979 +                                case 0x0002:    // movec cacr,d0
980 +                                        regs->d[0] = 0x3111;
981 +                                        break;
982 +                                case 0x1002:    // movec cacr,d1
983 +                                        regs->d[1] = 0x3111;
984 +                                        break;
985 +                                case 0x0003:    // movec tc,d0
986 +                                        regs->d[0] = 0;
987 +                                        break;
988 +                                case 0x1003:    // movec tc,d1
989 +                                        regs->d[1] = 0;
990 +                                        break;
991 +                                default:
992 +                                        goto ill;
993 +                        }
994 +                        INC_PC(4);
995 +                        break;
996 +
997 +                case 0x4e7b:    // movec x,cr
998 +                        switch (pc[1]) {
999 +                                case 0x0801:    // movec d0,vbr
1000 +                                        break;
1001 +                                case 0x0002:    // movec d0,cacr
1002 +                                case 0x1002:    // movec d1,cacr
1003 +                                        FlushCodeCache(NULL, 0);
1004 +                                        break;
1005 +                                default:
1006 +                                        goto ill;
1007 +                        }
1008 +                        INC_PC(4);
1009 +                        break;
1010 +
1011 +                case 0xf478:    // cpusha dc
1012 +                case 0xf4f8:    // cpusha dc/ic
1013 +                        FlushCodeCache(NULL, 0);
1014 +                        INC_PC(2);
1015 +                        break;
1016 +
1017 +                default:
1018 + ill:            printf("SIGILL num %d, code %d\n", sig, code);
1019 +                        printf(" context %p:\n", scp);
1020 +                        printf("  onstack %08x\n", scp->sc_onstack);
1021 +                        printf("  sp %08x\n", scp->sc_sp);
1022 +                        printf("  fp %08x\n", scp->sc_fp);
1023 +                        printf("  pc %08x\n", scp->sc_pc);
1024 +                        printf("   opcode %04x\n", opcode);
1025 +                        printf("  sr %08x\n", scp->sc_ps);
1026 +                        printf(" state %p:\n", state);
1027 +                        printf("  flags %d\n", state->ss_flags);
1028 +                        for (int i=0; i<8; i++)
1029 +                                printf("  d%d %08x\n", i, state->ss_frame.f_regs[i]);
1030 +                        for (int i=0; i<8; i++)
1031 +                                printf("  a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1032 +
1033 + #ifdef ENABLE_MON
1034 +                        char *arg[2] = {"rmon", NULL};
1035 +                        mon(1, arg);
1036 + #endif
1037 +                        QuitEmulator();
1038 +                        break;
1039          }
516        return NULL;
1040   }
1041 + #endif
1042  
1043  
1044   /*
1045   *  Display alert
1046   */
1047  
1048 < #if ENABLE_GTK
1048 > #ifdef ENABLE_GTK
1049   static void dl_destroyed(void)
1050   {
1051          gtk_main_quit();
# Line 566 | Line 1090 | void display_alert(int title_id, int pre
1090  
1091   void ErrorAlert(const char *text)
1092   {
1093 < #if ENABLE_GTK
1093 > #ifdef ENABLE_GTK
1094          if (PrefsFindBool("nogui") || x_display == NULL) {
1095                  printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1096                  return;
# Line 585 | Line 1109 | void ErrorAlert(const char *text)
1109  
1110   void WarningAlert(const char *text)
1111   {
1112 < #if ENABLE_GTK
1112 > #ifdef ENABLE_GTK
1113          if (PrefsFindBool("nogui") || x_display == NULL) {
1114                  printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1115                  return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines