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