1 |
|
/* |
2 |
|
* main_unix.cpp - Startup code for Unix |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-1999 Christian Bauer |
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 |
22 |
|
|
23 |
|
#include <stdio.h> |
24 |
|
#include <stdlib.h> |
25 |
– |
#include <pthread.h> |
25 |
|
#include <signal.h> |
26 |
+ |
#include <errno.h> |
27 |
+ |
#include <X11/Xlib.h> |
28 |
+ |
|
29 |
+ |
#ifdef HAVE_PTHREADS |
30 |
+ |
# include <pthread.h> |
31 |
+ |
#endif |
32 |
+ |
|
33 |
+ |
#if 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" |
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 |
< |
#include <X11/Xlib.h> |
85 |
< |
|
86 |
< |
#if ENABLE_GTK |
87 |
< |
#include <gtk/gtk.h> |
49 |
< |
#endif |
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 |
|
|
51 |
– |
#if ENABLE_XF86_DGA |
52 |
– |
#include <X11/Xlib.h> |
53 |
– |
#include <X11/Xutil.h> |
54 |
– |
#include <X11/extensions/xf86dga.h> |
55 |
– |
#endif |
89 |
|
|
90 |
< |
#if ENABLE_MON |
91 |
< |
#include "mon.h" |
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 |
|
|
62 |
– |
// Constants |
63 |
– |
const char ROM_FILE_NAME[] = "ROM"; |
64 |
– |
|
65 |
– |
|
101 |
|
// CPU and FPU type, addressing mode |
102 |
|
int CPUType; |
103 |
|
bool CPUIs68060; |
106 |
|
|
107 |
|
|
108 |
|
// Global variables |
109 |
< |
static char *x_display_name = NULL; // X11 display name |
109 |
> |
char *x_display_name = NULL; // X11 display name |
110 |
|
Display *x_display = NULL; // X11 display handle |
111 |
|
|
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 |
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 |
+ |
#endif |
138 |
+ |
|
139 |
+ |
#if USE_SCRATCHMEM_SUBTERFUGE |
140 |
+ |
uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes |
141 |
+ |
#endif |
142 |
+ |
|
143 |
+ |
static struct sigaction timer_sa; // sigaction used for timer |
144 |
|
|
145 |
|
#if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS) |
146 |
|
#define SIG_TIMER SIGRTMIN |
147 |
< |
static struct sigaction timer_sa; // sigaction used for timer |
91 |
< |
static timer_t timer; // 60Hz timer |
147 |
> |
static timer_t timer; // 60Hz timer |
148 |
|
#endif |
149 |
|
|
150 |
< |
#if ENABLE_MON |
151 |
< |
static struct sigaction sigint_sa; // sigaction for SIGINT handler |
150 |
> |
#ifdef ENABLE_MON |
151 |
> |
static struct sigaction sigint_sa; // sigaction for SIGINT handler |
152 |
|
static void sigint_handler(...); |
153 |
|
#endif |
154 |
|
|
155 |
+ |
#if REAL_ADDRESSING |
156 |
+ |
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
157 |
+ |
#endif |
158 |
+ |
|
159 |
|
|
160 |
|
// Prototypes |
161 |
|
static void *xpram_func(void *arg); |
162 |
|
static void *tick_func(void *arg); |
163 |
|
static void one_tick(...); |
164 |
+ |
#if !EMULATED_68K |
165 |
+ |
static void sigirq_handler(int sig, int code, struct sigcontext *scp); |
166 |
+ |
static void sigill_handler(int sig, int code, struct sigcontext *scp); |
167 |
+ |
extern "C" void EmulOpTrampoline(void); |
168 |
+ |
#endif |
169 |
|
|
170 |
|
|
171 |
|
/* |
190 |
|
* Main program |
191 |
|
*/ |
192 |
|
|
193 |
+ |
static void usage(const char *prg_name) |
194 |
+ |
{ |
195 |
+ |
printf("Usage: %s [OPTION...]\n", prg_name); |
196 |
+ |
printf("\nUnix options:\n"); |
197 |
+ |
printf(" --display STRING\n X display to use\n"); |
198 |
+ |
printf(" --break ADDRESS\n set ROM breakpoint\n"); |
199 |
+ |
printf(" --rominfo\n dump ROM information\n"); |
200 |
+ |
PrefsPrintUsage(); |
201 |
+ |
exit(0); |
202 |
+ |
} |
203 |
+ |
|
204 |
|
int main(int argc, char **argv) |
205 |
|
{ |
206 |
+ |
char str[256]; |
207 |
+ |
|
208 |
|
// Initialize variables |
209 |
|
RAMBaseHost = NULL; |
210 |
|
ROMBaseHost = NULL; |
215 |
|
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
216 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
217 |
|
|
218 |
< |
// Parse arguments |
218 |
> |
#ifdef ENABLE_GTK |
219 |
> |
// Init GTK |
220 |
> |
gtk_set_locale(); |
221 |
> |
gtk_init(&argc, &argv); |
222 |
> |
x_display_name = gdk_get_display(); // gtk_init() handles and removes the "--display" argument |
223 |
> |
#endif |
224 |
> |
|
225 |
> |
// Read preferences |
226 |
> |
PrefsInit(argc, argv); |
227 |
> |
|
228 |
> |
// Parse command line arguments |
229 |
|
for (int i=1; i<argc; i++) { |
230 |
< |
if (strcmp(argv[i], "-display") == 0 && ++i < argc) |
231 |
< |
x_display_name = argv[i]; |
232 |
< |
else if (strcmp(argv[i], "-break") == 0 && ++i < argc) |
233 |
< |
ROMBreakpoint = strtol(argv[i], NULL, 0); |
234 |
< |
else if (strcmp(argv[i], "-rominfo") == 0) |
230 |
> |
if (strcmp(argv[i], "--help") == 0) { |
231 |
> |
usage(argv[0]); |
232 |
> |
} else if (strcmp(argv[i], "--display") == 0) { |
233 |
> |
i++; |
234 |
> |
if (i < argc) |
235 |
> |
x_display_name = strdup(argv[i]); |
236 |
> |
} else if (strcmp(argv[i], "--break") == 0) { |
237 |
> |
i++; |
238 |
> |
if (i < argc) |
239 |
> |
ROMBreakpoint = strtol(argv[i], NULL, 0); |
240 |
> |
} else if (strcmp(argv[i], "--rominfo") == 0) { |
241 |
|
PrintROMInfo = true; |
242 |
+ |
} else if (argv[i][0] == '-') { |
243 |
+ |
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
244 |
+ |
usage(argv[0]); |
245 |
+ |
} |
246 |
|
} |
247 |
|
|
248 |
|
// Open display |
254 |
|
QuitEmulator(); |
255 |
|
} |
256 |
|
|
257 |
< |
#if ENABLE_XF86_DGA |
257 |
> |
#if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON) |
258 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
259 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
260 |
|
#endif |
261 |
|
|
164 |
– |
#if ENABLE_GTK |
165 |
– |
// Init GTK |
166 |
– |
gtk_set_locale(); |
167 |
– |
gtk_init(&argc, &argv); |
168 |
– |
#endif |
169 |
– |
|
170 |
– |
// Read preferences |
171 |
– |
PrefsInit(); |
172 |
– |
|
262 |
|
// Init system routines |
263 |
|
SysInit(); |
264 |
|
|
267 |
|
if (!PrefsEditor()) |
268 |
|
QuitEmulator(); |
269 |
|
|
270 |
< |
// Create area for Mac RAM |
270 |
> |
// Read RAM size |
271 |
|
RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary |
272 |
|
if (RAMSize < 1024*1024) { |
273 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
274 |
|
RAMSize = 1024*1024; |
275 |
|
} |
187 |
– |
RAMBaseHost = new uint8[RAMSize]; |
276 |
|
|
277 |
< |
// Create area for Mac ROM |
278 |
< |
ROMBaseHost = new uint8[0x100000]; |
277 |
> |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
278 |
> |
RAMSize = RAMSize & -getpagesize(); // Round down to page boundary |
279 |
> |
#endif |
280 |
> |
|
281 |
> |
// Initialize VM system |
282 |
> |
vm_init(); |
283 |
> |
|
284 |
> |
#if REAL_ADDRESSING |
285 |
> |
// Flag: RAM and ROM are contigously allocated from address 0 |
286 |
> |
bool memory_mapped_from_zero = false; |
287 |
> |
|
288 |
> |
// Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash |
289 |
> |
// when trying to map a too big chunk of memory starting at address 0 |
290 |
> |
#if defined(OS_solaris) || defined(OS_netbsd) |
291 |
> |
const bool can_map_all_memory = false; |
292 |
> |
#else |
293 |
> |
const bool can_map_all_memory = true; |
294 |
> |
#endif |
295 |
> |
|
296 |
> |
// Try to allocate all memory from 0x0000, if it is not known to crash |
297 |
> |
if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) { |
298 |
> |
D(bug("Could allocate RAM and ROM from 0x0000\n")); |
299 |
> |
memory_mapped_from_zero = true; |
300 |
> |
} |
301 |
> |
|
302 |
> |
// Otherwise, just create the Low Memory area (0x0000..0x2000) |
303 |
> |
else if (vm_acquire_fixed(0, 0x2000) == 0) { |
304 |
> |
D(bug("Could allocate the Low Memory globals\n")); |
305 |
> |
lm_area_mapped = true; |
306 |
> |
} |
307 |
> |
|
308 |
> |
// Exit on failure |
309 |
> |
else { |
310 |
> |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
311 |
> |
ErrorAlert(str); |
312 |
> |
QuitEmulator(); |
313 |
> |
} |
314 |
> |
#endif |
315 |
> |
|
316 |
> |
#if USE_SCRATCHMEM_SUBTERFUGE |
317 |
> |
// Allocate scratch memory |
318 |
> |
ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE); |
319 |
> |
if (ScratchMem == VM_MAP_FAILED) { |
320 |
> |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
321 |
> |
QuitEmulator(); |
322 |
> |
} |
323 |
> |
ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block |
324 |
> |
#endif |
325 |
|
|
326 |
+ |
// Create areas for Mac RAM and ROM |
327 |
+ |
#if REAL_ADDRESSING |
328 |
+ |
if (memory_mapped_from_zero) { |
329 |
+ |
RAMBaseHost = (uint8 *)0; |
330 |
+ |
ROMBaseHost = RAMBaseHost + RAMSize; |
331 |
+ |
} |
332 |
+ |
else |
333 |
+ |
#endif |
334 |
+ |
{ |
335 |
+ |
RAMBaseHost = (uint8 *)vm_acquire(RAMSize); |
336 |
+ |
ROMBaseHost = (uint8 *)vm_acquire(0x100000); |
337 |
+ |
if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) { |
338 |
+ |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
339 |
+ |
QuitEmulator(); |
340 |
+ |
} |
341 |
+ |
} |
342 |
+ |
|
343 |
+ |
#if DIRECT_ADDRESSING |
344 |
+ |
// RAMBaseMac shall always be zero |
345 |
+ |
MEMBaseDiff = (uintptr)RAMBaseHost; |
346 |
+ |
RAMBaseMac = 0; |
347 |
+ |
ROMBaseMac = Host2MacAddr(ROMBaseHost); |
348 |
+ |
#endif |
349 |
+ |
#if REAL_ADDRESSING |
350 |
+ |
RAMBaseMac = (uint32)RAMBaseHost; |
351 |
+ |
ROMBaseMac = (uint32)ROMBaseHost; |
352 |
+ |
#endif |
353 |
+ |
D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac)); |
354 |
+ |
D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac)); |
355 |
+ |
|
356 |
|
// Get rom file path from preferences |
357 |
|
const char *rom_path = PrefsFindString("rom"); |
358 |
|
|
376 |
|
QuitEmulator(); |
377 |
|
} |
378 |
|
|
379 |
+ |
#if !EMULATED_68K |
380 |
+ |
// Get CPU model |
381 |
+ |
int mib[2] = {CTL_HW, HW_MODEL}; |
382 |
+ |
char *model; |
383 |
+ |
size_t model_len; |
384 |
+ |
sysctl(mib, 2, NULL, &model_len, NULL, 0); |
385 |
+ |
model = (char *)malloc(model_len); |
386 |
+ |
sysctl(mib, 2, model, &model_len, NULL, 0); |
387 |
+ |
D(bug("Model: %s\n", model)); |
388 |
+ |
|
389 |
+ |
// Set CPU and FPU type |
390 |
+ |
CPUIs68060 = false; |
391 |
+ |
if (strstr(model, "020")) |
392 |
+ |
CPUType = 2; |
393 |
+ |
else if (strstr(model, "030")) |
394 |
+ |
CPUType = 3; |
395 |
+ |
else if (strstr(model, "040")) |
396 |
+ |
CPUType = 4; |
397 |
+ |
else if (strstr(model, "060")) { |
398 |
+ |
CPUType = 4; |
399 |
+ |
CPUIs68060 = true; |
400 |
+ |
} else { |
401 |
+ |
printf("WARNING: Cannot detect CPU type, assuming 68020\n"); |
402 |
+ |
CPUType = 2; |
403 |
+ |
} |
404 |
+ |
FPUType = 1; // NetBSD has an FPU emulation, so the FPU ought to be available at all times |
405 |
+ |
TwentyFourBitAddressing = false; |
406 |
+ |
#endif |
407 |
+ |
|
408 |
|
// Initialize everything |
409 |
|
if (!InitAll()) |
410 |
|
QuitEmulator(); |
411 |
+ |
D(bug("Initialization complete\n")); |
412 |
|
|
413 |
< |
// Start XPRAM watchdog thread |
414 |
< |
xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0); |
413 |
> |
#ifdef HAVE_PTHREADS |
414 |
> |
// Get handle of main thread |
415 |
> |
emul_thread = pthread_self(); |
416 |
> |
#endif |
417 |
> |
|
418 |
> |
#if !EMULATED_68K |
419 |
> |
// (Virtual) supervisor mode, disable interrupts |
420 |
> |
EmulatedSR = 0x2700; |
421 |
> |
|
422 |
> |
// Create and install stack for signal handlers |
423 |
> |
sig_stack = malloc(SIG_STACK_SIZE); |
424 |
> |
D(bug("Signal stack at %p\n", sig_stack)); |
425 |
> |
if (sig_stack == NULL) { |
426 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
427 |
> |
QuitEmulator(); |
428 |
> |
} |
429 |
> |
stack_t new_stack; |
430 |
> |
new_stack.ss_sp = sig_stack; |
431 |
> |
new_stack.ss_flags = 0; |
432 |
> |
new_stack.ss_size = SIG_STACK_SIZE; |
433 |
> |
if (sigaltstack(&new_stack, NULL) < 0) { |
434 |
> |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
435 |
> |
ErrorAlert(str); |
436 |
> |
QuitEmulator(); |
437 |
> |
} |
438 |
> |
|
439 |
> |
// Install SIGILL handler for emulating privileged instructions and |
440 |
> |
// executing A-Trap and EMUL_OP opcodes |
441 |
> |
sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling |
442 |
> |
sigaddset(&sigill_sa.sa_mask, SIG_IRQ); |
443 |
> |
sigaddset(&sigill_sa.sa_mask, SIGALRM); |
444 |
> |
sigill_sa.sa_handler = (void (*)(int))sigill_handler; |
445 |
> |
sigill_sa.sa_flags = SA_ONSTACK; |
446 |
> |
if (sigaction(SIGILL, &sigill_sa, NULL) < 0) { |
447 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno)); |
448 |
> |
ErrorAlert(str); |
449 |
> |
QuitEmulator(); |
450 |
> |
} |
451 |
> |
|
452 |
> |
// Install virtual 68k interrupt signal handler |
453 |
> |
sigemptyset(&sigirq_sa.sa_mask); |
454 |
> |
sigaddset(&sigirq_sa.sa_mask, SIGALRM); |
455 |
> |
sigirq_sa.sa_handler = (void (*)(int))sigirq_handler; |
456 |
> |
sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART; |
457 |
> |
if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) { |
458 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno)); |
459 |
> |
ErrorAlert(str); |
460 |
> |
QuitEmulator(); |
461 |
> |
} |
462 |
> |
#endif |
463 |
> |
|
464 |
> |
#ifdef ENABLE_MON |
465 |
> |
// Setup SIGINT handler to enter mon |
466 |
> |
sigemptyset(&sigint_sa.sa_mask); |
467 |
> |
sigint_sa.sa_handler = (void (*)(int))sigint_handler; |
468 |
> |
sigint_sa.sa_flags = 0; |
469 |
> |
sigaction(SIGINT, &sigint_sa, NULL); |
470 |
> |
#endif |
471 |
|
|
472 |
|
#if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS) |
473 |
< |
// Start 60Hz timer |
473 |
> |
|
474 |
> |
// POSIX.4 timers and real-time signals available, start 60Hz timer |
475 |
|
sigemptyset(&timer_sa.sa_mask); |
476 |
+ |
timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick; |
477 |
|
timer_sa.sa_flags = SA_SIGINFO | SA_RESTART; |
226 |
– |
timer_sa.sa_sigaction = one_tick; |
478 |
|
if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) { |
479 |
< |
printf("FATAL: cannot set up timer signal handler\n"); |
479 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno)); |
480 |
> |
ErrorAlert(str); |
481 |
|
QuitEmulator(); |
482 |
|
} |
483 |
|
struct sigevent timer_event; |
484 |
|
timer_event.sigev_notify = SIGEV_SIGNAL; |
485 |
|
timer_event.sigev_signo = SIG_TIMER; |
486 |
|
if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) { |
487 |
< |
printf("FATAL: cannot create timer\n"); |
487 |
> |
sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno)); |
488 |
> |
ErrorAlert(str); |
489 |
|
QuitEmulator(); |
490 |
|
} |
491 |
|
struct itimerspec req; |
493 |
|
req.it_value.tv_nsec = 16625000; |
494 |
|
req.it_interval.tv_sec = 0; |
495 |
|
req.it_interval.tv_nsec = 16625000; |
496 |
< |
if (timer_settime(timer, TIMER_RELTIME, &req, NULL) < 0) { |
497 |
< |
printf("FATAL: cannot start timer\n"); |
496 |
> |
if (timer_settime(timer, 0, &req, NULL) < 0) { |
497 |
> |
sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno)); |
498 |
> |
ErrorAlert(str); |
499 |
|
QuitEmulator(); |
500 |
|
} |
501 |
+ |
D(bug("60Hz timer started\n")); |
502 |
|
|
503 |
< |
#else |
503 |
> |
#elif defined(HAVE_PTHREADS) |
504 |
|
|
505 |
< |
// Start 60Hz thread |
505 |
> |
// POSIX threads available, start 60Hz thread |
506 |
|
pthread_attr_init(&tick_thread_attr); |
507 |
|
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
508 |
|
if (geteuid() == 0) { |
515 |
|
#endif |
516 |
|
tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0); |
517 |
|
if (!tick_thread_active) { |
518 |
< |
printf("FATAL: cannot create tick thread\n"); |
518 |
> |
sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno)); |
519 |
> |
ErrorAlert(str); |
520 |
> |
QuitEmulator(); |
521 |
> |
} |
522 |
> |
D(bug("60Hz thread started\n")); |
523 |
> |
|
524 |
> |
#else |
525 |
> |
|
526 |
> |
// Start 60Hz timer |
527 |
> |
sigemptyset(&timer_sa.sa_mask); // Block virtual 68k interrupts during SIGARLM handling |
528 |
> |
sigaddset(&timer_sa.sa_mask, SIG_IRQ); |
529 |
> |
timer_sa.sa_handler = one_tick; |
530 |
> |
timer_sa.sa_flags = SA_ONSTACK | SA_RESTART; |
531 |
> |
if (sigaction(SIGALRM, &timer_sa, NULL) < 0) { |
532 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno)); |
533 |
> |
ErrorAlert(str); |
534 |
|
QuitEmulator(); |
535 |
|
} |
536 |
+ |
struct itimerval req; |
537 |
+ |
req.it_interval.tv_sec = req.it_value.tv_sec = 0; |
538 |
+ |
req.it_interval.tv_usec = req.it_value.tv_usec = 16625; |
539 |
+ |
setitimer(ITIMER_REAL, &req, NULL); |
540 |
+ |
|
541 |
|
#endif |
542 |
|
|
543 |
< |
#if ENABLE_MON |
544 |
< |
// Setup SIGINT handler to enter mon |
545 |
< |
sigemptyset(&sigint_sa.sa_mask); |
546 |
< |
sigint_sa.sa_flags = 0; |
547 |
< |
sigint_sa.sa_handler = sigint_handler; |
273 |
< |
sigaction(SIGINT, &sigint_sa, NULL); |
543 |
> |
#ifdef HAVE_PTHREADS |
544 |
> |
// Start XPRAM watchdog thread |
545 |
> |
memcpy(last_xpram, XPRAM, 256); |
546 |
> |
xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0); |
547 |
> |
D(bug("XPRAM thread started\n")); |
548 |
|
#endif |
549 |
|
|
550 |
|
// Start 68k and jump to ROM boot routine |
551 |
+ |
D(bug("Starting emulation...\n")); |
552 |
|
Start680x0(); |
553 |
|
|
554 |
|
QuitEmulator(); |
562 |
|
|
563 |
|
void QuitEmulator(void) |
564 |
|
{ |
565 |
+ |
D(bug("QuitEmulator\n")); |
566 |
+ |
|
567 |
+ |
#if EMULATED_68K |
568 |
|
// Exit 680x0 emulation |
569 |
|
Exit680x0(); |
570 |
+ |
#endif |
571 |
|
|
572 |
|
#if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS) |
573 |
|
// Stop 60Hz timer |
574 |
|
timer_delete(timer); |
575 |
< |
#else |
575 |
> |
#elif defined(HAVE_PTHREADS) |
576 |
|
// Stop 60Hz thread |
577 |
|
if (tick_thread_active) { |
578 |
|
tick_thread_cancel = true; |
581 |
|
#endif |
582 |
|
pthread_join(tick_thread, NULL); |
583 |
|
} |
584 |
+ |
#else |
585 |
+ |
struct itimerval req; |
586 |
+ |
req.it_interval.tv_sec = req.it_value.tv_sec = 0; |
587 |
+ |
req.it_interval.tv_usec = req.it_value.tv_usec = 0; |
588 |
+ |
setitimer(ITIMER_REAL, &req, NULL); |
589 |
|
#endif |
590 |
|
|
591 |
+ |
#ifdef HAVE_PTHREADS |
592 |
|
// Stop XPRAM watchdog thread |
593 |
|
if (xpram_thread_active) { |
594 |
|
xpram_thread_cancel = true; |
597 |
|
#endif |
598 |
|
pthread_join(xpram_thread, NULL); |
599 |
|
} |
600 |
+ |
#endif |
601 |
|
|
602 |
|
// Deinitialize everything |
603 |
|
ExitAll(); |
604 |
|
|
605 |
< |
// Delete ROM area |
606 |
< |
delete[] ROMBaseHost; |
605 |
> |
// Free ROM/RAM areas |
606 |
> |
if (RAMBaseHost != VM_MAP_FAILED) { |
607 |
> |
vm_release(RAMBaseHost, RAMSize); |
608 |
> |
RAMBaseHost = NULL; |
609 |
> |
} |
610 |
> |
if (ROMBaseHost != VM_MAP_FAILED) { |
611 |
> |
vm_release(ROMBaseHost, 0x100000); |
612 |
> |
ROMBaseHost = NULL; |
613 |
> |
} |
614 |
|
|
615 |
< |
// Delete RAM area |
616 |
< |
delete[] RAMBaseHost; |
615 |
> |
#if USE_SCRATCHMEM_SUBTERFUGE |
616 |
> |
// Delete scratch memory area |
617 |
> |
if (ScratchMem != (uint8 *)VM_MAP_FAILED) { |
618 |
> |
vm_release((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE); |
619 |
> |
ScratchMem = NULL; |
620 |
> |
} |
621 |
> |
#endif |
622 |
> |
|
623 |
> |
#if REAL_ADDRESSING |
624 |
> |
// Delete Low Memory area |
625 |
> |
if (lm_area_mapped) |
626 |
> |
vm_release(0, 0x2000); |
627 |
> |
#endif |
628 |
> |
|
629 |
> |
// Exit VM wrappers |
630 |
> |
vm_exit(); |
631 |
|
|
632 |
|
// Exit system routines |
633 |
|
SysExit(); |
648 |
|
* or a dynamically recompiling emulator) |
649 |
|
*/ |
650 |
|
|
344 |
– |
#if EMULATED_68K |
651 |
|
void FlushCodeCache(void *start, uint32 size) |
652 |
|
{ |
653 |
< |
} |
653 |
> |
#if !EMULATED_68K && defined(__NetBSD__) |
654 |
> |
m68k_sync_icache(start, size); |
655 |
|
#endif |
656 |
+ |
} |
657 |
|
|
658 |
|
|
659 |
|
/* |
660 |
|
* SIGINT handler, enters mon |
661 |
|
*/ |
662 |
|
|
663 |
< |
#if ENABLE_MON |
663 |
> |
#ifdef ENABLE_MON |
664 |
|
static void sigint_handler(...) |
665 |
|
{ |
666 |
< |
char *arg[2] = {"rmon", NULL}; |
667 |
< |
mon(1, arg); |
666 |
> |
#if EMULATED_68K |
667 |
> |
uaecptr nextpc; |
668 |
> |
extern void m68k_dumpstate(uaecptr *nextpc); |
669 |
> |
m68k_dumpstate(&nextpc); |
670 |
> |
#else |
671 |
> |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
672 |
> |
mon(3, arg); |
673 |
|
QuitEmulator(); |
674 |
+ |
#endif |
675 |
|
} |
676 |
|
#endif |
677 |
|
|
682 |
|
|
683 |
|
uint32 InterruptFlags = 0; |
684 |
|
|
685 |
+ |
#if EMULATED_68K |
686 |
|
void SetInterruptFlag(uint32 flag) |
687 |
|
{ |
688 |
+ |
#ifdef HAVE_PTHREADS |
689 |
|
pthread_mutex_lock(&intflag_lock); |
690 |
|
InterruptFlags |= flag; |
691 |
|
pthread_mutex_unlock(&intflag_lock); |
692 |
+ |
#else |
693 |
+ |
InterruptFlags |= flag; // Pray that this is an atomic operation... |
694 |
+ |
#endif |
695 |
|
} |
696 |
|
|
697 |
|
void ClearInterruptFlag(uint32 flag) |
698 |
|
{ |
699 |
+ |
#ifdef HAVE_PTHREADS |
700 |
|
pthread_mutex_lock(&intflag_lock); |
701 |
|
InterruptFlags &= ~flag; |
702 |
|
pthread_mutex_unlock(&intflag_lock); |
703 |
+ |
#else |
704 |
+ |
InterruptFlags &= ~flag; |
705 |
+ |
#endif |
706 |
+ |
} |
707 |
+ |
#endif |
708 |
+ |
|
709 |
+ |
#if !EMULATED_68K |
710 |
+ |
void TriggerInterrupt(void) |
711 |
+ |
{ |
712 |
+ |
#if defined(HAVE_PTHREADS) |
713 |
+ |
pthread_kill(emul_thread, SIG_IRQ); |
714 |
+ |
#else |
715 |
+ |
raise(SIG_IRQ); |
716 |
+ |
#endif |
717 |
+ |
} |
718 |
+ |
|
719 |
+ |
void TriggerNMI(void) |
720 |
+ |
{ |
721 |
+ |
// not yet supported |
722 |
|
} |
723 |
+ |
#endif |
724 |
+ |
|
725 |
+ |
|
726 |
+ |
/* |
727 |
+ |
* XPRAM watchdog thread (saves XPRAM every minute) |
728 |
+ |
*/ |
729 |
+ |
|
730 |
+ |
static void xpram_watchdog(void) |
731 |
+ |
{ |
732 |
+ |
if (memcmp(last_xpram, XPRAM, 256)) { |
733 |
+ |
memcpy(last_xpram, XPRAM, 256); |
734 |
+ |
SaveXPRAM(); |
735 |
+ |
} |
736 |
+ |
} |
737 |
+ |
|
738 |
+ |
#ifdef HAVE_PTHREADS |
739 |
+ |
static void *xpram_func(void *arg) |
740 |
+ |
{ |
741 |
+ |
while (!xpram_thread_cancel) { |
742 |
+ |
for (int i=0; i<60 && !xpram_thread_cancel; i++) |
743 |
+ |
Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true |
744 |
+ |
xpram_watchdog(); |
745 |
+ |
} |
746 |
+ |
return NULL; |
747 |
+ |
} |
748 |
+ |
#endif |
749 |
|
|
750 |
|
|
751 |
|
/* |
752 |
|
* 60Hz thread (really 60.15Hz) |
753 |
|
*/ |
754 |
|
|
755 |
+ |
static void one_second(void) |
756 |
+ |
{ |
757 |
+ |
// Pseudo Mac 1Hz interrupt, update local time |
758 |
+ |
WriteMacInt32(0x20c, TimerDateTime()); |
759 |
+ |
|
760 |
+ |
SetInterruptFlag(INTFLAG_1HZ); |
761 |
+ |
TriggerInterrupt(); |
762 |
+ |
|
763 |
+ |
#ifndef HAVE_PTHREADS |
764 |
+ |
static int second_counter = 0; |
765 |
+ |
if (++second_counter > 60) { |
766 |
+ |
second_counter = 0; |
767 |
+ |
xpram_watchdog(); |
768 |
+ |
} |
769 |
+ |
#endif |
770 |
+ |
} |
771 |
+ |
|
772 |
|
static void one_tick(...) |
773 |
|
{ |
774 |
|
static int tick_counter = 0; |
393 |
– |
|
394 |
– |
// Pseudo Mac 1Hz interrupt, update local time |
775 |
|
if (++tick_counter > 60) { |
776 |
|
tick_counter = 0; |
777 |
< |
WriteMacInt32(0x20c, TimerDateTime()); |
777 |
> |
one_second(); |
778 |
|
} |
779 |
|
|
780 |
+ |
#ifndef HAVE_PTHREADS |
781 |
+ |
// No threads available, perform video refresh from here |
782 |
+ |
VideoRefresh(); |
783 |
+ |
#endif |
784 |
+ |
|
785 |
|
// Trigger 60Hz interrupt |
786 |
|
if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) { |
787 |
|
SetInterruptFlag(INTFLAG_60HZ); |
789 |
|
} |
790 |
|
} |
791 |
|
|
792 |
+ |
#ifdef HAVE_PTHREADS |
793 |
|
static void *tick_func(void *arg) |
794 |
|
{ |
795 |
+ |
uint64 next = GetTicks_usec(); |
796 |
|
while (!tick_thread_cancel) { |
410 |
– |
|
411 |
– |
// Wait |
412 |
– |
#ifdef HAVE_NANOSLEEP |
413 |
– |
struct timespec req = {0, 16625000}; |
414 |
– |
nanosleep(&req, NULL); |
415 |
– |
#else |
416 |
– |
usleep(16625); |
417 |
– |
#endif |
418 |
– |
|
419 |
– |
// Action |
797 |
|
one_tick(); |
798 |
+ |
next += 16625; |
799 |
+ |
int64 delay = next - GetTicks_usec(); |
800 |
+ |
if (delay > 0) |
801 |
+ |
Delay_usec(delay); |
802 |
+ |
else if (delay < -16625) |
803 |
+ |
next = GetTicks_usec(); |
804 |
|
} |
805 |
|
return NULL; |
806 |
|
} |
807 |
+ |
#endif |
808 |
|
|
809 |
|
|
810 |
|
/* |
811 |
< |
* XPRAM watchdog thread (saves XPRAM every minute) |
811 |
> |
* Get current value of microsecond timer |
812 |
|
*/ |
813 |
|
|
814 |
< |
void *xpram_func(void *arg) |
814 |
> |
uint64 GetTicks_usec(void) |
815 |
|
{ |
816 |
< |
uint8 last_xpram[256]; |
817 |
< |
memcpy(last_xpram, XPRAM, 256); |
816 |
> |
#ifdef HAVE_CLOCK_GETTIME |
817 |
> |
struct timespec t; |
818 |
> |
clock_gettime(CLOCK_REALTIME, &t); |
819 |
> |
return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
820 |
> |
#else |
821 |
> |
struct timeval t; |
822 |
> |
gettimeofday(&t, NULL); |
823 |
> |
return (uint64)t.tv_sec * 1000000 + t.tv_usec; |
824 |
> |
#endif |
825 |
> |
} |
826 |
|
|
827 |
< |
while (!xpram_thread_cancel) { |
828 |
< |
for (int i=0; i<60 && !xpram_thread_cancel; i++) { |
829 |
< |
#ifdef HAVE_NANOSLEEP |
830 |
< |
struct timespec req = {1, 0}; |
831 |
< |
nanosleep(&req, NULL); |
827 |
> |
|
828 |
> |
/* |
829 |
> |
* Delay by specified number of microseconds (<1 second) |
830 |
> |
* (adapted from SDL_Delay() source; this function is designed to provide |
831 |
> |
* the highest accuracy possible) |
832 |
> |
*/ |
833 |
> |
|
834 |
> |
#if defined(linux) |
835 |
> |
// Linux select() changes its timeout parameter upon return to contain |
836 |
> |
// the remaining time. Most other unixen leave it unchanged or undefined. |
837 |
> |
#define SELECT_SETS_REMAINING |
838 |
> |
#elif defined(__FreeBSD__) || defined(__sun__) |
839 |
> |
#define USE_NANOSLEEP |
840 |
> |
#elif defined(HAVE_PTHREADS) && defined(sgi) |
841 |
> |
// SGI pthreads has a bug when using pthreads+signals+nanosleep, |
842 |
> |
// so instead of using nanosleep, wait on a CV which is never signalled. |
843 |
> |
#define USE_COND_TIMEDWAIT |
844 |
> |
#endif |
845 |
> |
|
846 |
> |
void Delay_usec(uint32 usec) |
847 |
> |
{ |
848 |
> |
int was_error; |
849 |
> |
|
850 |
> |
#if defined(USE_NANOSLEEP) |
851 |
> |
struct timespec elapsed, tv; |
852 |
> |
#elif defined(USE_COND_TIMEDWAIT) |
853 |
> |
// Use a local mutex and cv, so threads remain independent |
854 |
> |
pthread_cond_t delay_cond = PTHREAD_COND_INITIALIZER; |
855 |
> |
pthread_mutex_t delay_mutex = PTHREAD_MUTEX_INITIALIZER; |
856 |
> |
struct timespec elapsed; |
857 |
> |
uint64 future; |
858 |
> |
#else |
859 |
> |
struct timeval tv; |
860 |
> |
#ifndef SELECT_SETS_REMAINING |
861 |
> |
uint64 then, now, elapsed; |
862 |
> |
#endif |
863 |
> |
#endif |
864 |
> |
|
865 |
> |
// Set the timeout interval - Linux only needs to do this once |
866 |
> |
#if defined(SELECT_SETS_REMAINING) |
867 |
> |
tv.tv_sec = 0; |
868 |
> |
tv.tv_usec = usec; |
869 |
> |
#elif defined(USE_NANOSLEEP) |
870 |
> |
elapsed.tv_sec = 0; |
871 |
> |
elapsed.tv_nsec = usec * 1000; |
872 |
> |
#elif defined(USE_COND_TIMEDWAIT) |
873 |
> |
future = GetTicks_usec() + usec; |
874 |
> |
elapsed.tv_sec = future / 1000000; |
875 |
> |
elapsed.tv_nsec = (future % 1000000) * 1000; |
876 |
> |
#else |
877 |
> |
then = GetTicks_usec(); |
878 |
> |
#endif |
879 |
> |
|
880 |
> |
do { |
881 |
> |
errno = 0; |
882 |
> |
#if defined(USE_NANOSLEEP) |
883 |
> |
tv.tv_sec = elapsed.tv_sec; |
884 |
> |
tv.tv_nsec = elapsed.tv_nsec; |
885 |
> |
was_error = nanosleep(&tv, &elapsed); |
886 |
> |
#elif defined(USE_COND_TIMEDWAIT) |
887 |
> |
was_error = pthread_mutex_lock(&delay_mutex); |
888 |
> |
was_error = pthread_cond_timedwait(&delay_cond, &delay_mutex, &elapsed); |
889 |
> |
was_error = pthread_mutex_unlock(&delay_mutex); |
890 |
|
#else |
891 |
< |
usleep(1000000); |
891 |
> |
#ifndef SELECT_SETS_REMAINING |
892 |
> |
// Calculate the time interval left (in case of interrupt) |
893 |
> |
now = GetTicks_usec(); |
894 |
> |
elapsed = now - then; |
895 |
> |
then = now; |
896 |
> |
if (elapsed >= usec) |
897 |
> |
break; |
898 |
> |
usec -= elapsed; |
899 |
> |
tv.tv_sec = 0; |
900 |
> |
tv.tv_usec = usec; |
901 |
> |
#endif |
902 |
> |
was_error = select(0, NULL, NULL, NULL, &tv); |
903 |
|
#endif |
904 |
+ |
} while (was_error && (errno == EINTR)); |
905 |
+ |
} |
906 |
+ |
|
907 |
+ |
|
908 |
+ |
#if !EMULATED_68K |
909 |
+ |
/* |
910 |
+ |
* Virtual 68k interrupt handler |
911 |
+ |
*/ |
912 |
+ |
|
913 |
+ |
static void sigirq_handler(int sig, int code, struct sigcontext *scp) |
914 |
+ |
{ |
915 |
+ |
// Interrupts disabled? Then do nothing |
916 |
+ |
if (EmulatedSR & 0x0700) |
917 |
+ |
return; |
918 |
+ |
|
919 |
+ |
struct sigstate *state = (struct sigstate *)scp->sc_ap; |
920 |
+ |
M68kRegisters *regs = (M68kRegisters *)&state->ss_frame; |
921 |
+ |
|
922 |
+ |
// Set up interrupt frame on stack |
923 |
+ |
uint32 a7 = regs->a[7]; |
924 |
+ |
a7 -= 2; |
925 |
+ |
WriteMacInt16(a7, 0x64); |
926 |
+ |
a7 -= 4; |
927 |
+ |
WriteMacInt32(a7, scp->sc_pc); |
928 |
+ |
a7 -= 2; |
929 |
+ |
WriteMacInt16(a7, scp->sc_ps | EmulatedSR); |
930 |
+ |
scp->sc_sp = regs->a[7] = a7; |
931 |
+ |
|
932 |
+ |
// Set interrupt level |
933 |
+ |
EmulatedSR |= 0x2100; |
934 |
+ |
|
935 |
+ |
// Jump to MacOS interrupt handler on return |
936 |
+ |
scp->sc_pc = ReadMacInt32(0x64); |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
|
940 |
+ |
/* |
941 |
+ |
* SIGILL handler, for emulation of privileged instructions and executing |
942 |
+ |
* A-Trap and EMUL_OP opcodes |
943 |
+ |
*/ |
944 |
+ |
|
945 |
+ |
static void sigill_handler(int sig, int code, struct sigcontext *scp) |
946 |
+ |
{ |
947 |
+ |
struct sigstate *state = (struct sigstate *)scp->sc_ap; |
948 |
+ |
uint16 *pc = (uint16 *)scp->sc_pc; |
949 |
+ |
uint16 opcode = *pc; |
950 |
+ |
M68kRegisters *regs = (M68kRegisters *)&state->ss_frame; |
951 |
+ |
|
952 |
+ |
#define INC_PC(n) scp->sc_pc += (n) |
953 |
+ |
|
954 |
+ |
#define GET_SR (scp->sc_ps | EmulatedSR) |
955 |
+ |
|
956 |
+ |
#define STORE_SR(v) \ |
957 |
+ |
scp->sc_ps = (v) & 0xff; \ |
958 |
+ |
EmulatedSR = (v) & 0xe700; \ |
959 |
+ |
if (((v) & 0x0700) == 0 && InterruptFlags) \ |
960 |
+ |
TriggerInterrupt(); |
961 |
+ |
|
962 |
+ |
//printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR); |
963 |
+ |
|
964 |
+ |
if ((opcode & 0xf000) == 0xa000) { |
965 |
+ |
|
966 |
+ |
// A-Line instruction, set up A-Line trap frame on stack |
967 |
+ |
uint32 a7 = regs->a[7]; |
968 |
+ |
a7 -= 2; |
969 |
+ |
WriteMacInt16(a7, 0x28); |
970 |
+ |
a7 -= 4; |
971 |
+ |
WriteMacInt32(a7, (uint32)pc); |
972 |
+ |
a7 -= 2; |
973 |
+ |
WriteMacInt16(a7, GET_SR); |
974 |
+ |
scp->sc_sp = regs->a[7] = a7; |
975 |
+ |
|
976 |
+ |
// Jump to MacOS A-Line handler on return |
977 |
+ |
scp->sc_pc = ReadMacInt32(0x28); |
978 |
+ |
|
979 |
+ |
} else if ((opcode & 0xff00) == 0x7100) { |
980 |
+ |
|
981 |
+ |
// Extended opcode, push registers on user stack |
982 |
+ |
uint32 a7 = regs->a[7]; |
983 |
+ |
a7 -= 4; |
984 |
+ |
WriteMacInt32(a7, (uint32)pc); |
985 |
+ |
a7 -= 2; |
986 |
+ |
WriteMacInt16(a7, scp->sc_ps); |
987 |
+ |
for (int i=7; i>=0; i--) { |
988 |
+ |
a7 -= 4; |
989 |
+ |
WriteMacInt32(a7, regs->a[i]); |
990 |
+ |
} |
991 |
+ |
for (int i=7; i>=0; i--) { |
992 |
+ |
a7 -= 4; |
993 |
+ |
WriteMacInt32(a7, regs->d[i]); |
994 |
+ |
} |
995 |
+ |
scp->sc_sp = regs->a[7] = a7; |
996 |
+ |
|
997 |
+ |
// Jump to EmulOp trampoline code on return |
998 |
+ |
scp->sc_pc = (uint32)EmulOpTrampoline; |
999 |
+ |
|
1000 |
+ |
} else switch (opcode) { // Emulate privileged instructions |
1001 |
+ |
|
1002 |
+ |
case 0x40e7: // move sr,-(sp) |
1003 |
+ |
regs->a[7] -= 2; |
1004 |
+ |
WriteMacInt16(regs->a[7], GET_SR); |
1005 |
+ |
scp->sc_sp = regs->a[7]; |
1006 |
+ |
INC_PC(2); |
1007 |
+ |
break; |
1008 |
+ |
|
1009 |
+ |
case 0x46df: { // move (sp)+,sr |
1010 |
+ |
uint16 sr = ReadMacInt16(regs->a[7]); |
1011 |
+ |
STORE_SR(sr); |
1012 |
+ |
regs->a[7] += 2; |
1013 |
+ |
scp->sc_sp = regs->a[7]; |
1014 |
+ |
INC_PC(2); |
1015 |
+ |
break; |
1016 |
|
} |
1017 |
< |
if (memcmp(last_xpram, XPRAM, 256)) { |
1018 |
< |
memcpy(last_xpram, XPRAM, 256); |
1019 |
< |
SaveXPRAM(); |
1017 |
> |
|
1018 |
> |
case 0x007c: { // ori #xxxx,sr |
1019 |
> |
uint16 sr = GET_SR | pc[1]; |
1020 |
> |
scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR |
1021 |
> |
EmulatedSR = sr & 0xe700; |
1022 |
> |
INC_PC(4); |
1023 |
> |
break; |
1024 |
> |
} |
1025 |
> |
|
1026 |
> |
case 0x027c: { // andi #xxxx,sr |
1027 |
> |
uint16 sr = GET_SR & pc[1]; |
1028 |
> |
STORE_SR(sr); |
1029 |
> |
INC_PC(4); |
1030 |
> |
break; |
1031 |
> |
} |
1032 |
> |
|
1033 |
> |
case 0x46fc: // move #xxxx,sr |
1034 |
> |
STORE_SR(pc[1]); |
1035 |
> |
INC_PC(4); |
1036 |
> |
break; |
1037 |
> |
|
1038 |
> |
case 0x46ef: { // move (xxxx,sp),sr |
1039 |
> |
uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]); |
1040 |
> |
STORE_SR(sr); |
1041 |
> |
INC_PC(4); |
1042 |
> |
break; |
1043 |
> |
} |
1044 |
> |
|
1045 |
> |
case 0x46d8: // move (a0)+,sr |
1046 |
> |
case 0x46d9: { // move (a1)+,sr |
1047 |
> |
uint16 sr = ReadMacInt16(regs->a[opcode & 7]); |
1048 |
> |
STORE_SR(sr); |
1049 |
> |
regs->a[opcode & 7] += 2; |
1050 |
> |
INC_PC(2); |
1051 |
> |
break; |
1052 |
> |
} |
1053 |
> |
|
1054 |
> |
case 0x40f8: // move sr,xxxx.w |
1055 |
> |
WriteMacInt16(pc[1], GET_SR); |
1056 |
> |
INC_PC(4); |
1057 |
> |
break; |
1058 |
> |
|
1059 |
> |
case 0x40d0: // move sr,(a0) |
1060 |
> |
case 0x40d1: // move sr,(a1) |
1061 |
> |
case 0x40d2: // move sr,(a2) |
1062 |
> |
case 0x40d3: // move sr,(a3) |
1063 |
> |
case 0x40d4: // move sr,(a4) |
1064 |
> |
case 0x40d5: // move sr,(a5) |
1065 |
> |
case 0x40d6: // move sr,(a6) |
1066 |
> |
case 0x40d7: // move sr,(sp) |
1067 |
> |
WriteMacInt16(regs->a[opcode & 7], GET_SR); |
1068 |
> |
INC_PC(2); |
1069 |
> |
break; |
1070 |
> |
|
1071 |
> |
case 0x40c0: // move sr,d0 |
1072 |
> |
case 0x40c1: // move sr,d1 |
1073 |
> |
case 0x40c2: // move sr,d2 |
1074 |
> |
case 0x40c3: // move sr,d3 |
1075 |
> |
case 0x40c4: // move sr,d4 |
1076 |
> |
case 0x40c5: // move sr,d5 |
1077 |
> |
case 0x40c6: // move sr,d6 |
1078 |
> |
case 0x40c7: // move sr,d7 |
1079 |
> |
regs->d[opcode & 7] = GET_SR; |
1080 |
> |
INC_PC(2); |
1081 |
> |
break; |
1082 |
> |
|
1083 |
> |
case 0x46c0: // move d0,sr |
1084 |
> |
case 0x46c1: // move d1,sr |
1085 |
> |
case 0x46c2: // move d2,sr |
1086 |
> |
case 0x46c3: // move d3,sr |
1087 |
> |
case 0x46c4: // move d4,sr |
1088 |
> |
case 0x46c5: // move d5,sr |
1089 |
> |
case 0x46c6: // move d6,sr |
1090 |
> |
case 0x46c7: { // move d7,sr |
1091 |
> |
uint16 sr = regs->d[opcode & 7]; |
1092 |
> |
STORE_SR(sr); |
1093 |
> |
INC_PC(2); |
1094 |
> |
break; |
1095 |
> |
} |
1096 |
> |
|
1097 |
> |
case 0xf327: // fsave -(sp) |
1098 |
> |
if (CPUIs68060) { |
1099 |
> |
regs->a[7] -= 4; |
1100 |
> |
WriteMacInt32(regs->a[7], 0x60000000); // Idle frame |
1101 |
> |
regs->a[7] -= 4; |
1102 |
> |
WriteMacInt32(regs->a[7], 0); |
1103 |
> |
regs->a[7] -= 4; |
1104 |
> |
WriteMacInt32(regs->a[7], 0); |
1105 |
> |
} else { |
1106 |
> |
regs->a[7] -= 4; |
1107 |
> |
WriteMacInt32(regs->a[7], 0x41000000); // Idle frame |
1108 |
> |
} |
1109 |
> |
scp->sc_sp = regs->a[7]; |
1110 |
> |
INC_PC(2); |
1111 |
> |
break; |
1112 |
> |
|
1113 |
> |
case 0xf35f: // frestore (sp)+ |
1114 |
> |
if (CPUIs68060) |
1115 |
> |
regs->a[7] += 12; |
1116 |
> |
else |
1117 |
> |
regs->a[7] += 4; |
1118 |
> |
scp->sc_sp = regs->a[7]; |
1119 |
> |
INC_PC(2); |
1120 |
> |
break; |
1121 |
> |
|
1122 |
> |
case 0x4e73: { // rte |
1123 |
> |
uint32 a7 = regs->a[7]; |
1124 |
> |
uint16 sr = ReadMacInt16(a7); |
1125 |
> |
a7 += 2; |
1126 |
> |
scp->sc_ps = sr & 0xff; |
1127 |
> |
EmulatedSR = sr & 0xe700; |
1128 |
> |
scp->sc_pc = ReadMacInt32(a7); |
1129 |
> |
a7 += 4; |
1130 |
> |
uint16 format = ReadMacInt16(a7) >> 12; |
1131 |
> |
a7 += 2; |
1132 |
> |
static const int frame_adj[16] = { |
1133 |
> |
0, 0, 4, 4, 8, 0, 0, 52, 50, 12, 24, 84, 16, 0, 0, 0 |
1134 |
> |
}; |
1135 |
> |
scp->sc_sp = regs->a[7] = a7 + frame_adj[format]; |
1136 |
> |
break; |
1137 |
|
} |
1138 |
+ |
|
1139 |
+ |
case 0x4e7a: // movec cr,x |
1140 |
+ |
switch (pc[1]) { |
1141 |
+ |
case 0x0002: // movec cacr,d0 |
1142 |
+ |
regs->d[0] = 0x3111; |
1143 |
+ |
break; |
1144 |
+ |
case 0x1002: // movec cacr,d1 |
1145 |
+ |
regs->d[1] = 0x3111; |
1146 |
+ |
break; |
1147 |
+ |
case 0x0003: // movec tc,d0 |
1148 |
+ |
case 0x0004: // movec itt0,d0 |
1149 |
+ |
case 0x0005: // movec itt1,d0 |
1150 |
+ |
case 0x0006: // movec dtt0,d0 |
1151 |
+ |
case 0x0007: // movec dtt1,d0 |
1152 |
+ |
case 0x0806: // movec urp,d0 |
1153 |
+ |
case 0x0807: // movec srp,d0 |
1154 |
+ |
regs->d[0] = 0; |
1155 |
+ |
break; |
1156 |
+ |
case 0x1000: // movec sfc,d1 |
1157 |
+ |
case 0x1001: // movec dfc,d1 |
1158 |
+ |
case 0x1003: // movec tc,d1 |
1159 |
+ |
case 0x1801: // movec vbr,d1 |
1160 |
+ |
regs->d[1] = 0; |
1161 |
+ |
break; |
1162 |
+ |
case 0x8801: // movec vbr,a0 |
1163 |
+ |
regs->a[0] = 0; |
1164 |
+ |
break; |
1165 |
+ |
case 0x9801: // movec vbr,a1 |
1166 |
+ |
regs->a[1] = 0; |
1167 |
+ |
break; |
1168 |
+ |
default: |
1169 |
+ |
goto ill; |
1170 |
+ |
} |
1171 |
+ |
INC_PC(4); |
1172 |
+ |
break; |
1173 |
+ |
|
1174 |
+ |
case 0x4e7b: // movec x,cr |
1175 |
+ |
switch (pc[1]) { |
1176 |
+ |
case 0x1000: // movec d1,sfc |
1177 |
+ |
case 0x1001: // movec d1,dfc |
1178 |
+ |
case 0x0801: // movec d0,vbr |
1179 |
+ |
case 0x1801: // movec d1,vbr |
1180 |
+ |
break; |
1181 |
+ |
case 0x0002: // movec d0,cacr |
1182 |
+ |
case 0x1002: // movec d1,cacr |
1183 |
+ |
FlushCodeCache(NULL, 0); |
1184 |
+ |
break; |
1185 |
+ |
default: |
1186 |
+ |
goto ill; |
1187 |
+ |
} |
1188 |
+ |
INC_PC(4); |
1189 |
+ |
break; |
1190 |
+ |
|
1191 |
+ |
case 0xf478: // cpusha dc |
1192 |
+ |
case 0xf4f8: // cpusha dc/ic |
1193 |
+ |
FlushCodeCache(NULL, 0); |
1194 |
+ |
INC_PC(2); |
1195 |
+ |
break; |
1196 |
+ |
|
1197 |
+ |
default: |
1198 |
+ |
ill: printf("SIGILL num %d, code %d\n", sig, code); |
1199 |
+ |
printf(" context %p:\n", scp); |
1200 |
+ |
printf(" onstack %08x\n", scp->sc_onstack); |
1201 |
+ |
printf(" sp %08x\n", scp->sc_sp); |
1202 |
+ |
printf(" fp %08x\n", scp->sc_fp); |
1203 |
+ |
printf(" pc %08x\n", scp->sc_pc); |
1204 |
+ |
printf(" opcode %04x\n", opcode); |
1205 |
+ |
printf(" sr %08x\n", scp->sc_ps); |
1206 |
+ |
printf(" state %p:\n", state); |
1207 |
+ |
printf(" flags %d\n", state->ss_flags); |
1208 |
+ |
for (int i=0; i<8; i++) |
1209 |
+ |
printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]); |
1210 |
+ |
for (int i=0; i<8; i++) |
1211 |
+ |
printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]); |
1212 |
+ |
|
1213 |
+ |
#ifdef ENABLE_MON |
1214 |
+ |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
1215 |
+ |
mon(3, arg); |
1216 |
+ |
#endif |
1217 |
+ |
QuitEmulator(); |
1218 |
+ |
break; |
1219 |
|
} |
449 |
– |
return NULL; |
1220 |
|
} |
1221 |
+ |
#endif |
1222 |
|
|
1223 |
|
|
1224 |
|
/* |
1225 |
|
* Display alert |
1226 |
|
*/ |
1227 |
|
|
1228 |
< |
#if ENABLE_GTK |
1228 |
> |
#ifdef ENABLE_GTK |
1229 |
|
static void dl_destroyed(void) |
1230 |
|
{ |
1231 |
|
gtk_main_quit(); |
1270 |
|
|
1271 |
|
void ErrorAlert(const char *text) |
1272 |
|
{ |
1273 |
< |
#if ENABLE_GTK |
1273 |
> |
#ifdef ENABLE_GTK |
1274 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
1275 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
1276 |
|
return; |
1289 |
|
|
1290 |
|
void WarningAlert(const char *text) |
1291 |
|
{ |
1292 |
< |
#if ENABLE_GTK |
1292 |
> |
#ifdef ENABLE_GTK |
1293 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
1294 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
1295 |
|
return; |