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