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 |
40 |
|
#include "ether.h" |
41 |
|
|
42 |
|
#include <stdio.h> |
43 |
+ |
#include <stdlib.h> |
44 |
|
|
45 |
|
#if ENABLE_MON |
46 |
|
#include "mon.h" |
77 |
|
// From main_*.cpp |
78 |
|
extern uintptr SignalStackBase(); |
79 |
|
|
80 |
+ |
// From rsrc_patches.cpp |
81 |
+ |
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h); |
82 |
+ |
|
83 |
|
// PowerPC EmulOp to exit from emulation looop |
84 |
|
const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1; |
85 |
|
|
137 |
|
uint32 get_xer() const { return xer().get(); } |
138 |
|
void set_xer(uint32 v) { xer().set(v); } |
139 |
|
|
140 |
+ |
// Execute EMUL_OP routine |
141 |
+ |
void execute_emul_op(uint32 emul_op); |
142 |
+ |
|
143 |
|
// Execute 68k routine |
144 |
|
void execute_68k(uint32 entry, M68kRegisters *r); |
145 |
|
|
149 |
|
// Execute MacOS/PPC code |
150 |
|
uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args); |
151 |
|
|
152 |
+ |
// Compile one instruction |
153 |
+ |
virtual bool compile1(codegen_context_t & cg_context); |
154 |
+ |
|
155 |
|
// Resource manager thunk |
156 |
|
void get_resource(uint32 old_get_resource); |
157 |
|
|
159 |
|
void interrupt(uint32 entry); |
160 |
|
void handle_interrupt(); |
161 |
|
|
152 |
– |
// Lazy memory allocator (one item at a time) |
153 |
– |
void *operator new(size_t size) |
154 |
– |
{ return allocator_helper< sheepshaver_cpu, lazy_allocator >::allocate(); } |
155 |
– |
void operator delete(void *p) |
156 |
– |
{ allocator_helper< sheepshaver_cpu, lazy_allocator >::deallocate(p); } |
157 |
– |
// FIXME: really make surre array allocation fail at link time? |
158 |
– |
void *operator new[](size_t); |
159 |
– |
void operator delete[](void *p); |
160 |
– |
|
162 |
|
// Make sure the SIGSEGV handler can access CPU registers |
163 |
|
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
164 |
|
}; |
165 |
|
|
166 |
< |
lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator; |
166 |
> |
// Memory allocator returning areas aligned on 16-byte boundaries |
167 |
> |
void *operator new(size_t size) |
168 |
> |
{ |
169 |
> |
void *p; |
170 |
> |
|
171 |
> |
#if defined(HAVE_POSIX_MEMALIGN) |
172 |
> |
if (posix_memalign(&p, 16, size) != 0) |
173 |
> |
throw std::bad_alloc(); |
174 |
> |
#elif defined(HAVE_MEMALIGN) |
175 |
> |
p = memalign(16, size); |
176 |
> |
#elif defined(HAVE_VALLOC) |
177 |
> |
p = valloc(size); // page-aligned! |
178 |
> |
#else |
179 |
> |
/* XXX: handle padding ourselves */ |
180 |
> |
p = malloc(size); |
181 |
> |
#endif |
182 |
> |
|
183 |
> |
return p; |
184 |
> |
} |
185 |
> |
|
186 |
> |
void operator delete(void *p) |
187 |
> |
{ |
188 |
> |
#if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC) |
189 |
> |
#if defined(__GLIBC__) |
190 |
> |
// this is known to work only with GNU libc |
191 |
> |
free(p); |
192 |
> |
#endif |
193 |
> |
#else |
194 |
> |
free(p); |
195 |
> |
#endif |
196 |
> |
} |
197 |
|
|
198 |
|
sheepshaver_cpu::sheepshaver_cpu() |
199 |
|
: powerpc_cpu(enable_jit_p()) |
203 |
|
|
204 |
|
void sheepshaver_cpu::init_decoder() |
205 |
|
{ |
175 |
– |
#ifndef PPC_NO_STATIC_II_INDEX_TABLE |
176 |
– |
static bool initialized = false; |
177 |
– |
if (initialized) |
178 |
– |
return; |
179 |
– |
initialized = true; |
180 |
– |
#endif |
181 |
– |
|
206 |
|
static const instr_info_t sheep_ii_table[] = { |
207 |
|
{ "sheep", |
208 |
|
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
235 |
|
typedef bit_field< 21, 25 > NATIVE_OP_field; |
236 |
|
typedef bit_field< 26, 31 > EMUL_OP_field; |
237 |
|
|
238 |
+ |
// Execute EMUL_OP routine |
239 |
+ |
void sheepshaver_cpu::execute_emul_op(uint32 emul_op) |
240 |
+ |
{ |
241 |
+ |
M68kRegisters r68; |
242 |
+ |
WriteMacInt32(XLM_68K_R25, gpr(25)); |
243 |
+ |
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP); |
244 |
+ |
for (int i = 0; i < 8; i++) |
245 |
+ |
r68.d[i] = gpr(8 + i); |
246 |
+ |
for (int i = 0; i < 7; i++) |
247 |
+ |
r68.a[i] = gpr(16 + i); |
248 |
+ |
r68.a[7] = gpr(1); |
249 |
+ |
uint32 saved_cr = get_cr() & CR_field<2>::mask(); |
250 |
+ |
uint32 saved_xer = get_xer(); |
251 |
+ |
EmulOp(&r68, gpr(24), emul_op); |
252 |
+ |
set_cr(saved_cr); |
253 |
+ |
set_xer(saved_xer); |
254 |
+ |
for (int i = 0; i < 8; i++) |
255 |
+ |
gpr(8 + i) = r68.d[i]; |
256 |
+ |
for (int i = 0; i < 7; i++) |
257 |
+ |
gpr(16 + i) = r68.a[i]; |
258 |
+ |
gpr(1) = r68.a[7]; |
259 |
+ |
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
260 |
+ |
} |
261 |
+ |
|
262 |
|
// Execute SheepShaver instruction |
263 |
|
void sheepshaver_cpu::execute_sheep(uint32 opcode) |
264 |
|
{ |
282 |
|
pc() += 4; |
283 |
|
break; |
284 |
|
|
285 |
< |
default: { // EMUL_OP |
286 |
< |
M68kRegisters r68; |
239 |
< |
WriteMacInt32(XLM_68K_R25, gpr(25)); |
240 |
< |
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP); |
241 |
< |
for (int i = 0; i < 8; i++) |
242 |
< |
r68.d[i] = gpr(8 + i); |
243 |
< |
for (int i = 0; i < 7; i++) |
244 |
< |
r68.a[i] = gpr(16 + i); |
245 |
< |
r68.a[7] = gpr(1); |
246 |
< |
uint32 saved_cr = get_cr() & CR_field<2>::mask(); |
247 |
< |
uint32 saved_xer = get_xer(); |
248 |
< |
EmulOp(&r68, gpr(24), EMUL_OP_field::extract(opcode) - 3); |
249 |
< |
set_cr(saved_cr); |
250 |
< |
set_xer(saved_xer); |
251 |
< |
for (int i = 0; i < 8; i++) |
252 |
< |
gpr(8 + i) = r68.d[i]; |
253 |
< |
for (int i = 0; i < 7; i++) |
254 |
< |
gpr(16 + i) = r68.a[i]; |
255 |
< |
gpr(1) = r68.a[7]; |
256 |
< |
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
285 |
> |
default: // EMUL_OP |
286 |
> |
execute_emul_op(EMUL_OP_field::extract(opcode) - 3); |
287 |
|
pc() += 4; |
288 |
|
break; |
289 |
|
} |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
// Compile one instruction |
293 |
+ |
bool sheepshaver_cpu::compile1(codegen_context_t & cg_context) |
294 |
+ |
{ |
295 |
+ |
#if PPC_ENABLE_JIT |
296 |
+ |
const instr_info_t *ii = cg_context.instr_info; |
297 |
+ |
if (ii->mnemo != PPC_I(SHEEP)) |
298 |
+ |
return false; |
299 |
+ |
|
300 |
+ |
bool compiled = false; |
301 |
+ |
powerpc_dyngen & dg = cg_context.codegen; |
302 |
+ |
uint32 opcode = cg_context.opcode; |
303 |
+ |
|
304 |
+ |
switch (opcode & 0x3f) { |
305 |
+ |
case 0: // EMUL_RETURN |
306 |
+ |
dg.gen_invoke(QuitEmulator); |
307 |
+ |
compiled = true; |
308 |
+ |
break; |
309 |
+ |
|
310 |
+ |
case 1: // EXEC_RETURN |
311 |
+ |
dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN); |
312 |
+ |
compiled = true; |
313 |
+ |
break; |
314 |
+ |
|
315 |
+ |
case 2: { // EXEC_NATIVE |
316 |
+ |
uint32 selector = NATIVE_OP_field::extract(opcode); |
317 |
+ |
switch (selector) { |
318 |
+ |
case NATIVE_PATCH_NAME_REGISTRY: |
319 |
+ |
dg.gen_invoke(DoPatchNameRegistry); |
320 |
+ |
compiled = true; |
321 |
+ |
break; |
322 |
+ |
case NATIVE_VIDEO_INSTALL_ACCEL: |
323 |
+ |
dg.gen_invoke(VideoInstallAccel); |
324 |
+ |
compiled = true; |
325 |
+ |
break; |
326 |
+ |
case NATIVE_VIDEO_VBL: |
327 |
+ |
dg.gen_invoke(VideoVBL); |
328 |
+ |
compiled = true; |
329 |
+ |
break; |
330 |
+ |
case NATIVE_GET_RESOURCE: |
331 |
+ |
case NATIVE_GET_1_RESOURCE: |
332 |
+ |
case NATIVE_GET_IND_RESOURCE: |
333 |
+ |
case NATIVE_GET_1_IND_RESOURCE: |
334 |
+ |
case NATIVE_R_GET_RESOURCE: { |
335 |
+ |
static const uint32 get_resource_ptr[] = { |
336 |
+ |
XLM_GET_RESOURCE, |
337 |
+ |
XLM_GET_1_RESOURCE, |
338 |
+ |
XLM_GET_IND_RESOURCE, |
339 |
+ |
XLM_GET_1_IND_RESOURCE, |
340 |
+ |
XLM_R_GET_RESOURCE |
341 |
+ |
}; |
342 |
+ |
uint32 old_get_resource = ReadMacInt32(get_resource_ptr[selector - NATIVE_GET_RESOURCE]); |
343 |
+ |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
344 |
+ |
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr(); |
345 |
+ |
dg.gen_invoke_CPU_im(func, old_get_resource); |
346 |
+ |
compiled = true; |
347 |
+ |
break; |
348 |
+ |
} |
349 |
+ |
case NATIVE_DISABLE_INTERRUPT: |
350 |
+ |
dg.gen_invoke(DisableInterrupt); |
351 |
+ |
compiled = true; |
352 |
+ |
break; |
353 |
+ |
case NATIVE_ENABLE_INTERRUPT: |
354 |
+ |
dg.gen_invoke(EnableInterrupt); |
355 |
+ |
compiled = true; |
356 |
+ |
break; |
357 |
+ |
case NATIVE_CHECK_LOAD_INVOC: |
358 |
+ |
dg.gen_load_T0_GPR(3); |
359 |
+ |
dg.gen_load_T1_GPR(4); |
360 |
+ |
dg.gen_se_16_32_T1(); |
361 |
+ |
dg.gen_load_T2_GPR(5); |
362 |
+ |
dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc); |
363 |
+ |
compiled = true; |
364 |
+ |
break; |
365 |
+ |
} |
366 |
+ |
if (FN_field::test(opcode)) { |
367 |
+ |
if (compiled) { |
368 |
+ |
dg.gen_load_A0_LR(); |
369 |
+ |
dg.gen_set_PC_A0(); |
370 |
+ |
} |
371 |
+ |
cg_context.done_compile = true; |
372 |
+ |
} |
373 |
+ |
else |
374 |
+ |
cg_context.done_compile = false; |
375 |
+ |
break; |
376 |
+ |
} |
377 |
+ |
|
378 |
+ |
default: { // EMUL_OP |
379 |
+ |
typedef void (*func_t)(dyngen_cpu_base, uint32); |
380 |
+ |
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr(); |
381 |
+ |
dg.gen_invoke_CPU_im(func, EMUL_OP_field::extract(opcode) - 3); |
382 |
+ |
cg_context.done_compile = false; |
383 |
+ |
compiled = true; |
384 |
+ |
break; |
385 |
|
} |
386 |
+ |
} |
387 |
+ |
return compiled; |
388 |
+ |
#endif |
389 |
+ |
return false; |
390 |
|
} |
391 |
|
|
392 |
|
// Handle MacOS interrupt |
622 |
|
} |
623 |
|
|
624 |
|
// Resource Manager thunk |
496 |
– |
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h); |
497 |
– |
|
625 |
|
inline void sheepshaver_cpu::get_resource(uint32 old_get_resource) |
626 |
|
{ |
627 |
|
uint32 type = gpr(3); |
731 |
|
else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
732 |
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
733 |
|
|
734 |
+ |
// Ignore writes to the zero page |
735 |
+ |
else if ((uint32)(addr - SheepMem::ZeroPage()) < (uint32)SheepMem::PageSize()) |
736 |
+ |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
737 |
+ |
|
738 |
|
// Ignore all other faults, if requested |
739 |
|
if (PrefsFindBool("ignoresegv")) |
740 |
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
985 |
|
case NATIVE_ETHER_RSRV: |
986 |
|
GPR(3) = ether_rsrv((queue_t *)GPR(3)); |
987 |
|
break; |
988 |
+ |
case NATIVE_SYNC_HOOK: |
989 |
+ |
GPR(3) = NQD_sync_hook(GPR(3)); |
990 |
+ |
break; |
991 |
+ |
case NATIVE_BITBLT_HOOK: |
992 |
+ |
GPR(3) = NQD_bitblt_hook(GPR(3)); |
993 |
+ |
break; |
994 |
+ |
case NATIVE_BITBLT: |
995 |
+ |
NQD_bitblt(GPR(3)); |
996 |
+ |
break; |
997 |
+ |
#if 0 |
998 |
+ |
case NATIVE_FILLRECT_HOOK: |
999 |
+ |
GPR(3) = NQD_fillrect_hook(GPR(3)); |
1000 |
+ |
break; |
1001 |
+ |
case NATIVE_INVRECT: |
1002 |
+ |
NQD_invrect(GPR(3)); |
1003 |
+ |
break; |
1004 |
+ |
case NATIVE_FILLRECT_8: |
1005 |
+ |
NQD_fillrect8(GPR(3)); |
1006 |
+ |
break; |
1007 |
+ |
case NATIVE_FILLRECT_32: |
1008 |
+ |
NQD_fillrect32(GPR(3)); |
1009 |
+ |
break; |
1010 |
+ |
#endif |
1011 |
|
#else |
1012 |
|
case NATIVE_ETHER_INIT: |
1013 |
|
// FIXME: needs more complicated thunks |
1059 |
|
case NATIVE_MAKE_EXECUTABLE: |
1060 |
|
MakeExecutable(0, (void *)GPR(4), GPR(5)); |
1061 |
|
break; |
1062 |
+ |
case NATIVE_CHECK_LOAD_INVOC: |
1063 |
+ |
check_load_invoc(GPR(3), GPR(4), GPR(5)); |
1064 |
+ |
break; |
1065 |
|
default: |
1066 |
|
printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector); |
1067 |
|
QuitEmulator(); |