ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.42
Committed: 2004-05-23T05:28:12Z (20 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.41: +20 -13 lines
Log Message:
Fix NativeOp code generation, especially in PPC_REENTRANT_JIT mode

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