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