42 |
|
|
43 |
|
#include <stdio.h> |
44 |
|
#include <stdlib.h> |
45 |
+ |
#ifdef HAVE_MALLOC_H |
46 |
+ |
#include <malloc.h> |
47 |
+ |
#endif |
48 |
+ |
|
49 |
+ |
#ifdef USE_SDL_VIDEO |
50 |
+ |
#include <SDL_events.h> |
51 |
+ |
#endif |
52 |
|
|
53 |
|
#if ENABLE_MON |
54 |
|
#include "mon.h" |
59 |
|
#include "debug.h" |
60 |
|
|
61 |
|
// Emulation time statistics |
62 |
< |
#define EMUL_TIME_STATS 1 |
62 |
> |
#ifndef EMUL_TIME_STATS |
63 |
> |
#define EMUL_TIME_STATS 0 |
64 |
> |
#endif |
65 |
|
|
66 |
|
#if EMUL_TIME_STATS |
67 |
|
static clock_t emul_start_time; |
68 |
< |
static uint32 interrupt_count = 0; |
68 |
> |
static uint32 interrupt_count = 0, ppc_interrupt_count = 0; |
69 |
|
static clock_t interrupt_time = 0; |
70 |
|
static uint32 exec68k_count = 0; |
71 |
|
static clock_t exec68k_time = 0; |
108 |
|
// Interrupts in native mode? |
109 |
|
#define INTERRUPTS_IN_NATIVE_MODE 1 |
110 |
|
|
102 |
– |
// Enable native EMUL_OPs to be run without a mode switch |
103 |
– |
#define ENABLE_NATIVE_EMUL_OP 1 |
104 |
– |
|
111 |
|
// Pointer to Kernel Data |
112 |
< |
static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE; |
112 |
> |
static KernelData * kernel_data; |
113 |
|
|
114 |
|
// SIGSEGV handler |
115 |
< |
static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
115 |
> |
sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
116 |
|
|
117 |
|
#if PPC_ENABLE_JIT && PPC_REENTRANT_JIT |
118 |
|
// Special trampolines for EmulOp and NativeOp |
142 |
|
void init_decoder(); |
143 |
|
void execute_sheep(uint32 opcode); |
144 |
|
|
139 |
– |
// Filter out EMUL_OP routines that only call native code |
140 |
– |
bool filter_execute_emul_op(uint32 emul_op); |
141 |
– |
|
142 |
– |
// "Native" EMUL_OP routines |
143 |
– |
void execute_emul_op_microseconds(); |
144 |
– |
void execute_emul_op_idle_time_1(); |
145 |
– |
void execute_emul_op_idle_time_2(); |
146 |
– |
|
145 |
|
// CPU context to preserve on interrupt |
146 |
|
class interrupt_context { |
147 |
|
uint32 gpr[32]; |
148 |
+ |
double fpr[32]; |
149 |
|
uint32 pc; |
150 |
|
uint32 lr; |
151 |
|
uint32 ctr; |
152 |
|
uint32 cr; |
153 |
|
uint32 xer; |
154 |
+ |
uint32 fpscr; |
155 |
|
sheepshaver_cpu *cpu; |
156 |
|
const char *where; |
157 |
|
public: |
185 |
|
// Execute MacOS/PPC code |
186 |
|
uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args); |
187 |
|
|
188 |
+ |
#if PPC_ENABLE_JIT |
189 |
|
// Compile one instruction |
190 |
|
virtual int compile1(codegen_context_t & cg_context); |
191 |
< |
|
191 |
> |
#endif |
192 |
|
// Resource manager thunk |
193 |
|
void get_resource(uint32 old_get_resource); |
194 |
|
|
198 |
|
|
199 |
|
// Make sure the SIGSEGV handler can access CPU registers |
200 |
|
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
201 |
+ |
|
202 |
+ |
// Memory allocator returning areas aligned on 16-byte boundaries |
203 |
+ |
void *operator new(size_t size); |
204 |
+ |
void operator delete(void *p); |
205 |
|
}; |
206 |
|
|
207 |
|
// Memory allocator returning areas aligned on 16-byte boundaries |
208 |
< |
void *operator new(size_t size) |
208 |
> |
void *sheepshaver_cpu::operator new(size_t size) |
209 |
|
{ |
210 |
|
void *p; |
211 |
|
|
224 |
|
return p; |
225 |
|
} |
226 |
|
|
227 |
< |
void operator delete(void *p) |
227 |
> |
void sheepshaver_cpu::operator delete(void *p) |
228 |
|
{ |
229 |
|
#if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC) |
230 |
|
#if defined(__GLIBC__) |
273 |
|
typedef bit_field< 20, 25 > NATIVE_OP_field; |
274 |
|
typedef bit_field< 26, 31 > EMUL_OP_field; |
275 |
|
|
271 |
– |
// "Native" EMUL_OP routines |
272 |
– |
#define GPR_A(REG) gpr(16 + (REG)) |
273 |
– |
#define GPR_D(REG) gpr( 8 + (REG)) |
274 |
– |
|
275 |
– |
void sheepshaver_cpu::execute_emul_op_microseconds() |
276 |
– |
{ |
277 |
– |
Microseconds(GPR_A(0), GPR_D(0)); |
278 |
– |
} |
279 |
– |
|
280 |
– |
void sheepshaver_cpu::execute_emul_op_idle_time_1() |
281 |
– |
{ |
282 |
– |
// Sleep if no events pending |
283 |
– |
if (ReadMacInt32(0x14c) == 0) |
284 |
– |
Delay_usec(16667); |
285 |
– |
GPR_A(0) = ReadMacInt32(0x2b6); |
286 |
– |
} |
287 |
– |
|
288 |
– |
void sheepshaver_cpu::execute_emul_op_idle_time_2() |
289 |
– |
{ |
290 |
– |
// Sleep if no events pending |
291 |
– |
if (ReadMacInt32(0x14c) == 0) |
292 |
– |
Delay_usec(16667); |
293 |
– |
GPR_D(0) = (uint32)-2; |
294 |
– |
} |
295 |
– |
|
296 |
– |
// Filter out EMUL_OP routines that only call native code |
297 |
– |
bool sheepshaver_cpu::filter_execute_emul_op(uint32 emul_op) |
298 |
– |
{ |
299 |
– |
switch (emul_op) { |
300 |
– |
case OP_MICROSECONDS: |
301 |
– |
execute_emul_op_microseconds(); |
302 |
– |
return true; |
303 |
– |
case OP_IDLE_TIME: |
304 |
– |
execute_emul_op_idle_time_1(); |
305 |
– |
return true; |
306 |
– |
case OP_IDLE_TIME_2: |
307 |
– |
execute_emul_op_idle_time_2(); |
308 |
– |
return true; |
309 |
– |
} |
310 |
– |
return false; |
311 |
– |
} |
312 |
– |
|
276 |
|
// Execute EMUL_OP routine |
277 |
|
void sheepshaver_cpu::execute_emul_op(uint32 emul_op) |
278 |
|
{ |
316 |
– |
#if ENABLE_NATIVE_EMUL_OP |
317 |
– |
// First, filter out EMUL_OPs that can be executed without a mode switch |
318 |
– |
if (filter_execute_emul_op(emul_op)) |
319 |
– |
return; |
320 |
– |
#endif |
321 |
– |
|
279 |
|
M68kRegisters r68; |
280 |
|
WriteMacInt32(XLM_68K_R25, gpr(25)); |
281 |
|
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP); |
328 |
|
} |
329 |
|
|
330 |
|
// Compile one instruction |
331 |
+ |
#if PPC_ENABLE_JIT |
332 |
|
int sheepshaver_cpu::compile1(codegen_context_t & cg_context) |
333 |
|
{ |
376 |
– |
#if PPC_ENABLE_JIT |
334 |
|
const instr_info_t *ii = cg_context.instr_info; |
335 |
|
if (ii->mnemo != PPC_I(SHEEP)) |
336 |
|
return COMPILE_FAILURE; |
401 |
|
status = COMPILE_CODE_OK; |
402 |
|
break; |
403 |
|
#endif |
447 |
– |
case NATIVE_DISABLE_INTERRUPT: |
448 |
– |
dg.gen_invoke(DisableInterrupt); |
449 |
– |
status = COMPILE_CODE_OK; |
450 |
– |
break; |
451 |
– |
case NATIVE_ENABLE_INTERRUPT: |
452 |
– |
dg.gen_invoke(EnableInterrupt); |
453 |
– |
status = COMPILE_CODE_OK; |
454 |
– |
break; |
404 |
|
case NATIVE_BITBLT: |
405 |
|
dg.gen_load_T0_GPR(3); |
406 |
|
dg.gen_invoke_T0((void (*)(uint32))NQD_bitblt); |
457 |
|
|
458 |
|
default: { // EMUL_OP |
459 |
|
uint32 emul_op = EMUL_OP_field::extract(opcode) - 3; |
511 |
– |
#if ENABLE_NATIVE_EMUL_OP |
512 |
– |
typedef void (*emul_op_func_t)(dyngen_cpu_base); |
513 |
– |
emul_op_func_t emul_op_func = 0; |
514 |
– |
switch (emul_op) { |
515 |
– |
case OP_MICROSECONDS: |
516 |
– |
emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_microseconds).ptr(); |
517 |
– |
break; |
518 |
– |
case OP_IDLE_TIME: |
519 |
– |
emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_1).ptr(); |
520 |
– |
break; |
521 |
– |
case OP_IDLE_TIME_2: |
522 |
– |
emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_2).ptr(); |
523 |
– |
break; |
524 |
– |
} |
525 |
– |
if (emul_op_func) { |
526 |
– |
dg.gen_invoke_CPU(emul_op_func); |
527 |
– |
cg_context.done_compile = false; |
528 |
– |
status = COMPILE_CODE_OK; |
529 |
– |
break; |
530 |
– |
} |
531 |
– |
#endif |
460 |
|
#if PPC_REENTRANT_JIT |
461 |
|
// Try to execute EmulOp trampoline |
462 |
|
dg.gen_set_PC_im(cg_context.pc + 4); |
476 |
|
} |
477 |
|
} |
478 |
|
return status; |
551 |
– |
#endif |
552 |
– |
return COMPILE_FAILURE; |
479 |
|
} |
480 |
+ |
#endif |
481 |
|
|
482 |
|
// CPU context to preserve on interrupt |
483 |
|
sheepshaver_cpu::interrupt_context::interrupt_context(sheepshaver_cpu *_cpu, const char *_where) |
488 |
|
|
489 |
|
// Save interrupt context |
490 |
|
memcpy(&gpr[0], &cpu->gpr(0), sizeof(gpr)); |
491 |
+ |
memcpy(&fpr[0], &cpu->fpr(0), sizeof(fpr)); |
492 |
|
pc = cpu->pc(); |
493 |
|
lr = cpu->lr(); |
494 |
|
ctr = cpu->ctr(); |
495 |
|
cr = cpu->get_cr(); |
496 |
|
xer = cpu->get_xer(); |
497 |
+ |
fpscr = cpu->fpscr(); |
498 |
|
#endif |
499 |
|
} |
500 |
|
|
508 |
|
if (gpr[i] != cpu->gpr(i)) |
509 |
|
printf(" r%d: %08x -> %08x\n", i, gpr[i], cpu->gpr(i)); |
510 |
|
} |
511 |
+ |
if (memcmp(&fpr[0], &cpu->fpr(0), sizeof(fpr)) != 0) { |
512 |
+ |
printf("FATAL: %s: interrupt clobbers registers\n", where); |
513 |
+ |
for (int i = 0; i < 32; i++) |
514 |
+ |
if (fpr[i] != cpu->fpr(i)) |
515 |
+ |
printf(" r%d: %f -> %f\n", i, fpr[i], cpu->fpr(i)); |
516 |
+ |
} |
517 |
|
if (pc != cpu->pc()) |
518 |
|
printf("FATAL: %s: interrupt clobbers PC\n", where); |
519 |
|
if (lr != cpu->lr()) |
524 |
|
printf("FATAL: %s: interrupt clobbers CR\n", where); |
525 |
|
if (xer != cpu->get_xer()) |
526 |
|
printf("FATAL: %s: interrupt clobbers XER\n", where); |
527 |
+ |
if (fpscr != cpu->fpscr()) |
528 |
+ |
printf("FATAL: %s: interrupt clobbers FPSCR\n", where); |
529 |
|
#endif |
530 |
|
} |
531 |
|
|
533 |
|
void sheepshaver_cpu::interrupt(uint32 entry) |
534 |
|
{ |
535 |
|
#if EMUL_TIME_STATS |
536 |
< |
interrupt_count++; |
536 |
> |
ppc_interrupt_count++; |
537 |
|
const clock_t interrupt_start = clock(); |
538 |
|
#endif |
539 |
|
|
819 |
|
* Initialize CPU emulation |
820 |
|
*/ |
821 |
|
|
822 |
< |
static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction) |
822 |
> |
sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction) |
823 |
|
{ |
824 |
|
#if ENABLE_VOSF |
825 |
|
// Handle screen fault |
831 |
|
const uintptr addr = (uintptr)fault_address; |
832 |
|
#if HAVE_SIGSEGV_SKIP_INSTRUCTION |
833 |
|
// Ignore writes to ROM |
834 |
< |
if ((addr - ROM_BASE) < ROM_SIZE) |
834 |
> |
if ((addr - (uintptr)ROMBaseHost) < ROM_SIZE) |
835 |
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
836 |
|
|
837 |
|
// Get program counter of target CPU |
891 |
|
|
892 |
|
void init_emul_ppc(void) |
893 |
|
{ |
894 |
+ |
// Get pointer to KernelData in host address space |
895 |
+ |
kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE); |
896 |
+ |
|
897 |
|
// Initialize main CPU emulator |
898 |
|
ppc_cpu = new sheepshaver_cpu(); |
899 |
|
ppc_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000)); |
900 |
|
ppc_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000)); |
901 |
|
WriteMacInt32(XLM_RUN_MODE, MODE_68K); |
902 |
|
|
963 |
– |
// Install the handler for SIGSEGV |
964 |
– |
sigsegv_install_handler(sigsegv_handler); |
965 |
– |
|
903 |
|
#if ENABLE_MON |
904 |
|
// Install "regs" command in cxmon |
905 |
|
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n"); |
925 |
|
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC)); |
926 |
|
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count, |
927 |
|
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time)); |
928 |
+ |
printf("Total ppc interrupt count: %d (%2.1f %%)\n", ppc_interrupt_count, |
929 |
+ |
(double(ppc_interrupt_count) * 100.0) / double(interrupt_count)); |
930 |
|
|
931 |
|
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \ |
932 |
|
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \ |
1002 |
|
|
1003 |
|
void sheepshaver_cpu::handle_interrupt(void) |
1004 |
|
{ |
1005 |
< |
// Do nothing if interrupts are disabled |
1006 |
< |
if (*(int32 *)XLM_IRQ_NEST > 0) |
1007 |
< |
return; |
1005 |
> |
#ifdef USE_SDL_VIDEO |
1006 |
> |
// We must fill in the events queue in the same thread that did call SDL_SetVideoMode() |
1007 |
> |
SDL_PumpEvents(); |
1008 |
> |
#endif |
1009 |
|
|
1010 |
< |
// Do nothing if there is no interrupt pending |
1011 |
< |
if (InterruptFlags == 0) |
1010 |
> |
// Do nothing if interrupts are disabled |
1011 |
> |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
1012 |
|
return; |
1013 |
|
|
1014 |
|
// Current interrupt nest level |
1015 |
|
static int interrupt_depth = 0; |
1016 |
|
++interrupt_depth; |
1017 |
+ |
#if EMUL_TIME_STATS |
1018 |
+ |
interrupt_count++; |
1019 |
+ |
#endif |
1020 |
|
|
1021 |
|
// Disable MacOS stack sniffer |
1022 |
|
WriteMacInt32(0x110, 0); |
1056 |
|
// 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0 |
1057 |
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) { |
1058 |
|
interrupt_context ctx(this, "68k mode"); |
1059 |
+ |
#if EMUL_TIME_STATS |
1060 |
+ |
const clock_t interrupt_start = clock(); |
1061 |
+ |
#endif |
1062 |
|
#if 1 |
1063 |
|
// Execute full 68k interrupt routine |
1064 |
|
M68kRegisters r; |
1065 |
|
uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level |
1066 |
|
WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1 |
1067 |
< |
static const uint8 proc[] = { |
1067 |
> |
static const uint8 proc_template[] = { |
1068 |
|
0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word) |
1069 |
|
0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address) |
1070 |
|
0x40, 0xe7, // move sr,-(sp) (saved SR) |
1072 |
|
0x4e, 0xd0, // jmp (a0) |
1073 |
|
M68K_RTS >> 8, M68K_RTS & 0xff // @1 |
1074 |
|
}; |
1075 |
< |
Execute68k((uint32)proc, &r); |
1075 |
> |
BUILD_SHEEPSHAVER_PROCEDURE(proc); |
1076 |
> |
Execute68k(proc, &r); |
1077 |
|
WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level |
1078 |
|
#else |
1079 |
|
// Only update cursor |
1085 |
|
} |
1086 |
|
} |
1087 |
|
#endif |
1088 |
+ |
#if EMUL_TIME_STATS |
1089 |
+ |
interrupt_time += (clock() - interrupt_start); |
1090 |
+ |
#endif |
1091 |
|
} |
1092 |
|
break; |
1093 |
|
#endif |
1122 |
|
VideoVBL(); |
1123 |
|
break; |
1124 |
|
case NATIVE_VIDEO_DO_DRIVER_IO: |
1125 |
< |
gpr(3) = (int32)(int16)VideoDoDriverIO((void *)gpr(3), (void *)gpr(4), |
1176 |
< |
(void *)gpr(5), gpr(6), gpr(7)); |
1125 |
> |
gpr(3) = (int32)(int16)VideoDoDriverIO(gpr(3), gpr(4), gpr(5), gpr(6), gpr(7)); |
1126 |
|
break; |
1178 |
– |
#ifdef WORDS_BIGENDIAN |
1127 |
|
case NATIVE_ETHER_IRQ: |
1128 |
|
EtherIRQ(); |
1129 |
|
break; |
1145 |
|
case NATIVE_ETHER_RSRV: |
1146 |
|
gpr(3) = ether_rsrv((queue_t *)gpr(3)); |
1147 |
|
break; |
1200 |
– |
#else |
1201 |
– |
case NATIVE_ETHER_INIT: |
1202 |
– |
// FIXME: needs more complicated thunks |
1203 |
– |
gpr(3) = false; |
1204 |
– |
break; |
1205 |
– |
#endif |
1148 |
|
case NATIVE_SYNC_HOOK: |
1149 |
|
gpr(3) = NQD_sync_hook(gpr(3)); |
1150 |
|
break; |
1199 |
|
get_resource_callbacks[selector - NATIVE_GET_RESOURCE](); |
1200 |
|
break; |
1201 |
|
} |
1260 |
– |
case NATIVE_DISABLE_INTERRUPT: |
1261 |
– |
DisableInterrupt(); |
1262 |
– |
break; |
1263 |
– |
case NATIVE_ENABLE_INTERRUPT: |
1264 |
– |
EnableInterrupt(); |
1265 |
– |
break; |
1202 |
|
case NATIVE_MAKE_EXECUTABLE: |
1203 |
< |
MakeExecutable(0, (void *)gpr(4), gpr(5)); |
1203 |
> |
MakeExecutable(0, gpr(4), gpr(5)); |
1204 |
|
break; |
1205 |
|
case NATIVE_CHECK_LOAD_INVOC: |
1206 |
|
check_load_invoc(gpr(3), gpr(4), gpr(5)); |