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