ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.41
Committed: 2001-10-07T19:50:20Z (22 years, 9 months ago) by cebix
Branch: MAIN
Changes since 1.40: +4 -4 lines
Log Message:
prepared XPRAM code for PowerMac emulation (8192 bytes NVRAM)

File Contents

# Content
1 /*
2 * main_unix.cpp - Startup code for Unix
3 *
4 * Basilisk II (C) 1997-2001 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
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 #include <errno.h>
27 #include <X11/Xlib.h>
28
29 #ifdef HAVE_PTHREADS
30 # include <pthread.h>
31 #endif
32
33 #if REAL_ADDRESSING || DIRECT_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 # include <gdk/gdk.h>
54 #endif
55
56 #ifdef ENABLE_XF86_DGA
57 # include <X11/Xutil.h>
58 # include <X11/extensions/xf86dga.h>
59 #endif
60
61 #include "cpu_emulation.h"
62 #include "sys.h"
63 #include "rom_patches.h"
64 #include "xpram.h"
65 #include "timer.h"
66 #include "video.h"
67 #include "emul_op.h"
68 #include "prefs.h"
69 #include "prefs_editor.h"
70 #include "macos_util.h"
71 #include "user_strings.h"
72 #include "version.h"
73 #include "main.h"
74 #include "vm_alloc.h"
75
76 #ifdef ENABLE_MON
77 # include "mon.h"
78 #endif
79
80 #define DEBUG 0
81 #include "debug.h"
82
83
84 // Constants
85 const char ROM_FILE_NAME[] = "ROM";
86 const int SIG_STACK_SIZE = SIGSTKSZ; // Size of signal stack
87 const int SCRATCH_MEM_SIZE = 0x10000; // Size of scratch memory area
88
89
90 #if !EMULATED_68K
91 // RAM and ROM pointers
92 uint32 RAMBaseMac; // RAM base (Mac address space)
93 uint8 *RAMBaseHost; // RAM base (host address space)
94 uint32 RAMSize; // Size of RAM
95 uint32 ROMBaseMac; // ROM base (Mac address space)
96 uint8 *ROMBaseHost; // ROM base (host address space)
97 uint32 ROMSize; // Size of ROM
98 #endif
99
100
101 // CPU and FPU type, addressing mode
102 int CPUType;
103 bool CPUIs68060;
104 int FPUType;
105 bool TwentyFourBitAddressing;
106
107
108 // Global variables
109 char *x_display_name = NULL; // X11 display name
110 Display *x_display = NULL; // X11 display handle
111
112 static uint8 last_xpram[XPRAM_SIZE]; // 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
120
121 static bool tick_thread_active = false; // Flag: 60Hz thread installed
122 static volatile bool tick_thread_cancel = false; // Flag: Cancel 60Hz thread
123 static pthread_t tick_thread; // 60Hz thread
124 static pthread_attr_t tick_thread_attr; // 60Hz thread attributes
125
126 static pthread_mutex_t intflag_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect InterruptFlags
127 #define LOCK_INTFLAGS pthread_mutex_lock(&intflag_lock)
128 #define UNLOCK_INTFLAGS pthread_mutex_unlock(&intflag_lock)
129
130 #else
131
132 #define LOCK_INTFLAGS
133 #define UNLOCK_INTFLAGS
134
135 #endif
136
137 #if !EMULATED_68K
138 #define SIG_IRQ SIGUSR1
139 static struct sigaction sigirq_sa; // Virtual 68k interrupt signal
140 static struct sigaction sigill_sa; // Illegal instruction
141 static void *sig_stack = NULL; // Stack for signal handlers
142 uint16 EmulatedSR; // Emulated bits of SR (supervisor bit and interrupt mask)
143 #endif
144
145 #if USE_SCRATCHMEM_SUBTERFUGE
146 uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes
147 #endif
148
149 static struct sigaction timer_sa; // sigaction used for timer
150
151 #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
152 #define SIG_TIMER SIGRTMIN
153 static timer_t timer; // 60Hz timer
154 #endif
155
156 #ifdef ENABLE_MON
157 static struct sigaction sigint_sa; // sigaction for SIGINT handler
158 static void sigint_handler(...);
159 #endif
160
161 #if REAL_ADDRESSING
162 static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
163 #endif
164
165
166 // Prototypes
167 static void *xpram_func(void *arg);
168 static void *tick_func(void *arg);
169 static void one_tick(...);
170 #if !EMULATED_68K
171 static void sigirq_handler(int sig, int code, struct sigcontext *scp);
172 static void sigill_handler(int sig, int code, struct sigcontext *scp);
173 extern "C" void EmulOpTrampoline(void);
174 #endif
175
176
177 /*
178 * Ersatz functions
179 */
180
181 extern "C" {
182
183 #ifndef HAVE_STRDUP
184 char *strdup(const char *s)
185 {
186 char *n = (char *)malloc(strlen(s) + 1);
187 strcpy(n, s);
188 return n;
189 }
190 #endif
191
192 }
193
194
195 /*
196 * Main program
197 */
198
199 static void usage(const char *prg_name)
200 {
201 printf("Usage: %s [OPTION...]\n", prg_name);
202 printf("\nUnix options:\n");
203 printf(" --display STRING\n X display to use\n");
204 printf(" --break ADDRESS\n set ROM breakpoint\n");
205 printf(" --rominfo\n dump ROM information\n");
206 PrefsPrintUsage();
207 exit(0);
208 }
209
210 int main(int argc, char **argv)
211 {
212 char str[256];
213
214 // Initialize variables
215 RAMBaseHost = NULL;
216 ROMBaseHost = NULL;
217 srand(time(NULL));
218 tzset();
219
220 // Print some info
221 printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
222 printf(" %s\n", GetString(STR_ABOUT_TEXT2));
223
224 #ifdef ENABLE_GTK
225 // Init GTK
226 gtk_set_locale();
227 gtk_init(&argc, &argv);
228 x_display_name = gdk_get_display(); // gtk_init() handles and removes the "--display" argument
229 #endif
230
231 // Read preferences
232 PrefsInit(argc, argv);
233
234 // Parse command line arguments
235 for (int i=1; i<argc; i++) {
236 if (strcmp(argv[i], "--help") == 0) {
237 usage(argv[0]);
238 } else if (strcmp(argv[i], "--display") == 0) {
239 i++;
240 if (i < argc)
241 x_display_name = strdup(argv[i]);
242 } else if (strcmp(argv[i], "--break") == 0) {
243 i++;
244 if (i < argc)
245 ROMBreakpoint = strtol(argv[i], NULL, 0);
246 } else if (strcmp(argv[i], "--rominfo") == 0) {
247 PrintROMInfo = true;
248 } else if (argv[i][0] == '-') {
249 fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
250 usage(argv[0]);
251 }
252 }
253
254 // Open display
255 x_display = XOpenDisplay(x_display_name);
256 if (x_display == NULL) {
257 char str[256];
258 sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
259 ErrorAlert(str);
260 QuitEmulator();
261 }
262
263 #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
264 // Fork out, so we can return from fullscreen mode when things get ugly
265 XF86DGAForkApp(DefaultScreen(x_display));
266 #endif
267
268 // Init system routines
269 SysInit();
270
271 // Show preferences editor
272 if (!PrefsFindBool("nogui"))
273 if (!PrefsEditor())
274 QuitEmulator();
275
276 // Read RAM size
277 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
278 if (RAMSize < 1024*1024) {
279 WarningAlert(GetString(STR_SMALL_RAM_WARN));
280 RAMSize = 1024*1024;
281 }
282
283 #if REAL_ADDRESSING || DIRECT_ADDRESSING
284 RAMSize = RAMSize & -getpagesize(); // Round down to page boundary
285 #endif
286
287 // Initialize VM system
288 vm_init();
289
290 #if REAL_ADDRESSING
291 // Flag: RAM and ROM are contigously allocated from address 0
292 bool memory_mapped_from_zero = false;
293
294 // Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash
295 // when trying to map a too big chunk of memory starting at address 0
296 #if defined(OS_solaris) || defined(OS_netbsd)
297 const bool can_map_all_memory = false;
298 #else
299 const bool can_map_all_memory = true;
300 #endif
301
302 // Try to allocate all memory from 0x0000, if it is not known to crash
303 if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) {
304 D(bug("Could allocate RAM and ROM from 0x0000\n"));
305 memory_mapped_from_zero = true;
306 }
307
308 // Otherwise, just create the Low Memory area (0x0000..0x2000)
309 else if (vm_acquire_fixed(0, 0x2000) == 0) {
310 D(bug("Could allocate the Low Memory globals\n"));
311 lm_area_mapped = true;
312 }
313
314 // Exit on failure
315 else {
316 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
317 ErrorAlert(str);
318 QuitEmulator();
319 }
320 #endif
321
322 // Create areas for Mac RAM and ROM
323 #if REAL_ADDRESSING
324 if (memory_mapped_from_zero) {
325 RAMBaseHost = (uint8 *)0;
326 ROMBaseHost = RAMBaseHost + RAMSize;
327 }
328 else
329 #endif
330 {
331 RAMBaseHost = (uint8 *)vm_acquire(RAMSize);
332 ROMBaseHost = (uint8 *)vm_acquire(0x100000);
333 if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) {
334 ErrorAlert(STR_NO_MEM_ERR);
335 QuitEmulator();
336 }
337 }
338
339 #if USE_SCRATCHMEM_SUBTERFUGE
340 // Allocate scratch memory
341 ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE);
342 if (ScratchMem == VM_MAP_FAILED) {
343 ErrorAlert(STR_NO_MEM_ERR);
344 QuitEmulator();
345 }
346 ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
347 #endif
348
349 #if DIRECT_ADDRESSING
350 // RAMBaseMac shall always be zero
351 MEMBaseDiff = (uintptr)RAMBaseHost;
352 RAMBaseMac = 0;
353 ROMBaseMac = Host2MacAddr(ROMBaseHost);
354 #endif
355 #if REAL_ADDRESSING
356 RAMBaseMac = (uint32)RAMBaseHost;
357 ROMBaseMac = (uint32)ROMBaseHost;
358 #endif
359 D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
360 D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
361
362 // Get rom file path from preferences
363 const char *rom_path = PrefsFindString("rom");
364
365 // Load Mac ROM
366 int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
367 if (rom_fd < 0) {
368 ErrorAlert(STR_NO_ROM_FILE_ERR);
369 QuitEmulator();
370 }
371 printf(GetString(STR_READING_ROM_FILE));
372 ROMSize = lseek(rom_fd, 0, SEEK_END);
373 if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
374 ErrorAlert(STR_ROM_SIZE_ERR);
375 close(rom_fd);
376 QuitEmulator();
377 }
378 lseek(rom_fd, 0, SEEK_SET);
379 if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
380 ErrorAlert(STR_ROM_FILE_READ_ERR);
381 close(rom_fd);
382 QuitEmulator();
383 }
384
385 #if !EMULATED_68K
386 // Get CPU model
387 int mib[2] = {CTL_HW, HW_MODEL};
388 char *model;
389 size_t model_len;
390 sysctl(mib, 2, NULL, &model_len, NULL, 0);
391 model = (char *)malloc(model_len);
392 sysctl(mib, 2, model, &model_len, NULL, 0);
393 D(bug("Model: %s\n", model));
394
395 // Set CPU and FPU type
396 CPUIs68060 = false;
397 if (strstr(model, "020"))
398 CPUType = 2;
399 else if (strstr(model, "030"))
400 CPUType = 3;
401 else if (strstr(model, "040"))
402 CPUType = 4;
403 else if (strstr(model, "060")) {
404 CPUType = 4;
405 CPUIs68060 = true;
406 } else {
407 printf("WARNING: Cannot detect CPU type, assuming 68020\n");
408 CPUType = 2;
409 }
410 FPUType = 1; // NetBSD has an FPU emulation, so the FPU ought to be available at all times
411 TwentyFourBitAddressing = false;
412 #endif
413
414 // Initialize everything
415 if (!InitAll())
416 QuitEmulator();
417 D(bug("Initialization complete\n"));
418
419 #ifdef HAVE_PTHREADS
420 // Get handle of main thread
421 emul_thread = pthread_self();
422 #endif
423
424 #if !EMULATED_68K
425 // (Virtual) supervisor mode, disable interrupts
426 EmulatedSR = 0x2700;
427
428 // Create and install stack for signal handlers
429 sig_stack = malloc(SIG_STACK_SIZE);
430 D(bug("Signal stack at %p\n", sig_stack));
431 if (sig_stack == NULL) {
432 ErrorAlert(STR_NOT_ENOUGH_MEMORY_ERR);
433 QuitEmulator();
434 }
435 stack_t new_stack;
436 new_stack.ss_sp = sig_stack;
437 new_stack.ss_flags = 0;
438 new_stack.ss_size = SIG_STACK_SIZE;
439 if (sigaltstack(&new_stack, NULL) < 0) {
440 sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
441 ErrorAlert(str);
442 QuitEmulator();
443 }
444
445 // Install SIGILL handler for emulating privileged instructions and
446 // executing A-Trap and EMUL_OP opcodes
447 sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling
448 sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
449 sigaddset(&sigill_sa.sa_mask, SIGALRM);
450 sigill_sa.sa_handler = (void (*)(int))sigill_handler;
451 sigill_sa.sa_flags = SA_ONSTACK;
452 if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
453 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
454 ErrorAlert(str);
455 QuitEmulator();
456 }
457
458 // Install virtual 68k interrupt signal handler
459 sigemptyset(&sigirq_sa.sa_mask);
460 sigaddset(&sigirq_sa.sa_mask, SIGALRM);
461 sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
462 sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
463 if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
464 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
465 ErrorAlert(str);
466 QuitEmulator();
467 }
468 #endif
469
470 #ifdef ENABLE_MON
471 // Setup SIGINT handler to enter mon
472 sigemptyset(&sigint_sa.sa_mask);
473 sigint_sa.sa_handler = (void (*)(int))sigint_handler;
474 sigint_sa.sa_flags = 0;
475 sigaction(SIGINT, &sigint_sa, NULL);
476 #endif
477
478 #if defined(HAVE_PTHREADS)
479
480 // POSIX threads available, start 60Hz thread
481 pthread_attr_init(&tick_thread_attr);
482 #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
483 if (geteuid() == 0) {
484 pthread_attr_setinheritsched(&tick_thread_attr, PTHREAD_EXPLICIT_SCHED);
485 pthread_attr_setschedpolicy(&tick_thread_attr, SCHED_FIFO);
486 struct sched_param fifo_param;
487 fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2;
488 pthread_attr_setschedparam(&tick_thread_attr, &fifo_param);
489 }
490 #endif
491 tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
492 if (!tick_thread_active) {
493 sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
494 ErrorAlert(str);
495 QuitEmulator();
496 }
497 D(bug("60Hz thread started\n"));
498
499 #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
500
501 // POSIX.4 timers and real-time signals available, start 60Hz timer
502 sigemptyset(&timer_sa.sa_mask);
503 timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick;
504 timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
505 if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
506 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
507 ErrorAlert(str);
508 QuitEmulator();
509 }
510 struct sigevent timer_event;
511 timer_event.sigev_notify = SIGEV_SIGNAL;
512 timer_event.sigev_signo = SIG_TIMER;
513 if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
514 sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
515 ErrorAlert(str);
516 QuitEmulator();
517 }
518 struct itimerspec req;
519 req.it_value.tv_sec = 0;
520 req.it_value.tv_nsec = 16625000;
521 req.it_interval.tv_sec = 0;
522 req.it_interval.tv_nsec = 16625000;
523 if (timer_settime(timer, 0, &req, NULL) < 0) {
524 sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
525 ErrorAlert(str);
526 QuitEmulator();
527 }
528 D(bug("60Hz timer started\n"));
529
530 #else
531
532 // Start 60Hz timer
533 sigemptyset(&timer_sa.sa_mask); // Block virtual 68k interrupts during SIGARLM handling
534 sigaddset(&timer_sa.sa_mask, SIG_IRQ);
535 timer_sa.sa_handler = one_tick;
536 timer_sa.sa_flags = SA_ONSTACK | SA_RESTART;
537 if (sigaction(SIGALRM, &timer_sa, NULL) < 0) {
538 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno));
539 ErrorAlert(str);
540 QuitEmulator();
541 }
542 struct itimerval req;
543 req.it_interval.tv_sec = req.it_value.tv_sec = 0;
544 req.it_interval.tv_usec = req.it_value.tv_usec = 16625;
545 setitimer(ITIMER_REAL, &req, NULL);
546
547 #endif
548
549 #ifdef HAVE_PTHREADS
550 // Start XPRAM watchdog thread
551 memcpy(last_xpram, XPRAM, XPRAM_SIZE);
552 xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
553 D(bug("XPRAM thread started\n"));
554 #endif
555
556 // Start 68k and jump to ROM boot routine
557 D(bug("Starting emulation...\n"));
558 Start680x0();
559
560 QuitEmulator();
561 return 0;
562 }
563
564
565 /*
566 * Quit emulator
567 */
568
569 void QuitEmulator(void)
570 {
571 D(bug("QuitEmulator\n"));
572
573 #if EMULATED_68K
574 // Exit 680x0 emulation
575 Exit680x0();
576 #endif
577
578 #if defined(HAVE_PTHREADS)
579 // Stop 60Hz thread
580 if (tick_thread_active) {
581 tick_thread_cancel = true;
582 #ifdef HAVE_PTHREAD_CANCEL
583 pthread_cancel(tick_thread);
584 #endif
585 pthread_join(tick_thread, NULL);
586 }
587 #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
588 // Stop 60Hz timer
589 timer_delete(timer);
590 #else
591 struct itimerval req;
592 req.it_interval.tv_sec = req.it_value.tv_sec = 0;
593 req.it_interval.tv_usec = req.it_value.tv_usec = 0;
594 setitimer(ITIMER_REAL, &req, NULL);
595 #endif
596
597 #ifdef HAVE_PTHREADS
598 // Stop XPRAM watchdog thread
599 if (xpram_thread_active) {
600 xpram_thread_cancel = true;
601 #ifdef HAVE_PTHREAD_CANCEL
602 pthread_cancel(xpram_thread);
603 #endif
604 pthread_join(xpram_thread, NULL);
605 }
606 #endif
607
608 // Deinitialize everything
609 ExitAll();
610
611 // Free ROM/RAM areas
612 if (RAMBaseHost != VM_MAP_FAILED) {
613 vm_release(RAMBaseHost, RAMSize);
614 RAMBaseHost = NULL;
615 }
616 if (ROMBaseHost != VM_MAP_FAILED) {
617 vm_release(ROMBaseHost, 0x100000);
618 ROMBaseHost = NULL;
619 }
620
621 #if USE_SCRATCHMEM_SUBTERFUGE
622 // Delete scratch memory area
623 if (ScratchMem != (uint8 *)VM_MAP_FAILED) {
624 vm_release((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
625 ScratchMem = NULL;
626 }
627 #endif
628
629 #if REAL_ADDRESSING
630 // Delete Low Memory area
631 if (lm_area_mapped)
632 vm_release(0, 0x2000);
633 #endif
634
635 // Exit VM wrappers
636 vm_exit();
637
638 // Exit system routines
639 SysExit();
640
641 // Exit preferences
642 PrefsExit();
643
644 // Close X11 server connection
645 if (x_display)
646 XCloseDisplay(x_display);
647
648 exit(0);
649 }
650
651
652 /*
653 * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
654 * or a dynamically recompiling emulator)
655 */
656
657 void FlushCodeCache(void *start, uint32 size)
658 {
659 #if !EMULATED_68K && defined(__NetBSD__)
660 m68k_sync_icache(start, size);
661 #endif
662 }
663
664
665 /*
666 * SIGINT handler, enters mon
667 */
668
669 #ifdef ENABLE_MON
670 static void sigint_handler(...)
671 {
672 #if EMULATED_68K
673 uaecptr nextpc;
674 extern void m68k_dumpstate(uaecptr *nextpc);
675 m68k_dumpstate(&nextpc);
676 #endif
677 VideoQuitFullScreen();
678 char *arg[4] = {"mon", "-m", "-r", NULL};
679 mon(3, arg);
680 QuitEmulator();
681 }
682 #endif
683
684
685 /*
686 * Mutexes
687 */
688
689 #ifdef HAVE_PTHREADS
690
691 struct B2_mutex {
692 B2_mutex() { pthread_mutex_init(&m, NULL); }
693 ~B2_mutex() { pthread_mutex_unlock(&m); pthread_mutex_destroy(&m); }
694 pthread_mutex_t m;
695 };
696
697 B2_mutex *B2_create_mutex(void)
698 {
699 return new B2_mutex;
700 }
701
702 void B2_lock_mutex(B2_mutex *mutex)
703 {
704 pthread_mutex_lock(&mutex->m);
705 }
706
707 void B2_unlock_mutex(B2_mutex *mutex)
708 {
709 pthread_mutex_unlock(&mutex->m);
710 }
711
712 void B2_delete_mutex(B2_mutex *mutex)
713 {
714 delete mutex;
715 }
716
717 #else
718
719 struct B2_mutex {
720 int dummy;
721 };
722
723 B2_mutex *B2_create_mutex(void)
724 {
725 return new B2_mutex;
726 }
727
728 void B2_lock_mutex(B2_mutex *mutex)
729 {
730 }
731
732 void B2_unlock_mutex(B2_mutex *mutex)
733 {
734 }
735
736 void B2_delete_mutex(B2_mutex *mutex)
737 {
738 delete mutex;
739 }
740
741 #endif
742
743
744 /*
745 * Interrupt flags (must be handled atomically!)
746 */
747
748 uint32 InterruptFlags = 0;
749
750 #if EMULATED_68K
751 void SetInterruptFlag(uint32 flag)
752 {
753 LOCK_INTFLAGS;
754 InterruptFlags |= flag;
755 UNLOCK_INTFLAGS;
756 }
757
758 void ClearInterruptFlag(uint32 flag)
759 {
760 LOCK_INTFLAGS;
761 InterruptFlags &= ~flag;
762 UNLOCK_INTFLAGS;
763 }
764 #endif
765
766 #if !EMULATED_68K
767 void TriggerInterrupt(void)
768 {
769 #if defined(HAVE_PTHREADS)
770 pthread_kill(emul_thread, SIG_IRQ);
771 #else
772 raise(SIG_IRQ);
773 #endif
774 }
775
776 void TriggerNMI(void)
777 {
778 // not yet supported
779 }
780 #endif
781
782
783 /*
784 * XPRAM watchdog thread (saves XPRAM every minute)
785 */
786
787 static void xpram_watchdog(void)
788 {
789 if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
790 memcpy(last_xpram, XPRAM, XPRAM_SIZE);
791 SaveXPRAM();
792 }
793 }
794
795 #ifdef HAVE_PTHREADS
796 static void *xpram_func(void *arg)
797 {
798 while (!xpram_thread_cancel) {
799 for (int i=0; i<60 && !xpram_thread_cancel; i++)
800 Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true
801 xpram_watchdog();
802 }
803 return NULL;
804 }
805 #endif
806
807
808 /*
809 * 60Hz thread (really 60.15Hz)
810 */
811
812 static void one_second(void)
813 {
814 // Pseudo Mac 1Hz interrupt, update local time
815 WriteMacInt32(0x20c, TimerDateTime());
816
817 SetInterruptFlag(INTFLAG_1HZ);
818 TriggerInterrupt();
819
820 #ifndef HAVE_PTHREADS
821 static int second_counter = 0;
822 if (++second_counter > 60) {
823 second_counter = 0;
824 xpram_watchdog();
825 }
826 #endif
827 }
828
829 static void one_tick(...)
830 {
831 static int tick_counter = 0;
832 if (++tick_counter > 60) {
833 tick_counter = 0;
834 one_second();
835 }
836
837 #ifndef HAVE_PTHREADS
838 // No threads available, perform video refresh and networking from here
839 VideoRefresh();
840 SetInterruptFlag(INTFLAG_ETHER);
841 #endif
842
843 // Trigger 60Hz interrupt
844 if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
845 SetInterruptFlag(INTFLAG_60HZ);
846 TriggerInterrupt();
847 }
848 }
849
850 #ifdef HAVE_PTHREADS
851 static void *tick_func(void *arg)
852 {
853 uint64 start = GetTicks_usec();
854 int64 ticks = 0;
855 uint64 next = GetTicks_usec();
856 while (!tick_thread_cancel) {
857 one_tick();
858 next += 16625;
859 int64 delay = next - GetTicks_usec();
860 if (delay > 0)
861 Delay_usec(delay);
862 else if (delay < -16625)
863 next = GetTicks_usec();
864 ticks++;
865 }
866 uint64 end = GetTicks_usec();
867 D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
868 return NULL;
869 }
870 #endif
871
872
873 #if !EMULATED_68K
874 /*
875 * Virtual 68k interrupt handler
876 */
877
878 static void sigirq_handler(int sig, int code, struct sigcontext *scp)
879 {
880 // Interrupts disabled? Then do nothing
881 if (EmulatedSR & 0x0700)
882 return;
883
884 struct sigstate *state = (struct sigstate *)scp->sc_ap;
885 M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
886
887 // Set up interrupt frame on stack
888 uint32 a7 = regs->a[7];
889 a7 -= 2;
890 WriteMacInt16(a7, 0x64);
891 a7 -= 4;
892 WriteMacInt32(a7, scp->sc_pc);
893 a7 -= 2;
894 WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
895 scp->sc_sp = regs->a[7] = a7;
896
897 // Set interrupt level
898 EmulatedSR |= 0x2100;
899
900 // Jump to MacOS interrupt handler on return
901 scp->sc_pc = ReadMacInt32(0x64);
902 }
903
904
905 /*
906 * SIGILL handler, for emulation of privileged instructions and executing
907 * A-Trap and EMUL_OP opcodes
908 */
909
910 static void sigill_handler(int sig, int code, struct sigcontext *scp)
911 {
912 struct sigstate *state = (struct sigstate *)scp->sc_ap;
913 uint16 *pc = (uint16 *)scp->sc_pc;
914 uint16 opcode = *pc;
915 M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
916
917 #define INC_PC(n) scp->sc_pc += (n)
918
919 #define GET_SR (scp->sc_ps | EmulatedSR)
920
921 #define STORE_SR(v) \
922 scp->sc_ps = (v) & 0xff; \
923 EmulatedSR = (v) & 0xe700; \
924 if (((v) & 0x0700) == 0 && InterruptFlags) \
925 TriggerInterrupt();
926
927 //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
928
929 if ((opcode & 0xf000) == 0xa000) {
930
931 // A-Line instruction, set up A-Line trap frame on stack
932 uint32 a7 = regs->a[7];
933 a7 -= 2;
934 WriteMacInt16(a7, 0x28);
935 a7 -= 4;
936 WriteMacInt32(a7, (uint32)pc);
937 a7 -= 2;
938 WriteMacInt16(a7, GET_SR);
939 scp->sc_sp = regs->a[7] = a7;
940
941 // Jump to MacOS A-Line handler on return
942 scp->sc_pc = ReadMacInt32(0x28);
943
944 } else if ((opcode & 0xff00) == 0x7100) {
945
946 // Extended opcode, push registers on user stack
947 uint32 a7 = regs->a[7];
948 a7 -= 4;
949 WriteMacInt32(a7, (uint32)pc);
950 a7 -= 2;
951 WriteMacInt16(a7, scp->sc_ps);
952 for (int i=7; i>=0; i--) {
953 a7 -= 4;
954 WriteMacInt32(a7, regs->a[i]);
955 }
956 for (int i=7; i>=0; i--) {
957 a7 -= 4;
958 WriteMacInt32(a7, regs->d[i]);
959 }
960 scp->sc_sp = regs->a[7] = a7;
961
962 // Jump to EmulOp trampoline code on return
963 scp->sc_pc = (uint32)EmulOpTrampoline;
964
965 } else switch (opcode) { // Emulate privileged instructions
966
967 case 0x40e7: // move sr,-(sp)
968 regs->a[7] -= 2;
969 WriteMacInt16(regs->a[7], GET_SR);
970 scp->sc_sp = regs->a[7];
971 INC_PC(2);
972 break;
973
974 case 0x46df: { // move (sp)+,sr
975 uint16 sr = ReadMacInt16(regs->a[7]);
976 STORE_SR(sr);
977 regs->a[7] += 2;
978 scp->sc_sp = regs->a[7];
979 INC_PC(2);
980 break;
981 }
982
983 case 0x007c: { // ori #xxxx,sr
984 uint16 sr = GET_SR | pc[1];
985 scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
986 EmulatedSR = sr & 0xe700;
987 INC_PC(4);
988 break;
989 }
990
991 case 0x027c: { // andi #xxxx,sr
992 uint16 sr = GET_SR & pc[1];
993 STORE_SR(sr);
994 INC_PC(4);
995 break;
996 }
997
998 case 0x46fc: // move #xxxx,sr
999 STORE_SR(pc[1]);
1000 INC_PC(4);
1001 break;
1002
1003 case 0x46ef: { // move (xxxx,sp),sr
1004 uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
1005 STORE_SR(sr);
1006 INC_PC(4);
1007 break;
1008 }
1009
1010 case 0x46d8: // move (a0)+,sr
1011 case 0x46d9: { // move (a1)+,sr
1012 uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
1013 STORE_SR(sr);
1014 regs->a[opcode & 7] += 2;
1015 INC_PC(2);
1016 break;
1017 }
1018
1019 case 0x40f8: // move sr,xxxx.w
1020 WriteMacInt16(pc[1], GET_SR);
1021 INC_PC(4);
1022 break;
1023
1024 case 0x40d0: // move sr,(a0)
1025 case 0x40d1: // move sr,(a1)
1026 case 0x40d2: // move sr,(a2)
1027 case 0x40d3: // move sr,(a3)
1028 case 0x40d4: // move sr,(a4)
1029 case 0x40d5: // move sr,(a5)
1030 case 0x40d6: // move sr,(a6)
1031 case 0x40d7: // move sr,(sp)
1032 WriteMacInt16(regs->a[opcode & 7], GET_SR);
1033 INC_PC(2);
1034 break;
1035
1036 case 0x40c0: // move sr,d0
1037 case 0x40c1: // move sr,d1
1038 case 0x40c2: // move sr,d2
1039 case 0x40c3: // move sr,d3
1040 case 0x40c4: // move sr,d4
1041 case 0x40c5: // move sr,d5
1042 case 0x40c6: // move sr,d6
1043 case 0x40c7: // move sr,d7
1044 regs->d[opcode & 7] = GET_SR;
1045 INC_PC(2);
1046 break;
1047
1048 case 0x46c0: // move d0,sr
1049 case 0x46c1: // move d1,sr
1050 case 0x46c2: // move d2,sr
1051 case 0x46c3: // move d3,sr
1052 case 0x46c4: // move d4,sr
1053 case 0x46c5: // move d5,sr
1054 case 0x46c6: // move d6,sr
1055 case 0x46c7: { // move d7,sr
1056 uint16 sr = regs->d[opcode & 7];
1057 STORE_SR(sr);
1058 INC_PC(2);
1059 break;
1060 }
1061
1062 case 0xf327: // fsave -(sp)
1063 regs->a[7] -= 4;
1064 WriteMacInt32(regs->a[7], 0x41000000); // Idle frame
1065 scp->sc_sp = regs->a[7];
1066 INC_PC(2);
1067 break;
1068
1069 case 0xf35f: // frestore (sp)+
1070 regs->a[7] += 4;
1071 scp->sc_sp = regs->a[7];
1072 INC_PC(2);
1073 break;
1074
1075 case 0x4e73: { // rte
1076 uint32 a7 = regs->a[7];
1077 uint16 sr = ReadMacInt16(a7);
1078 a7 += 2;
1079 scp->sc_ps = sr & 0xff;
1080 EmulatedSR = sr & 0xe700;
1081 scp->sc_pc = ReadMacInt32(a7);
1082 a7 += 4;
1083 uint16 format = ReadMacInt16(a7) >> 12;
1084 a7 += 2;
1085 static const int frame_adj[16] = {
1086 0, 0, 4, 4, 8, 0, 0, 52, 50, 12, 24, 84, 16, 0, 0, 0
1087 };
1088 scp->sc_sp = regs->a[7] = a7 + frame_adj[format];
1089 break;
1090 }
1091
1092 case 0x4e7a: // movec cr,x
1093 switch (pc[1]) {
1094 case 0x0002: // movec cacr,d0
1095 regs->d[0] = 0x3111;
1096 break;
1097 case 0x1002: // movec cacr,d1
1098 regs->d[1] = 0x3111;
1099 break;
1100 case 0x0003: // movec tc,d0
1101 case 0x0004: // movec itt0,d0
1102 case 0x0005: // movec itt1,d0
1103 case 0x0006: // movec dtt0,d0
1104 case 0x0007: // movec dtt1,d0
1105 case 0x0806: // movec urp,d0
1106 case 0x0807: // movec srp,d0
1107 regs->d[0] = 0;
1108 break;
1109 case 0x1000: // movec sfc,d1
1110 case 0x1001: // movec dfc,d1
1111 case 0x1003: // movec tc,d1
1112 case 0x1801: // movec vbr,d1
1113 regs->d[1] = 0;
1114 break;
1115 case 0x8801: // movec vbr,a0
1116 regs->a[0] = 0;
1117 break;
1118 case 0x9801: // movec vbr,a1
1119 regs->a[1] = 0;
1120 break;
1121 default:
1122 goto ill;
1123 }
1124 INC_PC(4);
1125 break;
1126
1127 case 0x4e7b: // movec x,cr
1128 switch (pc[1]) {
1129 case 0x1000: // movec d1,sfc
1130 case 0x1001: // movec d1,dfc
1131 case 0x0801: // movec d0,vbr
1132 case 0x1801: // movec d1,vbr
1133 break;
1134 case 0x0002: // movec d0,cacr
1135 case 0x1002: // movec d1,cacr
1136 FlushCodeCache(NULL, 0);
1137 break;
1138 default:
1139 goto ill;
1140 }
1141 INC_PC(4);
1142 break;
1143
1144 case 0xf478: // cpusha dc
1145 case 0xf4f8: // cpusha dc/ic
1146 FlushCodeCache(NULL, 0);
1147 INC_PC(2);
1148 break;
1149
1150 default:
1151 ill: printf("SIGILL num %d, code %d\n", sig, code);
1152 printf(" context %p:\n", scp);
1153 printf(" onstack %08x\n", scp->sc_onstack);
1154 printf(" sp %08x\n", scp->sc_sp);
1155 printf(" fp %08x\n", scp->sc_fp);
1156 printf(" pc %08x\n", scp->sc_pc);
1157 printf(" opcode %04x\n", opcode);
1158 printf(" sr %08x\n", scp->sc_ps);
1159 printf(" state %p:\n", state);
1160 printf(" flags %d\n", state->ss_flags);
1161 for (int i=0; i<8; i++)
1162 printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]);
1163 for (int i=0; i<8; i++)
1164 printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1165
1166 VideoQuitFullScreen();
1167 #ifdef ENABLE_MON
1168 char *arg[4] = {"mon", "-m", "-r", NULL};
1169 mon(3, arg);
1170 #endif
1171 QuitEmulator();
1172 break;
1173 }
1174 }
1175 #endif
1176
1177
1178 /*
1179 * Display alert
1180 */
1181
1182 #ifdef ENABLE_GTK
1183 static void dl_destroyed(void)
1184 {
1185 gtk_main_quit();
1186 }
1187
1188 static void dl_quit(GtkWidget *dialog)
1189 {
1190 gtk_widget_destroy(dialog);
1191 }
1192
1193 void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1194 {
1195 char str[256];
1196 sprintf(str, GetString(prefix_id), text);
1197
1198 GtkWidget *dialog = gtk_dialog_new();
1199 gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1200 gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1201 gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1202 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1203
1204 GtkWidget *label = gtk_label_new(str);
1205 gtk_widget_show(label);
1206 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1207
1208 GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1209 gtk_widget_show(button);
1210 gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1211 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1212 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1213 gtk_widget_grab_default(button);
1214 gtk_widget_show(dialog);
1215
1216 gtk_main();
1217 }
1218 #endif
1219
1220
1221 /*
1222 * Display error alert
1223 */
1224
1225 void ErrorAlert(const char *text)
1226 {
1227 #ifdef ENABLE_GTK
1228 if (PrefsFindBool("nogui") || x_display == NULL) {
1229 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1230 return;
1231 }
1232 VideoQuitFullScreen();
1233 display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1234 #else
1235 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1236 #endif
1237 }
1238
1239
1240 /*
1241 * Display warning alert
1242 */
1243
1244 void WarningAlert(const char *text)
1245 {
1246 #ifdef ENABLE_GTK
1247 if (PrefsFindBool("nogui") || x_display == NULL) {
1248 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1249 return;
1250 }
1251 display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1252 #else
1253 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1254 #endif
1255 }
1256
1257
1258 /*
1259 * Display choice alert
1260 */
1261
1262 bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1263 {
1264 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1265 return false; //!!
1266 }