ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.13
Committed: 2000-07-13T16:12:32Z (24 years ago) by cebix
Branch: MAIN
CVS Tags: snapshot-13072000
Changes since 1.12: +2 -2 lines
Log Message:
- DGA and SHM are only tried on local X11 displays
- re-integrated old window update method (better performance over a networked
  display connection), frameskip=0 selects new method, other values select
  old method
- fixed compilation errors

File Contents

# Content
1 /*
2 * main_unix.cpp - Startup code for Unix
3 *
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
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 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"
65 #include "video.h"
66 #include "emul_op.h"
67 #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 #ifdef ENABLE_MON
75 # include "mon.h"
76 #endif
77
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
100 int CPUType;
101 bool CPUIs68060;
102 int FPUType;
103 bool TwentyFourBitAddressing;
104
105
106 // Global variables
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
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 #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 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
157 // Prototypes
158 static void *xpram_func(void *arg);
159 static void *tick_func(void *arg);
160 static void one_tick(...);
161 #if !EMULATED_68K
162 static void sigirq_handler(int sig, int code, struct sigcontext *scp);
163 static void sigill_handler(int sig, int code, struct sigcontext *scp);
164 extern "C" void EmulOpTrampoline(void);
165 #endif
166
167
168 /*
169 * Ersatz functions
170 */
171
172 extern "C" {
173
174 #ifndef HAVE_STRDUP
175 char *strdup(const char *s)
176 {
177 char *n = (char *)malloc(strlen(s) + 1);
178 strcpy(n, s);
179 return n;
180 }
181 #endif
182
183 }
184
185
186 /*
187 * Main program
188 */
189
190 int main(int argc, char **argv)
191 {
192 char str[256];
193
194 // Initialize variables
195 RAMBaseHost = NULL;
196 ROMBaseHost = NULL;
197 srand(time(NULL));
198 tzset();
199
200 // Print some info
201 printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
202 printf(" %s\n", GetString(STR_ABOUT_TEXT2));
203
204 // Parse arguments
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
215 x_display = XOpenDisplay(x_display_name);
216 if (x_display == NULL) {
217 char str[256];
218 sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
219 ErrorAlert(str);
220 QuitEmulator();
221 }
222
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 #ifdef ENABLE_GTK
229 // Init GTK
230 gtk_set_locale();
231 gtk_init(&argc, &argv);
232 #endif
233
234 // Read preferences
235 PrefsInit();
236
237 // Init system routines
238 SysInit();
239
240 // Show preferences editor
241 if (!PrefsFindBool("nogui"))
242 if (!PrefsEditor())
243 QuitEmulator();
244
245 // Open /dev/zero
246 zero_fd = open("/dev/zero", O_RDWR);
247 if (zero_fd < 0) {
248 sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
249 ErrorAlert(str);
250 QuitEmulator();
251 }
252
253 // Read RAM size
254 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
255 if (RAMSize < 1024*1024) {
256 WarningAlert(GetString(STR_SMALL_RAM_WARN));
257 RAMSize = 1024*1024;
258 }
259
260 #if REAL_ADDRESSING
261 // Create Low Memory area (0x0000..0x2000)
262 if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) == (void *)-1) {
263 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
264 ErrorAlert(str);
265 QuitEmulator();
266 }
267 lm_area_mapped = true;
268 #endif
269
270 #if !EMULATED_68K
271 // Allocate scratch memory
272 ScratchMem = (uint32)malloc(SCRATCH_MEM_SIZE);
273 if (ScratchMem == NULL) {
274 ErrorAlert(GetString(STR_NO_MEM_ERR));
275 QuitEmulator();
276 }
277 ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
278 #endif
279
280 // Create areas for Mac RAM and ROM
281 #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");
313
314 // Load Mac ROM
315 int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
316 if (rom_fd < 0) {
317 ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
318 QuitEmulator();
319 }
320 printf(GetString(STR_READING_ROM_FILE));
321 ROMSize = lseek(rom_fd, 0, SEEK_END);
322 if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
323 ErrorAlert(GetString(STR_ROM_SIZE_ERR));
324 close(rom_fd);
325 QuitEmulator();
326 }
327 lseek(rom_fd, 0, SEEK_SET);
328 if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
329 ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
330 close(rom_fd);
331 QuitEmulator();
332 }
333
334 #if !EMULATED_68K
335 // Get CPU model
336 int mib[2] = {CTL_HW, HW_MODEL};
337 char *model;
338 size_t model_len;
339 sysctl(mib, 2, NULL, &model_len, NULL, 0);
340 model = (char *)malloc(model_len);
341 sysctl(mib, 2, model, &model_len, NULL, 0);
342 D(bug("Model: %s\n", model));
343
344 // Set CPU and FPU type
345 CPUIs68060 = false;
346 if (strstr(model, "020"))
347 CPUType = 2;
348 else if (strstr(model, "030"))
349 CPUType = 3;
350 else if (strstr(model, "040"))
351 CPUType = 4;
352 else if (strstr(model, "060")) {
353 CPUType = 4;
354 CPUIs68060 = true;
355 } else {
356 printf("WARNING: Cannot detect CPU type, assuming 68020\n");
357 CPUType = 2;
358 }
359 FPUType = 0; //!!
360 TwentyFourBitAddressing = false;
361 #endif
362
363 // Initialize everything
364 if (!InitAll())
365 QuitEmulator();
366 D(bug("Initialization complete\n"));
367
368 #ifdef HAVE_PTHREADS
369 // Get handle of main thread
370 emul_thread = pthread_self();
371 #endif
372
373 #if !EMULATED_68K
374 // (Virtual) supervisor mode, disable interrupts
375 EmulatedSR = 0x2700;
376
377 // Create and install stack for signal handlers
378 sig_stack = malloc(SIG_STACK_SIZE);
379 D(bug("Signal stack at %p\n", sig_stack));
380 if (sig_stack == NULL) {
381 ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR));
382 QuitEmulator();
383 }
384 stack_t new_stack;
385 new_stack.ss_sp = sig_stack;
386 new_stack.ss_flags = 0;
387 new_stack.ss_size = SIG_STACK_SIZE;
388 if (sigaltstack(&new_stack, NULL) < 0) {
389 sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
390 ErrorAlert(str);
391 QuitEmulator();
392 }
393
394 // Install SIGILL handler for emulating privileged instructions and
395 // executing A-Trap and EMUL_OP opcodes
396 sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling
397 sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
398 sigaddset(&sigill_sa.sa_mask, SIGALRM);
399 sigill_sa.sa_handler = (void (*)(int))sigill_handler;
400 sigill_sa.sa_flags = SA_ONSTACK;
401 if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
402 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
403 ErrorAlert(str);
404 QuitEmulator();
405 }
406
407 // Install virtual 68k interrupt signal handler
408 sigemptyset(&sigirq_sa.sa_mask);
409 sigaddset(&sigirq_sa.sa_mask, SIGALRM);
410 sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
411 sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
412 if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
413 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
414 ErrorAlert(str);
415 QuitEmulator();
416 }
417 #endif
418
419 #ifdef ENABLE_MON
420 // Setup SIGINT handler to enter mon
421 sigemptyset(&sigint_sa.sa_mask);
422 sigint_sa.sa_handler = sigint_handler;
423 sigint_sa.sa_flags = 0;
424 sigaction(SIGINT, &sigint_sa, NULL);
425 #endif
426
427 #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
428
429 // POSIX.4 timers and real-time signals available, start 60Hz timer
430 sigemptyset(&timer_sa.sa_mask);
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 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 sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
443 ErrorAlert(str);
444 QuitEmulator();
445 }
446 struct itimerspec req;
447 req.it_value.tv_sec = 0;
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, 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 #elif defined(HAVE_PTHREADS)
459
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) {
464 pthread_attr_setinheritsched(&tick_thread_attr, PTHREAD_EXPLICIT_SCHED);
465 pthread_attr_setschedpolicy(&tick_thread_attr, SCHED_FIFO);
466 struct sched_param fifo_param;
467 fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2;
468 pthread_attr_setschedparam(&tick_thread_attr, &fifo_param);
469 }
470 #endif
471 tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
472 if (!tick_thread_active) {
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();
510 return 0;
511 }
512
513
514 /*
515 * Quit emulator
516 */
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 #elif defined(HAVE_PTHREADS)
531 // Stop 60Hz thread
532 if (tick_thread_active) {
533 tick_thread_cancel = true;
534 #ifdef HAVE_PTHREAD_CANCEL
535 pthread_cancel(tick_thread);
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;
550 #ifdef HAVE_PTHREAD_CANCEL
551 pthread_cancel(xpram_thread);
552 #endif
553 pthread_join(xpram_thread, NULL);
554 }
555 #endif
556
557 // Deinitialize everything
558 ExitAll();
559
560 // Delete ROM area
561 delete[] ROMBaseHost;
562
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
585 // Exit preferences
586 PrefsExit();
587
588 // Close X11 server connection
589 if (x_display)
590 XCloseDisplay(x_display);
591
592 exit(0);
593 }
594
595
596 /*
597 * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
598 * or a dynamically recompiling emulator)
599 */
600
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
628
629 /*
630 * Interrupt flags (must be handled atomically!)
631 */
632
633 uint32 InterruptFlags = 0;
634
635 #if EMULATED_68K
636 void SetInterruptFlag(uint32 flag)
637 {
638 #ifdef HAVE_PTHREADS
639 pthread_mutex_lock(&intflag_lock);
640 InterruptFlags |= flag;
641 pthread_mutex_unlock(&intflag_lock);
642 #else
643 InterruptFlags |= flag; // Pray that this is an atomic operation...
644 #endif
645 }
646
647 void ClearInterruptFlag(uint32 flag)
648 {
649 #ifdef HAVE_PTHREADS
650 pthread_mutex_lock(&intflag_lock);
651 InterruptFlags &= ~flag;
652 pthread_mutex_unlock(&intflag_lock);
653 #else
654 InterruptFlags &= ~flag;
655 #endif
656 }
657 #endif
658
659 #if !EMULATED_68K
660 void TriggerInterrupt(void)
661 {
662 #if defined(HAVE_PTHREADS)
663 pthread_kill(emul_thread, SIG_IRQ);
664 #else
665 raise(SIG_IRQ);
666 #endif
667 }
668 #endif
669
670
671 /*
672 * XPRAM watchdog thread (saves XPRAM every minute)
673 */
674
675 static void xpram_watchdog(void)
676 {
677 if (memcmp(last_xpram, XPRAM, 256)) {
678 memcpy(last_xpram, XPRAM, 256);
679 SaveXPRAM();
680 }
681 }
682
683 #ifdef HAVE_PTHREADS
684 static void *xpram_func(void *arg)
685 {
686 while (!xpram_thread_cancel) {
687 for (int i=0; i<60 && !xpram_thread_cancel; i++) {
688 #ifdef HAVE_NANOSLEEP
689 struct timespec req = {1, 0};
690 nanosleep(&req, NULL);
691 #else
692 usleep(1000000);
693 #endif
694 }
695 xpram_watchdog();
696 }
697 return NULL;
698 }
699 #endif
700
701
702 /*
703 * 60Hz thread (really 60.15Hz)
704 */
705
706 static void one_second(void)
707 {
708 // Pseudo Mac 1Hz interrupt, update local time
709 WriteMacInt32(0x20c, TimerDateTime());
710
711 #ifndef HAVE_PTHREADS
712 static int second_counter = 0;
713 if (++second_counter > 60) {
714 second_counter = 0;
715 xpram_watchdog();
716 }
717 #endif
718 }
719
720 static void one_tick(...)
721 {
722 static int tick_counter = 0;
723 if (++tick_counter > 60) {
724 tick_counter = 0;
725 one_second();
726 }
727
728 #ifndef HAVE_PTHREADS
729 // No threads available, perform video refresh from here
730 VideoRefresh();
731 #endif
732
733 // Trigger 60Hz interrupt
734 if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
735 SetInterruptFlag(INTFLAG_60HZ);
736 TriggerInterrupt();
737 }
738 }
739
740 #ifdef HAVE_PTHREADS
741 static void *tick_func(void *arg)
742 {
743 while (!tick_thread_cancel) {
744
745 // Wait
746 #ifdef HAVE_NANOSLEEP
747 struct timespec req = {0, 16625000};
748 nanosleep(&req, NULL);
749 #else
750 usleep(16625);
751 #endif
752
753 // Action
754 one_tick();
755 }
756 return NULL;
757 }
758 #endif
759
760
761 #if !EMULATED_68K
762 /*
763 * Virtual 68k interrupt handler
764 */
765
766 static void sigirq_handler(int sig, int code, struct sigcontext *scp)
767 {
768 // Interrupts disabled? Then do nothing
769 if (EmulatedSR & 0x0700)
770 return;
771
772 struct sigstate *state = (struct sigstate *)scp->sc_ap;
773 M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
774
775 // Set up interrupt frame on stack
776 uint32 a7 = regs->a[7];
777 a7 -= 2;
778 WriteMacInt16(a7, 0x64);
779 a7 -= 4;
780 WriteMacInt32(a7, scp->sc_pc);
781 a7 -= 2;
782 WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
783 scp->sc_sp = regs->a[7] = a7;
784
785 // Set interrupt level
786 EmulatedSR |= 0x2100;
787
788 // Jump to MacOS interrupt handler on return
789 scp->sc_pc = ReadMacInt32(0x64);
790 }
791
792
793 /*
794 * SIGILL handler, for emulation of privileged instructions and executing
795 * A-Trap and EMUL_OP opcodes
796 */
797
798 static void sigill_handler(int sig, int code, struct sigcontext *scp)
799 {
800 struct sigstate *state = (struct sigstate *)scp->sc_ap;
801 uint16 *pc = (uint16 *)scp->sc_pc;
802 uint16 opcode = *pc;
803 M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
804
805 #define INC_PC(n) scp->sc_pc += (n)
806
807 #define GET_SR (scp->sc_ps | EmulatedSR)
808
809 #define STORE_SR(v) \
810 scp->sc_ps = (v) & 0xff; \
811 EmulatedSR = (v) & 0x2700; \
812 if (((v) & 0x0700) == 0 && InterruptFlags) \
813 TriggerInterrupt();
814
815 //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
816
817 if ((opcode & 0xf000) == 0xa000) {
818
819 // A-Line instruction, set up A-Line trap frame on stack
820 uint32 a7 = regs->a[7];
821 a7 -= 2;
822 WriteMacInt16(a7, 0x28);
823 a7 -= 4;
824 WriteMacInt32(a7, (uint32)pc);
825 a7 -= 2;
826 WriteMacInt16(a7, GET_SR);
827 scp->sc_sp = regs->a[7] = a7;
828
829 // Jump to MacOS A-Line handler on return
830 scp->sc_pc = ReadMacInt32(0x28);
831
832 } else if ((opcode & 0xff00) == 0x7100) {
833
834 // Extended opcode, push registers on user stack
835 uint32 a7 = regs->a[7];
836 a7 -= 4;
837 WriteMacInt32(a7, (uint32)pc);
838 a7 -= 2;
839 WriteMacInt16(a7, scp->sc_ps);
840 for (int i=7; i>=0; i--) {
841 a7 -= 4;
842 WriteMacInt32(a7, regs->a[i]);
843 }
844 for (int i=7; i>=0; i--) {
845 a7 -= 4;
846 WriteMacInt32(a7, regs->d[i]);
847 }
848 scp->sc_sp = regs->a[7] = a7;
849
850 // Jump to EmulOp trampoline code on return
851 scp->sc_pc = (uint32)EmulOpTrampoline;
852
853 } else switch (opcode) { // Emulate privileged instructions
854
855 case 0x40e7: // move sr,-(sp)
856 regs->a[7] -= 2;
857 WriteMacInt16(regs->a[7], GET_SR);
858 scp->sc_sp = regs->a[7];
859 INC_PC(2);
860 break;
861
862 case 0x46df: { // move (sp)+,sr
863 uint16 sr = ReadMacInt16(regs->a[7]);
864 STORE_SR(sr);
865 regs->a[7] += 2;
866 scp->sc_sp = regs->a[7];
867 INC_PC(2);
868 break;
869 }
870
871 case 0x007c: { // ori #xxxx,sr
872 uint16 sr = GET_SR | pc[1];
873 scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
874 EmulatedSR = sr & 0x2700;
875 INC_PC(4);
876 break;
877 }
878
879 case 0x027c: { // andi #xxxx,sr
880 uint16 sr = GET_SR & pc[1];
881 STORE_SR(sr);
882 INC_PC(4);
883 break;
884 }
885
886 case 0x46fc: // move #xxxx,sr
887 STORE_SR(pc[1]);
888 INC_PC(4);
889 break;
890
891 case 0x46ef: { // move (xxxx,sp),sr
892 uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
893 STORE_SR(sr);
894 INC_PC(4);
895 break;
896 }
897
898 case 0x46d8: // move (a0)+,sr
899 case 0x46d9: { // move (a1)+,sr
900 uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
901 STORE_SR(sr);
902 regs->a[opcode & 7] += 2;
903 INC_PC(2);
904 break;
905 }
906
907 case 0x40f8: // move sr,xxxx.w
908 WriteMacInt16(pc[1], GET_SR);
909 INC_PC(4);
910 break;
911
912 case 0x40d0: // move sr,(a0)
913 case 0x40d1: // move sr,(a1)
914 case 0x40d2: // move sr,(a2)
915 case 0x40d3: // move sr,(a3)
916 case 0x40d4: // move sr,(a4)
917 case 0x40d5: // move sr,(a5)
918 case 0x40d6: // move sr,(a6)
919 case 0x40d7: // move sr,(sp)
920 WriteMacInt16(regs->a[opcode & 7], GET_SR);
921 INC_PC(2);
922 break;
923
924 case 0x40c0: // move sr,d0
925 case 0x40c1: // move sr,d1
926 case 0x40c2: // move sr,d2
927 case 0x40c3: // move sr,d3
928 case 0x40c4: // move sr,d4
929 case 0x40c5: // move sr,d5
930 case 0x40c6: // move sr,d6
931 case 0x40c7: // move sr,d7
932 regs->d[opcode & 7] = GET_SR;
933 INC_PC(2);
934 break;
935
936 case 0x46c0: // move d0,sr
937 case 0x46c1: // move d1,sr
938 case 0x46c2: // move d2,sr
939 case 0x46c3: // move d3,sr
940 case 0x46c4: // move d4,sr
941 case 0x46c5: // move d5,sr
942 case 0x46c6: // move d6,sr
943 case 0x46c7: { // move d7,sr
944 uint16 sr = regs->d[opcode & 7];
945 STORE_SR(sr);
946 INC_PC(2);
947 break;
948 }
949
950 case 0xf327: // fsave -(sp)
951 goto ill; //!!
952
953 case 0xf35f: // frestore (sp)+
954 goto ill; //!!
955
956 case 0x4e73: { // rte (only handles format 0)
957 uint32 a7 = regs->a[7];
958 uint16 sr = ReadMacInt16(a7);
959 a7 += 2;
960 scp->sc_ps = sr & 0xff;
961 EmulatedSR = sr & 0x2700;
962 scp->sc_pc = ReadMacInt32(a7);
963 a7 += 6;
964 scp->sc_sp = regs->a[7] = a7;
965 break;
966 }
967
968 case 0x4e7a: // movec cr,x
969 switch (pc[1]) {
970 case 0x8801: // movec vbr,a0
971 regs->a[0] = 0;
972 break;
973 case 0x9801: // movec vbr,a1
974 regs->a[1] = 0;
975 break;
976 case 0x0002: // movec cacr,d0
977 regs->d[0] = 0x3111;
978 break;
979 case 0x1002: // movec cacr,d1
980 regs->d[1] = 0x3111;
981 break;
982 case 0x0003: // movec tc,d0
983 regs->d[0] = 0;
984 break;
985 case 0x1003: // movec tc,d1
986 regs->d[1] = 0;
987 break;
988 default:
989 goto ill;
990 }
991 INC_PC(4);
992 break;
993
994 case 0x4e7b: // movec x,cr
995 switch (pc[1]) {
996 case 0x0801: // movec d0,vbr
997 break;
998 case 0x0002: // movec d0,cacr
999 case 0x1002: // movec d1,cacr
1000 FlushCodeCache(NULL, 0);
1001 break;
1002 default:
1003 goto ill;
1004 }
1005 INC_PC(4);
1006 break;
1007
1008 case 0xf478: // cpusha dc
1009 case 0xf4f8: // cpusha dc/ic
1010 FlushCodeCache(NULL, 0);
1011 INC_PC(2);
1012 break;
1013
1014 default:
1015 ill: printf("SIGILL num %d, code %d\n", sig, code);
1016 printf(" context %p:\n", scp);
1017 printf(" onstack %08x\n", scp->sc_onstack);
1018 printf(" sp %08x\n", scp->sc_sp);
1019 printf(" fp %08x\n", scp->sc_fp);
1020 printf(" pc %08x\n", scp->sc_pc);
1021 printf(" opcode %04x\n", opcode);
1022 printf(" sr %08x\n", scp->sc_ps);
1023 printf(" state %p:\n", state);
1024 printf(" flags %d\n", state->ss_flags);
1025 for (int i=0; i<8; i++)
1026 printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]);
1027 for (int i=0; i<8; i++)
1028 printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1029
1030 #ifdef ENABLE_MON
1031 char *arg[2] = {"rmon", NULL};
1032 mon(1, arg);
1033 #endif
1034 QuitEmulator();
1035 break;
1036 }
1037 }
1038 #endif
1039
1040
1041 /*
1042 * Display alert
1043 */
1044
1045 #ifdef ENABLE_GTK
1046 static void dl_destroyed(void)
1047 {
1048 gtk_main_quit();
1049 }
1050
1051 static void dl_quit(GtkWidget *dialog)
1052 {
1053 gtk_widget_destroy(dialog);
1054 }
1055
1056 void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1057 {
1058 char str[256];
1059 sprintf(str, GetString(prefix_id), text);
1060
1061 GtkWidget *dialog = gtk_dialog_new();
1062 gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1063 gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1064 gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1065 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1066
1067 GtkWidget *label = gtk_label_new(str);
1068 gtk_widget_show(label);
1069 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1070
1071 GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1072 gtk_widget_show(button);
1073 gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1074 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1075 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1076 gtk_widget_grab_default(button);
1077 gtk_widget_show(dialog);
1078
1079 gtk_main();
1080 }
1081 #endif
1082
1083
1084 /*
1085 * Display error alert
1086 */
1087
1088 void ErrorAlert(const char *text)
1089 {
1090 #ifdef ENABLE_GTK
1091 if (PrefsFindBool("nogui") || x_display == NULL) {
1092 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1093 return;
1094 }
1095 VideoQuitFullScreen();
1096 display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1097 #else
1098 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1099 #endif
1100 }
1101
1102
1103 /*
1104 * Display warning alert
1105 */
1106
1107 void WarningAlert(const char *text)
1108 {
1109 #ifdef ENABLE_GTK
1110 if (PrefsFindBool("nogui") || x_display == NULL) {
1111 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1112 return;
1113 }
1114 display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1115 #else
1116 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1117 #endif
1118 }
1119
1120
1121 /*
1122 * Display choice alert
1123 */
1124
1125 bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1126 {
1127 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1128 return false; //!!
1129 }