5 |
|
* Based (in a small way) on the default main.m, |
6 |
|
and on Basilisk's main_unix.cpp |
7 |
|
* |
8 |
< |
* Basilisk II (C) 1997-2002 Christian Bauer |
8 |
> |
* Basilisk II (C) 1997-2008 Christian Bauer |
9 |
|
* |
10 |
|
* This program is free software; you can redistribute it and/or modify |
11 |
|
* it under the terms of the GNU General Public License as published by |
21 |
|
* along with this program; if not, write to the Free Software |
22 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
|
*/ |
24 |
< |
#define PTHREADS |
24 |
> |
|
25 |
> |
#import <AppKit/AppKit.h> |
26 |
> |
#undef check |
27 |
> |
|
28 |
> |
#define PTHREADS // Why is this here? |
29 |
|
#include "sysdeps.h" |
30 |
|
|
31 |
|
#ifdef HAVE_PTHREADS |
36 |
|
# include <sys/mman.h> |
37 |
|
#endif |
38 |
|
|
39 |
+ |
#include <string> |
40 |
+ |
using std::string; |
41 |
+ |
|
42 |
|
#include "cpu_emulation.h" |
43 |
|
#include "macos_util_macosx.h" |
44 |
|
#include "main.h" |
45 |
|
#include "prefs.h" |
46 |
|
#include "prefs_editor.h" |
47 |
|
#include "rom_patches.h" |
48 |
+ |
#include "sigsegv.h" |
49 |
|
#include "sys.h" |
42 |
– |
#include "timer.h" |
50 |
|
#include "user_strings.h" |
51 |
|
#include "version.h" |
52 |
|
#include "video.h" |
53 |
|
#include "vm_alloc.h" |
54 |
|
#include "xpram.h" |
55 |
|
|
56 |
+ |
#if USE_JIT |
57 |
+ |
extern void flush_icache_range(uint8 *start, uint32 size); // from compemu_support.cpp |
58 |
+ |
#endif |
59 |
+ |
|
60 |
|
#ifdef ENABLE_MON |
61 |
|
# include "mon.h" |
62 |
|
#endif |
65 |
|
#include "debug.h" |
66 |
|
|
67 |
|
|
57 |
– |
#import <AppKit/AppKit.h> |
58 |
– |
|
68 |
|
#include "main_macosx.h" // To bridge between main() and misc. classes |
69 |
|
|
70 |
|
|
71 |
|
// Constants |
72 |
|
const char ROM_FILE_NAME[] = "ROM"; |
64 |
– |
const int SIG_STACK_SIZE = SIGSTKSZ; // Size of signal stack |
73 |
|
const int SCRATCH_MEM_SIZE = 0x10000; // Size of scratch memory area |
74 |
|
|
75 |
|
|
99 |
|
uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes |
100 |
|
#endif |
101 |
|
|
94 |
– |
#if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS) |
95 |
– |
#define SIG_TIMER SIGRTMIN |
96 |
– |
static timer_t timer; // 60Hz timer |
97 |
– |
#endif |
98 |
– |
|
102 |
|
#ifdef ENABLE_MON |
103 |
|
static struct sigaction sigint_sa; // sigaction for SIGINT handler |
104 |
|
static void sigint_handler(...); |
109 |
|
#endif |
110 |
|
|
111 |
|
|
112 |
+ |
/* |
113 |
+ |
* Helpers to map memory that can be accessed from the Mac side |
114 |
+ |
*/ |
115 |
+ |
|
116 |
+ |
// NOTE: VM_MAP_32BIT is only used when compiling a 64-bit JIT on specific platforms |
117 |
+ |
void *vm_acquire_mac(size_t size) |
118 |
+ |
{ |
119 |
+ |
return vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT); |
120 |
+ |
} |
121 |
+ |
|
122 |
+ |
static int vm_acquire_mac_fixed(void *addr, size_t size) |
123 |
+ |
{ |
124 |
+ |
return vm_acquire_fixed(addr, size, VM_MAP_DEFAULT | VM_MAP_32BIT); |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
|
128 |
+ |
/* |
129 |
+ |
* SIGSEGV handler |
130 |
+ |
*/ |
131 |
+ |
|
132 |
+ |
static sigsegv_return_t sigsegv_handler(sigsegv_info_t *sip) |
133 |
+ |
{ |
134 |
+ |
const uintptr fault_address = (uintptr)sigsegv_get_fault_address(sip); |
135 |
+ |
#if ENABLE_VOSF |
136 |
+ |
// Handle screen fault |
137 |
+ |
extern bool Screen_fault_handler(sigsegv_info_t *sip); |
138 |
+ |
if (Screen_fault_handler(sip)) |
139 |
+ |
return SIGSEGV_RETURN_SUCCESS; |
140 |
+ |
#endif |
141 |
+ |
|
142 |
+ |
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
143 |
+ |
// Ignore writes to ROM |
144 |
+ |
if (((uintptr)fault_address - (uintptr)ROMBaseHost) < ROMSize) |
145 |
+ |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
146 |
+ |
|
147 |
+ |
// Ignore all other faults, if requested |
148 |
+ |
if (PrefsFindBool("ignoresegv")) |
149 |
+ |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
150 |
+ |
#endif |
151 |
+ |
|
152 |
+ |
return SIGSEGV_RETURN_FAILURE; |
153 |
+ |
} |
154 |
+ |
|
155 |
+ |
|
156 |
+ |
/* |
157 |
+ |
* Dump state when everything went wrong after a SEGV |
158 |
+ |
*/ |
159 |
+ |
|
160 |
+ |
static void sigsegv_dump_state(sigsegv_info_t *sip) |
161 |
+ |
{ |
162 |
+ |
const sigsegv_address_t fault_address = sigsegv_get_fault_address(sip); |
163 |
+ |
const sigsegv_address_t fault_instruction = sigsegv_get_fault_instruction_address(sip); |
164 |
+ |
fprintf(stderr, "Caught SIGSEGV at address %p", fault_address); |
165 |
+ |
if (fault_instruction != SIGSEGV_INVALID_ADDRESS) |
166 |
+ |
fprintf(stderr, " [IP=%p]", fault_instruction); |
167 |
+ |
fprintf(stderr, "\n"); |
168 |
+ |
uaecptr nextpc; |
169 |
+ |
extern void m68k_dumpstate(uaecptr *nextpc); |
170 |
+ |
m68k_dumpstate(&nextpc); |
171 |
+ |
#if USE_JIT && JIT_DEBUG |
172 |
+ |
extern void compiler_dumpstate(void); |
173 |
+ |
compiler_dumpstate(); |
174 |
+ |
#endif |
175 |
+ |
VideoQuitFullScreen(); |
176 |
+ |
#ifdef ENABLE_MON |
177 |
+ |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
178 |
+ |
mon(3, arg); |
179 |
+ |
#endif |
180 |
+ |
QuitEmulator(); |
181 |
+ |
} |
182 |
+ |
|
183 |
|
|
184 |
|
/* |
185 |
|
* Main program |
190 |
|
printf("Usage: %s [OPTION...]\n", prg_name); |
191 |
|
printf("\nUnix options:\n"); |
192 |
|
printf(" --help\n display this usage message\n"); |
193 |
+ |
printf(" --config FILE\n read/write configuration from/to FILE\n"); |
194 |
|
printf(" --break ADDRESS\n set ROM breakpoint\n"); |
195 |
|
printf(" --rominfo\n dump ROM information\n"); |
196 |
+ |
LoadPrefs(); // read the prefs file so PrefsPrintUsage() will print the correct default values |
197 |
|
PrefsPrintUsage(); |
198 |
|
exit(0); |
199 |
|
} |
210 |
|
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
211 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
212 |
|
|
137 |
– |
// Read preferences |
138 |
– |
PrefsInit(argc, argv); |
139 |
– |
|
213 |
|
// Parse command line arguments |
214 |
|
for (int i=1; i<argc; i++) { |
215 |
|
if (strcmp(argv[i], "--help") == 0) { |
220 |
|
i++; |
221 |
|
if (i < argc) |
222 |
|
ROMBreakpoint = strtol(argv[i], NULL, 0); |
223 |
+ |
} else if (strcmp(argv[i], "--config") == 0) { |
224 |
+ |
argv[i++] = NULL; |
225 |
+ |
if (i < argc) { |
226 |
+ |
extern string UserPrefsPath; // from prefs_unix.cpp |
227 |
+ |
UserPrefsPath = argv[i]; |
228 |
+ |
argv[i] = NULL; |
229 |
+ |
} |
230 |
|
} else if (strcmp(argv[i], "--rominfo") == 0) { |
231 |
|
PrintROMInfo = true; |
232 |
|
} else if (argv[i][0] == '-') { |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
+ |
// Read preferences |
239 |
+ |
PrefsInit(argc, argv); |
240 |
+ |
|
241 |
|
// Init system routines |
242 |
|
SysInit(); |
243 |
|
|
262 |
|
char str[256]; |
263 |
|
|
264 |
|
|
265 |
+ |
// Install the handler for SIGSEGV |
266 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
267 |
+ |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno)); |
268 |
+ |
ErrorAlert(str); |
269 |
+ |
QuitEmulator(); |
270 |
+ |
} |
271 |
+ |
|
272 |
+ |
// Register dump state function when we got mad after a segfault |
273 |
+ |
sigsegv_set_dump_state(sigsegv_dump_state); |
274 |
+ |
|
275 |
|
// Read RAM size |
276 |
|
RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary |
277 |
|
if (RAMSize < 1024*1024) { |
278 |
|
WarningAlert(GetString(STR_SMALL_RAM_WARN)); |
279 |
|
RAMSize = 1024*1024; |
280 |
|
} |
281 |
+ |
if (RAMSize > 1023*1024*1024) // Cap to 1023MB (APD crashes at 1GB) |
282 |
+ |
RAMSize = 1023*1024*1024; |
283 |
|
|
284 |
|
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
285 |
|
RAMSize = RAMSize & -getpagesize(); // Round down to page boundary |
291 |
|
#if REAL_ADDRESSING |
292 |
|
// Flag: RAM and ROM are contigously allocated from address 0 |
293 |
|
bool memory_mapped_from_zero = false; |
294 |
< |
|
295 |
< |
// Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash |
296 |
< |
// when trying to map a too big chunk of memory starting at address 0 |
297 |
< |
#if defined(OS_solaris) || defined(OS_netbsd) |
298 |
< |
const bool can_map_all_memory = false; |
204 |
< |
#else |
294 |
> |
|
295 |
> |
// Make sure to map RAM & ROM at address 0 only on platforms that |
296 |
> |
// supports linker scripts to relocate the Basilisk II executable |
297 |
> |
// above 0x70000000 |
298 |
> |
#if HAVE_LINKER_SCRIPT |
299 |
|
const bool can_map_all_memory = true; |
300 |
+ |
#else |
301 |
+ |
const bool can_map_all_memory = false; |
302 |
|
#endif |
303 |
|
|
304 |
|
// Try to allocate all memory from 0x0000, if it is not known to crash |
305 |
< |
if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) { |
305 |
> |
if (can_map_all_memory && (vm_acquire_mac_fixed(0, RAMSize + 0x100000) == 0)) { |
306 |
|
D(bug("Could allocate RAM and ROM from 0x0000\n")); |
307 |
|
memory_mapped_from_zero = true; |
308 |
|
} |
309 |
|
|
310 |
+ |
#ifndef PAGEZERO_HACK |
311 |
|
// Otherwise, just create the Low Memory area (0x0000..0x2000) |
312 |
< |
else if (vm_acquire_fixed(0, 0x2000) == 0) { |
312 |
> |
else if (vm_acquire_mac_fixed(0, 0x2000) == 0) { |
313 |
|
D(bug("Could allocate the Low Memory globals\n")); |
314 |
|
lm_area_mapped = true; |
315 |
|
} |
320 |
|
ErrorAlert(str); |
321 |
|
QuitEmulator(); |
322 |
|
} |
323 |
+ |
#endif |
324 |
|
#else |
325 |
|
*str = 0; // Eliminate unused variable warning |
326 |
< |
#endif |
326 |
> |
#endif /* REAL_ADDRESSING */ |
327 |
|
|
328 |
|
// Create areas for Mac RAM and ROM |
329 |
|
#if REAL_ADDRESSING |
334 |
|
else |
335 |
|
#endif |
336 |
|
{ |
337 |
< |
RAMBaseHost = (uint8 *)vm_acquire(RAMSize); |
338 |
< |
ROMBaseHost = (uint8 *)vm_acquire(0x100000); |
241 |
< |
if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) { |
337 |
> |
uint8 *ram_rom_area = (uint8 *)vm_acquire_mac(RAMSize + 0x100000); |
338 |
> |
if (ram_rom_area == VM_MAP_FAILED) { |
339 |
|
ErrorAlert(STR_NO_MEM_ERR); |
340 |
|
QuitEmulator(); |
341 |
|
} |
342 |
+ |
RAMBaseHost = ram_rom_area; |
343 |
+ |
ROMBaseHost = RAMBaseHost + RAMSize; |
344 |
|
} |
345 |
|
|
346 |
|
#if USE_SCRATCHMEM_SUBTERFUGE |
347 |
|
// Allocate scratch memory |
348 |
< |
ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE); |
348 |
> |
ScratchMem = (uint8 *)vm_acquire_mac(SCRATCH_MEM_SIZE); |
349 |
|
if (ScratchMem == VM_MAP_FAILED) { |
350 |
|
ErrorAlert(STR_NO_MEM_ERR); |
351 |
|
QuitEmulator(); |
360 |
|
ROMBaseMac = Host2MacAddr(ROMBaseHost); |
361 |
|
#endif |
362 |
|
#if REAL_ADDRESSING |
363 |
< |
RAMBaseMac = (uint32)RAMBaseHost; |
364 |
< |
ROMBaseMac = (uint32)ROMBaseHost; |
363 |
> |
RAMBaseMac = Host2MacAddr(RAMBaseHost); |
364 |
> |
ROMBaseMac = Host2MacAddr(ROMBaseHost); |
365 |
|
#endif |
366 |
|
D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac)); |
367 |
|
D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac)); |
429 |
|
|
430 |
|
// Free ROM/RAM areas |
431 |
|
if (RAMBaseHost != VM_MAP_FAILED) { |
432 |
< |
vm_release(RAMBaseHost, RAMSize); |
432 |
> |
vm_release(RAMBaseHost, RAMSize + 0x100000); |
433 |
|
RAMBaseHost = NULL; |
335 |
– |
} |
336 |
– |
if (ROMBaseHost != VM_MAP_FAILED) { |
337 |
– |
vm_release(ROMBaseHost, 0x100000); |
434 |
|
ROMBaseHost = NULL; |
435 |
|
} |
436 |
|
|
460 |
|
|
461 |
|
void QuitEmulator(void) |
462 |
|
{ |
367 |
– |
extern NSApplication *NSApp; |
368 |
– |
|
369 |
– |
|
463 |
|
QuitEmuNoExit(); |
464 |
|
|
465 |
|
// Stop run loop? |
476 |
|
|
477 |
|
void FlushCodeCache(void *start, uint32 size) |
478 |
|
{ |
479 |
+ |
#if USE_JIT |
480 |
+ |
if (UseJIT) |
481 |
+ |
flush_icache_range((uint8 *)start, size); |
482 |
+ |
#endif |
483 |
|
} |
484 |
|
|
485 |
|
|
501 |
|
#endif |
502 |
|
|
503 |
|
|
504 |
+ |
#ifdef HAVE_PTHREADS |
505 |
+ |
/* |
506 |
+ |
* Pthread configuration |
507 |
+ |
*/ |
508 |
+ |
|
509 |
+ |
void Set_pthread_attr(pthread_attr_t *attr, int priority) |
510 |
+ |
{ |
511 |
+ |
pthread_attr_init(attr); |
512 |
+ |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
513 |
+ |
// Some of these only work for superuser |
514 |
+ |
if (geteuid() == 0) { |
515 |
+ |
pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED); |
516 |
+ |
pthread_attr_setschedpolicy(attr, SCHED_FIFO); |
517 |
+ |
struct sched_param fifo_param; |
518 |
+ |
fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) |
519 |
+ |
+ sched_get_priority_max(SCHED_FIFO)) |
520 |
+ |
/ 2 + priority); |
521 |
+ |
pthread_attr_setschedparam(attr, &fifo_param); |
522 |
+ |
} |
523 |
+ |
if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) { |
524 |
+ |
#ifdef PTHREAD_SCOPE_BOUND_NP |
525 |
+ |
// If system scope is not available (eg. we're not running |
526 |
+ |
// with CAP_SCHED_MGT capability on an SGI box), try bound |
527 |
+ |
// scope. It exposes pthread scheduling to the kernel, |
528 |
+ |
// without setting realtime priority. |
529 |
+ |
pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP); |
530 |
+ |
#endif |
531 |
+ |
} |
532 |
+ |
#endif |
533 |
+ |
} |
534 |
+ |
#endif // HAVE_PTHREADS |
535 |
+ |
|
536 |
+ |
|
537 |
|
/* |
538 |
|
* Mutexes |
539 |
|
*/ |
541 |
|
#ifdef HAVE_PTHREADS |
542 |
|
|
543 |
|
struct B2_mutex { |
544 |
< |
B2_mutex() { pthread_mutex_init(&m, NULL); } |
545 |
< |
~B2_mutex() { pthread_mutex_unlock(&m); pthread_mutex_destroy(&m); } |
544 |
> |
B2_mutex() { |
545 |
> |
pthread_mutexattr_t attr; |
546 |
> |
pthread_mutexattr_init(&attr); |
547 |
> |
// Initialize the mutex for priority inheritance -- |
548 |
> |
// required for accurate timing. |
549 |
> |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL |
550 |
> |
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
551 |
> |
#endif |
552 |
> |
#if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL) |
553 |
> |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); |
554 |
> |
#endif |
555 |
> |
#ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED |
556 |
> |
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE); |
557 |
> |
#endif |
558 |
> |
pthread_mutex_init(&m, &attr); |
559 |
> |
pthread_mutexattr_destroy(&attr); |
560 |
> |
} |
561 |
> |
~B2_mutex() { |
562 |
> |
pthread_mutex_trylock(&m); // Make sure it's locked before |
563 |
> |
pthread_mutex_unlock(&m); // unlocking it. |
564 |
> |
pthread_mutex_destroy(&m); |
565 |
> |
} |
566 |
|
pthread_mutex_t m; |
567 |
|
}; |
568 |
|
|
645 |
|
NSString *error = [NSString stringWithCString: text]; |
646 |
|
NSString *button = [NSString stringWithCString: GetString(STR_QUIT_BUTTON) ]; |
647 |
|
|
498 |
– |
// If we have a full screen mode, quit it here? |
499 |
– |
|
648 |
|
NSLog(error); |
649 |
+ |
if ( PrefsFindBool("nogui") ) |
650 |
+ |
return; |
651 |
+ |
VideoQuitFullScreen(); |
652 |
|
NSRunCriticalAlertPanel(title, error, button, nil, nil); |
653 |
|
} |
654 |
|
|
665 |
|
NSString *button = [NSString stringWithCString: GetString(STR_OK_BUTTON) ]; |
666 |
|
|
667 |
|
NSLog(warning); |
668 |
+ |
if ( PrefsFindBool("nogui") ) |
669 |
+ |
return; |
670 |
+ |
VideoQuitFullScreen(); |
671 |
|
NSRunAlertPanel(title, warning, button, nil, nil); |
672 |
|
} |
673 |
|
|
684 |
|
NSString *yes = [NSString stringWithCString: pos]; |
685 |
|
NSString *no = [NSString stringWithCString: neg]; |
686 |
|
|
533 |
– |
NSLog(warning); |
687 |
|
return NSRunInformationalAlertPanel(title, warning, yes, no, nil); |
688 |
|
} |