ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.15
Committed: 2000-07-22T16:07:20Z (24 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.14: +4 -1 lines
Log Message:
- new FOURCC() macro in macos_util.h

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_unix.cpp - Startup code for Unix
3     *
4 cebix 1.11 * Basilisk II (C) 1997-2000 Christian Bauer
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22    
23     #include <stdio.h>
24     #include <stdlib.h>
25     #include <signal.h>
26 cebix 1.12 #include <errno.h>
27     #include <X11/Xlib.h>
28    
29     #ifdef HAVE_PTHREADS
30     # include <pthread.h>
31     #endif
32    
33     #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 cebix 1.1
60     #include "cpu_emulation.h"
61     #include "sys.h"
62 cebix 1.3 #include "rom_patches.h"
63 cebix 1.1 #include "xpram.h"
64     #include "timer.h"
65     #include "video.h"
66 cebix 1.12 #include "emul_op.h"
67 cebix 1.1 #include "prefs.h"
68     #include "prefs_editor.h"
69     #include "macos_util.h"
70     #include "user_strings.h"
71     #include "version.h"
72     #include "main.h"
73    
74 cebix 1.12 #ifdef ENABLE_MON
75     # include "mon.h"
76     #endif
77    
78 cebix 1.13 #define DEBUG 0
79 cebix 1.1 #include "debug.h"
80    
81    
82 cebix 1.12 // 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 cebix 1.1
87 cebix 1.4
88 cebix 1.12 #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 cebix 1.9 #endif
97    
98 cebix 1.1
99     // CPU and FPU type, addressing mode
100     int CPUType;
101     bool CPUIs68060;
102     int FPUType;
103     bool TwentyFourBitAddressing;
104    
105    
106     // Global variables
107 cebix 1.13 char *x_display_name = NULL; // X11 display name
108 cebix 1.1 Display *x_display = NULL; // X11 display handle
109    
110 cebix 1.12 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 cebix 1.1 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
119    
120     static bool tick_thread_active = false; // Flag: 60Hz thread installed
121     static volatile bool tick_thread_cancel = false; // Flag: Cancel 60Hz thread
122     static pthread_t tick_thread; // 60Hz thread
123     static pthread_attr_t tick_thread_attr; // 60Hz thread attributes
124    
125 cebix 1.12 #if EMULATED_68K
126 cebix 1.1 static pthread_mutex_t intflag_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect InterruptFlags
127 cebix 1.12 #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 cebix 1.1
141     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
142     #define SIG_TIMER SIGRTMIN
143 cebix 1.12 static timer_t timer; // 60Hz timer
144 cebix 1.1 #endif
145    
146 cebix 1.12 #ifdef ENABLE_MON
147     static struct sigaction sigint_sa; // sigaction for SIGINT handler
148 cebix 1.4 static void sigint_handler(...);
149 cebix 1.15 #endif
150    
151     #if REAL_ADDRESSING
152     static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
153 cebix 1.4 #endif
154    
155 cebix 1.12 #ifdef USE_MAPPED_MEMORY
156     extern char *address_space, *good_address_map;
157     #endif
158    
159 cebix 1.1
160     // Prototypes
161     static void *xpram_func(void *arg);
162     static void *tick_func(void *arg);
163     static void one_tick(...);
164 cebix 1.12 #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 cebix 1.1
170    
171     /*
172     * Ersatz functions
173     */
174    
175     extern "C" {
176    
177     #ifndef HAVE_STRDUP
178     char *strdup(const char *s)
179     {
180     char *n = (char *)malloc(strlen(s) + 1);
181     strcpy(n, s);
182     return n;
183     }
184     #endif
185    
186     }
187    
188    
189     /*
190     * Main program
191     */
192    
193     int main(int argc, char **argv)
194     {
195 cebix 1.12 char str[256];
196    
197 cebix 1.1 // Initialize variables
198     RAMBaseHost = NULL;
199     ROMBaseHost = NULL;
200     srand(time(NULL));
201     tzset();
202    
203     // Print some info
204     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
205     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
206    
207     // Parse arguments
208     for (int i=1; i<argc; i++) {
209     if (strcmp(argv[i], "-display") == 0 && ++i < argc)
210     x_display_name = argv[i];
211 cebix 1.4 else if (strcmp(argv[i], "-break") == 0 && ++i < argc)
212     ROMBreakpoint = strtol(argv[i], NULL, 0);
213 cebix 1.6 else if (strcmp(argv[i], "-rominfo") == 0)
214     PrintROMInfo = true;
215 cebix 1.1 }
216    
217     // Open display
218     x_display = XOpenDisplay(x_display_name);
219     if (x_display == NULL) {
220     char str[256];
221     sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
222     ErrorAlert(str);
223     QuitEmulator();
224     }
225    
226 cebix 1.12 #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
227 cebix 1.1 // Fork out, so we can return from fullscreen mode when things get ugly
228 cebix 1.2 XF86DGAForkApp(DefaultScreen(x_display));
229 cebix 1.1 #endif
230    
231 cebix 1.12 #ifdef ENABLE_GTK
232 cebix 1.1 // Init GTK
233     gtk_set_locale();
234     gtk_init(&argc, &argv);
235     #endif
236    
237     // Read preferences
238     PrefsInit();
239    
240     // Init system routines
241     SysInit();
242    
243     // Show preferences editor
244     if (!PrefsFindBool("nogui"))
245     if (!PrefsEditor())
246     QuitEmulator();
247    
248 cebix 1.12 // 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 cebix 1.9 // Read RAM size
257 cebix 1.1 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
258     if (RAMSize < 1024*1024) {
259     WarningAlert(GetString(STR_SMALL_RAM_WARN));
260     RAMSize = 1024*1024;
261     }
262 cebix 1.9
263 cebix 1.12 #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 cebix 1.9 // Create areas for Mac RAM and ROM
284 cebix 1.12 #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 cebix 1.9 if ((int)address_space < 0 || (int)good_address_map < 0) {
288     ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
289     QuitEmulator();
290     }
291 cebix 1.12 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 cebix 1.9 char *nam = tmpnam(NULL);
294     int good_address_fd = open(nam, O_CREAT | O_RDWR, 0600);
295     char buffer[4096];
296     memset(buffer, 1, sizeof(buffer));
297     write(good_address_fd, buffer, sizeof(buffer));
298     unlink(nam);
299     for (int i=0; i<RAMSize; i+=4096)
300     mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
301     for (int i=0; i<0x80000; i+=4096)
302     mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0);
303     #else
304 cebix 1.1 RAMBaseHost = new uint8[RAMSize];
305     ROMBaseHost = new uint8[0x100000];
306 cebix 1.9 #endif
307 cebix 1.12 #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 cebix 1.1
314     // Get rom file path from preferences
315     const char *rom_path = PrefsFindString("rom");
316    
317     // Load Mac ROM
318     int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
319     if (rom_fd < 0) {
320     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
321     QuitEmulator();
322     }
323     printf(GetString(STR_READING_ROM_FILE));
324     ROMSize = lseek(rom_fd, 0, SEEK_END);
325     if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
326     ErrorAlert(GetString(STR_ROM_SIZE_ERR));
327     close(rom_fd);
328     QuitEmulator();
329     }
330     lseek(rom_fd, 0, SEEK_SET);
331     if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
332     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
333     close(rom_fd);
334     QuitEmulator();
335     }
336    
337 cebix 1.12 #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 cebix 1.3 // Initialize everything
367     if (!InitAll())
368 cebix 1.1 QuitEmulator();
369 cebix 1.12 D(bug("Initialization complete\n"));
370    
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 cebix 1.1
422 cebix 1.12 #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 cebix 1.1
430     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
431 cebix 1.12
432     // POSIX.4 timers and real-time signals available, start 60Hz timer
433 cebix 1.1 sigemptyset(&timer_sa.sa_mask);
434 cebix 1.12 timer_sa.sa_sigaction = one_tick;
435 cebix 1.1 timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
436     if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
437 cebix 1.12 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
438     ErrorAlert(str);
439 cebix 1.1 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 cebix 1.12 sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
446     ErrorAlert(str);
447 cebix 1.1 QuitEmulator();
448     }
449     struct itimerspec req;
450     req.it_value.tv_sec = 0;
451     req.it_value.tv_nsec = 16625000;
452     req.it_interval.tv_sec = 0;
453     req.it_interval.tv_nsec = 16625000;
454 cebix 1.10 if (timer_settime(timer, 0, &req, NULL) < 0) {
455 cebix 1.12 sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
456     ErrorAlert(str);
457 cebix 1.1 QuitEmulator();
458     }
459 cebix 1.12 D(bug("60Hz timer started\n"));
460 cebix 1.1
461 cebix 1.12 #elif defined(HAVE_PTHREADS)
462 cebix 1.1
463 cebix 1.12 // POSIX threads available, start 60Hz thread
464 cebix 1.1 pthread_attr_init(&tick_thread_attr);
465     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
466     if (geteuid() == 0) {
467     pthread_attr_setinheritsched(&tick_thread_attr, PTHREAD_EXPLICIT_SCHED);
468     pthread_attr_setschedpolicy(&tick_thread_attr, SCHED_FIFO);
469     struct sched_param fifo_param;
470     fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2;
471     pthread_attr_setschedparam(&tick_thread_attr, &fifo_param);
472     }
473     #endif
474     tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
475     if (!tick_thread_active) {
476 cebix 1.12 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 cebix 1.1 QuitEmulator();
493     }
494 cebix 1.12 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 cebix 1.1 #endif
500    
501 cebix 1.12 #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 cebix 1.4 #endif
507    
508 cebix 1.1 // Start 68k and jump to ROM boot routine
509 cebix 1.12 D(bug("Starting emulation...\n"));
510 cebix 1.1 Start680x0();
511    
512     QuitEmulator();
513     return 0;
514     }
515    
516    
517     /*
518     * Quit emulator
519     */
520    
521     void QuitEmulator(void)
522     {
523 cebix 1.12 D(bug("QuitEmulator\n"));
524    
525     #if EMULATED_68K
526 cebix 1.1 // Exit 680x0 emulation
527     Exit680x0();
528 cebix 1.12 #endif
529 cebix 1.1
530     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
531     // Stop 60Hz timer
532     timer_delete(timer);
533 cebix 1.12 #elif defined(HAVE_PTHREADS)
534 cebix 1.1 // Stop 60Hz thread
535     if (tick_thread_active) {
536     tick_thread_cancel = true;
537     #ifdef HAVE_PTHREAD_CANCEL
538     pthread_cancel(tick_thread);
539     #endif
540     pthread_join(tick_thread, NULL);
541     }
542 cebix 1.12 #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 cebix 1.1 #endif
548    
549 cebix 1.12 #ifdef HAVE_PTHREADS
550 cebix 1.1 // Stop XPRAM watchdog thread
551     if (xpram_thread_active) {
552     xpram_thread_cancel = true;
553     #ifdef HAVE_PTHREAD_CANCEL
554     pthread_cancel(xpram_thread);
555     #endif
556     pthread_join(xpram_thread, NULL);
557     }
558 cebix 1.12 #endif
559 cebix 1.1
560 cebix 1.3 // Deinitialize everything
561     ExitAll();
562 cebix 1.1
563     // Delete ROM area
564     delete[] ROMBaseHost;
565    
566     // Delete RAM area
567     delete[] RAMBaseHost;
568    
569 cebix 1.12 #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 cebix 1.1 // Exit system routines
586     SysExit();
587    
588     // Exit preferences
589     PrefsExit();
590    
591     // Close X11 server connection
592     if (x_display)
593     XCloseDisplay(x_display);
594    
595     exit(0);
596     }
597    
598    
599     /*
600     * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
601     * or a dynamically recompiling emulator)
602     */
603    
604     void FlushCodeCache(void *start, uint32 size)
605     {
606 cebix 1.12 #if !EMULATED_68K && defined(__NetBSD__)
607     m68k_sync_icache(start, size);
608     #endif
609 cebix 1.4 }
610    
611    
612     /*
613     * SIGINT handler, enters mon
614     */
615    
616 cebix 1.12 #ifdef ENABLE_MON
617 cebix 1.4 static void sigint_handler(...)
618     {
619 cebix 1.12 #if EMULATED_68K
620 cebix 1.8 uaecptr nextpc;
621 cebix 1.12 extern void m68k_dumpstate(uaecptr *nextpc);
622 cebix 1.8 m68k_dumpstate(&nextpc);
623 cebix 1.12 #else
624 cebix 1.4 char *arg[2] = {"rmon", NULL};
625     mon(1, arg);
626     QuitEmulator();
627 cebix 1.12 #endif
628 cebix 1.1 }
629     #endif
630    
631    
632     /*
633     * Interrupt flags (must be handled atomically!)
634     */
635    
636     uint32 InterruptFlags = 0;
637    
638 cebix 1.12 #if EMULATED_68K
639 cebix 1.1 void SetInterruptFlag(uint32 flag)
640     {
641 cebix 1.12 #ifdef HAVE_PTHREADS
642 cebix 1.1 pthread_mutex_lock(&intflag_lock);
643     InterruptFlags |= flag;
644     pthread_mutex_unlock(&intflag_lock);
645 cebix 1.12 #else
646     InterruptFlags |= flag; // Pray that this is an atomic operation...
647     #endif
648 cebix 1.1 }
649    
650     void ClearInterruptFlag(uint32 flag)
651     {
652 cebix 1.12 #ifdef HAVE_PTHREADS
653 cebix 1.1 pthread_mutex_lock(&intflag_lock);
654     InterruptFlags &= ~flag;
655     pthread_mutex_unlock(&intflag_lock);
656 cebix 1.12 #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 cebix 1.1 }
702 cebix 1.12 #endif
703 cebix 1.1
704    
705     /*
706     * 60Hz thread (really 60.15Hz)
707     */
708    
709 cebix 1.12 static void one_second(void)
710     {
711     // Pseudo Mac 1Hz interrupt, update local time
712     WriteMacInt32(0x20c, TimerDateTime());
713    
714 cebix 1.14 SetInterruptFlag(INTFLAG_60HZ);
715     TriggerInterrupt();
716    
717 cebix 1.12 #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 cebix 1.1 static void one_tick(...)
727     {
728     static int tick_counter = 0;
729     if (++tick_counter > 60) {
730     tick_counter = 0;
731 cebix 1.12 one_second();
732 cebix 1.1 }
733    
734 cebix 1.12 #ifndef HAVE_PTHREADS
735     // No threads available, perform video refresh from here
736     VideoRefresh();
737     #endif
738    
739 cebix 1.1 // Trigger 60Hz interrupt
740     if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
741     SetInterruptFlag(INTFLAG_60HZ);
742     TriggerInterrupt();
743     }
744     }
745    
746 cebix 1.12 #ifdef HAVE_PTHREADS
747 cebix 1.1 static void *tick_func(void *arg)
748     {
749     while (!tick_thread_cancel) {
750    
751     // Wait
752     #ifdef HAVE_NANOSLEEP
753     struct timespec req = {0, 16625000};
754     nanosleep(&req, NULL);
755     #else
756     usleep(16625);
757     #endif
758    
759     // Action
760     one_tick();
761     }
762     return NULL;
763     }
764 cebix 1.12 #endif
765    
766    
767     #if !EMULATED_68K
768     /*
769     * Virtual 68k interrupt handler
770     */
771    
772     static void sigirq_handler(int sig, int code, struct sigcontext *scp)
773     {
774     // Interrupts disabled? Then do nothing
775     if (EmulatedSR & 0x0700)
776     return;
777    
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 cebix 1.1
798    
799     /*
800 cebix 1.12 * SIGILL handler, for emulation of privileged instructions and executing
801     * A-Trap and EMUL_OP opcodes
802 cebix 1.1 */
803    
804 cebix 1.12 static void sigill_handler(int sig, int code, struct sigcontext *scp)
805 cebix 1.1 {
806 cebix 1.12 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    
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 cebix 1.1
913 cebix 1.12 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 cebix 1.1 }
955 cebix 1.12
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 cebix 1.1 }
973 cebix 1.12
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 cebix 1.1 }
1043     }
1044 cebix 1.12 #endif
1045 cebix 1.1
1046    
1047     /*
1048     * Display alert
1049     */
1050    
1051 cebix 1.12 #ifdef ENABLE_GTK
1052 cebix 1.1 static void dl_destroyed(void)
1053     {
1054     gtk_main_quit();
1055     }
1056    
1057     static void dl_quit(GtkWidget *dialog)
1058     {
1059     gtk_widget_destroy(dialog);
1060     }
1061    
1062     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1063     {
1064     char str[256];
1065     sprintf(str, GetString(prefix_id), text);
1066    
1067     GtkWidget *dialog = gtk_dialog_new();
1068     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1069     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1070     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1071     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1072    
1073     GtkWidget *label = gtk_label_new(str);
1074     gtk_widget_show(label);
1075     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1076    
1077     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1078     gtk_widget_show(button);
1079     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1080     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1081     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1082     gtk_widget_grab_default(button);
1083     gtk_widget_show(dialog);
1084    
1085     gtk_main();
1086     }
1087     #endif
1088    
1089    
1090     /*
1091     * Display error alert
1092     */
1093    
1094     void ErrorAlert(const char *text)
1095     {
1096 cebix 1.12 #ifdef ENABLE_GTK
1097 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1098     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1099     return;
1100     }
1101     VideoQuitFullScreen();
1102     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1103     #else
1104     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1105     #endif
1106     }
1107    
1108    
1109     /*
1110     * Display warning alert
1111     */
1112    
1113     void WarningAlert(const char *text)
1114     {
1115 cebix 1.12 #ifdef ENABLE_GTK
1116 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1117     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1118     return;
1119     }
1120     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1121     #else
1122     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1123     #endif
1124     }
1125    
1126    
1127     /*
1128     * Display choice alert
1129     */
1130    
1131     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1132     {
1133     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1134     return false; //!!
1135     }