1 |
|
/* |
2 |
|
* main_unix.cpp - Emulation core, Unix implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2008 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) Christian Bauer and Marc Hellwig |
5 |
|
* |
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 |
136 |
|
#ifdef ENABLE_XF86_DGA |
137 |
|
#include <X11/Xlib.h> |
138 |
|
#include <X11/Xutil.h> |
139 |
< |
#include <X11/extensions/xf86dga.h> |
139 |
> |
#include <X11/extensions/Xxf86dga.h> |
140 |
|
#endif |
141 |
|
|
142 |
|
#ifdef ENABLE_MON |
161 |
|
const char ROM_FILE_NAME[] = "ROM"; |
162 |
|
const char ROM_FILE_NAME2[] = "Mac OS ROM"; |
163 |
|
|
164 |
< |
#if REAL_ADDRESSING |
165 |
< |
const uintptr RAM_BASE = 0x20000000; // Base address of RAM |
166 |
< |
#else |
164 |
> |
#if !REAL_ADDRESSING |
165 |
|
// FIXME: needs to be >= 0x04000000 |
166 |
|
const uintptr RAM_BASE = 0x10000000; // Base address of RAM |
167 |
|
#endif |
168 |
+ |
const uintptr ROM_BASE = 0x40800000; // Base address of ROM |
169 |
+ |
#if REAL_ADDRESSING |
170 |
+ |
const uint32 ROM_ALIGNMENT = 0x100000; // ROM must be aligned to a 1MB boundary |
171 |
+ |
#endif |
172 |
|
const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack |
173 |
|
|
174 |
|
|
179 |
|
#endif |
180 |
|
uint32 RAMBase; // Base address of Mac RAM |
181 |
|
uint32 RAMSize; // Size of Mac RAM |
182 |
+ |
uint32 ROMBase; // Base address of Mac ROM |
183 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
184 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
185 |
|
uint32 DRCacheAddr; // Address of DR Cache |
340 |
|
* Memory management helpers |
341 |
|
*/ |
342 |
|
|
343 |
< |
static inline int vm_mac_acquire(uint32 addr, uint32 size) |
343 |
> |
static inline uint8 *vm_mac_acquire(uint32 size) |
344 |
> |
{ |
345 |
> |
return (uint8 *)vm_acquire(size); |
346 |
> |
} |
347 |
> |
|
348 |
> |
static inline int vm_mac_acquire_fixed(uint32 addr, uint32 size) |
349 |
|
{ |
350 |
|
return vm_acquire_fixed(Mac2HostAddr(addr), size); |
351 |
|
} |
373 |
|
{ |
374 |
|
const int suffix_len = sizeof(".sheepvm") - 1; |
375 |
|
int len = strlen(path); |
376 |
< |
if (len > suffix_len && !strcmp(path + len - suffix_len, ".sheepvm")) { |
376 |
> |
if (len && path[len - 1] == '/') // to support both ".sheepvm" and ".sheepvm/" |
377 |
> |
len--; |
378 |
> |
if (len > suffix_len && !strncmp(path + len - suffix_len, ".sheepvm", suffix_len)) { |
379 |
|
struct stat d; |
380 |
|
if (!stat(path, &d) && S_ISDIR(d.st_mode)) { |
381 |
|
return true; |
384 |
|
return false; |
385 |
|
} |
386 |
|
|
387 |
< |
int main(int argc, char **argv) |
387 |
> |
static void get_system_info(void) |
388 |
|
{ |
379 |
– |
char str[256]; |
380 |
– |
int rom_fd; |
381 |
– |
FILE *proc_file; |
382 |
– |
const char *rom_path; |
383 |
– |
uint32 rom_size, actual; |
384 |
– |
uint8 *rom_tmp; |
385 |
– |
time_t now, expire; |
386 |
– |
bool memory_mapped_from_zero; |
387 |
– |
const char *vmdir = NULL; |
388 |
– |
|
389 |
– |
#ifdef USE_SDL_VIDEO |
390 |
– |
// Don't let SDL block the screensaver |
391 |
– |
putenv("SDL_VIDEO_ALLOW_SCREENSAVER=1"); |
392 |
– |
|
393 |
– |
// Make SDL pass through command-clicks and option-clicks unaltered |
394 |
– |
putenv("SDL_HAS3BUTTONMOUSE=1"); |
395 |
– |
#endif |
396 |
– |
|
397 |
– |
// Initialize variables |
398 |
– |
RAMBase = 0; |
399 |
– |
tzset(); |
400 |
– |
|
401 |
– |
// Print some info |
402 |
– |
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
403 |
– |
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
404 |
– |
|
389 |
|
#if !EMULATED_PPC |
390 |
< |
#ifdef SYSTEM_CLOBBERS_R2 |
407 |
< |
// Get TOC pointer |
408 |
< |
TOC = get_r2(); |
409 |
< |
#endif |
410 |
< |
#ifdef SYSTEM_CLOBBERS_R13 |
411 |
< |
// Get r13 register |
412 |
< |
R13 = get_r13(); |
413 |
< |
#endif |
414 |
< |
#endif |
415 |
< |
|
416 |
< |
// Parse command line arguments |
417 |
< |
for (int i=1; i<argc; i++) { |
418 |
< |
if (strcmp(argv[i], "--help") == 0) { |
419 |
< |
usage(argv[0]); |
420 |
< |
#ifndef USE_SDL_VIDEO |
421 |
< |
} else if (strcmp(argv[i], "--display") == 0) { |
422 |
< |
i++; |
423 |
< |
if (i < argc) |
424 |
< |
x_display_name = strdup(argv[i]); |
425 |
< |
#endif |
426 |
< |
} else if (strcmp(argv[i], "--gui-connection") == 0) { |
427 |
< |
argv[i++] = NULL; |
428 |
< |
if (i < argc) { |
429 |
< |
gui_connection_path = argv[i]; |
430 |
< |
argv[i] = NULL; |
431 |
< |
} |
432 |
< |
} else if (valid_vmdir(argv[i])) { |
433 |
< |
vmdir = argv[i]; |
434 |
< |
argv[i] = NULL; |
435 |
< |
printf("Using %s as vmdir.\n", vmdir); |
436 |
< |
if (chdir(vmdir)) { |
437 |
< |
printf("Failed to chdir to %s. Good bye.", vmdir); |
438 |
< |
exit(1); |
439 |
< |
} |
440 |
< |
break; |
441 |
< |
} |
442 |
< |
} |
443 |
< |
|
444 |
< |
// Remove processed arguments |
445 |
< |
for (int i=1; i<argc; i++) { |
446 |
< |
int k; |
447 |
< |
for (k=i; k<argc; k++) |
448 |
< |
if (argv[k] != NULL) |
449 |
< |
break; |
450 |
< |
if (k > i) { |
451 |
< |
k -= i; |
452 |
< |
for (int j=i+k; j<argc; j++) |
453 |
< |
argv[j-k] = argv[j]; |
454 |
< |
argc -= k; |
455 |
< |
} |
456 |
< |
} |
457 |
< |
|
458 |
< |
// Connect to the external GUI |
459 |
< |
if (gui_connection_path) { |
460 |
< |
if ((gui_connection = rpc_init_client(gui_connection_path)) == NULL) { |
461 |
< |
fprintf(stderr, "Failed to initialize RPC client connection to the GUI\n"); |
462 |
< |
return 1; |
463 |
< |
} |
464 |
< |
} |
465 |
< |
|
466 |
< |
#ifdef ENABLE_GTK |
467 |
< |
if (!gui_connection) { |
468 |
< |
// Init GTK |
469 |
< |
gtk_set_locale(); |
470 |
< |
gtk_init(&argc, &argv); |
471 |
< |
} |
472 |
< |
#endif |
473 |
< |
|
474 |
< |
// Read preferences |
475 |
< |
PrefsInit(vmdir, argc, argv); |
476 |
< |
|
477 |
< |
// Any command line arguments left? |
478 |
< |
for (int i=1; i<argc; i++) { |
479 |
< |
if (argv[i][0] == '-') { |
480 |
< |
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
481 |
< |
usage(argv[0]); |
482 |
< |
} |
483 |
< |
} |
484 |
< |
|
485 |
< |
#ifdef USE_SDL |
486 |
< |
// Initialize SDL system |
487 |
< |
int sdl_flags = 0; |
488 |
< |
#ifdef USE_SDL_VIDEO |
489 |
< |
sdl_flags |= SDL_INIT_VIDEO; |
490 |
< |
#endif |
491 |
< |
#ifdef USE_SDL_AUDIO |
492 |
< |
sdl_flags |= SDL_INIT_AUDIO; |
493 |
< |
#endif |
494 |
< |
assert(sdl_flags != 0); |
495 |
< |
if (SDL_Init(sdl_flags) == -1) { |
496 |
< |
char str[256]; |
497 |
< |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
498 |
< |
ErrorAlert(str); |
499 |
< |
goto quit; |
500 |
< |
} |
501 |
< |
atexit(SDL_Quit); |
502 |
< |
|
503 |
< |
// Don't let SDL catch SIGINT and SIGTERM signals |
504 |
< |
signal(SIGINT, SIG_DFL); |
505 |
< |
signal(SIGTERM, SIG_DFL); |
506 |
< |
#endif |
507 |
< |
|
508 |
< |
#ifndef USE_SDL_VIDEO |
509 |
< |
// Open display |
510 |
< |
x_display = XOpenDisplay(x_display_name); |
511 |
< |
if (x_display == NULL) { |
512 |
< |
char str[256]; |
513 |
< |
sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name)); |
514 |
< |
ErrorAlert(str); |
515 |
< |
goto quit; |
516 |
< |
} |
517 |
< |
|
518 |
< |
#if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON) |
519 |
< |
// Fork out, so we can return from fullscreen mode when things get ugly |
520 |
< |
XF86DGAForkApp(DefaultScreen(x_display)); |
521 |
< |
#endif |
522 |
< |
#endif |
523 |
< |
|
524 |
< |
#ifdef ENABLE_MON |
525 |
< |
// Initialize mon |
526 |
< |
mon_init(); |
527 |
< |
#endif |
528 |
< |
|
529 |
< |
#if !EMULATED_PPC |
530 |
< |
// Create and install stacks for signal handlers |
531 |
< |
sig_stack.ss_sp = malloc(SIG_STACK_SIZE); |
532 |
< |
D(bug("Signal stack at %p\n", sig_stack.ss_sp)); |
533 |
< |
if (sig_stack.ss_sp == NULL) { |
534 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
535 |
< |
goto quit; |
536 |
< |
} |
537 |
< |
sig_stack.ss_flags = 0; |
538 |
< |
sig_stack.ss_size = SIG_STACK_SIZE; |
539 |
< |
if (sigaltstack(&sig_stack, NULL) < 0) { |
540 |
< |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
541 |
< |
ErrorAlert(str); |
542 |
< |
goto quit; |
543 |
< |
} |
544 |
< |
extra_stack.ss_sp = malloc(SIG_STACK_SIZE); |
545 |
< |
D(bug("Extra stack at %p\n", extra_stack.ss_sp)); |
546 |
< |
if (extra_stack.ss_sp == NULL) { |
547 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
548 |
< |
goto quit; |
549 |
< |
} |
550 |
< |
extra_stack.ss_flags = 0; |
551 |
< |
extra_stack.ss_size = SIG_STACK_SIZE; |
552 |
< |
#endif |
553 |
< |
|
554 |
< |
#if !EMULATED_PPC |
555 |
< |
// Install SIGSEGV and SIGBUS handlers |
556 |
< |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
557 |
< |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
558 |
< |
sigsegv_action.sa_sigaction = sigsegv_handler; |
559 |
< |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
560 |
< |
#ifdef HAVE_SIGNAL_SA_RESTORER |
561 |
< |
sigsegv_action.sa_restorer = NULL; |
562 |
< |
#endif |
563 |
< |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
564 |
< |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno)); |
565 |
< |
ErrorAlert(str); |
566 |
< |
goto quit; |
567 |
< |
} |
568 |
< |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
569 |
< |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGBUS", strerror(errno)); |
570 |
< |
ErrorAlert(str); |
571 |
< |
goto quit; |
572 |
< |
} |
573 |
< |
#else |
574 |
< |
// Install SIGSEGV handler for CPU emulator |
575 |
< |
if (!sigsegv_install_handler(sigsegv_handler)) { |
576 |
< |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno)); |
577 |
< |
ErrorAlert(str); |
578 |
< |
goto quit; |
579 |
< |
} |
390 |
> |
FILE *proc_file; |
391 |
|
#endif |
392 |
|
|
582 |
– |
// Initialize VM system |
583 |
– |
vm_init(); |
584 |
– |
|
585 |
– |
// Get system info |
393 |
|
PVR = 0x00040000; // Default: 604 |
394 |
|
CPUClockSpeed = 100000000; // Default: 100MHz |
395 |
|
BusClockSpeed = 100000000; // Default: 100MHz |
396 |
|
TimebaseSpeed = 25000000; // Default: 25MHz |
397 |
+ |
|
398 |
|
#if EMULATED_PPC |
399 |
|
PVR = 0x000c0000; // Default: 7400 (with AltiVec) |
400 |
|
#elif defined(__APPLE__) && defined(__MACH__) |
429 |
|
} |
430 |
|
fclose(proc_file); |
431 |
|
} else { |
432 |
+ |
char str[256]; |
433 |
|
sprintf(str, GetString(STR_PROC_CPUINFO_WARN), strerror(errno)); |
434 |
|
WarningAlert(str); |
435 |
|
} |
484 |
|
{ 0xffff0000, 0x00350000, "Power4" }, |
485 |
|
{ 0xffff0000, 0x00390000, "PPC970" }, |
486 |
|
{ 0xffff0000, 0x003c0000, "PPC970FX" }, |
487 |
+ |
{ 0xffff0000, 0x00440000, "PPC970MP" }, |
488 |
|
{ 0xffff0000, 0x003a0000, "POWER5 (gr)" }, |
489 |
|
{ 0xffff0000, 0x003b0000, "POWER5+ (gs)" }, |
490 |
|
{ 0xffff0000, 0x003e0000, "POWER6" }, |
561 |
|
closedir(cpus_dir); |
562 |
|
} |
563 |
|
#endif |
564 |
+ |
|
565 |
|
// Remap any newer G4/G5 processor to plain G4 for compatibility |
566 |
|
switch (PVR >> 16) { |
567 |
|
case 0x8000: // 7450 |
571 |
|
case 0x8004: // 7448 |
572 |
|
case 0x0039: // 970 |
573 |
|
case 0x003c: // 970FX |
574 |
+ |
case 0x0044: // 970MP |
575 |
|
PVR = 0x000c0000; // 7400 |
576 |
|
break; |
577 |
|
} |
578 |
|
D(bug("PVR: %08x (assumed)\n", PVR)); |
579 |
+ |
} |
580 |
+ |
|
581 |
+ |
static bool load_mac_rom(void) |
582 |
+ |
{ |
583 |
+ |
uint32 rom_size, actual; |
584 |
+ |
uint8 *rom_tmp; |
585 |
+ |
const char *rom_path = PrefsFindString("rom"); |
586 |
+ |
int rom_fd = open(rom_path && *rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY); |
587 |
+ |
if (rom_fd < 0) { |
588 |
+ |
rom_fd = open(ROM_FILE_NAME2, O_RDONLY); |
589 |
+ |
if (rom_fd < 0) { |
590 |
+ |
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
591 |
+ |
return false; |
592 |
+ |
} |
593 |
+ |
} |
594 |
+ |
printf("%s", GetString(STR_READING_ROM_FILE)); |
595 |
+ |
rom_size = lseek(rom_fd, 0, SEEK_END); |
596 |
+ |
lseek(rom_fd, 0, SEEK_SET); |
597 |
+ |
rom_tmp = new uint8[ROM_SIZE]; |
598 |
+ |
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
599 |
+ |
close(rom_fd); |
600 |
+ |
|
601 |
+ |
// Decode Mac ROM |
602 |
+ |
if (!DecodeROM(rom_tmp, actual)) { |
603 |
+ |
if (rom_size != 4*1024*1024) { |
604 |
+ |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
605 |
+ |
return false; |
606 |
+ |
} else { |
607 |
+ |
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
608 |
+ |
return false; |
609 |
+ |
} |
610 |
+ |
} |
611 |
+ |
delete[] rom_tmp; |
612 |
+ |
return true; |
613 |
+ |
} |
614 |
+ |
|
615 |
+ |
static bool install_signal_handlers(void) |
616 |
+ |
{ |
617 |
+ |
char str[256]; |
618 |
+ |
#if !EMULATED_PPC |
619 |
+ |
// Create and install stacks for signal handlers |
620 |
+ |
sig_stack.ss_sp = malloc(SIG_STACK_SIZE); |
621 |
+ |
D(bug("Signal stack at %p\n", sig_stack.ss_sp)); |
622 |
+ |
if (sig_stack.ss_sp == NULL) { |
623 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
624 |
+ |
return false; |
625 |
+ |
} |
626 |
+ |
sig_stack.ss_flags = 0; |
627 |
+ |
sig_stack.ss_size = SIG_STACK_SIZE; |
628 |
+ |
if (sigaltstack(&sig_stack, NULL) < 0) { |
629 |
+ |
sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno)); |
630 |
+ |
ErrorAlert(str); |
631 |
+ |
return false; |
632 |
+ |
} |
633 |
+ |
extra_stack.ss_sp = malloc(SIG_STACK_SIZE); |
634 |
+ |
D(bug("Extra stack at %p\n", extra_stack.ss_sp)); |
635 |
+ |
if (extra_stack.ss_sp == NULL) { |
636 |
+ |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
637 |
+ |
return false; |
638 |
+ |
} |
639 |
+ |
extra_stack.ss_flags = 0; |
640 |
+ |
extra_stack.ss_size = SIG_STACK_SIZE; |
641 |
+ |
|
642 |
+ |
// Install SIGSEGV and SIGBUS handlers |
643 |
+ |
sigemptyset(&sigsegv_action.sa_mask); // Block interrupts during SEGV handling |
644 |
+ |
sigaddset(&sigsegv_action.sa_mask, SIGUSR2); |
645 |
+ |
sigsegv_action.sa_sigaction = sigsegv_handler; |
646 |
+ |
sigsegv_action.sa_flags = SA_ONSTACK | SA_SIGINFO; |
647 |
+ |
#ifdef HAVE_SIGNAL_SA_RESTORER |
648 |
+ |
sigsegv_action.sa_restorer = NULL; |
649 |
+ |
#endif |
650 |
+ |
if (sigaction(SIGSEGV, &sigsegv_action, NULL) < 0) { |
651 |
+ |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno)); |
652 |
+ |
ErrorAlert(str); |
653 |
+ |
return false; |
654 |
+ |
} |
655 |
+ |
if (sigaction(SIGBUS, &sigsegv_action, NULL) < 0) { |
656 |
+ |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGBUS", strerror(errno)); |
657 |
+ |
ErrorAlert(str); |
658 |
+ |
return false; |
659 |
+ |
} |
660 |
+ |
#else |
661 |
+ |
// Install SIGSEGV handler for CPU emulator |
662 |
+ |
if (!sigsegv_install_handler(sigsegv_handler)) { |
663 |
+ |
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno)); |
664 |
+ |
ErrorAlert(str); |
665 |
+ |
return false; |
666 |
+ |
} |
667 |
+ |
#endif |
668 |
+ |
return true; |
669 |
+ |
} |
670 |
+ |
|
671 |
+ |
static bool init_sdl() |
672 |
+ |
{ |
673 |
+ |
int sdl_flags = 0; |
674 |
+ |
#ifdef USE_SDL_VIDEO |
675 |
+ |
sdl_flags |= SDL_INIT_VIDEO; |
676 |
+ |
#endif |
677 |
+ |
#ifdef USE_SDL_AUDIO |
678 |
+ |
sdl_flags |= SDL_INIT_AUDIO; |
679 |
+ |
#endif |
680 |
+ |
assert(sdl_flags != 0); |
681 |
+ |
|
682 |
+ |
#ifdef USE_SDL_VIDEO |
683 |
+ |
// Don't let SDL block the screensaver |
684 |
+ |
setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", TRUE); |
685 |
+ |
|
686 |
+ |
// Make SDL pass through command-clicks and option-clicks unaltered |
687 |
+ |
setenv("SDL_HAS3BUTTONMOUSE", "1", TRUE); |
688 |
+ |
#endif |
689 |
+ |
|
690 |
+ |
if (SDL_Init(sdl_flags) == -1) { |
691 |
+ |
char str[256]; |
692 |
+ |
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError()); |
693 |
+ |
ErrorAlert(str); |
694 |
+ |
return false; |
695 |
+ |
} |
696 |
+ |
atexit(SDL_Quit); |
697 |
+ |
|
698 |
+ |
// Don't let SDL catch SIGINT and SIGTERM signals |
699 |
+ |
signal(SIGINT, SIG_DFL); |
700 |
+ |
signal(SIGTERM, SIG_DFL); |
701 |
+ |
return true; |
702 |
+ |
} |
703 |
+ |
|
704 |
+ |
int main(int argc, char **argv) |
705 |
+ |
{ |
706 |
+ |
char str[256]; |
707 |
+ |
bool memory_mapped_from_zero, ram_rom_areas_contiguous; |
708 |
+ |
const char *vmdir = NULL; |
709 |
+ |
|
710 |
+ |
// Initialize variables |
711 |
+ |
RAMBase = 0; |
712 |
+ |
tzset(); |
713 |
+ |
|
714 |
+ |
// Print some info |
715 |
+ |
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
716 |
+ |
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
717 |
+ |
|
718 |
+ |
#if !EMULATED_PPC |
719 |
+ |
#ifdef SYSTEM_CLOBBERS_R2 |
720 |
+ |
// Get TOC pointer |
721 |
+ |
TOC = get_r2(); |
722 |
+ |
#endif |
723 |
+ |
#ifdef SYSTEM_CLOBBERS_R13 |
724 |
+ |
// Get r13 register |
725 |
+ |
R13 = get_r13(); |
726 |
+ |
#endif |
727 |
+ |
#endif |
728 |
+ |
|
729 |
+ |
// Parse command line arguments |
730 |
+ |
for (int i=1; i<argc; i++) { |
731 |
+ |
if (strcmp(argv[i], "--help") == 0) { |
732 |
+ |
usage(argv[0]); |
733 |
+ |
#ifndef USE_SDL_VIDEO |
734 |
+ |
} else if (strcmp(argv[i], "--display") == 0) { |
735 |
+ |
i++; |
736 |
+ |
if (i < argc) |
737 |
+ |
x_display_name = strdup(argv[i]); |
738 |
+ |
#endif |
739 |
+ |
} else if (strcmp(argv[i], "--gui-connection") == 0) { |
740 |
+ |
argv[i++] = NULL; |
741 |
+ |
if (i < argc) { |
742 |
+ |
gui_connection_path = argv[i]; |
743 |
+ |
argv[i] = NULL; |
744 |
+ |
} |
745 |
+ |
} else if (valid_vmdir(argv[i])) { |
746 |
+ |
vmdir = argv[i]; |
747 |
+ |
argv[i] = NULL; |
748 |
+ |
printf("Using %s as vmdir.\n", vmdir); |
749 |
+ |
if (chdir(vmdir)) { |
750 |
+ |
printf("Failed to chdir to %s. Good bye.", vmdir); |
751 |
+ |
exit(1); |
752 |
+ |
} |
753 |
+ |
break; |
754 |
+ |
} |
755 |
+ |
} |
756 |
+ |
|
757 |
+ |
// Remove processed arguments |
758 |
+ |
for (int i=1; i<argc; i++) { |
759 |
+ |
int k; |
760 |
+ |
for (k=i; k<argc; k++) |
761 |
+ |
if (argv[k] != NULL) |
762 |
+ |
break; |
763 |
+ |
if (k > i) { |
764 |
+ |
k -= i; |
765 |
+ |
for (int j=i+k; j<argc; j++) |
766 |
+ |
argv[j-k] = argv[j]; |
767 |
+ |
argc -= k; |
768 |
+ |
} |
769 |
+ |
} |
770 |
+ |
|
771 |
+ |
// Connect to the external GUI |
772 |
+ |
if (gui_connection_path) { |
773 |
+ |
if ((gui_connection = rpc_init_client(gui_connection_path)) == NULL) { |
774 |
+ |
fprintf(stderr, "Failed to initialize RPC client connection to the GUI\n"); |
775 |
+ |
return 1; |
776 |
+ |
} |
777 |
+ |
} |
778 |
+ |
|
779 |
+ |
#ifdef ENABLE_GTK |
780 |
+ |
if (!gui_connection) { |
781 |
+ |
// Init GTK |
782 |
+ |
gtk_set_locale(); |
783 |
+ |
gtk_init(&argc, &argv); |
784 |
+ |
} |
785 |
+ |
#endif |
786 |
+ |
|
787 |
+ |
// Read preferences |
788 |
+ |
PrefsInit(vmdir, argc, argv); |
789 |
+ |
|
790 |
+ |
// Any command line arguments left? |
791 |
+ |
for (int i=1; i<argc; i++) { |
792 |
+ |
if (argv[i][0] == '-') { |
793 |
+ |
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
794 |
+ |
usage(argv[0]); |
795 |
+ |
} |
796 |
+ |
} |
797 |
+ |
|
798 |
+ |
#ifdef USE_SDL |
799 |
+ |
// Initialize SDL system |
800 |
+ |
if (!init_sdl()) |
801 |
+ |
goto quit; |
802 |
+ |
#endif |
803 |
+ |
|
804 |
+ |
#ifndef USE_SDL_VIDEO |
805 |
+ |
// Open display |
806 |
+ |
x_display = XOpenDisplay(x_display_name); |
807 |
+ |
if (x_display == NULL) { |
808 |
+ |
char str[256]; |
809 |
+ |
sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name)); |
810 |
+ |
ErrorAlert(str); |
811 |
+ |
goto quit; |
812 |
+ |
} |
813 |
+ |
|
814 |
+ |
#if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON) |
815 |
+ |
// Fork out, so we can return from fullscreen mode when things get ugly |
816 |
+ |
XF86DGAForkApp(DefaultScreen(x_display)); |
817 |
+ |
#endif |
818 |
+ |
#endif |
819 |
+ |
|
820 |
+ |
#ifdef ENABLE_MON |
821 |
+ |
// Initialize mon |
822 |
+ |
mon_init(); |
823 |
+ |
#endif |
824 |
+ |
|
825 |
+ |
// Install signal handlers |
826 |
+ |
if (!install_signal_handlers()) |
827 |
+ |
goto quit; |
828 |
+ |
|
829 |
+ |
// Initialize VM system |
830 |
+ |
vm_init(); |
831 |
+ |
|
832 |
+ |
// Get system info |
833 |
+ |
get_system_info(); |
834 |
|
|
835 |
|
// Init system routines |
836 |
|
SysInit(); |
863 |
|
D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed))); |
864 |
|
|
865 |
|
// Create area for DR Cache |
866 |
< |
if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
866 |
> |
if (vm_mac_acquire_fixed(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) { |
867 |
|
sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno)); |
868 |
|
ErrorAlert(str); |
869 |
|
goto quit; |
870 |
|
} |
871 |
|
dr_emulator_area_mapped = true; |
872 |
< |
if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
872 |
> |
if (vm_mac_acquire_fixed(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) { |
873 |
|
sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno)); |
874 |
|
ErrorAlert(str); |
875 |
|
goto quit; |
891 |
|
ErrorAlert(str); |
892 |
|
goto quit; |
893 |
|
} |
894 |
< |
|
828 |
< |
// Create area for Mac ROM |
829 |
< |
if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) { |
830 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
831 |
< |
ErrorAlert(str); |
832 |
< |
goto quit; |
833 |
< |
} |
834 |
< |
ROMBaseHost = Mac2HostAddr(ROM_BASE); |
835 |
< |
#if !EMULATED_PPC |
836 |
< |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
837 |
< |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
838 |
< |
ErrorAlert(str); |
839 |
< |
goto quit; |
840 |
< |
} |
841 |
< |
#endif |
842 |
< |
rom_area_mapped = true; |
843 |
< |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE)); |
844 |
< |
|
894 |
> |
|
895 |
|
// Create area for Mac RAM |
896 |
|
RAMSize = PrefsFindInt32("ramsize"); |
897 |
|
if (RAMSize < 8*1024*1024) { |
899 |
|
RAMSize = 8*1024*1024; |
900 |
|
} |
901 |
|
memory_mapped_from_zero = false; |
902 |
+ |
ram_rom_areas_contiguous = false; |
903 |
|
#if REAL_ADDRESSING && HAVE_LINKER_SCRIPT |
904 |
< |
if (vm_mac_acquire(0, RAMSize) == 0) { |
904 |
> |
if (vm_mac_acquire_fixed(0, RAMSize) == 0) { |
905 |
|
D(bug("Could allocate RAM from 0x0000\n")); |
906 |
|
RAMBase = 0; |
907 |
+ |
RAMBaseHost = Mac2HostAddr(RAMBase); |
908 |
|
memory_mapped_from_zero = true; |
909 |
|
} |
910 |
|
#endif |
911 |
|
if (!memory_mapped_from_zero) { |
912 |
|
#ifndef PAGEZERO_HACK |
913 |
|
// Create Low Memory area (0x0000..0x3000) |
914 |
< |
if (vm_mac_acquire(0, 0x3000) < 0) { |
914 |
> |
if (vm_mac_acquire_fixed(0, 0x3000) < 0) { |
915 |
|
sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno)); |
916 |
|
ErrorAlert(str); |
917 |
|
goto quit; |
918 |
|
} |
919 |
|
lm_area_mapped = true; |
920 |
|
#endif |
921 |
< |
if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) { |
921 |
> |
#if REAL_ADDRESSING |
922 |
> |
// Allocate RAM at any address. Since ROM must be higher than RAM, allocate the RAM |
923 |
> |
// and ROM areas contiguously, plus a little extra to allow for ROM address alignment. |
924 |
> |
RAMBaseHost = vm_mac_acquire(RAMSize + ROM_AREA_SIZE + ROM_ALIGNMENT); |
925 |
> |
if (RAMBaseHost == VM_MAP_FAILED) { |
926 |
> |
sprintf(str, GetString(STR_RAM_ROM_MMAP_ERR), strerror(errno)); |
927 |
> |
ErrorAlert(str); |
928 |
> |
goto quit; |
929 |
> |
} |
930 |
> |
RAMBase = Host2MacAddr(RAMBaseHost); |
931 |
> |
ROMBase = (RAMBase + RAMSize + ROM_ALIGNMENT -1) & -ROM_ALIGNMENT; |
932 |
> |
ROMBaseHost = Mac2HostAddr(ROMBase); |
933 |
> |
ram_rom_areas_contiguous = true; |
934 |
> |
#else |
935 |
> |
if (vm_mac_acquire_fixed(RAM_BASE, RAMSize) < 0) { |
936 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
937 |
|
ErrorAlert(str); |
938 |
|
goto quit; |
939 |
|
} |
940 |
|
RAMBase = RAM_BASE; |
941 |
+ |
RAMBaseHost = Mac2HostAddr(RAMBase); |
942 |
+ |
#endif |
943 |
|
} |
876 |
– |
RAMBaseHost = Mac2HostAddr(RAMBase); |
944 |
|
#if !EMULATED_PPC |
945 |
|
if (vm_protect(RAMBaseHost, RAMSize, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
946 |
|
sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno)); |
951 |
|
ram_area_mapped = true; |
952 |
|
D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase)); |
953 |
|
|
954 |
< |
if (RAMBase > ROM_BASE) { |
955 |
< |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
954 |
> |
if (RAMBase > KernelDataAddr) { |
955 |
> |
ErrorAlert(GetString(STR_RAM_AREA_TOO_HIGH_ERR)); |
956 |
|
goto quit; |
957 |
|
} |
891 |
– |
|
892 |
– |
// Load Mac ROM |
893 |
– |
rom_path = PrefsFindString("rom"); |
894 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY); |
895 |
– |
if (rom_fd < 0) { |
896 |
– |
rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME2, O_RDONLY); |
897 |
– |
if (rom_fd < 0) { |
898 |
– |
ErrorAlert(GetString(STR_NO_ROM_FILE_ERR)); |
899 |
– |
goto quit; |
900 |
– |
} |
901 |
– |
} |
902 |
– |
printf(GetString(STR_READING_ROM_FILE)); |
903 |
– |
rom_size = lseek(rom_fd, 0, SEEK_END); |
904 |
– |
lseek(rom_fd, 0, SEEK_SET); |
905 |
– |
rom_tmp = new uint8[ROM_SIZE]; |
906 |
– |
actual = read(rom_fd, (void *)rom_tmp, ROM_SIZE); |
907 |
– |
close(rom_fd); |
958 |
|
|
959 |
< |
// Decode Mac ROM |
960 |
< |
if (!DecodeROM(rom_tmp, actual)) { |
961 |
< |
if (rom_size != 4*1024*1024) { |
962 |
< |
ErrorAlert(GetString(STR_ROM_SIZE_ERR)); |
963 |
< |
goto quit; |
914 |
< |
} else { |
915 |
< |
ErrorAlert(GetString(STR_ROM_FILE_READ_ERR)); |
959 |
> |
// Create area for Mac ROM |
960 |
> |
if (!ram_rom_areas_contiguous) { |
961 |
> |
if (vm_mac_acquire_fixed(ROM_BASE, ROM_AREA_SIZE) < 0) { |
962 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
963 |
> |
ErrorAlert(str); |
964 |
|
goto quit; |
965 |
|
} |
966 |
+ |
ROMBase = ROM_BASE; |
967 |
+ |
ROMBaseHost = Mac2HostAddr(ROMBase); |
968 |
|
} |
969 |
< |
delete[] rom_tmp; |
969 |
> |
#if !EMULATED_PPC |
970 |
> |
if (vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_WRITE | VM_PAGE_EXECUTE) < 0) { |
971 |
> |
sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno)); |
972 |
> |
ErrorAlert(str); |
973 |
> |
goto quit; |
974 |
> |
} |
975 |
> |
#endif |
976 |
> |
rom_area_mapped = true; |
977 |
> |
D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROMBase)); |
978 |
> |
|
979 |
> |
if (RAMBase > ROMBase) { |
980 |
> |
ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR)); |
981 |
> |
goto quit; |
982 |
> |
} |
983 |
> |
|
984 |
> |
// Load Mac ROM |
985 |
> |
if (!load_mac_rom()) |
986 |
> |
goto quit; |
987 |
|
|
988 |
|
// Initialize everything |
989 |
|
if (!InitAll(vmdir)) |
992 |
|
|
993 |
|
// Clear caches (as we loaded and patched code) and write protect ROM |
994 |
|
#if !EMULATED_PPC |
995 |
< |
flush_icache_range(ROM_BASE, ROM_BASE + ROM_AREA_SIZE); |
995 |
> |
flush_icache_range(ROMBase, ROMBase + ROM_AREA_SIZE); |
996 |
|
#endif |
997 |
|
vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ | VM_PAGE_EXECUTE); |
998 |
|
|
1107 |
|
|
1108 |
|
// Delete ROM area |
1109 |
|
if (rom_area_mapped) |
1110 |
< |
vm_mac_release(ROM_BASE, ROM_AREA_SIZE); |
1110 |
> |
vm_mac_release(ROMBase, ROM_AREA_SIZE); |
1111 |
|
|
1112 |
|
// Delete DR cache areas |
1113 |
|
if (dr_emulator_area_mapped) |
1226 |
|
// Jump to ROM boot routine |
1227 |
|
D(bug("Jumping to ROM\n")); |
1228 |
|
#if EMULATED_PPC |
1229 |
< |
jump_to_rom(ROM_BASE + 0x310000); |
1229 |
> |
jump_to_rom(ROMBase + 0x310000); |
1230 |
|
#else |
1231 |
< |
jump_to_rom(ROM_BASE + 0x310000, (uint32)emulator_data); |
1231 |
> |
jump_to_rom(ROMBase + 0x310000, (uint32)emulator_data); |
1232 |
|
#endif |
1233 |
|
D(bug("Returned from ROM\n")); |
1234 |
|
|
1314 |
|
|
1315 |
|
void MakeExecutable(int dummy, uint32 start, uint32 length) |
1316 |
|
{ |
1317 |
< |
if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE))) |
1317 |
> |
if ((start >= ROMBase) && (start < (ROMBase + ROM_SIZE))) |
1318 |
|
return; |
1319 |
|
#if EMULATED_PPC |
1320 |
|
FlushCodeCache(start, start + length); |
1657 |
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1658 |
|
DisableInterrupt(); |
1659 |
|
if (ROMType == ROMTYPE_NEWWORLD) |
1660 |
< |
ppc_interrupt(ROM_BASE + 0x312b1c, KernelDataAddr); |
1660 |
> |
ppc_interrupt(ROMBase + 0x312b1c, KernelDataAddr); |
1661 |
|
else |
1662 |
< |
ppc_interrupt(ROM_BASE + 0x312a3c, KernelDataAddr); |
1662 |
> |
ppc_interrupt(ROMBase + 0x312a3c, KernelDataAddr); |
1663 |
|
|
1664 |
|
// Reset normal stack |
1665 |
|
sigaltstack(&sig_stack, NULL); |
1745 |
|
num_segv++; |
1746 |
|
|
1747 |
|
// Fault in Mac ROM or RAM or DR Cache? |
1748 |
< |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE)); |
1748 |
> |
bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)) || (r->pc() >= DR_CACHE_BASE && r->pc() < (DR_CACHE_BASE + DR_CACHE_SIZE)); |
1749 |
|
if (mac_fault) { |
1750 |
|
|
1751 |
|
// "VM settings" during MacOS 8 installation |
1752 |
< |
if (r->pc() == ROM_BASE + 0x488160 && r->gpr(20) == 0xf8000000) { |
1752 |
> |
if (r->pc() == ROMBase + 0x488160 && r->gpr(20) == 0xf8000000) { |
1753 |
|
r->pc() += 4; |
1754 |
|
r->gpr(8) = 0; |
1755 |
|
return; |
1756 |
|
|
1757 |
|
// MacOS 8.5 installation |
1758 |
< |
} else if (r->pc() == ROM_BASE + 0x488140 && r->gpr(16) == 0xf8000000) { |
1758 |
> |
} else if (r->pc() == ROMBase + 0x488140 && r->gpr(16) == 0xf8000000) { |
1759 |
|
r->pc() += 4; |
1760 |
|
r->gpr(8) = 0; |
1761 |
|
return; |
1762 |
|
|
1763 |
|
// MacOS 8 serial drivers on startup |
1764 |
< |
} else if (r->pc() == ROM_BASE + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1764 |
> |
} else if (r->pc() == ROMBase + 0x48e080 && (r->gpr(8) == 0xf3012002 || r->gpr(8) == 0xf3012000)) { |
1765 |
|
r->pc() += 4; |
1766 |
|
r->gpr(8) = 0; |
1767 |
|
return; |
1768 |
|
|
1769 |
|
// MacOS 8.1 serial drivers on startup |
1770 |
< |
} else if (r->pc() == ROM_BASE + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1770 |
> |
} else if (r->pc() == ROMBase + 0x48c5e0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1771 |
|
r->pc() += 4; |
1772 |
|
return; |
1773 |
< |
} else if (r->pc() == ROM_BASE + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1773 |
> |
} else if (r->pc() == ROMBase + 0x4a10a0 && (r->gpr(20) == 0xf3012002 || r->gpr(20) == 0xf3012000)) { |
1774 |
|
r->pc() += 4; |
1775 |
|
return; |
1776 |
|
|
1903 |
|
|
1904 |
|
// Ignore ROM writes (including to the zero page, which is read-only) |
1905 |
|
if (transfer_type == TYPE_STORE && |
1906 |
< |
((addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) || |
1906 |
> |
((addr >= ROMBase && addr < ROMBase + ROM_SIZE) || |
1907 |
|
(addr >= SheepMem::ZeroPage() && addr < SheepMem::ZeroPage() + SheepMem::PageSize()))) { |
1908 |
|
// D(bug("WARNING: %s write access to ROM at %08lx, pc %08lx\n", transfer_size == SIZE_BYTE ? "Byte" : transfer_size == SIZE_HALFWORD ? "Halfword" : "Word", addr, r->pc())); |
1909 |
|
if (addr_mode == MODE_U || addr_mode == MODE_UX) |
1994 |
|
#endif |
1995 |
|
|
1996 |
|
// Fault in Mac ROM or RAM? |
1997 |
< |
bool mac_fault = (r->pc() >= ROM_BASE) && (r->pc() < (ROM_BASE + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1997 |
> |
bool mac_fault = (r->pc() >= ROMBase) && (r->pc() < (ROMBase + ROM_AREA_SIZE)) || (r->pc() >= RAMBase) && (r->pc() < (RAMBase + RAMSize)); |
1998 |
|
if (mac_fault) { |
1999 |
|
|
2000 |
|
// Get opcode and divide into fields |
2162 |
|
|
2163 |
|
// Allocate SheepShaver globals |
2164 |
|
proc = base; |
2165 |
< |
if (vm_mac_acquire(base, size) < 0) |
2165 |
> |
if (vm_mac_acquire_fixed(base, size) < 0) |
2166 |
|
return false; |
2167 |
|
|
2168 |
|
// Allocate page with all bits set to 0, right in the middle |
2175 |
|
#if EMULATED_PPC |
2176 |
|
// Allocate alternate stack for PowerPC interrupt routine |
2177 |
|
sig_stack = base + size; |
2178 |
< |
if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0) |
2178 |
> |
if (vm_mac_acquire_fixed(sig_stack, SIG_STACK_SIZE) < 0) |
2179 |
|
return false; |
2180 |
|
#endif |
2181 |
|
|