1 |
/* |
2 |
* sheepshaver_glue.cpp - Glue Kheperix CPU to SheepShaver CPU engine interface |
3 |
* |
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 |
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 |
#include "cpu_emulation.h" |
23 |
#include "main.h" |
24 |
#include "prefs.h" |
25 |
#include "xlowmem.h" |
26 |
#include "emul_op.h" |
27 |
#include "rom_patches.h" |
28 |
#include "macos_util.h" |
29 |
#include "block-alloc.hpp" |
30 |
#include "sigsegv.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 |
#include "timer.h" |
42 |
|
43 |
#include <stdio.h> |
44 |
#include <stdlib.h> |
45 |
|
46 |
#ifdef USE_SDL_VIDEO |
47 |
#include <SDL_events.h> |
48 |
#endif |
49 |
|
50 |
#if ENABLE_MON |
51 |
#include "mon.h" |
52 |
#include "mon_disass.h" |
53 |
#endif |
54 |
|
55 |
#define DEBUG 0 |
56 |
#include "debug.h" |
57 |
|
58 |
// Emulation time statistics |
59 |
#ifndef EMUL_TIME_STATS |
60 |
#define EMUL_TIME_STATS 0 |
61 |
#endif |
62 |
|
63 |
#if EMUL_TIME_STATS |
64 |
static clock_t emul_start_time; |
65 |
static uint32 interrupt_count = 0, ppc_interrupt_count = 0; |
66 |
static clock_t interrupt_time = 0; |
67 |
static uint32 exec68k_count = 0; |
68 |
static clock_t exec68k_time = 0; |
69 |
static uint32 native_exec_count = 0; |
70 |
static clock_t native_exec_time = 0; |
71 |
static uint32 macos_exec_count = 0; |
72 |
static clock_t macos_exec_time = 0; |
73 |
#endif |
74 |
|
75 |
static void enter_mon(void) |
76 |
{ |
77 |
// Start up mon in real-mode |
78 |
#if ENABLE_MON |
79 |
char *arg[4] = {"mon", "-m", "-r", NULL}; |
80 |
mon(3, arg); |
81 |
#endif |
82 |
} |
83 |
|
84 |
// From main_*.cpp |
85 |
extern uintptr SignalStackBase(); |
86 |
|
87 |
// From rsrc_patches.cpp |
88 |
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h); |
89 |
|
90 |
// PowerPC EmulOp to exit from emulation looop |
91 |
const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1; |
92 |
|
93 |
// Enable interrupt routine safety checks? |
94 |
#define SAFE_INTERRUPT_PPC 1 |
95 |
|
96 |
// Enable Execute68k() safety checks? |
97 |
#define SAFE_EXEC_68K 1 |
98 |
|
99 |
// Save FP state in Execute68k()? |
100 |
#define SAVE_FP_EXEC_68K 1 |
101 |
|
102 |
// Interrupts in EMUL_OP mode? |
103 |
#define INTERRUPTS_IN_EMUL_OP_MODE 1 |
104 |
|
105 |
// Interrupts in native mode? |
106 |
#define INTERRUPTS_IN_NATIVE_MODE 1 |
107 |
|
108 |
// Pointer to Kernel Data |
109 |
static KernelData * kernel_data; |
110 |
|
111 |
// SIGSEGV handler |
112 |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
113 |
|
114 |
#if PPC_ENABLE_JIT && PPC_REENTRANT_JIT |
115 |
// Special trampolines for EmulOp and NativeOp |
116 |
static uint8 *emul_op_trampoline; |
117 |
static uint8 *native_op_trampoline; |
118 |
#endif |
119 |
|
120 |
// JIT Compiler enabled? |
121 |
static inline bool enable_jit_p() |
122 |
{ |
123 |
return PrefsFindBool("jit"); |
124 |
} |
125 |
|
126 |
|
127 |
/** |
128 |
* PowerPC emulator glue with special 'sheep' opcodes |
129 |
**/ |
130 |
|
131 |
enum { |
132 |
PPC_I(SHEEP) = PPC_I(MAX), |
133 |
PPC_I(SHEEP_MAX) |
134 |
}; |
135 |
|
136 |
class sheepshaver_cpu |
137 |
: public powerpc_cpu |
138 |
{ |
139 |
void init_decoder(); |
140 |
void execute_sheep(uint32 opcode); |
141 |
|
142 |
// CPU context to preserve on interrupt |
143 |
class interrupt_context { |
144 |
uint32 gpr[32]; |
145 |
uint32 pc; |
146 |
uint32 lr; |
147 |
uint32 ctr; |
148 |
uint32 cr; |
149 |
uint32 xer; |
150 |
sheepshaver_cpu *cpu; |
151 |
const char *where; |
152 |
public: |
153 |
interrupt_context(sheepshaver_cpu *_cpu, const char *_where); |
154 |
~interrupt_context(); |
155 |
}; |
156 |
|
157 |
public: |
158 |
|
159 |
// Constructor |
160 |
sheepshaver_cpu(); |
161 |
|
162 |
// CR & XER accessors |
163 |
uint32 get_cr() const { return cr().get(); } |
164 |
void set_cr(uint32 v) { cr().set(v); } |
165 |
uint32 get_xer() const { return xer().get(); } |
166 |
void set_xer(uint32 v) { xer().set(v); } |
167 |
|
168 |
// Execute NATIVE_OP routine |
169 |
void execute_native_op(uint32 native_op); |
170 |
|
171 |
// Execute EMUL_OP routine |
172 |
void execute_emul_op(uint32 emul_op); |
173 |
|
174 |
// Execute 68k routine |
175 |
void execute_68k(uint32 entry, M68kRegisters *r); |
176 |
|
177 |
// Execute ppc routine |
178 |
void execute_ppc(uint32 entry); |
179 |
|
180 |
// Execute MacOS/PPC code |
181 |
uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args); |
182 |
|
183 |
// Compile one instruction |
184 |
virtual int compile1(codegen_context_t & cg_context); |
185 |
|
186 |
// Resource manager thunk |
187 |
void get_resource(uint32 old_get_resource); |
188 |
|
189 |
// Handle MacOS interrupt |
190 |
void interrupt(uint32 entry); |
191 |
void handle_interrupt(); |
192 |
|
193 |
// Make sure the SIGSEGV handler can access CPU registers |
194 |
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
195 |
}; |
196 |
|
197 |
// Memory allocator returning areas aligned on 16-byte boundaries |
198 |
void *operator new(size_t size) |
199 |
{ |
200 |
void *p; |
201 |
|
202 |
#if defined(HAVE_POSIX_MEMALIGN) |
203 |
if (posix_memalign(&p, 16, size) != 0) |
204 |
throw std::bad_alloc(); |
205 |
#elif defined(HAVE_MEMALIGN) |
206 |
p = memalign(16, size); |
207 |
#elif defined(HAVE_VALLOC) |
208 |
p = valloc(size); // page-aligned! |
209 |
#else |
210 |
/* XXX: handle padding ourselves */ |
211 |
p = malloc(size); |
212 |
#endif |
213 |
|
214 |
return p; |
215 |
} |
216 |
|
217 |
void operator delete(void *p) |
218 |
{ |
219 |
#if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC) |
220 |
#if defined(__GLIBC__) |
221 |
// this is known to work only with GNU libc |
222 |
free(p); |
223 |
#endif |
224 |
#else |
225 |
free(p); |
226 |
#endif |
227 |
} |
228 |
|
229 |
sheepshaver_cpu::sheepshaver_cpu() |
230 |
: powerpc_cpu(enable_jit_p()) |
231 |
{ |
232 |
init_decoder(); |
233 |
} |
234 |
|
235 |
void sheepshaver_cpu::init_decoder() |
236 |
{ |
237 |
static const instr_info_t sheep_ii_table[] = { |
238 |
{ "sheep", |
239 |
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
240 |
NULL, |
241 |
PPC_I(SHEEP), |
242 |
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
243 |
} |
244 |
}; |
245 |
|
246 |
const int ii_count = sizeof(sheep_ii_table)/sizeof(sheep_ii_table[0]); |
247 |
D(bug("SheepShaver extra decode table has %d entries\n", ii_count)); |
248 |
|
249 |
for (int i = 0; i < ii_count; i++) { |
250 |
const instr_info_t * ii = &sheep_ii_table[i]; |
251 |
init_decoder_entry(ii); |
252 |
} |
253 |
} |
254 |
|
255 |
/* NativeOp instruction format: |
256 |
+------------+-------------------------+--+-----------+------------+ |
257 |
| 6 | |FN| OP | 2 | |
258 |
+------------+-------------------------+--+-----------+------------+ |
259 |
0 5 |6 18 19 20 25 26 31 |
260 |
*/ |
261 |
|
262 |
typedef bit_field< 19, 19 > FN_field; |
263 |
typedef bit_field< 20, 25 > NATIVE_OP_field; |
264 |
typedef bit_field< 26, 31 > EMUL_OP_field; |
265 |
|
266 |
// Execute EMUL_OP routine |
267 |
void sheepshaver_cpu::execute_emul_op(uint32 emul_op) |
268 |
{ |
269 |
M68kRegisters r68; |
270 |
WriteMacInt32(XLM_68K_R25, gpr(25)); |
271 |
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP); |
272 |
for (int i = 0; i < 8; i++) |
273 |
r68.d[i] = gpr(8 + i); |
274 |
for (int i = 0; i < 7; i++) |
275 |
r68.a[i] = gpr(16 + i); |
276 |
r68.a[7] = gpr(1); |
277 |
uint32 saved_cr = get_cr() & CR_field<2>::mask(); |
278 |
uint32 saved_xer = get_xer(); |
279 |
EmulOp(&r68, gpr(24), emul_op); |
280 |
set_cr(saved_cr); |
281 |
set_xer(saved_xer); |
282 |
for (int i = 0; i < 8; i++) |
283 |
gpr(8 + i) = r68.d[i]; |
284 |
for (int i = 0; i < 7; i++) |
285 |
gpr(16 + i) = r68.a[i]; |
286 |
gpr(1) = r68.a[7]; |
287 |
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
288 |
} |
289 |
|
290 |
// Execute SheepShaver instruction |
291 |
void sheepshaver_cpu::execute_sheep(uint32 opcode) |
292 |
{ |
293 |
// D(bug("Extended opcode %08x at %08x (68k pc %08x)\n", opcode, pc(), gpr(24))); |
294 |
assert((((opcode >> 26) & 0x3f) == 6) && OP_MAX <= 64 + 3); |
295 |
|
296 |
switch (opcode & 0x3f) { |
297 |
case 0: // EMUL_RETURN |
298 |
QuitEmulator(); |
299 |
break; |
300 |
|
301 |
case 1: // EXEC_RETURN |
302 |
spcflags().set(SPCFLAG_CPU_EXEC_RETURN); |
303 |
break; |
304 |
|
305 |
case 2: // EXEC_NATIVE |
306 |
execute_native_op(NATIVE_OP_field::extract(opcode)); |
307 |
if (FN_field::test(opcode)) |
308 |
pc() = lr(); |
309 |
else |
310 |
pc() += 4; |
311 |
break; |
312 |
|
313 |
default: // EMUL_OP |
314 |
execute_emul_op(EMUL_OP_field::extract(opcode) - 3); |
315 |
pc() += 4; |
316 |
break; |
317 |
} |
318 |
} |
319 |
|
320 |
// Compile one instruction |
321 |
int sheepshaver_cpu::compile1(codegen_context_t & cg_context) |
322 |
{ |
323 |
#if PPC_ENABLE_JIT |
324 |
const instr_info_t *ii = cg_context.instr_info; |
325 |
if (ii->mnemo != PPC_I(SHEEP)) |
326 |
return COMPILE_FAILURE; |
327 |
|
328 |
int status = COMPILE_FAILURE; |
329 |
powerpc_dyngen & dg = cg_context.codegen; |
330 |
uint32 opcode = cg_context.opcode; |
331 |
|
332 |
switch (opcode & 0x3f) { |
333 |
case 0: // EMUL_RETURN |
334 |
dg.gen_invoke(QuitEmulator); |
335 |
status = COMPILE_CODE_OK; |
336 |
break; |
337 |
|
338 |
case 1: // EXEC_RETURN |
339 |
dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN); |
340 |
// Don't check for pending interrupts, we do know we have to |
341 |
// get out of this block ASAP |
342 |
dg.gen_exec_return(); |
343 |
status = COMPILE_EPILOGUE_OK; |
344 |
break; |
345 |
|
346 |
case 2: { // EXEC_NATIVE |
347 |
uint32 selector = NATIVE_OP_field::extract(opcode); |
348 |
switch (selector) { |
349 |
#if !PPC_REENTRANT_JIT |
350 |
// Filter out functions that may invoke Execute68k() or |
351 |
// CallMacOS(), this would break reentrancy as they could |
352 |
// invalidate the translation cache and even overwrite |
353 |
// continuation code when we are done with them. |
354 |
case NATIVE_PATCH_NAME_REGISTRY: |
355 |
dg.gen_invoke(DoPatchNameRegistry); |
356 |
status = COMPILE_CODE_OK; |
357 |
break; |
358 |
case NATIVE_VIDEO_INSTALL_ACCEL: |
359 |
dg.gen_invoke(VideoInstallAccel); |
360 |
status = COMPILE_CODE_OK; |
361 |
break; |
362 |
case NATIVE_VIDEO_VBL: |
363 |
dg.gen_invoke(VideoVBL); |
364 |
status = COMPILE_CODE_OK; |
365 |
break; |
366 |
case NATIVE_GET_RESOURCE: |
367 |
case NATIVE_GET_1_RESOURCE: |
368 |
case NATIVE_GET_IND_RESOURCE: |
369 |
case NATIVE_GET_1_IND_RESOURCE: |
370 |
case NATIVE_R_GET_RESOURCE: { |
371 |
static const uint32 get_resource_ptr[] = { |
372 |
XLM_GET_RESOURCE, |
373 |
XLM_GET_1_RESOURCE, |
374 |
XLM_GET_IND_RESOURCE, |
375 |
XLM_GET_1_IND_RESOURCE, |
376 |
XLM_R_GET_RESOURCE |
377 |
}; |
378 |
uint32 old_get_resource = ReadMacInt32(get_resource_ptr[selector - NATIVE_GET_RESOURCE]); |
379 |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
380 |
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr(); |
381 |
dg.gen_invoke_CPU_im(func, old_get_resource); |
382 |
status = COMPILE_CODE_OK; |
383 |
break; |
384 |
} |
385 |
case NATIVE_CHECK_LOAD_INVOC: |
386 |
dg.gen_load_T0_GPR(3); |
387 |
dg.gen_load_T1_GPR(4); |
388 |
dg.gen_se_16_32_T1(); |
389 |
dg.gen_load_T2_GPR(5); |
390 |
dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc); |
391 |
status = COMPILE_CODE_OK; |
392 |
break; |
393 |
#endif |
394 |
case NATIVE_BITBLT: |
395 |
dg.gen_load_T0_GPR(3); |
396 |
dg.gen_invoke_T0((void (*)(uint32))NQD_bitblt); |
397 |
status = COMPILE_CODE_OK; |
398 |
break; |
399 |
case NATIVE_INVRECT: |
400 |
dg.gen_load_T0_GPR(3); |
401 |
dg.gen_invoke_T0((void (*)(uint32))NQD_invrect); |
402 |
status = COMPILE_CODE_OK; |
403 |
break; |
404 |
case NATIVE_FILLRECT: |
405 |
dg.gen_load_T0_GPR(3); |
406 |
dg.gen_invoke_T0((void (*)(uint32))NQD_fillrect); |
407 |
status = COMPILE_CODE_OK; |
408 |
break; |
409 |
} |
410 |
// Could we fully translate this NativeOp? |
411 |
if (status == COMPILE_CODE_OK) { |
412 |
if (!FN_field::test(opcode)) |
413 |
cg_context.done_compile = false; |
414 |
else { |
415 |
dg.gen_load_A0_LR(); |
416 |
dg.gen_set_PC_A0(); |
417 |
cg_context.done_compile = true; |
418 |
} |
419 |
break; |
420 |
} |
421 |
#if PPC_REENTRANT_JIT |
422 |
// Try to execute NativeOp trampoline |
423 |
if (!FN_field::test(opcode)) |
424 |
dg.gen_set_PC_im(cg_context.pc + 4); |
425 |
else { |
426 |
dg.gen_load_A0_LR(); |
427 |
dg.gen_set_PC_A0(); |
428 |
} |
429 |
dg.gen_mov_32_T0_im(selector); |
430 |
dg.gen_jmp(native_op_trampoline); |
431 |
cg_context.done_compile = true; |
432 |
status = COMPILE_EPILOGUE_OK; |
433 |
break; |
434 |
#endif |
435 |
// Invoke NativeOp handler |
436 |
if (!FN_field::test(opcode)) { |
437 |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
438 |
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr(); |
439 |
dg.gen_invoke_CPU_im(func, selector); |
440 |
cg_context.done_compile = false; |
441 |
status = COMPILE_CODE_OK; |
442 |
} |
443 |
// Otherwise, let it generate a call to execute_sheep() which |
444 |
// will cause necessary updates to the program counter |
445 |
break; |
446 |
} |
447 |
|
448 |
default: { // EMUL_OP |
449 |
uint32 emul_op = EMUL_OP_field::extract(opcode) - 3; |
450 |
#if PPC_REENTRANT_JIT |
451 |
// Try to execute EmulOp trampoline |
452 |
dg.gen_set_PC_im(cg_context.pc + 4); |
453 |
dg.gen_mov_32_T0_im(emul_op); |
454 |
dg.gen_jmp(emul_op_trampoline); |
455 |
cg_context.done_compile = true; |
456 |
status = COMPILE_EPILOGUE_OK; |
457 |
break; |
458 |
#endif |
459 |
// Invoke EmulOp handler |
460 |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
461 |
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr(); |
462 |
dg.gen_invoke_CPU_im(func, emul_op); |
463 |
cg_context.done_compile = false; |
464 |
status = COMPILE_CODE_OK; |
465 |
break; |
466 |
} |
467 |
} |
468 |
return status; |
469 |
#endif |
470 |
return COMPILE_FAILURE; |
471 |
} |
472 |
|
473 |
// CPU context to preserve on interrupt |
474 |
sheepshaver_cpu::interrupt_context::interrupt_context(sheepshaver_cpu *_cpu, const char *_where) |
475 |
{ |
476 |
#if SAFE_INTERRUPT_PPC >= 2 |
477 |
cpu = _cpu; |
478 |
where = _where; |
479 |
|
480 |
// Save interrupt context |
481 |
memcpy(&gpr[0], &cpu->gpr(0), sizeof(gpr)); |
482 |
pc = cpu->pc(); |
483 |
lr = cpu->lr(); |
484 |
ctr = cpu->ctr(); |
485 |
cr = cpu->get_cr(); |
486 |
xer = cpu->get_xer(); |
487 |
#endif |
488 |
} |
489 |
|
490 |
sheepshaver_cpu::interrupt_context::~interrupt_context() |
491 |
{ |
492 |
#if SAFE_INTERRUPT_PPC >= 2 |
493 |
// Check whether CPU context was preserved by interrupt |
494 |
if (memcmp(&gpr[0], &cpu->gpr(0), sizeof(gpr)) != 0) { |
495 |
printf("FATAL: %s: interrupt clobbers registers\n", where); |
496 |
for (int i = 0; i < 32; i++) |
497 |
if (gpr[i] != cpu->gpr(i)) |
498 |
printf(" r%d: %08x -> %08x\n", i, gpr[i], cpu->gpr(i)); |
499 |
} |
500 |
if (pc != cpu->pc()) |
501 |
printf("FATAL: %s: interrupt clobbers PC\n", where); |
502 |
if (lr != cpu->lr()) |
503 |
printf("FATAL: %s: interrupt clobbers LR\n", where); |
504 |
if (ctr != cpu->ctr()) |
505 |
printf("FATAL: %s: interrupt clobbers CTR\n", where); |
506 |
if (cr != cpu->get_cr()) |
507 |
printf("FATAL: %s: interrupt clobbers CR\n", where); |
508 |
if (xer != cpu->get_xer()) |
509 |
printf("FATAL: %s: interrupt clobbers XER\n", where); |
510 |
#endif |
511 |
} |
512 |
|
513 |
// Handle MacOS interrupt |
514 |
void sheepshaver_cpu::interrupt(uint32 entry) |
515 |
{ |
516 |
#if EMUL_TIME_STATS |
517 |
ppc_interrupt_count++; |
518 |
const clock_t interrupt_start = clock(); |
519 |
#endif |
520 |
|
521 |
#if SAFE_INTERRUPT_PPC |
522 |
static int depth = 0; |
523 |
if (depth != 0) |
524 |
printf("FATAL: sheepshaver_cpu::interrupt() called more than once: %d\n", depth); |
525 |
depth++; |
526 |
#endif |
527 |
|
528 |
// Save program counters and branch registers |
529 |
uint32 saved_pc = pc(); |
530 |
uint32 saved_lr = lr(); |
531 |
uint32 saved_ctr= ctr(); |
532 |
uint32 saved_sp = gpr(1); |
533 |
|
534 |
// Initialize stack pointer to SheepShaver alternate stack base |
535 |
gpr(1) = SignalStackBase() - 64; |
536 |
|
537 |
// Build trampoline to return from interrupt |
538 |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
539 |
|
540 |
// Prepare registers for nanokernel interrupt routine |
541 |
kernel_data->v[0x004 >> 2] = htonl(gpr(1)); |
542 |
kernel_data->v[0x018 >> 2] = htonl(gpr(6)); |
543 |
|
544 |
gpr(6) = ntohl(kernel_data->v[0x65c >> 2]); |
545 |
assert(gpr(6) != 0); |
546 |
WriteMacInt32(gpr(6) + 0x13c, gpr(7)); |
547 |
WriteMacInt32(gpr(6) + 0x144, gpr(8)); |
548 |
WriteMacInt32(gpr(6) + 0x14c, gpr(9)); |
549 |
WriteMacInt32(gpr(6) + 0x154, gpr(10)); |
550 |
WriteMacInt32(gpr(6) + 0x15c, gpr(11)); |
551 |
WriteMacInt32(gpr(6) + 0x164, gpr(12)); |
552 |
WriteMacInt32(gpr(6) + 0x16c, gpr(13)); |
553 |
|
554 |
gpr(1) = KernelDataAddr; |
555 |
gpr(7) = ntohl(kernel_data->v[0x660 >> 2]); |
556 |
gpr(8) = 0; |
557 |
gpr(10) = trampoline.addr(); |
558 |
gpr(12) = trampoline.addr(); |
559 |
gpr(13) = get_cr(); |
560 |
|
561 |
// rlwimi. r7,r7,8,0,0 |
562 |
uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7)); |
563 |
record_cr0(result); |
564 |
gpr(7) = result; |
565 |
|
566 |
gpr(11) = 0xf072; // MSR (SRR1) |
567 |
cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000)); |
568 |
|
569 |
// Enter nanokernel |
570 |
execute(entry); |
571 |
|
572 |
// Restore program counters and branch registers |
573 |
pc() = saved_pc; |
574 |
lr() = saved_lr; |
575 |
ctr()= saved_ctr; |
576 |
gpr(1) = saved_sp; |
577 |
|
578 |
#if EMUL_TIME_STATS |
579 |
interrupt_time += (clock() - interrupt_start); |
580 |
#endif |
581 |
|
582 |
#if SAFE_INTERRUPT_PPC |
583 |
depth--; |
584 |
#endif |
585 |
} |
586 |
|
587 |
// Execute 68k routine |
588 |
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r) |
589 |
{ |
590 |
#if EMUL_TIME_STATS |
591 |
exec68k_count++; |
592 |
const clock_t exec68k_start = clock(); |
593 |
#endif |
594 |
|
595 |
#if SAFE_EXEC_68K |
596 |
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP) |
597 |
printf("FATAL: Execute68k() not called from EMUL_OP mode\n"); |
598 |
#endif |
599 |
|
600 |
// Save program counters and branch registers |
601 |
uint32 saved_pc = pc(); |
602 |
uint32 saved_lr = lr(); |
603 |
uint32 saved_ctr= ctr(); |
604 |
uint32 saved_cr = get_cr(); |
605 |
|
606 |
// Create MacOS stack frame |
607 |
// FIXME: make sure MacOS doesn't expect PPC registers to live on top |
608 |
uint32 sp = gpr(1); |
609 |
gpr(1) -= 56; |
610 |
WriteMacInt32(gpr(1), sp); |
611 |
|
612 |
// Save PowerPC registers |
613 |
uint32 saved_GPRs[19]; |
614 |
memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13)); |
615 |
#if SAVE_FP_EXEC_68K |
616 |
double saved_FPRs[18]; |
617 |
memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14)); |
618 |
#endif |
619 |
|
620 |
// Setup registers for 68k emulator |
621 |
cr().set(CR_SO_field<2>::mask()); // Supervisor mode |
622 |
for (int i = 0; i < 8; i++) // d[0]..d[7] |
623 |
gpr(8 + i) = r->d[i]; |
624 |
for (int i = 0; i < 7; i++) // a[0]..a[6] |
625 |
gpr(16 + i) = r->a[i]; |
626 |
gpr(23) = 0; |
627 |
gpr(24) = entry; |
628 |
gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR |
629 |
gpr(26) = 0; |
630 |
gpr(28) = 0; // VBR |
631 |
gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]); // Pointer to opcode table |
632 |
gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]); // Address of emulator |
633 |
gpr(31) = KernelDataAddr + 0x1000; |
634 |
|
635 |
// Push return address (points to EXEC_RETURN opcode) on stack |
636 |
gpr(1) -= 4; |
637 |
WriteMacInt32(gpr(1), XLM_EXEC_RETURN_OPCODE); |
638 |
|
639 |
// Rentering 68k emulator |
640 |
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
641 |
|
642 |
// Set r0 to 0 for 68k emulator |
643 |
gpr(0) = 0; |
644 |
|
645 |
// Execute 68k opcode |
646 |
uint32 opcode = ReadMacInt16(gpr(24)); |
647 |
gpr(27) = (int32)(int16)ReadMacInt16(gpr(24) += 2); |
648 |
gpr(29) += opcode * 8; |
649 |
execute(gpr(29)); |
650 |
|
651 |
// Save r25 (contains current 68k interrupt level) |
652 |
WriteMacInt32(XLM_68K_R25, gpr(25)); |
653 |
|
654 |
// Reentering EMUL_OP mode |
655 |
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP); |
656 |
|
657 |
// Save 68k registers |
658 |
for (int i = 0; i < 8; i++) // d[0]..d[7] |
659 |
r->d[i] = gpr(8 + i); |
660 |
for (int i = 0; i < 7; i++) // a[0]..a[6] |
661 |
r->a[i] = gpr(16 + i); |
662 |
|
663 |
// Restore PowerPC registers |
664 |
memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13)); |
665 |
#if SAVE_FP_EXEC_68K |
666 |
memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14)); |
667 |
#endif |
668 |
|
669 |
// Cleanup stack |
670 |
gpr(1) += 56; |
671 |
|
672 |
// Restore program counters and branch registers |
673 |
pc() = saved_pc; |
674 |
lr() = saved_lr; |
675 |
ctr()= saved_ctr; |
676 |
set_cr(saved_cr); |
677 |
|
678 |
#if EMUL_TIME_STATS |
679 |
exec68k_time += (clock() - exec68k_start); |
680 |
#endif |
681 |
} |
682 |
|
683 |
// Call MacOS PPC code |
684 |
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args) |
685 |
{ |
686 |
#if EMUL_TIME_STATS |
687 |
macos_exec_count++; |
688 |
const clock_t macos_exec_start = clock(); |
689 |
#endif |
690 |
|
691 |
// Save program counters and branch registers |
692 |
uint32 saved_pc = pc(); |
693 |
uint32 saved_lr = lr(); |
694 |
uint32 saved_ctr= ctr(); |
695 |
|
696 |
// Build trampoline with EXEC_RETURN |
697 |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
698 |
lr() = trampoline.addr(); |
699 |
|
700 |
gpr(1) -= 64; // Create stack frame |
701 |
uint32 proc = ReadMacInt32(tvect); // Get routine address |
702 |
uint32 toc = ReadMacInt32(tvect + 4); // Get TOC pointer |
703 |
|
704 |
// Save PowerPC registers |
705 |
uint32 regs[8]; |
706 |
regs[0] = gpr(2); |
707 |
for (int i = 0; i < nargs; i++) |
708 |
regs[i + 1] = gpr(i + 3); |
709 |
|
710 |
// Prepare and call MacOS routine |
711 |
gpr(2) = toc; |
712 |
for (int i = 0; i < nargs; i++) |
713 |
gpr(i + 3) = args[i]; |
714 |
execute(proc); |
715 |
uint32 retval = gpr(3); |
716 |
|
717 |
// Restore PowerPC registers |
718 |
for (int i = 0; i <= nargs; i++) |
719 |
gpr(i + 2) = regs[i]; |
720 |
|
721 |
// Cleanup stack |
722 |
gpr(1) += 64; |
723 |
|
724 |
// Restore program counters and branch registers |
725 |
pc() = saved_pc; |
726 |
lr() = saved_lr; |
727 |
ctr()= saved_ctr; |
728 |
|
729 |
#if EMUL_TIME_STATS |
730 |
macos_exec_time += (clock() - macos_exec_start); |
731 |
#endif |
732 |
|
733 |
return retval; |
734 |
} |
735 |
|
736 |
// Execute ppc routine |
737 |
inline void sheepshaver_cpu::execute_ppc(uint32 entry) |
738 |
{ |
739 |
// Save branch registers |
740 |
uint32 saved_lr = lr(); |
741 |
|
742 |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
743 |
WriteMacInt32(trampoline.addr(), POWERPC_EXEC_RETURN); |
744 |
lr() = trampoline.addr(); |
745 |
|
746 |
execute(entry); |
747 |
|
748 |
// Restore branch registers |
749 |
lr() = saved_lr; |
750 |
} |
751 |
|
752 |
// Resource Manager thunk |
753 |
inline void sheepshaver_cpu::get_resource(uint32 old_get_resource) |
754 |
{ |
755 |
uint32 type = gpr(3); |
756 |
int16 id = gpr(4); |
757 |
|
758 |
// Create stack frame |
759 |
gpr(1) -= 56; |
760 |
|
761 |
// Call old routine |
762 |
execute_ppc(old_get_resource); |
763 |
|
764 |
// Call CheckLoad() |
765 |
uint32 handle = gpr(3); |
766 |
check_load_invoc(type, id, handle); |
767 |
gpr(3) = handle; |
768 |
|
769 |
// Cleanup stack |
770 |
gpr(1) += 56; |
771 |
} |
772 |
|
773 |
|
774 |
/** |
775 |
* SheepShaver CPU engine interface |
776 |
**/ |
777 |
|
778 |
// PowerPC CPU emulator |
779 |
static sheepshaver_cpu *ppc_cpu = NULL; |
780 |
|
781 |
void FlushCodeCache(uintptr start, uintptr end) |
782 |
{ |
783 |
D(bug("FlushCodeCache(%08x, %08x)\n", start, end)); |
784 |
ppc_cpu->invalidate_cache_range(start, end); |
785 |
} |
786 |
|
787 |
// Dump PPC registers |
788 |
static void dump_registers(void) |
789 |
{ |
790 |
ppc_cpu->dump_registers(); |
791 |
} |
792 |
|
793 |
// Dump log |
794 |
static void dump_log(void) |
795 |
{ |
796 |
ppc_cpu->dump_log(); |
797 |
} |
798 |
|
799 |
/* |
800 |
* Initialize CPU emulation |
801 |
*/ |
802 |
|
803 |
sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction) |
804 |
{ |
805 |
#if ENABLE_VOSF |
806 |
// Handle screen fault |
807 |
extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t); |
808 |
if (Screen_fault_handler(fault_address, fault_instruction)) |
809 |
return SIGSEGV_RETURN_SUCCESS; |
810 |
#endif |
811 |
|
812 |
const uintptr addr = (uintptr)fault_address; |
813 |
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
814 |
// Ignore writes to ROM |
815 |
if ((addr - (uintptr)ROMBaseHost) < ROM_SIZE) |
816 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
817 |
|
818 |
// Get program counter of target CPU |
819 |
sheepshaver_cpu * const cpu = ppc_cpu; |
820 |
const uint32 pc = cpu->pc(); |
821 |
|
822 |
// Fault in Mac ROM or RAM? |
823 |
bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize)) || (pc >= DR_CACHE_BASE && pc < (DR_CACHE_BASE + DR_CACHE_SIZE)); |
824 |
if (mac_fault) { |
825 |
|
826 |
// "VM settings" during MacOS 8 installation |
827 |
if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000) |
828 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
829 |
|
830 |
// MacOS 8.5 installation |
831 |
else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000) |
832 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
833 |
|
834 |
// MacOS 8 serial drivers on startup |
835 |
else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000)) |
836 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
837 |
|
838 |
// MacOS 8.1 serial drivers on startup |
839 |
else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
840 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
841 |
else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
842 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
843 |
|
844 |
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM) |
845 |
else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(16) == 0xf3012002 || cpu->gpr(16) == 0xf3012000)) |
846 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
847 |
else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
848 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
849 |
|
850 |
// Ignore writes to the zero page |
851 |
else if ((uint32)(addr - SheepMem::ZeroPage()) < (uint32)SheepMem::PageSize()) |
852 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
853 |
|
854 |
// Ignore all other faults, if requested |
855 |
if (PrefsFindBool("ignoresegv")) |
856 |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
857 |
} |
858 |
#else |
859 |
#error "FIXME: You don't have the capability to skip instruction within signal handlers" |
860 |
#endif |
861 |
|
862 |
printf("SIGSEGV\n"); |
863 |
printf(" pc %p\n", fault_instruction); |
864 |
printf(" ea %p\n", fault_address); |
865 |
dump_registers(); |
866 |
ppc_cpu->dump_log(); |
867 |
enter_mon(); |
868 |
QuitEmulator(); |
869 |
|
870 |
return SIGSEGV_RETURN_FAILURE; |
871 |
} |
872 |
|
873 |
void init_emul_ppc(void) |
874 |
{ |
875 |
// Get pointer to KernelData in host address space |
876 |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
877 |
|
878 |
// Initialize main CPU emulator |
879 |
ppc_cpu = new sheepshaver_cpu(); |
880 |
ppc_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000)); |
881 |
ppc_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000)); |
882 |
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
883 |
|
884 |
#if ENABLE_MON |
885 |
// Install "regs" command in cxmon |
886 |
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n"); |
887 |
mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n"); |
888 |
#endif |
889 |
|
890 |
#if EMUL_TIME_STATS |
891 |
emul_start_time = clock(); |
892 |
#endif |
893 |
} |
894 |
|
895 |
/* |
896 |
* Deinitialize emulation |
897 |
*/ |
898 |
|
899 |
void exit_emul_ppc(void) |
900 |
{ |
901 |
#if EMUL_TIME_STATS |
902 |
clock_t emul_end_time = clock(); |
903 |
|
904 |
printf("### Statistics for SheepShaver emulation parts\n"); |
905 |
const clock_t emul_time = emul_end_time - emul_start_time; |
906 |
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC)); |
907 |
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count, |
908 |
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time)); |
909 |
printf("Total ppc interrupt count: %d (%2.1f %%)\n", ppc_interrupt_count, |
910 |
(double(ppc_interrupt_count) * 100.0) / double(interrupt_count)); |
911 |
|
912 |
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \ |
913 |
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \ |
914 |
printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \ |
915 |
double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \ |
916 |
100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \ |
917 |
} while (0) |
918 |
|
919 |
PRINT_STATS("Execute68k[Trap] execution", exec68k); |
920 |
PRINT_STATS("NativeOp execution", native_exec); |
921 |
PRINT_STATS("MacOS routine execution", macos_exec); |
922 |
|
923 |
#undef PRINT_STATS |
924 |
printf("\n"); |
925 |
#endif |
926 |
|
927 |
delete ppc_cpu; |
928 |
} |
929 |
|
930 |
#if PPC_ENABLE_JIT && PPC_REENTRANT_JIT |
931 |
// Initialize EmulOp trampolines |
932 |
void init_emul_op_trampolines(basic_dyngen & dg) |
933 |
{ |
934 |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
935 |
func_t func; |
936 |
|
937 |
// EmulOp |
938 |
emul_op_trampoline = dg.gen_start(); |
939 |
func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr(); |
940 |
dg.gen_invoke_CPU_T0(func); |
941 |
dg.gen_exec_return(); |
942 |
dg.gen_end(); |
943 |
|
944 |
// NativeOp |
945 |
native_op_trampoline = dg.gen_start(); |
946 |
func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr(); |
947 |
dg.gen_invoke_CPU_T0(func); |
948 |
dg.gen_exec_return(); |
949 |
dg.gen_end(); |
950 |
|
951 |
D(bug("EmulOp trampoline: %p\n", emul_op_trampoline)); |
952 |
D(bug("NativeOp trampoline: %p\n", native_op_trampoline)); |
953 |
} |
954 |
#endif |
955 |
|
956 |
/* |
957 |
* Emulation loop |
958 |
*/ |
959 |
|
960 |
void emul_ppc(uint32 entry) |
961 |
{ |
962 |
#if 0 |
963 |
ppc_cpu->start_log(); |
964 |
#endif |
965 |
// start emulation loop and enable code translation or caching |
966 |
ppc_cpu->execute(entry); |
967 |
} |
968 |
|
969 |
/* |
970 |
* Handle PowerPC interrupt |
971 |
*/ |
972 |
|
973 |
void TriggerInterrupt(void) |
974 |
{ |
975 |
#if 0 |
976 |
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
977 |
#else |
978 |
// Trigger interrupt to main cpu only |
979 |
if (ppc_cpu) |
980 |
ppc_cpu->trigger_interrupt(); |
981 |
#endif |
982 |
} |
983 |
|
984 |
void sheepshaver_cpu::handle_interrupt(void) |
985 |
{ |
986 |
#ifdef USE_SDL_VIDEO |
987 |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
988 |
SDL_PumpEvents(); |
989 |
#endif |
990 |
|
991 |
// Do nothing if interrupts are disabled |
992 |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
993 |
return; |
994 |
|
995 |
// Current interrupt nest level |
996 |
static int interrupt_depth = 0; |
997 |
++interrupt_depth; |
998 |
#if EMUL_TIME_STATS |
999 |
interrupt_count++; |
1000 |
#endif |
1001 |
|
1002 |
// Disable MacOS stack sniffer |
1003 |
WriteMacInt32(0x110, 0); |
1004 |
|
1005 |
// Interrupt action depends on current run mode |
1006 |
switch (ReadMacInt32(XLM_RUN_MODE)) { |
1007 |
case MODE_68K: |
1008 |
// 68k emulator active, trigger 68k interrupt level 1 |
1009 |
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
1010 |
set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
1011 |
break; |
1012 |
|
1013 |
#if INTERRUPTS_IN_NATIVE_MODE |
1014 |
case MODE_NATIVE: |
1015 |
// 68k emulator inactive, in nanokernel? |
1016 |
if (gpr(1) != KernelDataAddr && interrupt_depth == 1) { |
1017 |
interrupt_context ctx(this, "PowerPC mode"); |
1018 |
|
1019 |
// Prepare for 68k interrupt level 1 |
1020 |
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
1021 |
WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc, |
1022 |
ReadMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc) |
1023 |
| tswap32(kernel_data->v[0x674 >> 2])); |
1024 |
|
1025 |
// Execute nanokernel interrupt routine (this will activate the 68k emulator) |
1026 |
DisableInterrupt(); |
1027 |
if (ROMType == ROMTYPE_NEWWORLD) |
1028 |
ppc_cpu->interrupt(ROM_BASE + 0x312b1c); |
1029 |
else |
1030 |
ppc_cpu->interrupt(ROM_BASE + 0x312a3c); |
1031 |
} |
1032 |
break; |
1033 |
#endif |
1034 |
|
1035 |
#if INTERRUPTS_IN_EMUL_OP_MODE |
1036 |
case MODE_EMUL_OP: |
1037 |
// 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0 |
1038 |
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1039 |
interrupt_context ctx(this, "68k mode"); |
1040 |
#if EMUL_TIME_STATS |
1041 |
const clock_t interrupt_start = clock(); |
1042 |
#endif |
1043 |
#if 1 |
1044 |
// Execute full 68k interrupt routine |
1045 |
M68kRegisters r; |
1046 |
uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level |
1047 |
WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1 |
1048 |
static const uint8 proc[] = { |
1049 |
0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word) |
1050 |
0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address) |
1051 |
0x40, 0xe7, // move sr,-(sp) (saved SR) |
1052 |
0x20, 0x78, 0x00, 0x064, // move.l $64,a0 |
1053 |
0x4e, 0xd0, // jmp (a0) |
1054 |
M68K_RTS >> 8, M68K_RTS & 0xff // @1 |
1055 |
}; |
1056 |
Execute68k(Host2MacAddr((uint8 *)proc), &r); |
1057 |
WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level |
1058 |
#else |
1059 |
// Only update cursor |
1060 |
if (HasMacStarted()) { |
1061 |
if (InterruptFlags & INTFLAG_VIA) { |
1062 |
ClearInterruptFlag(INTFLAG_VIA); |
1063 |
ADBInterrupt(); |
1064 |
ExecuteNative(NATIVE_VIDEO_VBL); |
1065 |
} |
1066 |
} |
1067 |
#endif |
1068 |
#if EMUL_TIME_STATS |
1069 |
interrupt_time += (clock() - interrupt_start); |
1070 |
#endif |
1071 |
} |
1072 |
break; |
1073 |
#endif |
1074 |
} |
1075 |
|
1076 |
// We are done with this interrupt |
1077 |
--interrupt_depth; |
1078 |
} |
1079 |
|
1080 |
static void get_resource(void); |
1081 |
static void get_1_resource(void); |
1082 |
static void get_ind_resource(void); |
1083 |
static void get_1_ind_resource(void); |
1084 |
static void r_get_resource(void); |
1085 |
|
1086 |
// Execute NATIVE_OP routine |
1087 |
void sheepshaver_cpu::execute_native_op(uint32 selector) |
1088 |
{ |
1089 |
#if EMUL_TIME_STATS |
1090 |
native_exec_count++; |
1091 |
const clock_t native_exec_start = clock(); |
1092 |
#endif |
1093 |
|
1094 |
switch (selector) { |
1095 |
case NATIVE_PATCH_NAME_REGISTRY: |
1096 |
DoPatchNameRegistry(); |
1097 |
break; |
1098 |
case NATIVE_VIDEO_INSTALL_ACCEL: |
1099 |
VideoInstallAccel(); |
1100 |
break; |
1101 |
case NATIVE_VIDEO_VBL: |
1102 |
VideoVBL(); |
1103 |
break; |
1104 |
case NATIVE_VIDEO_DO_DRIVER_IO: |
1105 |
gpr(3) = (int32)(int16)VideoDoDriverIO(gpr(3), gpr(4), gpr(5), gpr(6), gpr(7)); |
1106 |
break; |
1107 |
#ifdef WORDS_BIGENDIAN |
1108 |
case NATIVE_ETHER_IRQ: |
1109 |
EtherIRQ(); |
1110 |
break; |
1111 |
case NATIVE_ETHER_INIT: |
1112 |
gpr(3) = InitStreamModule((void *)gpr(3)); |
1113 |
break; |
1114 |
case NATIVE_ETHER_TERM: |
1115 |
TerminateStreamModule(); |
1116 |
break; |
1117 |
case NATIVE_ETHER_OPEN: |
1118 |
gpr(3) = ether_open((queue_t *)gpr(3), (void *)gpr(4), gpr(5), gpr(6), (void*)gpr(7)); |
1119 |
break; |
1120 |
case NATIVE_ETHER_CLOSE: |
1121 |
gpr(3) = ether_close((queue_t *)gpr(3), gpr(4), (void *)gpr(5)); |
1122 |
break; |
1123 |
case NATIVE_ETHER_WPUT: |
1124 |
gpr(3) = ether_wput((queue_t *)gpr(3), (mblk_t *)gpr(4)); |
1125 |
break; |
1126 |
case NATIVE_ETHER_RSRV: |
1127 |
gpr(3) = ether_rsrv((queue_t *)gpr(3)); |
1128 |
break; |
1129 |
#else |
1130 |
case NATIVE_ETHER_INIT: |
1131 |
// FIXME: needs more complicated thunks |
1132 |
gpr(3) = false; |
1133 |
break; |
1134 |
#endif |
1135 |
case NATIVE_SYNC_HOOK: |
1136 |
gpr(3) = NQD_sync_hook(gpr(3)); |
1137 |
break; |
1138 |
case NATIVE_BITBLT_HOOK: |
1139 |
gpr(3) = NQD_bitblt_hook(gpr(3)); |
1140 |
break; |
1141 |
case NATIVE_BITBLT: |
1142 |
NQD_bitblt(gpr(3)); |
1143 |
break; |
1144 |
case NATIVE_FILLRECT_HOOK: |
1145 |
gpr(3) = NQD_fillrect_hook(gpr(3)); |
1146 |
break; |
1147 |
case NATIVE_INVRECT: |
1148 |
NQD_invrect(gpr(3)); |
1149 |
break; |
1150 |
case NATIVE_FILLRECT: |
1151 |
NQD_fillrect(gpr(3)); |
1152 |
break; |
1153 |
case NATIVE_SERIAL_NOTHING: |
1154 |
case NATIVE_SERIAL_OPEN: |
1155 |
case NATIVE_SERIAL_PRIME_IN: |
1156 |
case NATIVE_SERIAL_PRIME_OUT: |
1157 |
case NATIVE_SERIAL_CONTROL: |
1158 |
case NATIVE_SERIAL_STATUS: |
1159 |
case NATIVE_SERIAL_CLOSE: { |
1160 |
typedef int16 (*SerialCallback)(uint32, uint32); |
1161 |
static const SerialCallback serial_callbacks[] = { |
1162 |
SerialNothing, |
1163 |
SerialOpen, |
1164 |
SerialPrimeIn, |
1165 |
SerialPrimeOut, |
1166 |
SerialControl, |
1167 |
SerialStatus, |
1168 |
SerialClose |
1169 |
}; |
1170 |
gpr(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](gpr(3), gpr(4)); |
1171 |
break; |
1172 |
} |
1173 |
case NATIVE_GET_RESOURCE: |
1174 |
case NATIVE_GET_1_RESOURCE: |
1175 |
case NATIVE_GET_IND_RESOURCE: |
1176 |
case NATIVE_GET_1_IND_RESOURCE: |
1177 |
case NATIVE_R_GET_RESOURCE: { |
1178 |
typedef void (*GetResourceCallback)(void); |
1179 |
static const GetResourceCallback get_resource_callbacks[] = { |
1180 |
::get_resource, |
1181 |
::get_1_resource, |
1182 |
::get_ind_resource, |
1183 |
::get_1_ind_resource, |
1184 |
::r_get_resource |
1185 |
}; |
1186 |
get_resource_callbacks[selector - NATIVE_GET_RESOURCE](); |
1187 |
break; |
1188 |
} |
1189 |
case NATIVE_MAKE_EXECUTABLE: |
1190 |
MakeExecutable(0, gpr(4), gpr(5)); |
1191 |
break; |
1192 |
case NATIVE_CHECK_LOAD_INVOC: |
1193 |
check_load_invoc(gpr(3), gpr(4), gpr(5)); |
1194 |
break; |
1195 |
default: |
1196 |
printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector); |
1197 |
QuitEmulator(); |
1198 |
break; |
1199 |
} |
1200 |
|
1201 |
#if EMUL_TIME_STATS |
1202 |
native_exec_time += (clock() - native_exec_start); |
1203 |
#endif |
1204 |
} |
1205 |
|
1206 |
/* |
1207 |
* Execute 68k subroutine (must be ended with EXEC_RETURN) |
1208 |
* This must only be called by the emul_thread when in EMUL_OP mode |
1209 |
* r->a[7] is unused, the routine runs on the caller's stack |
1210 |
*/ |
1211 |
|
1212 |
void Execute68k(uint32 pc, M68kRegisters *r) |
1213 |
{ |
1214 |
ppc_cpu->execute_68k(pc, r); |
1215 |
} |
1216 |
|
1217 |
/* |
1218 |
* Execute 68k A-Trap from EMUL_OP routine |
1219 |
* r->a[7] is unused, the routine runs on the caller's stack |
1220 |
*/ |
1221 |
|
1222 |
void Execute68kTrap(uint16 trap, M68kRegisters *r) |
1223 |
{ |
1224 |
SheepVar proc_var(4); |
1225 |
uint32 proc = proc_var.addr(); |
1226 |
WriteMacInt16(proc, trap); |
1227 |
WriteMacInt16(proc + 2, M68K_RTS); |
1228 |
Execute68k(proc, r); |
1229 |
} |
1230 |
|
1231 |
/* |
1232 |
* Call MacOS PPC code |
1233 |
*/ |
1234 |
|
1235 |
uint32 call_macos(uint32 tvect) |
1236 |
{ |
1237 |
return ppc_cpu->execute_macos_code(tvect, 0, NULL); |
1238 |
} |
1239 |
|
1240 |
uint32 call_macos1(uint32 tvect, uint32 arg1) |
1241 |
{ |
1242 |
const uint32 args[] = { arg1 }; |
1243 |
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args); |
1244 |
} |
1245 |
|
1246 |
uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2) |
1247 |
{ |
1248 |
const uint32 args[] = { arg1, arg2 }; |
1249 |
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args); |
1250 |
} |
1251 |
|
1252 |
uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3) |
1253 |
{ |
1254 |
const uint32 args[] = { arg1, arg2, arg3 }; |
1255 |
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args); |
1256 |
} |
1257 |
|
1258 |
uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4) |
1259 |
{ |
1260 |
const uint32 args[] = { arg1, arg2, arg3, arg4 }; |
1261 |
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args); |
1262 |
} |
1263 |
|
1264 |
uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5) |
1265 |
{ |
1266 |
const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 }; |
1267 |
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args); |
1268 |
} |
1269 |
|
1270 |
uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6) |
1271 |
{ |
1272 |
const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 }; |
1273 |
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args); |
1274 |
} |
1275 |
|
1276 |
uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7) |
1277 |
{ |
1278 |
const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 }; |
1279 |
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args); |
1280 |
} |
1281 |
|
1282 |
/* |
1283 |
* Resource Manager thunks |
1284 |
*/ |
1285 |
|
1286 |
void get_resource(void) |
1287 |
{ |
1288 |
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE)); |
1289 |
} |
1290 |
|
1291 |
void get_1_resource(void) |
1292 |
{ |
1293 |
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE)); |
1294 |
} |
1295 |
|
1296 |
void get_ind_resource(void) |
1297 |
{ |
1298 |
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE)); |
1299 |
} |
1300 |
|
1301 |
void get_1_ind_resource(void) |
1302 |
{ |
1303 |
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE)); |
1304 |
} |
1305 |
|
1306 |
void r_get_resource(void) |
1307 |
{ |
1308 |
ppc_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE)); |
1309 |
} |