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