ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.37
Committed: 2004-05-15T17:26:28Z (20 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.36: +83 -1 lines
Log Message:
Don't take an EMUL_OP mode switch for Microseconds() and SynchIdleTime()

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