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 |
|
|
43 |
|
#include "mon_disass.h" |
44 |
|
#endif |
45 |
|
|
46 |
< |
#define DEBUG 1 |
46 |
> |
#define DEBUG 0 |
47 |
|
#include "debug.h" |
48 |
|
|
49 |
+ |
// Emulation time statistics |
50 |
+ |
#define EMUL_TIME_STATS 1 |
51 |
+ |
|
52 |
+ |
#if EMUL_TIME_STATS |
53 |
+ |
static clock_t emul_start_time; |
54 |
+ |
static uint32 interrupt_count = 0; |
55 |
+ |
static clock_t interrupt_time = 0; |
56 |
+ |
static uint32 exec68k_count = 0; |
57 |
+ |
static clock_t exec68k_time = 0; |
58 |
+ |
static uint32 native_exec_count = 0; |
59 |
+ |
static clock_t native_exec_time = 0; |
60 |
+ |
static uint32 macos_exec_count = 0; |
61 |
+ |
static clock_t macos_exec_time = 0; |
62 |
+ |
#endif |
63 |
+ |
|
64 |
|
static void enter_mon(void) |
65 |
|
{ |
66 |
|
// Start up mon in real-mode |
71 |
|
} |
72 |
|
|
73 |
|
// Enable multicore (main/interrupts) cpu emulation? |
74 |
< |
#define MULTICORE_CPU 0 |
74 |
> |
#define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0) |
75 |
|
|
76 |
|
// Enable Execute68k() safety checks? |
77 |
|
#define SAFE_EXEC_68K 1 |
93 |
|
* PowerPC emulator glue with special 'sheep' opcodes |
94 |
|
**/ |
95 |
|
|
82 |
– |
struct sheepshaver_exec_return { }; |
83 |
– |
|
96 |
|
class sheepshaver_cpu |
97 |
|
: public powerpc_cpu |
98 |
|
{ |
101 |
|
|
102 |
|
public: |
103 |
|
|
104 |
< |
sheepshaver_cpu() |
105 |
< |
: powerpc_cpu() |
94 |
< |
{ init_decoder(); } |
104 |
> |
// Constructor |
105 |
> |
sheepshaver_cpu(); |
106 |
|
|
107 |
|
// Condition Register accessors |
108 |
|
uint32 get_cr() const { return cr().get(); } |
109 |
|
void set_cr(uint32 v) { cr().set(v); } |
110 |
|
|
111 |
|
// Execution loop |
112 |
< |
void execute(uint32 pc); |
112 |
> |
void execute(uint32 entry, bool enable_cache = false); |
113 |
|
|
114 |
|
// Execute 68k routine |
115 |
|
void execute_68k(uint32 entry, M68kRegisters *r); |
125 |
|
|
126 |
|
// Handle MacOS interrupt |
127 |
|
void interrupt(uint32 entry); |
128 |
< |
|
118 |
< |
// spcflags for interrupts handling |
119 |
< |
static uint32 spcflags; |
128 |
> |
void handle_interrupt(); |
129 |
|
|
130 |
|
// Lazy memory allocator (one item at a time) |
131 |
|
void *operator new(size_t size) |
137 |
|
void operator delete[](void *p); |
138 |
|
}; |
139 |
|
|
131 |
– |
uint32 sheepshaver_cpu::spcflags = 0; |
140 |
|
lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator; |
141 |
|
|
142 |
+ |
sheepshaver_cpu::sheepshaver_cpu() |
143 |
+ |
: powerpc_cpu() |
144 |
+ |
{ |
145 |
+ |
init_decoder(); |
146 |
+ |
} |
147 |
+ |
|
148 |
|
void sheepshaver_cpu::init_decoder() |
149 |
|
{ |
150 |
|
#ifndef PPC_NO_STATIC_II_INDEX_TABLE |
156 |
|
|
157 |
|
static const instr_info_t sheep_ii_table[] = { |
158 |
|
{ "sheep", |
159 |
< |
(execute_fn)&sheepshaver_cpu::execute_sheep, |
159 |
> |
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
160 |
|
NULL, |
161 |
|
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
162 |
|
} |
197 |
|
break; |
198 |
|
|
199 |
|
case 1: // EXEC_RETURN |
200 |
< |
throw sheepshaver_exec_return(); |
200 |
> |
spcflags().set(SPCFLAG_CPU_EXEC_RETURN); |
201 |
|
break; |
202 |
|
|
203 |
|
case 2: // EXEC_NATIVE |
230 |
|
} |
231 |
|
} |
232 |
|
|
219 |
– |
// Checks for pending interrupts |
220 |
– |
struct execute_nothing { |
221 |
– |
static inline void execute(powerpc_cpu *) { } |
222 |
– |
}; |
223 |
– |
|
224 |
– |
struct execute_spcflags_check { |
225 |
– |
static inline void execute(powerpc_cpu *cpu) { |
226 |
– |
#if !ASYNC_IRQ |
227 |
– |
if (SPCFLAGS_TEST(SPCFLAG_ALL_BUT_EXEC_RETURN)) { |
228 |
– |
if (SPCFLAGS_TEST( SPCFLAG_ENTER_MON )) { |
229 |
– |
SPCFLAGS_CLEAR( SPCFLAG_ENTER_MON ); |
230 |
– |
enter_mon(); |
231 |
– |
} |
232 |
– |
if (SPCFLAGS_TEST( SPCFLAG_DOINT )) { |
233 |
– |
SPCFLAGS_CLEAR( SPCFLAG_DOINT ); |
234 |
– |
HandleInterrupt(); |
235 |
– |
} |
236 |
– |
if (SPCFLAGS_TEST( SPCFLAG_INT )) { |
237 |
– |
SPCFLAGS_CLEAR( SPCFLAG_INT ); |
238 |
– |
SPCFLAGS_SET( SPCFLAG_DOINT ); |
239 |
– |
} |
240 |
– |
} |
241 |
– |
#endif |
242 |
– |
} |
243 |
– |
}; |
244 |
– |
|
233 |
|
// Execution loop |
234 |
< |
void sheepshaver_cpu::execute(uint32 entry) |
234 |
> |
void sheepshaver_cpu::execute(uint32 entry, bool enable_cache) |
235 |
|
{ |
236 |
< |
try { |
249 |
< |
pc() = entry; |
250 |
< |
powerpc_cpu::do_execute<execute_nothing, execute_spcflags_check>(); |
251 |
< |
} |
252 |
< |
catch (sheepshaver_exec_return const &) { |
253 |
< |
// Nothing, simply return |
254 |
< |
} |
255 |
< |
catch (...) { |
256 |
< |
printf("ERROR: execute() received an unknown exception!\n"); |
257 |
< |
QuitEmulator(); |
258 |
< |
} |
236 |
> |
powerpc_cpu::execute(entry, enable_cache); |
237 |
|
} |
238 |
|
|
239 |
|
// Handle MacOS interrupt |
240 |
|
void sheepshaver_cpu::interrupt(uint32 entry) |
241 |
|
{ |
242 |
+ |
#if EMUL_TIME_STATS |
243 |
+ |
interrupt_count++; |
244 |
+ |
const clock_t interrupt_start = clock(); |
245 |
+ |
#endif |
246 |
+ |
|
247 |
|
#if !MULTICORE_CPU |
248 |
|
// Save program counters and branch registers |
249 |
|
uint32 saved_pc = pc(); |
297 |
|
ctr()= saved_ctr; |
298 |
|
gpr(1) = saved_sp; |
299 |
|
#endif |
300 |
+ |
|
301 |
+ |
#if EMUL_TIME_STATS |
302 |
+ |
interrupt_time += (clock() - interrupt_start); |
303 |
+ |
#endif |
304 |
|
} |
305 |
|
|
306 |
|
// Execute 68k routine |
307 |
|
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r) |
308 |
|
{ |
309 |
+ |
#if EMUL_TIME_STATS |
310 |
+ |
exec68k_count++; |
311 |
+ |
const clock_t exec68k_start = clock(); |
312 |
+ |
#endif |
313 |
+ |
|
314 |
|
#if SAFE_EXEC_68K |
315 |
|
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP) |
316 |
|
printf("FATAL: Execute68k() not called from EMUL_OP mode\n"); |
393 |
|
lr() = saved_lr; |
394 |
|
ctr()= saved_ctr; |
395 |
|
set_cr(saved_cr); |
396 |
+ |
|
397 |
+ |
#if EMUL_TIME_STATS |
398 |
+ |
exec68k_time += (clock() - exec68k_start); |
399 |
+ |
#endif |
400 |
|
} |
401 |
|
|
402 |
|
// Call MacOS PPC code |
403 |
|
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args) |
404 |
|
{ |
405 |
+ |
#if EMUL_TIME_STATS |
406 |
+ |
macos_exec_count++; |
407 |
+ |
const clock_t macos_exec_start = clock(); |
408 |
+ |
#endif |
409 |
+ |
|
410 |
|
// Save program counters and branch registers |
411 |
|
uint32 saved_pc = pc(); |
412 |
|
uint32 saved_lr = lr(); |
445 |
|
lr() = saved_lr; |
446 |
|
ctr()= saved_ctr; |
447 |
|
|
448 |
+ |
#if EMUL_TIME_STATS |
449 |
+ |
macos_exec_time += (clock() - macos_exec_start); |
450 |
+ |
#endif |
451 |
+ |
|
452 |
|
return retval; |
453 |
|
} |
454 |
|
|
592 |
|
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n"); |
593 |
|
mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n"); |
594 |
|
#endif |
595 |
+ |
|
596 |
+ |
#if EMUL_TIME_STATS |
597 |
+ |
emul_start_time = clock(); |
598 |
+ |
#endif |
599 |
+ |
} |
600 |
+ |
|
601 |
+ |
/* |
602 |
+ |
* Deinitialize emulation |
603 |
+ |
*/ |
604 |
+ |
|
605 |
+ |
void exit_emul_ppc(void) |
606 |
+ |
{ |
607 |
+ |
#if EMUL_TIME_STATS |
608 |
+ |
clock_t emul_end_time = clock(); |
609 |
+ |
|
610 |
+ |
printf("### Statistics for SheepShaver emulation parts\n"); |
611 |
+ |
const clock_t emul_time = emul_end_time - emul_start_time; |
612 |
+ |
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC)); |
613 |
+ |
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count, |
614 |
+ |
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time)); |
615 |
+ |
|
616 |
+ |
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \ |
617 |
+ |
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \ |
618 |
+ |
printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \ |
619 |
+ |
double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \ |
620 |
+ |
100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \ |
621 |
+ |
} while (0) |
622 |
+ |
|
623 |
+ |
PRINT_STATS("Execute68k[Trap] execution", exec68k); |
624 |
+ |
PRINT_STATS("NativeOp execution", native_exec); |
625 |
+ |
PRINT_STATS("MacOS routine execution", macos_exec); |
626 |
+ |
|
627 |
+ |
#undef PRINT_STATS |
628 |
+ |
printf("\n"); |
629 |
+ |
#endif |
630 |
+ |
|
631 |
+ |
delete main_cpu; |
632 |
+ |
#if MULTICORE_CPU |
633 |
+ |
delete interrupt_cpu; |
634 |
+ |
#endif |
635 |
|
} |
636 |
|
|
637 |
|
/* |
641 |
|
void emul_ppc(uint32 entry) |
642 |
|
{ |
643 |
|
current_cpu = main_cpu; |
644 |
+ |
#if DEBUG |
645 |
|
current_cpu->start_log(); |
646 |
< |
current_cpu->execute(entry); |
646 |
> |
#endif |
647 |
> |
// start emulation loop and enable code translation or caching |
648 |
> |
current_cpu->execute(entry, true); |
649 |
|
} |
650 |
|
|
651 |
|
/* |
652 |
|
* Handle PowerPC interrupt |
653 |
|
*/ |
654 |
|
|
655 |
< |
// Atomic operations |
656 |
< |
extern int atomic_add(int *var, int v); |
657 |
< |
extern int atomic_and(int *var, int v); |
658 |
< |
extern int atomic_or(int *var, int v); |
659 |
< |
|
660 |
< |
#if !ASYNC_IRQ |
655 |
> |
#if ASYNC_IRQ |
656 |
> |
void HandleInterrupt(void) |
657 |
> |
{ |
658 |
> |
main_cpu->handle_interrupt(); |
659 |
> |
} |
660 |
> |
#else |
661 |
|
void TriggerInterrupt(void) |
662 |
|
{ |
663 |
|
#if 0 |
664 |
|
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
665 |
|
#else |
666 |
< |
SPCFLAGS_SET( SPCFLAG_INT ); |
666 |
> |
// Trigger interrupt to main cpu only |
667 |
> |
if (main_cpu) |
668 |
> |
main_cpu->trigger_interrupt(); |
669 |
|
#endif |
670 |
|
} |
671 |
|
#endif |
672 |
|
|
673 |
< |
void HandleInterrupt(void) |
673 |
> |
void sheepshaver_cpu::handle_interrupt(void) |
674 |
|
{ |
675 |
|
// Do nothing if interrupts are disabled |
676 |
|
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
689 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
690 |
|
assert(current_cpu == main_cpu); |
691 |
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
692 |
< |
main_cpu->set_cr(main_cpu->get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
692 |
> |
set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
693 |
|
break; |
694 |
|
|
695 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
696 |
|
case MODE_NATIVE: |
697 |
|
// 68k emulator inactive, in nanokernel? |
698 |
|
assert(current_cpu == main_cpu); |
699 |
< |
if (main_cpu->gpr(1) != KernelDataAddr) { |
699 |
> |
if (gpr(1) != KernelDataAddr) { |
700 |
|
// Prepare for 68k interrupt level 1 |
701 |
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
702 |
|
WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc, |
797 |
|
|
798 |
|
static void NativeOp(int selector) |
799 |
|
{ |
800 |
+ |
#if EMUL_TIME_STATS |
801 |
+ |
native_exec_count++; |
802 |
+ |
const clock_t native_exec_start = clock(); |
803 |
+ |
#endif |
804 |
+ |
|
805 |
|
switch (selector) { |
806 |
|
case NATIVE_PATCH_NAME_REGISTRY: |
807 |
|
DoPatchNameRegistry(); |
865 |
|
QuitEmulator(); |
866 |
|
break; |
867 |
|
} |
868 |
+ |
|
869 |
+ |
#if EMUL_TIME_STATS |
870 |
+ |
native_exec_time += (clock() - native_exec_start); |
871 |
+ |
#endif |
872 |
|
} |
873 |
|
|
874 |
|
/* |
961 |
|
} |
962 |
|
|
963 |
|
/* |
905 |
– |
* Atomic operations |
906 |
– |
*/ |
907 |
– |
|
908 |
– |
int atomic_add(int *var, int v) |
909 |
– |
{ |
910 |
– |
int ret = *var; |
911 |
– |
*var += v; |
912 |
– |
return ret; |
913 |
– |
} |
914 |
– |
|
915 |
– |
int atomic_and(int *var, int v) |
916 |
– |
{ |
917 |
– |
int ret = *var; |
918 |
– |
*var &= v; |
919 |
– |
return ret; |
920 |
– |
} |
921 |
– |
|
922 |
– |
int atomic_or(int *var, int v) |
923 |
– |
{ |
924 |
– |
int ret = *var; |
925 |
– |
*var |= v; |
926 |
– |
return ret; |
927 |
– |
} |
928 |
– |
|
929 |
– |
/* |
964 |
|
* Resource Manager thunks |
965 |
|
*/ |
966 |
|
|