ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.53
Committed: 2004-11-22T22:04:38Z (19 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.52: +10 -6 lines
Log Message:
Use BUILD_SHEEPSHAVER_PROCEDURE to allocate static procedures into the
SheepShaver globals. Fix build of sheepshaver_glue.cpp without JIT.

File Contents

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