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 |
|
|
46 |
|
#include "mon_disass.h" |
47 |
|
#endif |
48 |
|
|
49 |
< |
#define DEBUG 1 |
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 |
+ |
// PowerPC EmulOp to exit from emulation looop |
80 |
+ |
const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1; |
81 |
+ |
|
82 |
|
// Enable multicore (main/interrupts) cpu emulation? |
83 |
< |
#define MULTICORE_CPU 0 |
83 |
> |
#define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0) |
84 |
|
|
85 |
|
// Enable Execute68k() safety checks? |
86 |
|
#define SAFE_EXEC_68K 1 |
97 |
|
// Pointer to Kernel Data |
98 |
|
static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE; |
99 |
|
|
100 |
+ |
// SIGSEGV handler |
101 |
+ |
static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
102 |
+ |
|
103 |
+ |
// JIT Compiler enabled? |
104 |
+ |
static inline bool enable_jit_p() |
105 |
+ |
{ |
106 |
+ |
return PrefsFindBool("jit"); |
107 |
+ |
} |
108 |
+ |
|
109 |
|
|
110 |
|
/** |
111 |
|
* PowerPC emulator glue with special 'sheep' opcodes |
112 |
|
**/ |
113 |
|
|
114 |
< |
struct sheepshaver_exec_return { }; |
114 |
> |
enum { |
115 |
> |
PPC_I(SHEEP) = PPC_I(MAX), |
116 |
> |
PPC_I(SHEEP_MAX) |
117 |
> |
}; |
118 |
|
|
119 |
|
class sheepshaver_cpu |
120 |
|
: public powerpc_cpu |
124 |
|
|
125 |
|
public: |
126 |
|
|
127 |
< |
sheepshaver_cpu() |
128 |
< |
: powerpc_cpu() |
94 |
< |
{ init_decoder(); } |
127 |
> |
// Constructor |
128 |
> |
sheepshaver_cpu(); |
129 |
|
|
130 |
|
// Condition Register accessors |
131 |
|
uint32 get_cr() const { return cr().get(); } |
132 |
|
void set_cr(uint32 v) { cr().set(v); } |
133 |
|
|
100 |
– |
// Execution loop |
101 |
– |
void execute(uint32 pc); |
102 |
– |
|
134 |
|
// Execute 68k routine |
135 |
|
void execute_68k(uint32 entry, M68kRegisters *r); |
136 |
|
|
145 |
|
|
146 |
|
// Handle MacOS interrupt |
147 |
|
void interrupt(uint32 entry); |
148 |
< |
|
118 |
< |
// spcflags for interrupts handling |
119 |
< |
static uint32 spcflags; |
148 |
> |
void handle_interrupt(); |
149 |
|
|
150 |
|
// Lazy memory allocator (one item at a time) |
151 |
|
void *operator new(size_t size) |
155 |
|
// FIXME: really make surre array allocation fail at link time? |
156 |
|
void *operator new[](size_t); |
157 |
|
void operator delete[](void *p); |
158 |
+ |
|
159 |
+ |
// Make sure the SIGSEGV handler can access CPU registers |
160 |
+ |
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t); |
161 |
|
}; |
162 |
|
|
131 |
– |
uint32 sheepshaver_cpu::spcflags = 0; |
163 |
|
lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator; |
164 |
|
|
165 |
+ |
sheepshaver_cpu::sheepshaver_cpu() |
166 |
+ |
: powerpc_cpu(enable_jit_p()) |
167 |
+ |
{ |
168 |
+ |
init_decoder(); |
169 |
+ |
} |
170 |
+ |
|
171 |
|
void sheepshaver_cpu::init_decoder() |
172 |
|
{ |
173 |
|
#ifndef PPC_NO_STATIC_II_INDEX_TABLE |
179 |
|
|
180 |
|
static const instr_info_t sheep_ii_table[] = { |
181 |
|
{ "sheep", |
182 |
< |
(execute_fn)&sheepshaver_cpu::execute_sheep, |
182 |
> |
(execute_pmf)&sheepshaver_cpu::execute_sheep, |
183 |
|
NULL, |
184 |
+ |
PPC_I(SHEEP), |
185 |
|
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP |
186 |
|
} |
187 |
|
}; |
219 |
|
case 0: // EMUL_RETURN |
220 |
|
QuitEmulator(); |
221 |
|
break; |
222 |
< |
|
222 |
> |
|
223 |
|
case 1: // EXEC_RETURN |
224 |
< |
throw sheepshaver_exec_return(); |
224 |
> |
spcflags().set(SPCFLAG_CPU_EXEC_RETURN); |
225 |
|
break; |
226 |
|
|
227 |
|
case 2: // EXEC_NATIVE |
254 |
|
} |
255 |
|
} |
256 |
|
|
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 |
– |
|
245 |
– |
// Execution loop |
246 |
– |
void sheepshaver_cpu::execute(uint32 entry) |
247 |
– |
{ |
248 |
– |
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 |
– |
} |
259 |
– |
} |
260 |
– |
|
257 |
|
// Handle MacOS interrupt |
258 |
|
void sheepshaver_cpu::interrupt(uint32 entry) |
259 |
|
{ |
260 |
+ |
#if EMUL_TIME_STATS |
261 |
+ |
interrupt_count++; |
262 |
+ |
const clock_t interrupt_start = clock(); |
263 |
+ |
#endif |
264 |
+ |
|
265 |
|
#if !MULTICORE_CPU |
266 |
|
// Save program counters and branch registers |
267 |
|
uint32 saved_pc = pc(); |
271 |
|
#endif |
272 |
|
|
273 |
|
// Initialize stack pointer to SheepShaver alternate stack base |
274 |
< |
gpr(1) = SheepStack1Base - 64; |
274 |
> |
gpr(1) = SignalStackBase() - 64; |
275 |
|
|
276 |
|
// Build trampoline to return from interrupt |
277 |
< |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
277 |
> |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
278 |
|
|
279 |
|
// Prepare registers for nanokernel interrupt routine |
280 |
|
kernel_data->v[0x004 >> 2] = htonl(gpr(1)); |
293 |
|
gpr(1) = KernelDataAddr; |
294 |
|
gpr(7) = ntohl(kernel_data->v[0x660 >> 2]); |
295 |
|
gpr(8) = 0; |
296 |
< |
gpr(10) = (uint32)trampoline; |
297 |
< |
gpr(12) = (uint32)trampoline; |
298 |
< |
gpr(13) = cr().get(); |
296 |
> |
gpr(10) = trampoline.addr(); |
297 |
> |
gpr(12) = trampoline.addr(); |
298 |
> |
gpr(13) = get_cr(); |
299 |
|
|
300 |
|
// rlwimi. r7,r7,8,0,0 |
301 |
|
uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7)); |
303 |
|
gpr(7) = result; |
304 |
|
|
305 |
|
gpr(11) = 0xf072; // MSR (SRR1) |
306 |
< |
cr().set((gpr(11) & 0x0fff0000) | (cr().get() & ~0x0fff0000)); |
306 |
> |
cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000)); |
307 |
|
|
308 |
|
// Enter nanokernel |
309 |
|
execute(entry); |
315 |
|
ctr()= saved_ctr; |
316 |
|
gpr(1) = saved_sp; |
317 |
|
#endif |
318 |
+ |
|
319 |
+ |
#if EMUL_TIME_STATS |
320 |
+ |
interrupt_time += (clock() - interrupt_start); |
321 |
+ |
#endif |
322 |
|
} |
323 |
|
|
324 |
|
// Execute 68k routine |
325 |
|
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r) |
326 |
|
{ |
327 |
+ |
#if EMUL_TIME_STATS |
328 |
+ |
exec68k_count++; |
329 |
+ |
const clock_t exec68k_start = clock(); |
330 |
+ |
#endif |
331 |
+ |
|
332 |
|
#if SAFE_EXEC_68K |
333 |
|
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP) |
334 |
|
printf("FATAL: Execute68k() not called from EMUL_OP mode\n"); |
338 |
|
uint32 saved_pc = pc(); |
339 |
|
uint32 saved_lr = lr(); |
340 |
|
uint32 saved_ctr= ctr(); |
341 |
+ |
uint32 saved_cr = get_cr(); |
342 |
|
|
343 |
|
// Create MacOS stack frame |
344 |
|
// FIXME: make sure MacOS doesn't expect PPC registers to live on top |
410 |
|
pc() = saved_pc; |
411 |
|
lr() = saved_lr; |
412 |
|
ctr()= saved_ctr; |
413 |
+ |
set_cr(saved_cr); |
414 |
+ |
|
415 |
+ |
#if EMUL_TIME_STATS |
416 |
+ |
exec68k_time += (clock() - exec68k_start); |
417 |
+ |
#endif |
418 |
|
} |
419 |
|
|
420 |
|
// Call MacOS PPC code |
421 |
|
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args) |
422 |
|
{ |
423 |
+ |
#if EMUL_TIME_STATS |
424 |
+ |
macos_exec_count++; |
425 |
+ |
const clock_t macos_exec_start = clock(); |
426 |
+ |
#endif |
427 |
+ |
|
428 |
|
// Save program counters and branch registers |
429 |
|
uint32 saved_pc = pc(); |
430 |
|
uint32 saved_lr = lr(); |
431 |
|
uint32 saved_ctr= ctr(); |
432 |
|
|
433 |
|
// Build trampoline with EXEC_RETURN |
434 |
< |
uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
435 |
< |
lr() = (uint32)trampoline; |
434 |
> |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
435 |
> |
lr() = trampoline.addr(); |
436 |
|
|
437 |
|
gpr(1) -= 64; // Create stack frame |
438 |
|
uint32 proc = ReadMacInt32(tvect); // Get routine address |
463 |
|
lr() = saved_lr; |
464 |
|
ctr()= saved_ctr; |
465 |
|
|
466 |
+ |
#if EMUL_TIME_STATS |
467 |
+ |
macos_exec_time += (clock() - macos_exec_start); |
468 |
+ |
#endif |
469 |
+ |
|
470 |
|
return retval; |
471 |
|
} |
472 |
|
|
476 |
|
// Save branch registers |
477 |
|
uint32 saved_lr = lr(); |
478 |
|
|
479 |
< |
const uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) }; |
480 |
< |
lr() = (uint32)trampoline; |
479 |
> |
SheepVar32 trampoline = POWERPC_EXEC_RETURN; |
480 |
> |
WriteMacInt32(trampoline.addr(), POWERPC_EXEC_RETURN); |
481 |
> |
lr() = trampoline.addr(); |
482 |
|
|
483 |
|
execute(entry); |
484 |
|
|
572 |
|
if ((addr - ROM_BASE) < ROM_SIZE) |
573 |
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
574 |
|
|
575 |
< |
// Ignore all other faults, if requested |
576 |
< |
if (PrefsFindBool("ignoresegv")) |
577 |
< |
return SIGSEGV_RETURN_FAILURE; |
575 |
> |
// Get program counter of target CPU |
576 |
> |
sheepshaver_cpu * const cpu = current_cpu; |
577 |
> |
const uint32 pc = cpu->pc(); |
578 |
> |
|
579 |
> |
// Fault in Mac ROM or RAM? |
580 |
> |
bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize)); |
581 |
> |
if (mac_fault) { |
582 |
> |
|
583 |
> |
// "VM settings" during MacOS 8 installation |
584 |
> |
if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000) |
585 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
586 |
> |
|
587 |
> |
// MacOS 8.5 installation |
588 |
> |
else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000) |
589 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
590 |
> |
|
591 |
> |
// MacOS 8 serial drivers on startup |
592 |
> |
else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000)) |
593 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
594 |
> |
|
595 |
> |
// MacOS 8.1 serial drivers on startup |
596 |
> |
else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
597 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
598 |
> |
else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000)) |
599 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
600 |
> |
|
601 |
> |
// Ignore all other faults, if requested |
602 |
> |
if (PrefsFindBool("ignoresegv")) |
603 |
> |
return SIGSEGV_RETURN_SKIP_INSTRUCTION; |
604 |
> |
} |
605 |
|
#else |
606 |
|
#error "FIXME: You don't have the capability to skip instruction within signal handlers" |
607 |
|
#endif |
638 |
|
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n"); |
639 |
|
mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n"); |
640 |
|
#endif |
641 |
+ |
|
642 |
+ |
#if EMUL_TIME_STATS |
643 |
+ |
emul_start_time = clock(); |
644 |
+ |
#endif |
645 |
+ |
} |
646 |
+ |
|
647 |
+ |
/* |
648 |
+ |
* Deinitialize emulation |
649 |
+ |
*/ |
650 |
+ |
|
651 |
+ |
void exit_emul_ppc(void) |
652 |
+ |
{ |
653 |
+ |
#if EMUL_TIME_STATS |
654 |
+ |
clock_t emul_end_time = clock(); |
655 |
+ |
|
656 |
+ |
printf("### Statistics for SheepShaver emulation parts\n"); |
657 |
+ |
const clock_t emul_time = emul_end_time - emul_start_time; |
658 |
+ |
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC)); |
659 |
+ |
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count, |
660 |
+ |
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time)); |
661 |
+ |
|
662 |
+ |
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \ |
663 |
+ |
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \ |
664 |
+ |
printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \ |
665 |
+ |
double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \ |
666 |
+ |
100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \ |
667 |
+ |
} while (0) |
668 |
+ |
|
669 |
+ |
PRINT_STATS("Execute68k[Trap] execution", exec68k); |
670 |
+ |
PRINT_STATS("NativeOp execution", native_exec); |
671 |
+ |
PRINT_STATS("MacOS routine execution", macos_exec); |
672 |
+ |
|
673 |
+ |
#undef PRINT_STATS |
674 |
+ |
printf("\n"); |
675 |
+ |
#endif |
676 |
+ |
|
677 |
+ |
delete main_cpu; |
678 |
+ |
#if MULTICORE_CPU |
679 |
+ |
delete interrupt_cpu; |
680 |
+ |
#endif |
681 |
|
} |
682 |
|
|
683 |
|
/* |
687 |
|
void emul_ppc(uint32 entry) |
688 |
|
{ |
689 |
|
current_cpu = main_cpu; |
690 |
+ |
#if DEBUG |
691 |
|
current_cpu->start_log(); |
692 |
+ |
#endif |
693 |
+ |
// start emulation loop and enable code translation or caching |
694 |
|
current_cpu->execute(entry); |
695 |
|
} |
696 |
|
|
698 |
|
* Handle PowerPC interrupt |
699 |
|
*/ |
700 |
|
|
701 |
< |
// Atomic operations |
702 |
< |
extern int atomic_add(int *var, int v); |
703 |
< |
extern int atomic_and(int *var, int v); |
704 |
< |
extern int atomic_or(int *var, int v); |
705 |
< |
|
706 |
< |
#if !ASYNC_IRQ |
701 |
> |
#if ASYNC_IRQ |
702 |
> |
void HandleInterrupt(void) |
703 |
> |
{ |
704 |
> |
main_cpu->handle_interrupt(); |
705 |
> |
} |
706 |
> |
#else |
707 |
|
void TriggerInterrupt(void) |
708 |
|
{ |
709 |
|
#if 0 |
710 |
|
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1); |
711 |
|
#else |
712 |
< |
SPCFLAGS_SET( SPCFLAG_INT ); |
712 |
> |
// Trigger interrupt to main cpu only |
713 |
> |
if (main_cpu) |
714 |
> |
main_cpu->trigger_interrupt(); |
715 |
|
#endif |
716 |
|
} |
717 |
|
#endif |
718 |
|
|
719 |
< |
void HandleInterrupt(void) |
719 |
> |
void sheepshaver_cpu::handle_interrupt(void) |
720 |
|
{ |
721 |
|
// Do nothing if interrupts are disabled |
722 |
< |
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0) |
722 |
> |
if (*(int32 *)XLM_IRQ_NEST > 0) |
723 |
|
return; |
724 |
|
|
725 |
|
// Do nothing if there is no interrupt pending |
735 |
|
// 68k emulator active, trigger 68k interrupt level 1 |
736 |
|
assert(current_cpu == main_cpu); |
737 |
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
738 |
< |
main_cpu->set_cr(main_cpu->get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
738 |
> |
set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2])); |
739 |
|
break; |
740 |
|
|
741 |
|
#if INTERRUPTS_IN_NATIVE_MODE |
742 |
|
case MODE_NATIVE: |
743 |
|
// 68k emulator inactive, in nanokernel? |
744 |
|
assert(current_cpu == main_cpu); |
745 |
< |
if (main_cpu->gpr(1) != KernelDataAddr) { |
745 |
> |
if (gpr(1) != KernelDataAddr) { |
746 |
|
// Prepare for 68k interrupt level 1 |
747 |
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1); |
748 |
|
WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc, |
786 |
|
if (InterruptFlags & INTFLAG_VIA) { |
787 |
|
ClearInterruptFlag(INTFLAG_VIA); |
788 |
|
ADBInterrupt(); |
789 |
< |
ExecutePPC(VideoVBL); |
789 |
> |
ExecuteNative(NATIVE_VIDEO_VBL); |
790 |
|
} |
791 |
|
} |
792 |
|
#endif |
796 |
|
} |
797 |
|
} |
798 |
|
|
701 |
– |
/* |
702 |
– |
* Execute NATIVE_OP opcode (called by PowerPC emulator) |
703 |
– |
*/ |
704 |
– |
|
705 |
– |
#define POWERPC_NATIVE_OP_INIT(LR, OP) \ |
706 |
– |
tswap32(POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2) |
707 |
– |
|
708 |
– |
// FIXME: Make sure 32-bit relocations are used |
709 |
– |
const uint32 NativeOpTable[NATIVE_OP_MAX] = { |
710 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_PATCH_NAME_REGISTRY), |
711 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_INSTALL_ACCEL), |
712 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_VBL), |
713 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_DO_DRIVER_IO), |
714 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_IRQ), |
715 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_INIT), |
716 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_TERM), |
717 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_OPEN), |
718 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_CLOSE), |
719 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_WPUT), |
720 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_RSRV), |
721 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_NOTHING), |
722 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_OPEN), |
723 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_IN), |
724 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_OUT), |
725 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CONTROL), |
726 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_STATUS), |
727 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CLOSE), |
728 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_RESOURCE), |
729 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_RESOURCE), |
730 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_IND_RESOURCE), |
731 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_IND_RESOURCE), |
732 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_R_GET_RESOURCE), |
733 |
– |
POWERPC_NATIVE_OP_INIT(0, NATIVE_DISABLE_INTERRUPT), |
734 |
– |
POWERPC_NATIVE_OP_INIT(0, NATIVE_ENABLE_INTERRUPT), |
735 |
– |
POWERPC_NATIVE_OP_INIT(1, NATIVE_MAKE_EXECUTABLE), |
736 |
– |
}; |
737 |
– |
|
799 |
|
static void get_resource(void); |
800 |
|
static void get_1_resource(void); |
801 |
|
static void get_ind_resource(void); |
806 |
|
|
807 |
|
static void NativeOp(int selector) |
808 |
|
{ |
809 |
+ |
#if EMUL_TIME_STATS |
810 |
+ |
native_exec_count++; |
811 |
+ |
const clock_t native_exec_start = clock(); |
812 |
+ |
#endif |
813 |
+ |
|
814 |
|
switch (selector) { |
815 |
|
case NATIVE_PATCH_NAME_REGISTRY: |
816 |
|
DoPatchNameRegistry(); |
825 |
|
GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4), |
826 |
|
(void *)GPR(5), GPR(6), GPR(7)); |
827 |
|
break; |
828 |
< |
case NATIVE_GET_RESOURCE: |
829 |
< |
get_resource(); |
828 |
> |
#ifdef WORDS_BIGENDIAN |
829 |
> |
case NATIVE_ETHER_IRQ: |
830 |
> |
EtherIRQ(); |
831 |
|
break; |
832 |
< |
case NATIVE_GET_1_RESOURCE: |
833 |
< |
get_1_resource(); |
832 |
> |
case NATIVE_ETHER_INIT: |
833 |
> |
GPR(3) = InitStreamModule((void *)GPR(3)); |
834 |
|
break; |
835 |
< |
case NATIVE_GET_IND_RESOURCE: |
836 |
< |
get_ind_resource(); |
835 |
> |
case NATIVE_ETHER_TERM: |
836 |
> |
TerminateStreamModule(); |
837 |
|
break; |
838 |
< |
case NATIVE_GET_1_IND_RESOURCE: |
839 |
< |
get_1_ind_resource(); |
838 |
> |
case NATIVE_ETHER_OPEN: |
839 |
> |
GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7)); |
840 |
> |
break; |
841 |
> |
case NATIVE_ETHER_CLOSE: |
842 |
> |
GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5)); |
843 |
|
break; |
844 |
< |
case NATIVE_R_GET_RESOURCE: |
845 |
< |
r_get_resource(); |
844 |
> |
case NATIVE_ETHER_WPUT: |
845 |
> |
GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4)); |
846 |
|
break; |
847 |
+ |
case NATIVE_ETHER_RSRV: |
848 |
+ |
GPR(3) = ether_rsrv((queue_t *)GPR(3)); |
849 |
+ |
break; |
850 |
+ |
#else |
851 |
+ |
case NATIVE_ETHER_INIT: |
852 |
+ |
// FIXME: needs more complicated thunks |
853 |
+ |
GPR(3) = false; |
854 |
+ |
break; |
855 |
+ |
#endif |
856 |
|
case NATIVE_SERIAL_NOTHING: |
857 |
|
case NATIVE_SERIAL_OPEN: |
858 |
|
case NATIVE_SERIAL_PRIME_IN: |
873 |
|
GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4)); |
874 |
|
break; |
875 |
|
} |
876 |
+ |
case NATIVE_GET_RESOURCE: |
877 |
+ |
case NATIVE_GET_1_RESOURCE: |
878 |
+ |
case NATIVE_GET_IND_RESOURCE: |
879 |
+ |
case NATIVE_GET_1_IND_RESOURCE: |
880 |
+ |
case NATIVE_R_GET_RESOURCE: { |
881 |
+ |
typedef void (*GetResourceCallback)(void); |
882 |
+ |
static const GetResourceCallback get_resource_callbacks[] = { |
883 |
+ |
get_resource, |
884 |
+ |
get_1_resource, |
885 |
+ |
get_ind_resource, |
886 |
+ |
get_1_ind_resource, |
887 |
+ |
r_get_resource |
888 |
+ |
}; |
889 |
+ |
get_resource_callbacks[selector - NATIVE_GET_RESOURCE](); |
890 |
+ |
break; |
891 |
+ |
} |
892 |
|
case NATIVE_DISABLE_INTERRUPT: |
893 |
|
DisableInterrupt(); |
894 |
|
break; |
903 |
|
QuitEmulator(); |
904 |
|
break; |
905 |
|
} |
811 |
– |
} |
812 |
– |
|
813 |
– |
/* |
814 |
– |
* Execute native subroutine (LR must contain return address) |
815 |
– |
*/ |
906 |
|
|
907 |
< |
void ExecuteNative(int selector) |
908 |
< |
{ |
909 |
< |
uint32 tvect[2]; |
820 |
< |
tvect[0] = tswap32(POWERPC_NATIVE_OP_FUNC(selector)); |
821 |
< |
tvect[1] = 0; // Fake TVECT |
822 |
< |
RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect); |
823 |
< |
M68kRegisters r; |
824 |
< |
Execute68k((uint32)&desc, &r); |
907 |
> |
#if EMUL_TIME_STATS |
908 |
> |
native_exec_time += (clock() - native_exec_start); |
909 |
> |
#endif |
910 |
|
} |
911 |
|
|
912 |
|
/* |
927 |
|
|
928 |
|
void Execute68kTrap(uint16 trap, M68kRegisters *r) |
929 |
|
{ |
930 |
< |
uint16 proc[2]; |
931 |
< |
proc[0] = htons(trap); |
932 |
< |
proc[1] = htons(M68K_RTS); |
933 |
< |
Execute68k((uint32)proc, r); |
930 |
> |
SheepVar proc_var(4); |
931 |
> |
uint32 proc = proc_var.addr(); |
932 |
> |
WriteMacInt16(proc, trap); |
933 |
> |
WriteMacInt16(proc + 2, M68K_RTS); |
934 |
> |
Execute68k(proc, r); |
935 |
|
} |
936 |
|
|
937 |
|
/* |
986 |
|
} |
987 |
|
|
988 |
|
/* |
903 |
– |
* Atomic operations |
904 |
– |
*/ |
905 |
– |
|
906 |
– |
int atomic_add(int *var, int v) |
907 |
– |
{ |
908 |
– |
int ret = *var; |
909 |
– |
*var += v; |
910 |
– |
return ret; |
911 |
– |
} |
912 |
– |
|
913 |
– |
int atomic_and(int *var, int v) |
914 |
– |
{ |
915 |
– |
int ret = *var; |
916 |
– |
*var &= v; |
917 |
– |
return ret; |
918 |
– |
} |
919 |
– |
|
920 |
– |
int atomic_or(int *var, int v) |
921 |
– |
{ |
922 |
– |
int ret = *var; |
923 |
– |
*var |= v; |
924 |
– |
return ret; |
925 |
– |
} |
926 |
– |
|
927 |
– |
/* |
989 |
|
* Resource Manager thunks |
990 |
|
*/ |
991 |
|
|