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