/[cebix]/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
ViewVC logotype

Annotation of /SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.55 - (hide annotations)
Sat Dec 18 18:40:04 2004 UTC (10 years ago) by gbeauche
Branch: MAIN
Changes since 1.54: +0 -7 lines
ethernet seems to work with sheepnet, even on kernel 2.6/x86_64!

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

Christian Bauer">Christian Bauer
ViewVC Help
Powered by ViewVC 1.1.17