1 |
|
/* |
2 |
|
* main_beos.cpp - Emulation core, BeOS implementation |
3 |
|
* |
4 |
< |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
4 |
> |
* SheepShaver (C) 1997-2004 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 |
88 |
|
#include "macos_util.h" |
89 |
|
#include "rom_patches.h" |
90 |
|
#include "user_strings.h" |
91 |
+ |
#include "thunks.h" |
92 |
|
|
93 |
|
#include "sheep_driver.h" |
94 |
|
|
117 |
|
const char RAM_AREA_NAME[] = "Macintosh RAM"; |
118 |
|
const char ROM_AREA_NAME[] = "Macintosh ROM"; |
119 |
|
const char DR_CACHE_AREA_NAME[] = "Macintosh DR Cache"; |
120 |
< |
|
121 |
< |
const uint32 ROM_AREA_SIZE = 0x500000; // Size of ROM area |
121 |
< |
|
122 |
< |
const uint32 KERNEL_DATA_BASE = 0x68ffe000; // Address of Kernel Data |
123 |
< |
const uint32 KERNEL_DATA2_BASE = 0x5fffe000;// Alternate address of Kernel Data |
124 |
< |
const uint32 KERNEL_AREA_SIZE = 0x2000; // Size of Kernel Data area |
120 |
> |
const char DR_EMULATOR_AREA_NAME[] = "Macintosh DR Emulator"; |
121 |
> |
const char SHEEP_AREA_NAME[] = "SheepShaver Virtual Stack"; |
122 |
|
|
123 |
|
const uint32 SIG_STACK_SIZE = 8192; // Size of signal stack |
124 |
|
|
125 |
|
const uint32 MSG_START = 'strt'; // Emulator start message |
126 |
|
|
127 |
|
|
131 |
– |
// Emulator Data |
132 |
– |
struct EmulatorData { |
133 |
– |
uint32 v[0x400]; |
134 |
– |
}; |
135 |
– |
|
136 |
– |
|
137 |
– |
// Kernel Data |
138 |
– |
struct KernelData { |
139 |
– |
uint32 v[0x400]; |
140 |
– |
EmulatorData ed; |
141 |
– |
}; |
142 |
– |
|
143 |
– |
|
128 |
|
// Application object |
129 |
|
class SheepShaver : public BApplication { |
130 |
|
public: |
143 |
|
// Initialize other variables |
144 |
|
sheep_fd = -1; |
145 |
|
emulator_data = NULL; |
146 |
< |
kernel_area = kernel_area2 = rom_area = ram_area = dr_cache_area = -1; |
146 |
> |
kernel_area = kernel_area2 = rom_area = ram_area = dr_cache_area = dr_emulator_area = -1; |
147 |
|
emul_thread = nvram_thread = tick_thread = -1; |
148 |
|
ReadyForSignals = false; |
149 |
|
AllowQuitting = true; |
191 |
|
area_id rom_area; // ROM area ID |
192 |
|
area_id ram_area; // RAM area ID |
193 |
|
area_id dr_cache_area; // DR Cache area ID |
194 |
+ |
area_id dr_emulator_area; // DR Emulator area ID |
195 |
|
|
196 |
|
struct sigaction sigusr1_action; // Interrupt signal (of emulator thread) |
197 |
|
struct sigaction sigsegv_action; // Data access exception signal (of emulator thread) |
215 |
|
uint32 KernelDataAddr; // Address of Kernel Data |
216 |
|
uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM |
217 |
|
uint32 DRCacheAddr; // Address of DR Cache |
218 |
+ |
uint32 DREmulatorAddr; // Address of DR Emulator |
219 |
|
uint32 PVR; // Theoretical PVR |
220 |
|
int64 CPUClockSpeed; // Processor clock speed (Hz) |
221 |
|
int64 BusClockSpeed; // Bus clock speed (Hz) |
223 |
|
|
224 |
|
static void *sig_stack = NULL; // Stack for signal handlers |
225 |
|
static void *extra_stack = NULL; // Stack for SIGSEGV inside interrupt handler |
226 |
+ |
uint32 SheepMem::page_size; // Size of a native page |
227 |
+ |
uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros |
228 |
+ |
uintptr SheepMem::base; // Address of SheepShaver data |
229 |
+ |
uintptr SheepMem::top; // Top of SheepShaver data (stack like storage) |
230 |
+ |
static area_id SheepMemArea; // SheepShaver data area ID |
231 |
|
|
232 |
|
|
233 |
|
// Prototypes |
316 |
|
area_id old_dr_cache_area = find_area(DR_CACHE_AREA_NAME); |
317 |
|
if (old_dr_cache_area > 0) |
318 |
|
delete_area(old_dr_cache_area); |
319 |
+ |
area_id old_dr_emulator_area = find_area(DR_EMULATOR_AREA_NAME); |
320 |
+ |
if (old_dr_emulator_area > 0) |
321 |
+ |
delete_area(old_dr_emulator_area); |
322 |
|
|
323 |
|
// Read preferences |
324 |
|
int argc = 0; |
366 |
|
void SheepShaver::StartEmulator(void) |
367 |
|
{ |
368 |
|
char str[256]; |
369 |
+ |
int16 i16; |
370 |
|
|
371 |
|
// Open sheep driver and remap low memory |
372 |
|
sheep_fd = open("/dev/sheep", 0); |
407 |
|
} |
408 |
|
D(bug("Kernel Data 2 area %ld at %p\n", kernel_area2, kernel_data2)); |
409 |
|
|
410 |
+ |
// Create area for SheepShaver data |
411 |
+ |
if (!SheepMem::Init()) { |
412 |
+ |
sprintf(str, GetString(STR_NO_SHEEP_MEM_AREA_ERR), strerror(SheepMemArea), SheepMemArea); |
413 |
+ |
ErrorAlert(str); |
414 |
+ |
PostMessage(B_QUIT_REQUESTED); |
415 |
+ |
return; |
416 |
+ |
} |
417 |
+ |
|
418 |
|
// Create area for Mac RAM |
419 |
|
RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary |
420 |
|
if (RAMSize < 8*1024*1024) { |
464 |
|
} |
465 |
|
D(bug("DR Cache area %ld at %p\n", dr_cache_area, DRCacheAddr)); |
466 |
|
|
467 |
+ |
// Create area for DR Emulator |
468 |
+ |
DREmulatorAddr = DR_EMULATOR_BASE; |
469 |
+ |
dr_emulator_area = create_area(DR_EMULATOR_AREA_NAME, (void **)&DREmulatorAddr, B_EXACT_ADDRESS, DR_EMULATOR_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); |
470 |
+ |
if (dr_emulator_area < 0) { |
471 |
+ |
sprintf(str, GetString(STR_NO_KERNEL_DATA_ERR), strerror(dr_emulator_area), dr_emulator_area); |
472 |
+ |
ErrorAlert(str); |
473 |
+ |
PostMessage(B_QUIT_REQUESTED); |
474 |
+ |
return; |
475 |
+ |
} |
476 |
+ |
D(bug("DR Emulator area %ld at %p\n", dr_emulator_area, DREmulatorAddr)); |
477 |
+ |
|
478 |
|
// Load NVRAM |
479 |
|
XPRAMInit(); |
480 |
|
|
481 |
|
// Set boot volume |
482 |
< |
drive = PrefsFindInt32("bootdrive"); |
482 |
> |
i16 = PrefsFindInt32("bootdrive"); |
483 |
|
XPRAM[0x1378] = i16 >> 8; |
484 |
|
XPRAM[0x1379] = i16 & 0xff; |
485 |
< |
driver = PrefsFindInt32("bootdriver"); |
485 |
> |
i16 = PrefsFindInt32("bootdriver"); |
486 |
|
XPRAM[0x137a] = i16 >> 8; |
487 |
|
XPRAM[0x137b] = i16 & 0xff; |
488 |
|
|
495 |
|
boot_globs[1] = htonl(RAMSize); |
496 |
|
boot_globs[2] = htonl((uint32)-1); // End of bank table |
497 |
|
|
498 |
+ |
// Init thunks |
499 |
+ |
if (!InitThunks()) { |
500 |
+ |
PostMessage(B_QUIT_REQUESTED); |
501 |
+ |
return; |
502 |
+ |
} |
503 |
+ |
|
504 |
|
// Init drivers |
505 |
|
SonyInit(); |
506 |
|
DiskInit(); |
597 |
|
WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR |
598 |
|
WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch |
599 |
|
WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode) |
600 |
+ |
WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0 |
601 |
|
#if !EMULATED_PPC |
602 |
|
WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator |
603 |
|
WriteMacInt32(XLM_ETHER_INIT, *(uint32 *)InitStreamModule); // DLPI ethernet driver functions |
694 |
|
DiskExit(); |
695 |
|
SonyExit(); |
696 |
|
|
697 |
+ |
// Delete thunks |
698 |
+ |
ThunksExit(); |
699 |
+ |
|
700 |
+ |
// Delete SheepShaver globals |
701 |
+ |
SheepMem::Exit(); |
702 |
+ |
|
703 |
+ |
// Delete DR Emulator area |
704 |
+ |
if (dr_emulator_area >= 0) |
705 |
+ |
delete_area(dr_emulator_area); |
706 |
+ |
|
707 |
|
// Delete DR Cache area |
708 |
|
if (dr_cache_area >= 0) |
709 |
|
delete_area(dr_cache_area); |
748 |
|
|
749 |
|
void SheepShaver::init_rom(void) |
750 |
|
{ |
751 |
+ |
// Size of a native page |
752 |
+ |
page_size = B_PAGE_SIZE; |
753 |
+ |
|
754 |
|
// Create area for ROM |
755 |
|
void *rom_addr = (void *)ROM_BASE; |
756 |
|
rom_area = create_area(ROM_AREA_NAME, &rom_addr, B_EXACT_ADDRESS, ROM_AREA_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); |
1295 |
|
|
1296 |
|
|
1297 |
|
/* |
1264 |
– |
* Execute PPC code from EMUL_OP routine (real mode switch) |
1265 |
– |
*/ |
1266 |
– |
|
1267 |
– |
void ExecutePPC(void (*func)()) |
1268 |
– |
{ |
1269 |
– |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, func); |
1270 |
– |
M68kRegisters r; |
1271 |
– |
Execute68k((uint32)&desc, &r); |
1272 |
– |
} |
1273 |
– |
|
1274 |
– |
|
1275 |
– |
/* |
1298 |
|
* Quit emulator (must only be called from main thread) |
1299 |
|
*/ |
1300 |
|
|
1349 |
|
|
1350 |
|
void PatchAfterStartup(void) |
1351 |
|
{ |
1352 |
< |
ExecutePPC(VideoInstallAccel); |
1352 |
> |
ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL); |
1353 |
|
InstallExtFS(); |
1354 |
|
} |
1355 |
|
|
1358 |
|
* NVRAM watchdog thread (saves NVRAM every minute) |
1359 |
|
*/ |
1360 |
|
|
1361 |
< |
static status_t SheepShaver::nvram_func(void *arg) |
1361 |
> |
status_t SheepShaver::nvram_func(void *arg) |
1362 |
|
{ |
1363 |
|
SheepShaver *obj = (SheepShaver *)arg; |
1364 |
|
|
1597 |
|
if (InterruptFlags & INTFLAG_VIA) { |
1598 |
|
ClearInterruptFlag(INTFLAG_VIA); |
1599 |
|
ADBInterrupt(); |
1600 |
< |
ExecutePPC(VideoVBL); |
1600 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
1601 |
|
} |
1602 |
|
} |
1603 |
|
#endif |
2092 |
|
} |
2093 |
|
|
2094 |
|
|
2095 |
+ |
/* |
2096 |
+ |
* Helpers to share 32-bit addressable data with MacOS |
2097 |
+ |
*/ |
2098 |
+ |
|
2099 |
+ |
bool SheepMem::Init(void) |
2100 |
+ |
{ |
2101 |
+ |
// Delete old area |
2102 |
+ |
area_id old_sheep_area = find_area(SHEEP_AREA_NAME); |
2103 |
+ |
if (old_sheep_area > 0) |
2104 |
+ |
delete_area(old_sheep_area); |
2105 |
+ |
|
2106 |
+ |
// Create area for SheepShaver data |
2107 |
+ |
base = 0x60000000; |
2108 |
+ |
SheepMemArea = create_area(SHEEP_AREA_NAME, (void **)&base, B_BASE_ADDRESS, size, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); |
2109 |
+ |
if (SheepMemArea < 0) |
2110 |
+ |
return false; |
2111 |
+ |
|
2112 |
+ |
// Create read-only area with all bits set to 0 |
2113 |
+ |
static const uint8 const_zero_page[4096] = {0,}; |
2114 |
+ |
zero_page = const_zero_page; |
2115 |
+ |
|
2116 |
+ |
D(bug("SheepShaver area %ld at %p\n", SheepMemArea, base)); |
2117 |
+ |
top = base + size; |
2118 |
+ |
return true; |
2119 |
+ |
} |
2120 |
+ |
|
2121 |
+ |
void SheepMem::Exit(void) |
2122 |
+ |
{ |
2123 |
+ |
if (SheepMemArea >= 0) |
2124 |
+ |
delete_area(SheepMemArea); |
2125 |
+ |
} |
2126 |
+ |
|
2127 |
+ |
|
2128 |
|
/* |
2129 |
|
* Display error alert |
2130 |
|
*/ |