22 |
|
|
23 |
|
#include <stdio.h> |
24 |
|
#include <stdlib.h> |
25 |
– |
#include <pthread.h> |
25 |
|
#include <signal.h> |
26 |
+ |
#include <errno.h> |
27 |
+ |
#include <X11/Xlib.h> |
28 |
+ |
|
29 |
+ |
#ifdef HAVE_PTHREADS |
30 |
+ |
# include <pthread.h> |
31 |
+ |
#endif |
32 |
+ |
|
33 |
+ |
#if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING || 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 |
+ |
#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" |
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" |
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 |
< |
#include <X11/Xlib.h> |
83 |
< |
|
84 |
< |
#if ENABLE_GTK |
85 |
< |
#include <gtk/gtk.h> |
49 |
< |
#endif |
50 |
< |
|
51 |
< |
#if ENABLE_XF86_DGA |
52 |
< |
#include <X11/Xlib.h> |
53 |
< |
#include <X11/Xutil.h> |
54 |
< |
#include <X11/extensions/xf86dga.h> |
55 |
< |
#endif |
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 |
|
|
57 |
– |
#if ENABLE_MON |
58 |
– |
#include "mon.h" |
59 |
– |
#endif |
87 |
|
|
88 |
< |
#ifdef USE_MAPPED_MEMORY |
89 |
< |
#include <sys/mman.h> |
90 |
< |
extern char *address_space, *good_address_map; |
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 |
|
|
67 |
– |
// Constants |
68 |
– |
const char ROM_FILE_NAME[] = "ROM"; |
69 |
– |
|
70 |
– |
|
99 |
|
// CPU and FPU type, addressing mode |
100 |
|
int CPUType; |
101 |
|
bool CPUIs68060; |
104 |
|
|
105 |
|
|
106 |
|
// Global variables |
107 |
< |
static char *x_display_name = NULL; // X11 display name |
107 |
> |
char *x_display_name = NULL; // X11 display name |
108 |
|
Display *x_display = NULL; // X11 display handle |
109 |
|
|
110 |
+ |
static int zero_fd = -1; // FD of /dev/zero |
111 |
+ |
static uint8 last_xpram[256]; // Buffer for monitoring XPRAM changes |
112 |
+ |
|
113 |
+ |
#ifdef HAVE_PTHREADS |
114 |
+ |
static pthread_t emul_thread; // Handle of MacOS emulation thread (main thread) |
115 |
+ |
|
116 |
|
static bool xpram_thread_active = false; // Flag: XPRAM watchdog installed |
117 |
|
static volatile bool xpram_thread_cancel = false; // Flag: Cancel XPRAM thread |
118 |
|
static pthread_t xpram_thread; // XPRAM watchdog |
122 |
|
static pthread_t tick_thread; // 60Hz thread |
123 |
|
static pthread_attr_t tick_thread_attr; // 60Hz thread attributes |
124 |
|
|
125 |
+ |
#if EMULATED_68K |
126 |
|
static pthread_mutex_t intflag_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect InterruptFlags |
127 |
+ |
#endif |
128 |
+ |
#endif |
129 |
+ |
|
130 |
+ |
#if !EMULATED_68K |
131 |
+ |
#define SIG_IRQ SIGUSR1 |
132 |
+ |
static struct sigaction sigirq_sa; // Virtual 68k interrupt signal |
133 |
+ |
static struct sigaction sigill_sa; // Illegal instruction |
134 |
+ |
static void *sig_stack = NULL; // Stack for signal handlers |
135 |
+ |
uint16 EmulatedSR; // Emulated bits of SR (supervisor bit and interrupt mask) |
136 |
+ |
#endif |
137 |
+ |
|
138 |
+ |
#if USE_SCRATCHMEM_SUBTERFUGE |
139 |
+ |
uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes |
140 |
+ |
#endif |
141 |
+ |
|
142 |
+ |
static struct sigaction timer_sa; // sigaction used for timer |
143 |
|
|
144 |
|
#if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS) |
145 |
|
#define SIG_TIMER SIGRTMIN |
146 |
< |
static struct sigaction timer_sa; // sigaction used for timer |
96 |
< |
static timer_t timer; // 60Hz timer |
146 |
> |
static timer_t timer; // 60Hz timer |
147 |
|
#endif |
148 |
|
|
149 |
< |
#if ENABLE_MON |
150 |
< |
static struct sigaction sigint_sa; // sigaction for SIGINT handler |
149 |
> |
#ifdef ENABLE_MON |
150 |
> |
static struct sigaction sigint_sa; // sigaction for SIGINT handler |
151 |
|
static void sigint_handler(...); |
152 |
|
#endif |
153 |
|
|
154 |
+ |
#if REAL_ADDRESSING |
155 |
+ |
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped |
156 |
+ |
static bool memory_mapped_from_zero = false; // Flag: Could allocate RAM area from 0 |
157 |
+ |
#endif |
158 |
+ |
|
159 |
+ |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
160 |
+ |
static uint32 mapped_ram_rom_size; // Total size of mmap()ed RAM/ROM area |
161 |
+ |
#endif |
162 |
+ |
|
163 |
+ |
#ifdef USE_MAPPED_MEMORY |
164 |
+ |
extern char *address_space, *good_address_map; |
165 |
+ |
#endif |
166 |
+ |
|
167 |
|
|
168 |
|
// Prototypes |
169 |
|
static void *xpram_func(void *arg); |
170 |
|
static void *tick_func(void *arg); |
171 |
|
static void one_tick(...); |
172 |
+ |
#if !EMULATED_68K |
173 |
+ |
static void sigirq_handler(int sig, int code, struct sigcontext *scp); |
174 |
+ |
static void sigill_handler(int sig, int code, struct sigcontext *scp); |
175 |
+ |
extern "C" void EmulOpTrampoline(void); |
176 |
+ |
#endif |
177 |
|
|
178 |
|
|
179 |
|
/* |
200 |
|
|
201 |
|
int main(int argc, char **argv) |
202 |
|
{ |
203 |
+ |
char str[256]; |
204 |
+ |
|
205 |
|
// Initialize variables |
206 |
|
RAMBaseHost = NULL; |
207 |
|
ROMBaseHost = NULL; |
231 |
|
QuitEmulator(); |
232 |
|
} |
233 |
|
|
234 |
< |
#if ENABLE_XF86_DGA && !ENABLE_MON |
234 |
> |
#if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON) |
235 |
|
// Fork out, so we can return from fullscreen mode when things get ugly |
236 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
237 |
|
#endif |
238 |
|
|
239 |
< |
#if ENABLE_GTK |
239 |
> |
#ifdef ENABLE_GTK |
240 |
|
// Init GTK |
241 |
|
gtk_set_locale(); |
242 |
|
gtk_init(&argc, &argv); |
253 |
|
if (!PrefsEditor()) |
254 |
|
QuitEmulator(); |
255 |
|
|
256 |
+ |
// Open /dev/zero |
257 |
+ |
zero_fd = open("/dev/zero", O_RDWR); |
258 |
+ |
if (zero_fd < 0) { |
259 |
+ |
sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno)); |
260 |
+ |
ErrorAlert(str); |
261 |
+ |
QuitEmulator(); |
262 |
+ |
} |
263 |
+ |
|
264 |
|
// Read RAM size |
265 |
|
RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary |
266 |
|
if (RAMSize < 1024*1024) { |
268 |
|
RAMSize = 1024*1024; |
269 |
|
} |
270 |
|
|
271 |
+ |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
272 |
+ |
const uint32 page_size = getpagesize(); |
273 |
+ |
const uint32 page_mask = page_size - 1; |
274 |
+ |
const uint32 aligned_ram_size = (RAMSize + page_mask) & ~page_mask; |
275 |
+ |
mapped_ram_rom_size = aligned_ram_size + 0x100000; |
276 |
+ |
#endif |
277 |
+ |
|
278 |
+ |
#if REAL_ADDRESSING |
279 |
+ |
// Try to allocate the complete address space from zero |
280 |
+ |
// gb-- the Solaris manpage about mmap(2) states that using MAP_FIXED |
281 |
+ |
// implies undefined behaviour for further use of sbrk(), malloc(), etc. |
282 |
+ |
// cebix-- on NetBSD/m68k, this causes a segfault |
283 |
+ |
#if defined(OS_solaris) || defined(OS_netbsd) |
284 |
+ |
// Anyway, it doesn't work... |
285 |
+ |
if (0) { |
286 |
+ |
#else |
287 |
+ |
if (mmap((caddr_t)0x0000, mapped_ram_rom_size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) { |
288 |
+ |
#endif |
289 |
+ |
D(bug("Could allocate RAM and ROM from 0x0000\n")); |
290 |
+ |
memory_mapped_from_zero = true; |
291 |
+ |
} |
292 |
+ |
// Create Low Memory area (0x0000..0x2000) |
293 |
+ |
else if (mmap((char *)0x0000, 0x2000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, zero_fd, 0) != MAP_FAILED) { |
294 |
+ |
D(bug("Could allocate the Low Memory globals\n")); |
295 |
+ |
lm_area_mapped = true; |
296 |
+ |
} |
297 |
+ |
// Exit on error |
298 |
+ |
else { |
299 |
+ |
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
300 |
+ |
ErrorAlert(str); |
301 |
+ |
QuitEmulator(); |
302 |
+ |
} |
303 |
+ |
#endif |
304 |
+ |
|
305 |
+ |
#if USE_SCRATCHMEM_SUBTERFUGE |
306 |
+ |
// Allocate scratch memory |
307 |
+ |
ScratchMem = (uint8 *)malloc(SCRATCH_MEM_SIZE); |
308 |
+ |
if (ScratchMem == NULL) { |
309 |
+ |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
310 |
+ |
QuitEmulator(); |
311 |
+ |
} |
312 |
+ |
ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block |
313 |
+ |
#endif |
314 |
+ |
|
315 |
|
// Create areas for Mac RAM and ROM |
316 |
< |
#ifdef USE_MAPPED_MEMORY |
317 |
< |
int fd = open("/dev/zero", O_RDWR); |
318 |
< |
good_address_map = (char *)mmap(NULL, 1<<24, PROT_READ, MAP_PRIVATE, fd, 0); |
197 |
< |
address_space = (char *)mmap(NULL, 1<<24, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
316 |
> |
#if defined(USE_MAPPED_MEMORY) |
317 |
> |
good_address_map = (char *)mmap(NULL, 1<<24, PROT_READ, MAP_PRIVATE, zero_fd, 0); |
318 |
> |
address_space = (char *)mmap(NULL, 1<<24, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0); |
319 |
|
if ((int)address_space < 0 || (int)good_address_map < 0) { |
320 |
|
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
321 |
|
QuitEmulator(); |
322 |
|
} |
323 |
< |
RAMBaseHost = (uint8 *)mmap(address_space, RAMSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd, 0); |
324 |
< |
ROMBaseHost = (uint8 *)mmap(address_space + 0x00400000, 0x80000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, fd, 0); |
204 |
< |
close(fd); |
323 |
> |
RAMBaseHost = (uint8 *)mmap(address_space, RAMSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0); |
324 |
> |
ROMBaseHost = (uint8 *)mmap(address_space + 0x00400000, 0x80000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0); |
325 |
|
char *nam = tmpnam(NULL); |
326 |
|
int good_address_fd = open(nam, O_CREAT | O_RDWR, 0600); |
327 |
|
char buffer[4096]; |
332 |
|
mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); |
333 |
|
for (int i=0; i<0x80000; i+=4096) |
334 |
|
mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); |
335 |
+ |
#elif REAL_ADDRESSING || DIRECT_ADDRESSING |
336 |
+ |
// gb-- Overkill, needs to be cleaned up. Probably explode it for either |
337 |
+ |
// real or direct addressing mode. |
338 |
+ |
#if REAL_ADDRESSING |
339 |
+ |
if (memory_mapped_from_zero) { |
340 |
+ |
RAMBaseHost = (uint8 *)0; |
341 |
+ |
ROMBaseHost = RAMBaseHost + aligned_ram_size; |
342 |
+ |
} |
343 |
+ |
else |
344 |
+ |
#endif |
345 |
+ |
{ |
346 |
+ |
RAMBaseHost = (uint8 *)mmap(0, mapped_ram_rom_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0); |
347 |
+ |
if (RAMBaseHost == (uint8 *)MAP_FAILED) { |
348 |
+ |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
349 |
+ |
QuitEmulator(); |
350 |
+ |
} |
351 |
+ |
ROMBaseHost = RAMBaseHost + aligned_ram_size; |
352 |
+ |
} |
353 |
|
#else |
354 |
< |
RAMBaseHost = new uint8[RAMSize]; |
355 |
< |
ROMBaseHost = new uint8[0x100000]; |
354 |
> |
RAMBaseHost = (uint8 *)malloc(RAMSize); |
355 |
> |
ROMBaseHost = (uint8 *)malloc(0x100000); |
356 |
> |
if (RAMBaseHost == NULL || ROMBaseHost == NULL) { |
357 |
> |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
358 |
> |
QuitEmulator(); |
359 |
> |
} |
360 |
|
#endif |
361 |
|
|
362 |
+ |
#if DIRECT_ADDRESSING |
363 |
+ |
// Initialize MEMBaseDiff now so that Host2MacAddr in the Video module |
364 |
+ |
// will return correct results |
365 |
+ |
RAMBaseMac = 0; |
366 |
+ |
ROMBaseMac = RAMBaseMac + aligned_ram_size; |
367 |
+ |
InitMEMBaseDiff(RAMBaseHost, RAMBaseMac); |
368 |
+ |
#endif |
369 |
+ |
#if REAL_ADDRESSING // && !EMULATED_68K |
370 |
+ |
RAMBaseMac = (uint32)RAMBaseHost; |
371 |
+ |
ROMBaseMac = (uint32)ROMBaseHost; |
372 |
+ |
#endif |
373 |
+ |
D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac)); |
374 |
+ |
D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac)); |
375 |
+ |
|
376 |
|
// Get rom file path from preferences |
377 |
|
const char *rom_path = PrefsFindString("rom"); |
378 |
|
|
396 |
|
QuitEmulator(); |
397 |
|
} |
398 |
|
|
399 |
+ |
#if !EMULATED_68K |
400 |
+ |
// Get CPU model |
401 |
+ |
int mib[2] = {CTL_HW, HW_MODEL}; |
402 |
+ |
char *model; |
403 |
+ |
size_t model_len; |
404 |
+ |
sysctl(mib, 2, NULL, &model_len, NULL, 0); |
405 |
+ |
model = (char *)malloc(model_len); |
406 |
+ |
sysctl(mib, 2, model, &model_len, NULL, 0); |
407 |
+ |
D(bug("Model: %s\n", model)); |
408 |
+ |
|
409 |
+ |
// Set CPU and FPU type |
410 |
+ |
CPUIs68060 = false; |
411 |
+ |
if (strstr(model, "020")) |
412 |
+ |
CPUType = 2; |
413 |
+ |
else if (strstr(model, "030")) |
414 |
+ |
CPUType = 3; |
415 |
+ |
else if (strstr(model, "040")) |
416 |
+ |
CPUType = 4; |
417 |
+ |
else if (strstr(model, "060")) { |
418 |
+ |
CPUType = 4; |
419 |
+ |
CPUIs68060 = true; |
420 |
+ |
} else { |
421 |
+ |
printf("WARNING: Cannot detect CPU type, assuming 68020\n"); |
422 |
+ |
CPUType = 2; |
423 |
+ |
} |
424 |
+ |
FPUType = 0; //!! |
425 |
+ |
TwentyFourBitAddressing = false; |
426 |
+ |
#endif |
427 |
+ |
|
428 |
|
// Initialize everything |
429 |
|
if (!InitAll()) |
430 |
|
QuitEmulator(); |
431 |
+ |
D(bug("Initialization complete\n")); |
432 |
|
|
433 |
< |
// Start XPRAM watchdog thread |
434 |
< |
xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0); |
433 |
> |
#ifdef HAVE_PTHREADS |
434 |
> |
// Get handle of main thread |
435 |
> |
emul_thread = pthread_self(); |
436 |
> |
#endif |
437 |
> |
|
438 |
> |
#if !EMULATED_68K |
439 |
> |
// (Virtual) supervisor mode, disable interrupts |
440 |
> |
EmulatedSR = 0x2700; |
441 |
> |
|
442 |
> |
// Create and install stack for signal handlers |
443 |
> |
sig_stack = malloc(SIG_STACK_SIZE); |
444 |
> |
D(bug("Signal stack at %p\n", sig_stack)); |
445 |
> |
if (sig_stack == NULL) { |
446 |
> |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
447 |
> |
QuitEmulator(); |
448 |
> |
} |
449 |
> |
stack_t new_stack; |
450 |
> |
new_stack.ss_sp = sig_stack; |
451 |
> |
new_stack.ss_flags = 0; |
452 |
> |
new_stack.ss_size = SIG_STACK_SIZE; |
453 |
> |
if (sigaltstack(&new_stack, NULL) < 0) { |
454 |
> |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
455 |
> |
ErrorAlert(str); |
456 |
> |
QuitEmulator(); |
457 |
> |
} |
458 |
> |
|
459 |
> |
// Install SIGILL handler for emulating privileged instructions and |
460 |
> |
// executing A-Trap and EMUL_OP opcodes |
461 |
> |
sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling |
462 |
> |
sigaddset(&sigill_sa.sa_mask, SIG_IRQ); |
463 |
> |
sigaddset(&sigill_sa.sa_mask, SIGALRM); |
464 |
> |
sigill_sa.sa_handler = (void (*)(int))sigill_handler; |
465 |
> |
sigill_sa.sa_flags = SA_ONSTACK; |
466 |
> |
if (sigaction(SIGILL, &sigill_sa, NULL) < 0) { |
467 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno)); |
468 |
> |
ErrorAlert(str); |
469 |
> |
QuitEmulator(); |
470 |
> |
} |
471 |
> |
|
472 |
> |
// Install virtual 68k interrupt signal handler |
473 |
> |
sigemptyset(&sigirq_sa.sa_mask); |
474 |
> |
sigaddset(&sigirq_sa.sa_mask, SIGALRM); |
475 |
> |
sigirq_sa.sa_handler = (void (*)(int))sigirq_handler; |
476 |
> |
sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART; |
477 |
> |
if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) { |
478 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno)); |
479 |
> |
ErrorAlert(str); |
480 |
> |
QuitEmulator(); |
481 |
> |
} |
482 |
> |
#endif |
483 |
> |
|
484 |
> |
#ifdef ENABLE_MON |
485 |
> |
// Setup SIGINT handler to enter mon |
486 |
> |
sigemptyset(&sigint_sa.sa_mask); |
487 |
> |
sigint_sa.sa_handler = (void (*)(int))sigint_handler; |
488 |
> |
sigint_sa.sa_flags = 0; |
489 |
> |
sigaction(SIGINT, &sigint_sa, NULL); |
490 |
> |
#endif |
491 |
|
|
492 |
|
#if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS) |
493 |
< |
// Start 60Hz timer |
493 |
> |
|
494 |
> |
// POSIX.4 timers and real-time signals available, start 60Hz timer |
495 |
|
sigemptyset(&timer_sa.sa_mask); |
496 |
+ |
timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick; |
497 |
|
timer_sa.sa_flags = SA_SIGINFO | SA_RESTART; |
254 |
– |
timer_sa.sa_sigaction = one_tick; |
498 |
|
if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) { |
499 |
< |
printf("FATAL: cannot set up timer signal handler\n"); |
499 |
> |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno)); |
500 |
> |
ErrorAlert(str); |
501 |
|
QuitEmulator(); |
502 |
|
} |
503 |
|
struct sigevent timer_event; |
504 |
|
timer_event.sigev_notify = SIGEV_SIGNAL; |
505 |
|
timer_event.sigev_signo = SIG_TIMER; |
506 |
|
if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) { |
507 |
< |
printf("FATAL: cannot create timer\n"); |
507 |
> |
sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno)); |
508 |
> |
ErrorAlert(str); |
509 |
|
QuitEmulator(); |
510 |
|
} |
511 |
|
struct itimerspec req; |
514 |
|
req.it_interval.tv_sec = 0; |
515 |
|
req.it_interval.tv_nsec = 16625000; |
516 |
|
if (timer_settime(timer, 0, &req, NULL) < 0) { |
517 |
< |
printf("FATAL: cannot start timer\n"); |
517 |
> |
sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno)); |
518 |
> |
ErrorAlert(str); |
519 |
|
QuitEmulator(); |
520 |
|
} |
521 |
+ |
D(bug("60Hz timer started\n")); |
522 |
|
|
523 |
< |
#else |
523 |
> |
#elif defined(HAVE_PTHREADS) |
524 |
|
|
525 |
< |
// Start 60Hz thread |
525 |
> |
// POSIX threads available, start 60Hz thread |
526 |
|
pthread_attr_init(&tick_thread_attr); |
527 |
|
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
528 |
|
if (geteuid() == 0) { |
535 |
|
#endif |
536 |
|
tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0); |
537 |
|
if (!tick_thread_active) { |
538 |
< |
printf("FATAL: cannot create tick thread\n"); |
538 |
> |
sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno)); |
539 |
> |
ErrorAlert(str); |
540 |
|
QuitEmulator(); |
541 |
|
} |
542 |
+ |
D(bug("60Hz thread started\n")); |
543 |
+ |
|
544 |
+ |
#else |
545 |
+ |
|
546 |
+ |
// Start 60Hz timer |
547 |
+ |
sigemptyset(&timer_sa.sa_mask); // Block virtual 68k interrupts during SIGARLM handling |
548 |
+ |
sigaddset(&timer_sa.sa_mask, SIG_IRQ); |
549 |
+ |
timer_sa.sa_handler = one_tick; |
550 |
+ |
timer_sa.sa_flags = SA_ONSTACK | SA_RESTART; |
551 |
+ |
if (sigaction(SIGALRM, &timer_sa, NULL) < 0) { |
552 |
+ |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno)); |
553 |
+ |
ErrorAlert(str); |
554 |
+ |
QuitEmulator(); |
555 |
+ |
} |
556 |
+ |
struct itimerval req; |
557 |
+ |
req.it_interval.tv_sec = req.it_value.tv_sec = 0; |
558 |
+ |
req.it_interval.tv_usec = req.it_value.tv_usec = 16625; |
559 |
+ |
setitimer(ITIMER_REAL, &req, NULL); |
560 |
+ |
|
561 |
|
#endif |
562 |
|
|
563 |
< |
#if ENABLE_MON |
564 |
< |
// Setup SIGINT handler to enter mon |
565 |
< |
sigemptyset(&sigint_sa.sa_mask); |
566 |
< |
sigint_sa.sa_flags = 0; |
567 |
< |
sigint_sa.sa_handler = sigint_handler; |
301 |
< |
sigaction(SIGINT, &sigint_sa, NULL); |
563 |
> |
#ifdef HAVE_PTHREADS |
564 |
> |
// Start XPRAM watchdog thread |
565 |
> |
memcpy(last_xpram, XPRAM, 256); |
566 |
> |
xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0); |
567 |
> |
D(bug("XPRAM thread started\n")); |
568 |
|
#endif |
569 |
|
|
570 |
|
// Start 68k and jump to ROM boot routine |
571 |
+ |
D(bug("Starting emulation...\n")); |
572 |
|
Start680x0(); |
573 |
|
|
574 |
|
QuitEmulator(); |
582 |
|
|
583 |
|
void QuitEmulator(void) |
584 |
|
{ |
585 |
+ |
D(bug("QuitEmulator\n")); |
586 |
+ |
|
587 |
+ |
#if EMULATED_68K |
588 |
|
// Exit 680x0 emulation |
589 |
|
Exit680x0(); |
590 |
+ |
#endif |
591 |
|
|
592 |
|
#if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS) |
593 |
|
// Stop 60Hz timer |
594 |
|
timer_delete(timer); |
595 |
< |
#else |
595 |
> |
#elif defined(HAVE_PTHREADS) |
596 |
|
// Stop 60Hz thread |
597 |
|
if (tick_thread_active) { |
598 |
|
tick_thread_cancel = true; |
601 |
|
#endif |
602 |
|
pthread_join(tick_thread, NULL); |
603 |
|
} |
604 |
+ |
#else |
605 |
+ |
struct itimerval req; |
606 |
+ |
req.it_interval.tv_sec = req.it_value.tv_sec = 0; |
607 |
+ |
req.it_interval.tv_usec = req.it_value.tv_usec = 0; |
608 |
+ |
setitimer(ITIMER_REAL, &req, NULL); |
609 |
|
#endif |
610 |
|
|
611 |
+ |
#ifdef HAVE_PTHREADS |
612 |
|
// Stop XPRAM watchdog thread |
613 |
|
if (xpram_thread_active) { |
614 |
|
xpram_thread_cancel = true; |
617 |
|
#endif |
618 |
|
pthread_join(xpram_thread, NULL); |
619 |
|
} |
620 |
+ |
#endif |
621 |
|
|
622 |
|
// Deinitialize everything |
623 |
|
ExitAll(); |
624 |
|
|
625 |
< |
// Delete ROM area |
626 |
< |
delete[] ROMBaseHost; |
625 |
> |
// Free ROM/RAM areas |
626 |
> |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
627 |
> |
if (memory_mapped_from_zero) |
628 |
> |
munmap((caddr_t)0x0000, mapped_ram_rom_size); |
629 |
> |
else if (RAMBaseHost != (uint8 *)MAP_FAILED) { |
630 |
> |
munmap((caddr_t)RAMBaseHost, mapped_ram_rom_size); |
631 |
> |
RAMBaseHost = NULL; |
632 |
> |
} |
633 |
> |
#else |
634 |
> |
if (ROMBaseHost) { |
635 |
> |
free(ROMBaseHost); |
636 |
> |
ROMBaseHost = NULL; |
637 |
> |
} |
638 |
> |
if (RAMBaseHost) { |
639 |
> |
free(RAMBaseHost); |
640 |
> |
RAMBaseHost = NULL; |
641 |
> |
} |
642 |
> |
#endif |
643 |
> |
|
644 |
> |
#if USE_SCRATCHMEM_SUBTERFUGE |
645 |
> |
// Delete scratch memory area |
646 |
> |
if (ScratchMem) { |
647 |
> |
free((void *)(ScratchMem - SCRATCH_MEM_SIZE/2)); |
648 |
> |
ScratchMem = NULL; |
649 |
> |
} |
650 |
> |
#endif |
651 |
> |
|
652 |
> |
#if REAL_ADDRESSING |
653 |
> |
// Delete Low Memory area |
654 |
> |
if (lm_area_mapped) |
655 |
> |
munmap((char *)0x0000, 0x2000); |
656 |
> |
#endif |
657 |
|
|
658 |
< |
// Delete RAM area |
659 |
< |
delete[] RAMBaseHost; |
658 |
> |
// Close /dev/zero |
659 |
> |
if (zero_fd > 0) |
660 |
> |
close(zero_fd); |
661 |
|
|
662 |
|
// Exit system routines |
663 |
|
SysExit(); |
678 |
|
* or a dynamically recompiling emulator) |
679 |
|
*/ |
680 |
|
|
372 |
– |
#if EMULATED_68K |
681 |
|
void FlushCodeCache(void *start, uint32 size) |
682 |
|
{ |
683 |
< |
} |
683 |
> |
#if !EMULATED_68K && defined(__NetBSD__) |
684 |
> |
m68k_sync_icache(start, size); |
685 |
|
#endif |
686 |
+ |
} |
687 |
|
|
688 |
|
|
689 |
|
/* |
690 |
|
* SIGINT handler, enters mon |
691 |
|
*/ |
692 |
|
|
693 |
< |
#if ENABLE_MON |
384 |
< |
extern void m68k_dumpstate(uaecptr *nextpc); |
693 |
> |
#ifdef ENABLE_MON |
694 |
|
static void sigint_handler(...) |
695 |
|
{ |
696 |
+ |
#if EMULATED_68K |
697 |
|
uaecptr nextpc; |
698 |
+ |
extern void m68k_dumpstate(uaecptr *nextpc); |
699 |
|
m68k_dumpstate(&nextpc); |
700 |
< |
char *arg[2] = {"rmon", NULL}; |
701 |
< |
mon(1, arg); |
700 |
> |
#else |
701 |
> |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
702 |
> |
mon(3, arg); |
703 |
|
QuitEmulator(); |
704 |
+ |
#endif |
705 |
|
} |
706 |
|
#endif |
707 |
|
|
712 |
|
|
713 |
|
uint32 InterruptFlags = 0; |
714 |
|
|
715 |
+ |
#if EMULATED_68K |
716 |
|
void SetInterruptFlag(uint32 flag) |
717 |
|
{ |
718 |
+ |
#ifdef HAVE_PTHREADS |
719 |
|
pthread_mutex_lock(&intflag_lock); |
720 |
|
InterruptFlags |= flag; |
721 |
|
pthread_mutex_unlock(&intflag_lock); |
722 |
+ |
#else |
723 |
+ |
InterruptFlags |= flag; // Pray that this is an atomic operation... |
724 |
+ |
#endif |
725 |
|
} |
726 |
|
|
727 |
|
void ClearInterruptFlag(uint32 flag) |
728 |
|
{ |
729 |
+ |
#ifdef HAVE_PTHREADS |
730 |
|
pthread_mutex_lock(&intflag_lock); |
731 |
|
InterruptFlags &= ~flag; |
732 |
|
pthread_mutex_unlock(&intflag_lock); |
733 |
+ |
#else |
734 |
+ |
InterruptFlags &= ~flag; |
735 |
+ |
#endif |
736 |
+ |
} |
737 |
+ |
#endif |
738 |
+ |
|
739 |
+ |
#if !EMULATED_68K |
740 |
+ |
void TriggerInterrupt(void) |
741 |
+ |
{ |
742 |
+ |
#if defined(HAVE_PTHREADS) |
743 |
+ |
pthread_kill(emul_thread, SIG_IRQ); |
744 |
+ |
#else |
745 |
+ |
raise(SIG_IRQ); |
746 |
+ |
#endif |
747 |
+ |
} |
748 |
+ |
|
749 |
+ |
void TriggerNMI(void) |
750 |
+ |
{ |
751 |
+ |
// not yet supported |
752 |
+ |
} |
753 |
+ |
#endif |
754 |
+ |
|
755 |
+ |
|
756 |
+ |
/* |
757 |
+ |
* XPRAM watchdog thread (saves XPRAM every minute) |
758 |
+ |
*/ |
759 |
+ |
|
760 |
+ |
static void xpram_watchdog(void) |
761 |
+ |
{ |
762 |
+ |
if (memcmp(last_xpram, XPRAM, 256)) { |
763 |
+ |
memcpy(last_xpram, XPRAM, 256); |
764 |
+ |
SaveXPRAM(); |
765 |
+ |
} |
766 |
+ |
} |
767 |
+ |
|
768 |
+ |
#ifdef HAVE_PTHREADS |
769 |
+ |
static void *xpram_func(void *arg) |
770 |
+ |
{ |
771 |
+ |
while (!xpram_thread_cancel) { |
772 |
+ |
for (int i=0; i<60 && !xpram_thread_cancel; i++) |
773 |
+ |
Delay_usec(1000000); |
774 |
+ |
xpram_watchdog(); |
775 |
+ |
} |
776 |
+ |
return NULL; |
777 |
|
} |
778 |
+ |
#endif |
779 |
|
|
780 |
|
|
781 |
|
/* |
782 |
|
* 60Hz thread (really 60.15Hz) |
783 |
|
*/ |
784 |
|
|
785 |
+ |
static void one_second(void) |
786 |
+ |
{ |
787 |
+ |
// Pseudo Mac 1Hz interrupt, update local time |
788 |
+ |
WriteMacInt32(0x20c, TimerDateTime()); |
789 |
+ |
|
790 |
+ |
SetInterruptFlag(INTFLAG_1HZ); |
791 |
+ |
TriggerInterrupt(); |
792 |
+ |
|
793 |
+ |
#ifndef HAVE_PTHREADS |
794 |
+ |
static int second_counter = 0; |
795 |
+ |
if (++second_counter > 60) { |
796 |
+ |
second_counter = 0; |
797 |
+ |
xpram_watchdog(); |
798 |
+ |
} |
799 |
+ |
#endif |
800 |
+ |
} |
801 |
+ |
|
802 |
|
static void one_tick(...) |
803 |
|
{ |
804 |
|
static int tick_counter = 0; |
424 |
– |
|
425 |
– |
// Pseudo Mac 1Hz interrupt, update local time |
805 |
|
if (++tick_counter > 60) { |
806 |
|
tick_counter = 0; |
807 |
< |
WriteMacInt32(0x20c, TimerDateTime()); |
807 |
> |
one_second(); |
808 |
|
} |
809 |
|
|
810 |
+ |
#ifndef HAVE_PTHREADS |
811 |
+ |
// No threads available, perform video refresh from here |
812 |
+ |
VideoRefresh(); |
813 |
+ |
#endif |
814 |
+ |
|
815 |
|
// Trigger 60Hz interrupt |
816 |
|
if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) { |
817 |
|
SetInterruptFlag(INTFLAG_60HZ); |
819 |
|
} |
820 |
|
} |
821 |
|
|
822 |
+ |
#ifdef HAVE_PTHREADS |
823 |
|
static void *tick_func(void *arg) |
824 |
|
{ |
825 |
+ |
uint64 next = GetTicks_usec(); |
826 |
|
while (!tick_thread_cancel) { |
441 |
– |
|
442 |
– |
// Wait |
443 |
– |
#ifdef HAVE_NANOSLEEP |
444 |
– |
struct timespec req = {0, 16625000}; |
445 |
– |
nanosleep(&req, NULL); |
446 |
– |
#else |
447 |
– |
usleep(16625); |
448 |
– |
#endif |
449 |
– |
|
450 |
– |
// Action |
827 |
|
one_tick(); |
828 |
+ |
next += 16625; |
829 |
+ |
int64 delay = next - GetTicks_usec(); |
830 |
+ |
if (delay > 0) |
831 |
+ |
Delay_usec(delay); |
832 |
+ |
else if (delay < -16625) |
833 |
+ |
next = GetTicks_usec(); |
834 |
|
} |
835 |
|
return NULL; |
836 |
|
} |
837 |
+ |
#endif |
838 |
|
|
839 |
|
|
840 |
|
/* |
841 |
< |
* XPRAM watchdog thread (saves XPRAM every minute) |
841 |
> |
* Get current value of microsecond timer |
842 |
|
*/ |
843 |
|
|
844 |
< |
void *xpram_func(void *arg) |
844 |
> |
uint64 GetTicks_usec(void) |
845 |
|
{ |
846 |
< |
uint8 last_xpram[256]; |
847 |
< |
memcpy(last_xpram, XPRAM, 256); |
846 |
> |
#ifdef HAVE_CLOCK_GETTIME |
847 |
> |
struct timespec t; |
848 |
> |
clock_gettime(CLOCK_REALTIME, &t); |
849 |
> |
return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
850 |
> |
#else |
851 |
> |
struct timeval t; |
852 |
> |
gettimeofday(&t, NULL); |
853 |
> |
return (uint64)t.tv_sec * 1000000 + t.tv_usec; |
854 |
> |
#endif |
855 |
> |
} |
856 |
|
|
857 |
< |
while (!xpram_thread_cancel) { |
858 |
< |
for (int i=0; i<60 && !xpram_thread_cancel; i++) { |
859 |
< |
#ifdef HAVE_NANOSLEEP |
860 |
< |
struct timespec req = {1, 0}; |
861 |
< |
nanosleep(&req, NULL); |
857 |
> |
|
858 |
> |
/* |
859 |
> |
* Delay by specified number of microseconds (<1 second) |
860 |
> |
* (adapted from SDL_Delay() source) |
861 |
> |
*/ |
862 |
> |
|
863 |
> |
void Delay_usec(uint32 usec) |
864 |
> |
{ |
865 |
> |
int was_error; |
866 |
> |
#ifndef __linux__ // Non-Linux implementations need to calculate time left |
867 |
> |
uint64 then, now, elapsed; |
868 |
> |
#endif |
869 |
> |
struct timeval tv; |
870 |
> |
|
871 |
> |
// Set the timeout interval - Linux only needs to do this once |
872 |
> |
#ifdef __linux__ |
873 |
> |
tv.tv_sec = 0; |
874 |
> |
tv.tv_usec = usec; |
875 |
|
#else |
876 |
< |
usleep(1000000); |
876 |
> |
then = GetTicks_usec(); |
877 |
|
#endif |
878 |
+ |
do { |
879 |
+ |
errno = 0; |
880 |
+ |
#ifndef __linux__ |
881 |
+ |
/* Calculate the time interval left (in case of interrupt) */ |
882 |
+ |
now = GetTicks_usec(); |
883 |
+ |
elapsed = now - then; |
884 |
+ |
then = now; |
885 |
+ |
if (elapsed >= usec) |
886 |
+ |
break; |
887 |
+ |
usec -= elapsed; |
888 |
+ |
tv.tv_sec = 0; |
889 |
+ |
tv.tv_usec = usec; |
890 |
+ |
#endif |
891 |
+ |
was_error = select(0, NULL, NULL, NULL, &tv); |
892 |
+ |
} while (was_error && (errno == EINTR)); |
893 |
+ |
} |
894 |
+ |
|
895 |
+ |
|
896 |
+ |
#if !EMULATED_68K |
897 |
+ |
/* |
898 |
+ |
* Virtual 68k interrupt handler |
899 |
+ |
*/ |
900 |
+ |
|
901 |
+ |
static void sigirq_handler(int sig, int code, struct sigcontext *scp) |
902 |
+ |
{ |
903 |
+ |
// Interrupts disabled? Then do nothing |
904 |
+ |
if (EmulatedSR & 0x0700) |
905 |
+ |
return; |
906 |
+ |
|
907 |
+ |
struct sigstate *state = (struct sigstate *)scp->sc_ap; |
908 |
+ |
M68kRegisters *regs = (M68kRegisters *)&state->ss_frame; |
909 |
+ |
|
910 |
+ |
// Set up interrupt frame on stack |
911 |
+ |
uint32 a7 = regs->a[7]; |
912 |
+ |
a7 -= 2; |
913 |
+ |
WriteMacInt16(a7, 0x64); |
914 |
+ |
a7 -= 4; |
915 |
+ |
WriteMacInt32(a7, scp->sc_pc); |
916 |
+ |
a7 -= 2; |
917 |
+ |
WriteMacInt16(a7, scp->sc_ps | EmulatedSR); |
918 |
+ |
scp->sc_sp = regs->a[7] = a7; |
919 |
+ |
|
920 |
+ |
// Set interrupt level |
921 |
+ |
EmulatedSR |= 0x2100; |
922 |
+ |
|
923 |
+ |
// Jump to MacOS interrupt handler on return |
924 |
+ |
scp->sc_pc = ReadMacInt32(0x64); |
925 |
+ |
} |
926 |
+ |
|
927 |
+ |
|
928 |
+ |
/* |
929 |
+ |
* SIGILL handler, for emulation of privileged instructions and executing |
930 |
+ |
* A-Trap and EMUL_OP opcodes |
931 |
+ |
*/ |
932 |
+ |
|
933 |
+ |
static void sigill_handler(int sig, int code, struct sigcontext *scp) |
934 |
+ |
{ |
935 |
+ |
struct sigstate *state = (struct sigstate *)scp->sc_ap; |
936 |
+ |
uint16 *pc = (uint16 *)scp->sc_pc; |
937 |
+ |
uint16 opcode = *pc; |
938 |
+ |
M68kRegisters *regs = (M68kRegisters *)&state->ss_frame; |
939 |
+ |
|
940 |
+ |
#define INC_PC(n) scp->sc_pc += (n) |
941 |
+ |
|
942 |
+ |
#define GET_SR (scp->sc_ps | EmulatedSR) |
943 |
+ |
|
944 |
+ |
#define STORE_SR(v) \ |
945 |
+ |
scp->sc_ps = (v) & 0xff; \ |
946 |
+ |
EmulatedSR = (v) & 0x2700; \ |
947 |
+ |
if (((v) & 0x0700) == 0 && InterruptFlags) \ |
948 |
+ |
TriggerInterrupt(); |
949 |
+ |
|
950 |
+ |
//printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR); |
951 |
+ |
|
952 |
+ |
if ((opcode & 0xf000) == 0xa000) { |
953 |
+ |
|
954 |
+ |
// A-Line instruction, set up A-Line trap frame on stack |
955 |
+ |
uint32 a7 = regs->a[7]; |
956 |
+ |
a7 -= 2; |
957 |
+ |
WriteMacInt16(a7, 0x28); |
958 |
+ |
a7 -= 4; |
959 |
+ |
WriteMacInt32(a7, (uint32)pc); |
960 |
+ |
a7 -= 2; |
961 |
+ |
WriteMacInt16(a7, GET_SR); |
962 |
+ |
scp->sc_sp = regs->a[7] = a7; |
963 |
+ |
|
964 |
+ |
// Jump to MacOS A-Line handler on return |
965 |
+ |
scp->sc_pc = ReadMacInt32(0x28); |
966 |
+ |
|
967 |
+ |
} else if ((opcode & 0xff00) == 0x7100) { |
968 |
+ |
|
969 |
+ |
// Extended opcode, push registers on user stack |
970 |
+ |
uint32 a7 = regs->a[7]; |
971 |
+ |
a7 -= 4; |
972 |
+ |
WriteMacInt32(a7, (uint32)pc); |
973 |
+ |
a7 -= 2; |
974 |
+ |
WriteMacInt16(a7, scp->sc_ps); |
975 |
+ |
for (int i=7; i>=0; i--) { |
976 |
+ |
a7 -= 4; |
977 |
+ |
WriteMacInt32(a7, regs->a[i]); |
978 |
+ |
} |
979 |
+ |
for (int i=7; i>=0; i--) { |
980 |
+ |
a7 -= 4; |
981 |
+ |
WriteMacInt32(a7, regs->d[i]); |
982 |
+ |
} |
983 |
+ |
scp->sc_sp = regs->a[7] = a7; |
984 |
+ |
|
985 |
+ |
// Jump to EmulOp trampoline code on return |
986 |
+ |
scp->sc_pc = (uint32)EmulOpTrampoline; |
987 |
+ |
|
988 |
+ |
} else switch (opcode) { // Emulate privileged instructions |
989 |
+ |
|
990 |
+ |
case 0x40e7: // move sr,-(sp) |
991 |
+ |
regs->a[7] -= 2; |
992 |
+ |
WriteMacInt16(regs->a[7], GET_SR); |
993 |
+ |
scp->sc_sp = regs->a[7]; |
994 |
+ |
INC_PC(2); |
995 |
+ |
break; |
996 |
+ |
|
997 |
+ |
case 0x46df: { // move (sp)+,sr |
998 |
+ |
uint16 sr = ReadMacInt16(regs->a[7]); |
999 |
+ |
STORE_SR(sr); |
1000 |
+ |
regs->a[7] += 2; |
1001 |
+ |
scp->sc_sp = regs->a[7]; |
1002 |
+ |
INC_PC(2); |
1003 |
+ |
break; |
1004 |
+ |
} |
1005 |
+ |
|
1006 |
+ |
case 0x007c: { // ori #xxxx,sr |
1007 |
+ |
uint16 sr = GET_SR | pc[1]; |
1008 |
+ |
scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR |
1009 |
+ |
EmulatedSR = sr & 0x2700; |
1010 |
+ |
INC_PC(4); |
1011 |
+ |
break; |
1012 |
|
} |
1013 |
< |
if (memcmp(last_xpram, XPRAM, 256)) { |
1014 |
< |
memcpy(last_xpram, XPRAM, 256); |
1015 |
< |
SaveXPRAM(); |
1013 |
> |
|
1014 |
> |
case 0x027c: { // andi #xxxx,sr |
1015 |
> |
uint16 sr = GET_SR & pc[1]; |
1016 |
> |
STORE_SR(sr); |
1017 |
> |
INC_PC(4); |
1018 |
> |
break; |
1019 |
|
} |
1020 |
+ |
|
1021 |
+ |
case 0x46fc: // move #xxxx,sr |
1022 |
+ |
STORE_SR(pc[1]); |
1023 |
+ |
INC_PC(4); |
1024 |
+ |
break; |
1025 |
+ |
|
1026 |
+ |
case 0x46ef: { // move (xxxx,sp),sr |
1027 |
+ |
uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]); |
1028 |
+ |
STORE_SR(sr); |
1029 |
+ |
INC_PC(4); |
1030 |
+ |
break; |
1031 |
+ |
} |
1032 |
+ |
|
1033 |
+ |
case 0x46d8: // move (a0)+,sr |
1034 |
+ |
case 0x46d9: { // move (a1)+,sr |
1035 |
+ |
uint16 sr = ReadMacInt16(regs->a[opcode & 7]); |
1036 |
+ |
STORE_SR(sr); |
1037 |
+ |
regs->a[opcode & 7] += 2; |
1038 |
+ |
INC_PC(2); |
1039 |
+ |
break; |
1040 |
+ |
} |
1041 |
+ |
|
1042 |
+ |
case 0x40f8: // move sr,xxxx.w |
1043 |
+ |
WriteMacInt16(pc[1], GET_SR); |
1044 |
+ |
INC_PC(4); |
1045 |
+ |
break; |
1046 |
+ |
|
1047 |
+ |
case 0x40d0: // move sr,(a0) |
1048 |
+ |
case 0x40d1: // move sr,(a1) |
1049 |
+ |
case 0x40d2: // move sr,(a2) |
1050 |
+ |
case 0x40d3: // move sr,(a3) |
1051 |
+ |
case 0x40d4: // move sr,(a4) |
1052 |
+ |
case 0x40d5: // move sr,(a5) |
1053 |
+ |
case 0x40d6: // move sr,(a6) |
1054 |
+ |
case 0x40d7: // move sr,(sp) |
1055 |
+ |
WriteMacInt16(regs->a[opcode & 7], GET_SR); |
1056 |
+ |
INC_PC(2); |
1057 |
+ |
break; |
1058 |
+ |
|
1059 |
+ |
case 0x40c0: // move sr,d0 |
1060 |
+ |
case 0x40c1: // move sr,d1 |
1061 |
+ |
case 0x40c2: // move sr,d2 |
1062 |
+ |
case 0x40c3: // move sr,d3 |
1063 |
+ |
case 0x40c4: // move sr,d4 |
1064 |
+ |
case 0x40c5: // move sr,d5 |
1065 |
+ |
case 0x40c6: // move sr,d6 |
1066 |
+ |
case 0x40c7: // move sr,d7 |
1067 |
+ |
regs->d[opcode & 7] = GET_SR; |
1068 |
+ |
INC_PC(2); |
1069 |
+ |
break; |
1070 |
+ |
|
1071 |
+ |
case 0x46c0: // move d0,sr |
1072 |
+ |
case 0x46c1: // move d1,sr |
1073 |
+ |
case 0x46c2: // move d2,sr |
1074 |
+ |
case 0x46c3: // move d3,sr |
1075 |
+ |
case 0x46c4: // move d4,sr |
1076 |
+ |
case 0x46c5: // move d5,sr |
1077 |
+ |
case 0x46c6: // move d6,sr |
1078 |
+ |
case 0x46c7: { // move d7,sr |
1079 |
+ |
uint16 sr = regs->d[opcode & 7]; |
1080 |
+ |
STORE_SR(sr); |
1081 |
+ |
INC_PC(2); |
1082 |
+ |
break; |
1083 |
+ |
} |
1084 |
+ |
|
1085 |
+ |
case 0xf327: // fsave -(sp) |
1086 |
+ |
goto ill; //!! |
1087 |
+ |
|
1088 |
+ |
case 0xf35f: // frestore (sp)+ |
1089 |
+ |
goto ill; //!! |
1090 |
+ |
|
1091 |
+ |
case 0x4e73: { // rte (only handles format 0) |
1092 |
+ |
uint32 a7 = regs->a[7]; |
1093 |
+ |
uint16 sr = ReadMacInt16(a7); |
1094 |
+ |
a7 += 2; |
1095 |
+ |
scp->sc_ps = sr & 0xff; |
1096 |
+ |
EmulatedSR = sr & 0x2700; |
1097 |
+ |
scp->sc_pc = ReadMacInt32(a7); |
1098 |
+ |
a7 += 6; |
1099 |
+ |
scp->sc_sp = regs->a[7] = a7; |
1100 |
+ |
break; |
1101 |
+ |
} |
1102 |
+ |
|
1103 |
+ |
case 0x4e7a: // movec cr,x |
1104 |
+ |
switch (pc[1]) { |
1105 |
+ |
case 0x8801: // movec vbr,a0 |
1106 |
+ |
regs->a[0] = 0; |
1107 |
+ |
break; |
1108 |
+ |
case 0x9801: // movec vbr,a1 |
1109 |
+ |
regs->a[1] = 0; |
1110 |
+ |
break; |
1111 |
+ |
case 0x0002: // movec cacr,d0 |
1112 |
+ |
regs->d[0] = 0x3111; |
1113 |
+ |
break; |
1114 |
+ |
case 0x1002: // movec cacr,d1 |
1115 |
+ |
regs->d[1] = 0x3111; |
1116 |
+ |
break; |
1117 |
+ |
case 0x0003: // movec tc,d0 |
1118 |
+ |
regs->d[0] = 0; |
1119 |
+ |
break; |
1120 |
+ |
case 0x1003: // movec tc,d1 |
1121 |
+ |
regs->d[1] = 0; |
1122 |
+ |
break; |
1123 |
+ |
default: |
1124 |
+ |
goto ill; |
1125 |
+ |
} |
1126 |
+ |
INC_PC(4); |
1127 |
+ |
break; |
1128 |
+ |
|
1129 |
+ |
case 0x4e7b: // movec x,cr |
1130 |
+ |
switch (pc[1]) { |
1131 |
+ |
case 0x0801: // movec d0,vbr |
1132 |
+ |
break; |
1133 |
+ |
case 0x0002: // movec d0,cacr |
1134 |
+ |
case 0x1002: // movec d1,cacr |
1135 |
+ |
FlushCodeCache(NULL, 0); |
1136 |
+ |
break; |
1137 |
+ |
default: |
1138 |
+ |
goto ill; |
1139 |
+ |
} |
1140 |
+ |
INC_PC(4); |
1141 |
+ |
break; |
1142 |
+ |
|
1143 |
+ |
case 0xf478: // cpusha dc |
1144 |
+ |
case 0xf4f8: // cpusha dc/ic |
1145 |
+ |
FlushCodeCache(NULL, 0); |
1146 |
+ |
INC_PC(2); |
1147 |
+ |
break; |
1148 |
+ |
|
1149 |
+ |
default: |
1150 |
+ |
ill: printf("SIGILL num %d, code %d\n", sig, code); |
1151 |
+ |
printf(" context %p:\n", scp); |
1152 |
+ |
printf(" onstack %08x\n", scp->sc_onstack); |
1153 |
+ |
printf(" sp %08x\n", scp->sc_sp); |
1154 |
+ |
printf(" fp %08x\n", scp->sc_fp); |
1155 |
+ |
printf(" pc %08x\n", scp->sc_pc); |
1156 |
+ |
printf(" opcode %04x\n", opcode); |
1157 |
+ |
printf(" sr %08x\n", scp->sc_ps); |
1158 |
+ |
printf(" state %p:\n", state); |
1159 |
+ |
printf(" flags %d\n", state->ss_flags); |
1160 |
+ |
for (int i=0; i<8; i++) |
1161 |
+ |
printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]); |
1162 |
+ |
for (int i=0; i<8; i++) |
1163 |
+ |
printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]); |
1164 |
+ |
|
1165 |
+ |
#ifdef ENABLE_MON |
1166 |
+ |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
1167 |
+ |
mon(3, arg); |
1168 |
+ |
#endif |
1169 |
+ |
QuitEmulator(); |
1170 |
+ |
break; |
1171 |
|
} |
480 |
– |
return NULL; |
1172 |
|
} |
1173 |
+ |
#endif |
1174 |
|
|
1175 |
|
|
1176 |
|
/* |
1177 |
|
* Display alert |
1178 |
|
*/ |
1179 |
|
|
1180 |
< |
#if ENABLE_GTK |
1180 |
> |
#ifdef ENABLE_GTK |
1181 |
|
static void dl_destroyed(void) |
1182 |
|
{ |
1183 |
|
gtk_main_quit(); |
1222 |
|
|
1223 |
|
void ErrorAlert(const char *text) |
1224 |
|
{ |
1225 |
< |
#if ENABLE_GTK |
1225 |
> |
#ifdef ENABLE_GTK |
1226 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
1227 |
|
printf(GetString(STR_SHELL_ERROR_PREFIX), text); |
1228 |
|
return; |
1241 |
|
|
1242 |
|
void WarningAlert(const char *text) |
1243 |
|
{ |
1244 |
< |
#if ENABLE_GTK |
1244 |
> |
#ifdef ENABLE_GTK |
1245 |
|
if (PrefsFindBool("nogui") || x_display == NULL) { |
1246 |
|
printf(GetString(STR_SHELL_WARNING_PREFIX), text); |
1247 |
|
return; |