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