1 |
|
/* |
2 |
|
* sheepshaver_glue.cpp - Glue Kheperix CPU to SheepShaver CPU engine interface |
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 |
28 |
|
#include "macos_util.h" |
29 |
|
#include "block-alloc.hpp" |
30 |
|
#include "sigsegv.h" |
31 |
– |
#include "spcflags.h" |
31 |
|
#include "cpu/ppc/ppc-cpu.hpp" |
32 |
|
#include "cpu/ppc/ppc-operations.hpp" |
33 |
+ |
#include "cpu/ppc/ppc-instructions.hpp" |
34 |
+ |
#include "thunks.h" |
35 |
|
|
36 |
|
// Used for NativeOp trampolines |
37 |
|
#include "video.h" |
38 |
|
#include "name_registry.h" |
39 |
|
#include "serial.h" |
40 |
+ |
#include "ether.h" |
41 |
|
|
42 |
|
#include <stdio.h> |
43 |
|
|
49 |
|
#define DEBUG 0 |
50 |
|
#include "debug.h" |
51 |
|
|
52 |
+ |
// Emulation time statistics |
53 |
+ |
#define EMUL_TIME_STATS 1 |
54 |
+ |
|
55 |
+ |
#if EMUL_TIME_STATS |
56 |
+ |
static clock_t emul_start_time; |
57 |
+ |
static uint32 interrupt_count = 0; |
58 |
+ |
static clock_t interrupt_time = 0; |
59 |
+ |
static uint32 exec68k_count = 0; |
60 |
+ |
static clock_t exec68k_time = 0; |
61 |
+ |
static uint32 native_exec_count = 0; |
62 |
+ |
static clock_t native_exec_time = 0; |
63 |
+ |
static uint32 macos_exec_count = 0; |
64 |
+ |
static clock_t macos_exec_time = 0; |
65 |
+ |
#endif |
66 |
+ |
|
67 |
|
static void enter_mon(void) |
68 |
|
{ |
69 |
|
// Start up mon in real-mode |
73 |
|
#endif |
74 |
|
} |
75 |
|
|
76 |
+ |
// From main_*.cpp |
77 |
+ |
extern uintptr SignalStackBase(); |
78 |
+ |
|
79 |
+ |
// From rsrc_patches.cpp |
80 |
+ |
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h); |
81 |
+ |
|
82 |
+ |
// PowerPC EmulOp to exit from emulation looop |
83 |
+ |
const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1; |
84 |
+ |
|
85 |
|
// Enable multicore (main/interrupts) cpu emulation? |
86 |
|
#define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0) |
87 |
|
|
100 |
|
// Pointer to Kernel Data |
101 |
|
static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE; |
102 |
|
|
103 |
+ |
// SIGSEGV handler |
104 |
+ |
static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
105 |
+ |
|
106 |
+ |
// JIT Compiler enabled? |
107 |
+ |
static inline bool enable_jit_p() |
108 |
+ |
{ |
109 |
+ |
return PrefsFindBool("jit"); |
110 |
+ |
} |
111 |
+ |
|
112 |
|
|
113 |
|
/** |
114 |
|
* PowerPC emulator glue with special 'sheep' opcodes |
115 |
|
**/ |
116 |
|
|
117 |
< |
struct sheepshaver_exec_return { }; |
117 |
> |
enum { |
118 |
> |
PPC_I(SHEEP) = PPC_I(MAX), |
119 |
> |
PPC_I(SHEEP_MAX) |
120 |
> |
}; |
121 |
|
|
122 |
|
class sheepshaver_cpu |
123 |
|
: public powerpc_cpu |
130 |
|
// Constructor |
131 |
|
sheepshaver_cpu(); |
132 |
|
|
133 |
< |
// Condition Register accessors |
133 |
> |
// CR & XER accessors |
134 |
|
uint32 get_cr() const { return cr().get(); } |
135 |
|
void set_cr(uint32 v) { cr().set(v); } |
136 |
+ |
uint32 get_xer() const { return xer().get(); } |
137 |
+ |
void set_xer(uint32 v) { xer().set(v); } |
138 |
|
|
139 |
< |
// Execution loop |
140 |
< |
void execute(uint32 entry, bool enable_cache = false); |
139 |
> |
// Execute EMUL_OP routine |
140 |
> |
void execute_emul_op(uint32 emul_op); |
141 |
|
|
142 |
|
// Execute 68k routine |
143 |
|
void execute_68k(uint32 entry, M68kRegisters *r); |
148 |
|
// Execute MacOS/PPC code |
149 |
|
uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args); |
150 |
|
|
151 |
+ |
// Compile one instruction |
152 |
+ |
virtual bool compile1(codegen_context_t & cg_context); |
153 |
+ |
|
154 |
|
// Resource manager thunk |
155 |
|
void get_resource(uint32 old_get_resource); |
156 |
|
|
158 |
|
void interrupt(uint32 entry); |
159 |
|
void handle_interrupt(); |
160 |
|
|
161 |
< |
// spcflags for interrupts handling |
162 |
< |
static uint32 spcflags; |
120 |
< |
|
121 |
< |
// Lazy memory allocator (one item at a time) |
122 |
< |
void *operator new(size_t size) |
123 |
< |
{ return allocator_helper< sheepshaver_cpu, lazy_allocator >::allocate(); } |
124 |
< |
void operator delete(void *p) |
125 |
< |
{ allocator_helper< sheepshaver_cpu, lazy_allocator >::deallocate(p); } |
126 |
< |
// FIXME: really make surre array allocation fail at link time? |
127 |
< |
void *operator new[](size_t); |
128 |
< |
void operator delete[](void *p); |
161 |
> |
// Make sure the SIGSEGV handler can access CPU registers |
162 |
> |
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
163 |
|
}; |
164 |
|
|
165 |
< |
uint32 sheepshaver_cpu::spcflags = 0; |
166 |
< |
lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator; |
165 |
> |
// Memory allocator returning areas aligned on 16-byte boundaries |
166 |
> |
void *operator new(size_t size) |
167 |
> |
{ |
168 |
> |
void *p; |
169 |
> |
|
170 |
> |
/* XXX: try different approaches */ |
171 |
> |
if (posix_memalign(&p, 16, size) != 0) |
172 |
> |
throw std::bad_alloc(); |
173 |
> |
|
174 |
> |
return p; |
175 |
> |
} |
176 |
> |
|
177 |
> |
void operator delete(void *p) |
178 |
> |
{ |
179 |
> |
free(p); |
180 |
> |
} |
181 |
|
|
182 |
|
sheepshaver_cpu::sheepshaver_cpu() |
183 |
< |
: powerpc_cpu() |
183 |
> |
: powerpc_cpu(enable_jit_p()) |
184 |
|
{ |
185 |
|
init_decoder(); |
186 |
|
} |
187 |
|
|
188 |
|
void sheepshaver_cpu::init_decoder() |
189 |
|
{ |
142 |
– |
#ifndef PPC_NO_STATIC_II_INDEX_TABLE |
143 |
– |
static bool initialized = false; |
144 |
– |
if (initialized) |
145 |
– |
return; |
146 |
– |
initialized = true; |
147 |
– |
#endif |
148 |
– |
|
190 |
|
static const instr_info_t sheep_ii_table[] = { |
191 |
|
{ "sheep", |
192 |
< |
(execute_fn)&sheepshaver_cpu::execute_sheep, |
192 |
> |
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
193 |
|
NULL, |
194 |
+ |
PPC_I(SHEEP), |
195 |
|
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
196 |
|
} |
197 |
|
}; |
219 |
|
typedef bit_field< 21, 25 > NATIVE_OP_field; |
220 |
|
typedef bit_field< 26, 31 > EMUL_OP_field; |
221 |
|
|
222 |
+ |
// Execute EMUL_OP routine |
223 |
+ |
void sheepshaver_cpu::execute_emul_op(uint32 emul_op) |
224 |
+ |
{ |
225 |
+ |
M68kRegisters r68; |
226 |
+ |
WriteMacInt32(XLM_68K_R25, gpr(25)); |
227 |
+ |
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP); |
228 |
+ |
for (int i = 0; i < 8; i++) |
229 |
+ |
r68.d[i] = gpr(8 + i); |
230 |
+ |
for (int i = 0; i < 7; i++) |
231 |
+ |
r68.a[i] = gpr(16 + i); |
232 |
+ |
r68.a[7] = gpr(1); |
233 |
+ |
uint32 saved_cr = get_cr() & CR_field<2>::mask(); |
234 |
+ |
uint32 saved_xer = get_xer(); |
235 |
+ |
EmulOp(&r68, gpr(24), emul_op); |
236 |
+ |
set_cr(saved_cr); |
237 |
+ |
set_xer(saved_xer); |
238 |
+ |
for (int i = 0; i < 8; i++) |
239 |
+ |
gpr(8 + i) = r68.d[i]; |
240 |
+ |
for (int i = 0; i < 7; i++) |
241 |
+ |
gpr(16 + i) = r68.a[i]; |
242 |
+ |
gpr(1) = r68.a[7]; |
243 |
+ |
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
244 |
+ |
} |
245 |
+ |
|
246 |
|
// Execute SheepShaver instruction |
247 |
|
void sheepshaver_cpu::execute_sheep(uint32 opcode) |
248 |
|
{ |
255 |
|
break; |
256 |
|
|
257 |
|
case 1: // EXEC_RETURN |
258 |
< |
throw sheepshaver_exec_return(); |
258 |
> |
spcflags().set(SPCFLAG_CPU_EXEC_RETURN); |
259 |
|
break; |
260 |
|
|
261 |
|
case 2: // EXEC_NATIVE |
266 |
|
pc() += 4; |
267 |
|
break; |
268 |
|
|
269 |
< |
default: { // EMUL_OP |
270 |
< |
M68kRegisters r68; |
205 |
< |
WriteMacInt32(XLM_68K_R25, gpr(25)); |
206 |
< |
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP); |
207 |
< |
for (int i = 0; i < 8; i++) |
208 |
< |
r68.d[i] = gpr(8 + i); |
209 |
< |
for (int i = 0; i < 7; i++) |
210 |
< |
r68.a[i] = gpr(16 + i); |
211 |
< |
r68.a[7] = gpr(1); |
212 |
< |
EmulOp(&r68, gpr(24), EMUL_OP_field::extract(opcode) - 3); |
213 |
< |
for (int i = 0; i < 8; i++) |
214 |
< |
gpr(8 + i) = r68.d[i]; |
215 |
< |
for (int i = 0; i < 7; i++) |
216 |
< |
gpr(16 + i) = r68.a[i]; |
217 |
< |
gpr(1) = r68.a[7]; |
218 |
< |
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
269 |
> |
default: // EMUL_OP |
270 |
> |
execute_emul_op(EMUL_OP_field::extract(opcode) - 3); |
271 |
|
pc() += 4; |
272 |
|
break; |
273 |
|
} |
222 |
– |
} |
274 |
|
} |
275 |
|
|
276 |
< |
// Execution loop |
277 |
< |
void sheepshaver_cpu::execute(uint32 entry, bool enable_cache) |
276 |
> |
// Compile one instruction |
277 |
> |
bool sheepshaver_cpu::compile1(codegen_context_t & cg_context) |
278 |
|
{ |
279 |
< |
try { |
280 |
< |
powerpc_cpu::execute(entry, enable_cache); |
279 |
> |
#if PPC_ENABLE_JIT |
280 |
> |
const instr_info_t *ii = cg_context.instr_info; |
281 |
> |
if (ii->mnemo != PPC_I(SHEEP)) |
282 |
> |
return false; |
283 |
> |
|
284 |
> |
bool compiled = false; |
285 |
> |
powerpc_dyngen & dg = cg_context.codegen; |
286 |
> |
uint32 opcode = cg_context.opcode; |
287 |
> |
|
288 |
> |
switch (opcode & 0x3f) { |
289 |
> |
case 0: // EMUL_RETURN |
290 |
> |
dg.gen_invoke(QuitEmulator); |
291 |
> |
compiled = true; |
292 |
> |
break; |
293 |
> |
|
294 |
> |
case 1: // EXEC_RETURN |
295 |
> |
dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN); |
296 |
> |
compiled = true; |
297 |
> |
break; |
298 |
> |
|
299 |
> |
case 2: { // EXEC_NATIVE |
300 |
> |
uint32 selector = NATIVE_OP_field::extract(opcode); |
301 |
> |
switch (selector) { |
302 |
> |
case NATIVE_PATCH_NAME_REGISTRY: |
303 |
> |
dg.gen_invoke(DoPatchNameRegistry); |
304 |
> |
compiled = true; |
305 |
> |
break; |
306 |
> |
case NATIVE_VIDEO_INSTALL_ACCEL: |
307 |
> |
dg.gen_invoke(VideoInstallAccel); |
308 |
> |
compiled = true; |
309 |
> |
break; |
310 |
> |
case NATIVE_VIDEO_VBL: |
311 |
> |
dg.gen_invoke(VideoVBL); |
312 |
> |
compiled = true; |
313 |
> |
break; |
314 |
> |
case NATIVE_GET_RESOURCE: |
315 |
> |
case NATIVE_GET_1_RESOURCE: |
316 |
> |
case NATIVE_GET_IND_RESOURCE: |
317 |
> |
case NATIVE_GET_1_IND_RESOURCE: |
318 |
> |
case NATIVE_R_GET_RESOURCE: { |
319 |
> |
static const uint32 get_resource_ptr[] = { |
320 |
> |
XLM_GET_RESOURCE, |
321 |
> |
XLM_GET_1_RESOURCE, |
322 |
> |
XLM_GET_IND_RESOURCE, |
323 |
> |
XLM_GET_1_IND_RESOURCE, |
324 |
> |
XLM_R_GET_RESOURCE |
325 |
> |
}; |
326 |
> |
uint32 old_get_resource = ReadMacInt32(get_resource_ptr[selector - NATIVE_GET_RESOURCE]); |
327 |
> |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
328 |
> |
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr(); |
329 |
> |
dg.gen_invoke_CPU_im(func, old_get_resource); |
330 |
> |
compiled = true; |
331 |
> |
break; |
332 |
> |
} |
333 |
> |
case NATIVE_DISABLE_INTERRUPT: |
334 |
> |
dg.gen_invoke(DisableInterrupt); |
335 |
> |
compiled = true; |
336 |
> |
break; |
337 |
> |
case NATIVE_ENABLE_INTERRUPT: |
338 |
> |
dg.gen_invoke(EnableInterrupt); |
339 |
> |
compiled = true; |
340 |
> |
break; |
341 |
> |
case NATIVE_CHECK_LOAD_INVOC: |
342 |
> |
dg.gen_load_T0_GPR(3); |
343 |
> |
dg.gen_load_T1_GPR(4); |
344 |
> |
dg.gen_se_16_32_T1(); |
345 |
> |
dg.gen_load_T2_GPR(5); |
346 |
> |
dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc); |
347 |
> |
compiled = true; |
348 |
> |
break; |
349 |
> |
} |
350 |
> |
if (FN_field::test(opcode)) { |
351 |
> |
if (compiled) { |
352 |
> |
dg.gen_load_A0_LR(); |
353 |
> |
dg.gen_set_PC_A0(); |
354 |
> |
} |
355 |
> |
cg_context.done_compile = true; |
356 |
> |
} |
357 |
> |
else |
358 |
> |
cg_context.done_compile = false; |
359 |
> |
break; |
360 |
|
} |
361 |
< |
catch (sheepshaver_exec_return const &) { |
362 |
< |
// Nothing, simply return |
361 |
> |
|
362 |
> |
default: { // EMUL_OP |
363 |
> |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
364 |
> |
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr(); |
365 |
> |
dg.gen_invoke_CPU_im(func, EMUL_OP_field::extract(opcode) - 3); |
366 |
> |
cg_context.done_compile = false; |
367 |
> |
compiled = true; |
368 |
> |
break; |
369 |
|
} |
234 |
– |
catch (...) { |
235 |
– |
printf("ERROR: execute() received an unknown exception!\n"); |
236 |
– |
QuitEmulator(); |
370 |
|
} |
371 |
+ |
return compiled; |
372 |
+ |
#endif |
373 |
+ |
return false; |
374 |
|
} |
375 |
|
|
376 |
|
// Handle MacOS interrupt |
377 |
|
void sheepshaver_cpu::interrupt(uint32 entry) |
378 |
|
{ |
379 |
+ |
#if EMUL_TIME_STATS |
380 |
+ |
interrupt_count++; |
381 |
+ |
const clock_t interrupt_start = clock(); |
382 |
+ |
#endif |
383 |
+ |
|
384 |
|
#if !MULTICORE_CPU |
385 |
|
// Save program counters and branch registers |
386 |
|
uint32 saved_pc = pc(); |
390 |
|
#endif |
391 |
|
|
392 |
|
// Initialize stack pointer to SheepShaver alternate stack base |
393 |
< |
gpr(1) = SheepStack1Base - 64; |
393 |
> |
gpr(1) = SignalStackBase() - 64; |
394 |
|
|
395 |
|
// Build trampoline to return from interrupt |
396 |
< |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
396 |
> |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
397 |
|
|
398 |
|
// Prepare registers for nanokernel interrupt routine |
399 |
|
kernel_data->v[0x004 >> 2] = htonl(gpr(1)); |
412 |
|
gpr(1) = KernelDataAddr; |
413 |
|
gpr(7) = ntohl(kernel_data->v[0x660 >> 2]); |
414 |
|
gpr(8) = 0; |
415 |
< |
gpr(10) = (uint32)trampoline; |
416 |
< |
gpr(12) = (uint32)trampoline; |
415 |
> |
gpr(10) = trampoline.addr(); |
416 |
> |
gpr(12) = trampoline.addr(); |
417 |
|
gpr(13) = get_cr(); |
418 |
|
|
419 |
|
// rlwimi. r7,r7,8,0,0 |
434 |
|
ctr()= saved_ctr; |
435 |
|
gpr(1) = saved_sp; |
436 |
|
#endif |
437 |
+ |
|
438 |
+ |
#if EMUL_TIME_STATS |
439 |
+ |
interrupt_time += (clock() - interrupt_start); |
440 |
+ |
#endif |
441 |
|
} |
442 |
|
|
443 |
|
// Execute 68k routine |
444 |
|
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r) |
445 |
|
{ |
446 |
+ |
#if EMUL_TIME_STATS |
447 |
+ |
exec68k_count++; |
448 |
+ |
const clock_t exec68k_start = clock(); |
449 |
+ |
#endif |
450 |
+ |
|
451 |
|
#if SAFE_EXEC_68K |
452 |
|
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP) |
453 |
|
printf("FATAL: Execute68k() not called from EMUL_OP mode\n"); |
530 |
|
lr() = saved_lr; |
531 |
|
ctr()= saved_ctr; |
532 |
|
set_cr(saved_cr); |
533 |
+ |
|
534 |
+ |
#if EMUL_TIME_STATS |
535 |
+ |
exec68k_time += (clock() - exec68k_start); |
536 |
+ |
#endif |
537 |
|
} |
538 |
|
|
539 |
|
// Call MacOS PPC code |
540 |
|
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args) |
541 |
|
{ |
542 |
+ |
#if EMUL_TIME_STATS |
543 |
+ |
macos_exec_count++; |
544 |
+ |
const clock_t macos_exec_start = clock(); |
545 |
+ |
#endif |
546 |
+ |
|
547 |
|
// Save program counters and branch registers |
548 |
|
uint32 saved_pc = pc(); |
549 |
|
uint32 saved_lr = lr(); |
550 |
|
uint32 saved_ctr= ctr(); |
551 |
|
|
552 |
|
// Build trampoline with EXEC_RETURN |
553 |
< |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
554 |
< |
lr() = (uint32)trampoline; |
553 |
> |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
554 |
> |
lr() = trampoline.addr(); |
555 |
|
|
556 |
|
gpr(1) -= 64; // Create stack frame |
557 |
|
uint32 proc = ReadMacInt32(tvect); // Get routine address |
582 |
|
lr() = saved_lr; |
583 |
|
ctr()= saved_ctr; |
584 |
|
|
585 |
+ |
#if EMUL_TIME_STATS |
586 |
+ |
macos_exec_time += (clock() - macos_exec_start); |
587 |
+ |
#endif |
588 |
+ |
|
589 |
|
return retval; |
590 |
|
} |
591 |
|
|
595 |
|
// Save branch registers |
596 |
|
uint32 saved_lr = lr(); |
597 |
|
|
598 |
< |
const uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
599 |
< |
lr() = (uint32)trampoline; |
598 |
> |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
599 |
> |
WriteMacInt32(trampoline.addr(), POWERPC_EXEC_RETURN); |
600 |
> |
lr() = trampoline.addr(); |
601 |
|
|
602 |
|
execute(entry); |
603 |
|
|
606 |
|
} |
607 |
|
|
608 |
|
// Resource Manager thunk |
445 |
– |
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h); |
446 |
– |
|
609 |
|
inline void sheepshaver_cpu::get_resource(uint32 old_get_resource) |
610 |
|
{ |
611 |
|
uint32 type = gpr(3); |
689 |
|
if ((addr - ROM_BASE) < ROM_SIZE) |
690 |
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
691 |
|
|
692 |
< |
// Ignore all other faults, if requested |
693 |
< |
if (PrefsFindBool("ignoresegv")) |
694 |
< |
return SIGSEGV_RETURN_FAILURE; |
692 |
> |
// Get program counter of target CPU |
693 |
> |
sheepshaver_cpu * const cpu = current_cpu; |
694 |
> |
const uint32 pc = cpu->pc(); |
695 |
> |
|
696 |
> |
// Fault in Mac ROM or RAM? |
697 |
> |
bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize)); |
698 |
> |
if (mac_fault) { |
699 |
> |
|
700 |
> |
// "VM settings" during MacOS 8 installation |
701 |
> |
if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000) |
702 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
703 |
> |
|
704 |
> |
// MacOS 8.5 installation |
705 |
> |
else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000) |
706 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
707 |
> |
|
708 |
> |
// MacOS 8 serial drivers on startup |
709 |
> |
else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000)) |
710 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
711 |
> |
|
712 |
> |
// MacOS 8.1 serial drivers on startup |
713 |
> |
else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
714 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
715 |
> |
else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
716 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
717 |
> |
|
718 |
> |
// Ignore all other faults, if requested |
719 |
> |
if (PrefsFindBool("ignoresegv")) |
720 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
721 |
> |
} |
722 |
|
#else |
723 |
|
#error "FIXME: You don't have the capability to skip instruction within signal handlers" |
724 |
|
#endif |
740 |
|
// Initialize main CPU emulator |
741 |
|
main_cpu = new sheepshaver_cpu(); |
742 |
|
main_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000)); |
743 |
+ |
main_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000)); |
744 |
|
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
745 |
|
|
746 |
|
#if MULTICORE_CPU |
756 |
|
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n"); |
757 |
|
mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n"); |
758 |
|
#endif |
759 |
+ |
|
760 |
+ |
#if EMUL_TIME_STATS |
761 |
+ |
emul_start_time = clock(); |
762 |
+ |
#endif |
763 |
+ |
} |
764 |
+ |
|
765 |
+ |
/* |
766 |
+ |
* Deinitialize emulation |
767 |
+ |
*/ |
768 |
+ |
|
769 |
+ |
void exit_emul_ppc(void) |
770 |
+ |
{ |
771 |
+ |
#if EMUL_TIME_STATS |
772 |
+ |
clock_t emul_end_time = clock(); |
773 |
+ |
|
774 |
+ |
printf("### Statistics for SheepShaver emulation parts\n"); |
775 |
+ |
const clock_t emul_time = emul_end_time - emul_start_time; |
776 |
+ |
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC)); |
777 |
+ |
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count, |
778 |
+ |
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time)); |
779 |
+ |
|
780 |
+ |
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \ |
781 |
+ |
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \ |
782 |
+ |
printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \ |
783 |
+ |
double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \ |
784 |
+ |
100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \ |
785 |
+ |
} while (0) |
786 |
+ |
|
787 |
+ |
PRINT_STATS("Execute68k[Trap] execution", exec68k); |
788 |
+ |
PRINT_STATS("NativeOp execution", native_exec); |
789 |
+ |
PRINT_STATS("MacOS routine execution", macos_exec); |
790 |
+ |
|
791 |
+ |
#undef PRINT_STATS |
792 |
+ |
printf("\n"); |
793 |
+ |
#endif |
794 |
+ |
|
795 |
+ |
delete main_cpu; |
796 |
+ |
#if MULTICORE_CPU |
797 |
+ |
delete interrupt_cpu; |
798 |
+ |
#endif |
799 |
|
} |
800 |
|
|
801 |
|
/* |
805 |
|
void emul_ppc(uint32 entry) |
806 |
|
{ |
807 |
|
current_cpu = main_cpu; |
808 |
< |
#if DEBUG |
808 |
> |
#if 0 |
809 |
|
current_cpu->start_log(); |
810 |
|
#endif |
811 |
|
// start emulation loop and enable code translation or caching |
812 |
< |
current_cpu->execute(entry, true); |
812 |
> |
current_cpu->execute(entry); |
813 |
|
} |
814 |
|
|
815 |
|
/* |
816 |
|
* Handle PowerPC interrupt |
817 |
|
*/ |
818 |
|
|
819 |
< |
#if !ASYNC_IRQ |
819 |
> |
#if ASYNC_IRQ |
820 |
> |
void HandleInterrupt(void) |
821 |
> |
{ |
822 |
> |
main_cpu->handle_interrupt(); |
823 |
> |
} |
824 |
> |
#else |
825 |
|
void TriggerInterrupt(void) |
826 |
|
{ |
827 |
|
#if 0 |
837 |
|
void sheepshaver_cpu::handle_interrupt(void) |
838 |
|
{ |
839 |
|
// Do nothing if interrupts are disabled |
840 |
< |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
840 |
> |
if (*(int32 *)XLM_IRQ_NEST > 0) |
841 |
|
return; |
842 |
|
|
843 |
|
// Do nothing if there is no interrupt pending |
904 |
|
if (InterruptFlags & INTFLAG_VIA) { |
905 |
|
ClearInterruptFlag(INTFLAG_VIA); |
906 |
|
ADBInterrupt(); |
907 |
< |
ExecutePPC(VideoVBL); |
907 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
908 |
|
} |
909 |
|
} |
910 |
|
#endif |
914 |
|
} |
915 |
|
} |
916 |
|
|
682 |
– |
/* |
683 |
– |
* Execute NATIVE_OP opcode (called by PowerPC emulator) |
684 |
– |
*/ |
685 |
– |
|
686 |
– |
#define POWERPC_NATIVE_OP_INIT(LR, OP) \ |
687 |
– |
tswap32(POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2) |
688 |
– |
|
689 |
– |
// FIXME: Make sure 32-bit relocations are used |
690 |
– |
const uint32 NativeOpTable[NATIVE_OP_MAX] = { |
691 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_PATCH_NAME_REGISTRY), |
692 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_INSTALL_ACCEL), |
693 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_VBL), |
694 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_DO_DRIVER_IO), |
695 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_IRQ), |
696 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_INIT), |
697 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_TERM), |
698 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_OPEN), |
699 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_CLOSE), |
700 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_WPUT), |
701 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_RSRV), |
702 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_NOTHING), |
703 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_OPEN), |
704 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_IN), |
705 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_OUT), |
706 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CONTROL), |
707 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_STATUS), |
708 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CLOSE), |
709 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_RESOURCE), |
710 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_RESOURCE), |
711 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_IND_RESOURCE), |
712 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_IND_RESOURCE), |
713 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_R_GET_RESOURCE), |
714 |
– |
POWERPC_NATIVE_OP_INIT(0, NATIVE_DISABLE_INTERRUPT), |
715 |
– |
POWERPC_NATIVE_OP_INIT(0, NATIVE_ENABLE_INTERRUPT), |
716 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_MAKE_EXECUTABLE), |
717 |
– |
}; |
718 |
– |
|
917 |
|
static void get_resource(void); |
918 |
|
static void get_1_resource(void); |
919 |
|
static void get_ind_resource(void); |
924 |
|
|
925 |
|
static void NativeOp(int selector) |
926 |
|
{ |
927 |
+ |
#if EMUL_TIME_STATS |
928 |
+ |
native_exec_count++; |
929 |
+ |
const clock_t native_exec_start = clock(); |
930 |
+ |
#endif |
931 |
+ |
|
932 |
|
switch (selector) { |
933 |
|
case NATIVE_PATCH_NAME_REGISTRY: |
934 |
|
DoPatchNameRegistry(); |
943 |
|
GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4), |
944 |
|
(void *)GPR(5), GPR(6), GPR(7)); |
945 |
|
break; |
946 |
< |
case NATIVE_GET_RESOURCE: |
947 |
< |
get_resource(); |
946 |
> |
#ifdef WORDS_BIGENDIAN |
947 |
> |
case NATIVE_ETHER_IRQ: |
948 |
> |
EtherIRQ(); |
949 |
|
break; |
950 |
< |
case NATIVE_GET_1_RESOURCE: |
951 |
< |
get_1_resource(); |
950 |
> |
case NATIVE_ETHER_INIT: |
951 |
> |
GPR(3) = InitStreamModule((void *)GPR(3)); |
952 |
|
break; |
953 |
< |
case NATIVE_GET_IND_RESOURCE: |
954 |
< |
get_ind_resource(); |
953 |
> |
case NATIVE_ETHER_TERM: |
954 |
> |
TerminateStreamModule(); |
955 |
|
break; |
956 |
< |
case NATIVE_GET_1_IND_RESOURCE: |
957 |
< |
get_1_ind_resource(); |
956 |
> |
case NATIVE_ETHER_OPEN: |
957 |
> |
GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7)); |
958 |
> |
break; |
959 |
> |
case NATIVE_ETHER_CLOSE: |
960 |
> |
GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5)); |
961 |
|
break; |
962 |
< |
case NATIVE_R_GET_RESOURCE: |
963 |
< |
r_get_resource(); |
962 |
> |
case NATIVE_ETHER_WPUT: |
963 |
> |
GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4)); |
964 |
> |
break; |
965 |
> |
case NATIVE_ETHER_RSRV: |
966 |
> |
GPR(3) = ether_rsrv((queue_t *)GPR(3)); |
967 |
> |
break; |
968 |
> |
#else |
969 |
> |
case NATIVE_ETHER_INIT: |
970 |
> |
// FIXME: needs more complicated thunks |
971 |
> |
GPR(3) = false; |
972 |
|
break; |
973 |
+ |
#endif |
974 |
|
case NATIVE_SERIAL_NOTHING: |
975 |
|
case NATIVE_SERIAL_OPEN: |
976 |
|
case NATIVE_SERIAL_PRIME_IN: |
991 |
|
GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4)); |
992 |
|
break; |
993 |
|
} |
994 |
+ |
case NATIVE_GET_RESOURCE: |
995 |
+ |
case NATIVE_GET_1_RESOURCE: |
996 |
+ |
case NATIVE_GET_IND_RESOURCE: |
997 |
+ |
case NATIVE_GET_1_IND_RESOURCE: |
998 |
+ |
case NATIVE_R_GET_RESOURCE: { |
999 |
+ |
typedef void (*GetResourceCallback)(void); |
1000 |
+ |
static const GetResourceCallback get_resource_callbacks[] = { |
1001 |
+ |
get_resource, |
1002 |
+ |
get_1_resource, |
1003 |
+ |
get_ind_resource, |
1004 |
+ |
get_1_ind_resource, |
1005 |
+ |
r_get_resource |
1006 |
+ |
}; |
1007 |
+ |
get_resource_callbacks[selector - NATIVE_GET_RESOURCE](); |
1008 |
+ |
break; |
1009 |
+ |
} |
1010 |
|
case NATIVE_DISABLE_INTERRUPT: |
1011 |
|
DisableInterrupt(); |
1012 |
|
break; |
1016 |
|
case NATIVE_MAKE_EXECUTABLE: |
1017 |
|
MakeExecutable(0, (void *)GPR(4), GPR(5)); |
1018 |
|
break; |
1019 |
+ |
case NATIVE_CHECK_LOAD_INVOC: |
1020 |
+ |
check_load_invoc(GPR(3), GPR(4), GPR(5)); |
1021 |
+ |
break; |
1022 |
|
default: |
1023 |
|
printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector); |
1024 |
|
QuitEmulator(); |
1025 |
|
break; |
1026 |
|
} |
792 |
– |
} |
1027 |
|
|
1028 |
< |
/* |
1029 |
< |
* Execute native subroutine (LR must contain return address) |
1030 |
< |
*/ |
797 |
< |
|
798 |
< |
void ExecuteNative(int selector) |
799 |
< |
{ |
800 |
< |
uint32 tvect[2]; |
801 |
< |
tvect[0] = tswap32(POWERPC_NATIVE_OP_FUNC(selector)); |
802 |
< |
tvect[1] = 0; // Fake TVECT |
803 |
< |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
804 |
< |
M68kRegisters r; |
805 |
< |
Execute68k((uint32)&desc, &r); |
1028 |
> |
#if EMUL_TIME_STATS |
1029 |
> |
native_exec_time += (clock() - native_exec_start); |
1030 |
> |
#endif |
1031 |
|
} |
1032 |
|
|
1033 |
|
/* |
1048 |
|
|
1049 |
|
void Execute68kTrap(uint16 trap, M68kRegisters *r) |
1050 |
|
{ |
1051 |
< |
uint16 proc[2]; |
1052 |
< |
proc[0] = htons(trap); |
1053 |
< |
proc[1] = htons(M68K_RTS); |
1054 |
< |
Execute68k((uint32)proc, r); |
1051 |
> |
SheepVar proc_var(4); |
1052 |
> |
uint32 proc = proc_var.addr(); |
1053 |
> |
WriteMacInt16(proc, trap); |
1054 |
> |
WriteMacInt16(proc + 2, M68K_RTS); |
1055 |
> |
Execute68k(proc, r); |
1056 |
|
} |
1057 |
|
|
1058 |
|
/* |