ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.1
Committed: 2003-09-07T14:25:01Z (21 years ago) by gbeauche
Branch: MAIN
Log Message:
Merge in old kpx_cpu snapshot for debugging

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * sheepshaver_glue.cpp - Glue Kheperix CPU to SheepShaver CPU engine interface
3     *
4     * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
5     *
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     #include "xlowmem.h"
25     #include "emul_op.h"
26     #include "rom_patches.h"
27     #include "macos_util.h"
28     #include "block-alloc.hpp"
29     #include "sigsegv.h"
30     #include "cpu/ppc/ppc-cpu.hpp"
31     #include "cpu/ppc/ppc-operations.hpp"
32    
33     // Used for NativeOp trampolines
34     #include "video.h"
35     #include "name_registry.h"
36     #include "serial.h"
37    
38     #include <stdio.h>
39    
40     #if ENABLE_MON
41     #include "mon.h"
42     #include "mon_disass.h"
43     #endif
44    
45     #define DEBUG 1
46     #include "debug.h"
47    
48     static void enter_mon(void)
49     {
50     // Start up mon in real-mode
51     #if ENABLE_MON
52     char *arg[4] = {"mon", "-m", "-r", NULL};
53     mon(3, arg);
54     #endif
55     }
56    
57     // Enable Execute68k() safety checks?
58     #define SAFE_EXEC_68K 1
59    
60     // Save FP state in Execute68k()?
61     #define SAVE_FP_EXEC_68K 1
62    
63     // Interrupts in EMUL_OP mode?
64     #define INTERRUPTS_IN_EMUL_OP_MODE 1
65    
66     // Interrupts in native mode?
67     #define INTERRUPTS_IN_NATIVE_MODE 1
68    
69     // 68k Emulator Data
70     struct EmulatorData {
71     uint32 v[0x400];
72     };
73    
74     // Kernel Data
75     struct KernelData {
76     uint32 v[0x400];
77     EmulatorData ed;
78     };
79    
80     // Pointer to Kernel Data
81     static KernelData * const kernel_data = (KernelData *)0x68ffe000;
82    
83    
84     /**
85     * PowerPC emulator glue with special 'sheep' opcodes
86     **/
87    
88     struct sheepshaver_exec_return { };
89    
90     class sheepshaver_cpu
91     : public powerpc_cpu
92     {
93     void init_decoder();
94     void execute_sheep(uint32 opcode);
95    
96     public:
97    
98     sheepshaver_cpu()
99     : powerpc_cpu()
100     { init_decoder(); }
101    
102     // Stack pointer accessors
103     uint32 get_sp() const { return gpr(1); }
104     void set_sp(uint32 v) { gpr(1) = v; }
105    
106     // Condition Register accessors
107     uint32 get_cr() const { return cr().get(); }
108     void set_cr(uint32 v) { cr().set(v); }
109    
110     // Execution loop
111     void execute(uint32 pc);
112    
113     // Execute 68k routine
114     void execute_68k(uint32 entry, M68kRegisters *r);
115    
116     // Execute MacOS/PPC code
117     uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args);
118    
119     // Resource manager thunk
120     void get_resource(uint32 old_get_resource);
121    
122     // Handle MacOS interrupt
123     void interrupt(uint32 entry, uint32 sp);
124    
125     // Lazy memory allocator (one item at a time)
126     void *operator new(size_t size)
127     { return allocator_helper< sheepshaver_cpu, lazy_allocator >::allocate(); }
128     void operator delete(void *p)
129     { allocator_helper< sheepshaver_cpu, lazy_allocator >::deallocate(p); }
130     // FIXME: really make surre array allocation fail at link time?
131     void *operator new[](size_t);
132     void operator delete[](void *p);
133     };
134    
135     lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator;
136    
137     void sheepshaver_cpu::init_decoder()
138     {
139     #ifndef PPC_NO_STATIC_II_INDEX_TABLE
140     static bool initialized = false;
141     if (initialized)
142     return;
143     initialized = true;
144     #endif
145    
146     static const instr_info_t sheep_ii_table[] = {
147     { "sheep",
148     (execute_fn)&sheepshaver_cpu::execute_sheep,
149     NULL,
150     D_form, 6, 0, CFLOW_TRAP
151     }
152     };
153    
154     const int ii_count = sizeof(sheep_ii_table)/sizeof(sheep_ii_table[0]);
155     D(bug("SheepShaver extra decode table has %d entries\n", ii_count));
156    
157     for (int i = 0; i < ii_count; i++) {
158     const instr_info_t * ii = &sheep_ii_table[i];
159     init_decoder_entry(ii);
160     }
161     }
162    
163     // Forward declaration for native opcode handler
164     static void NativeOp(int selector);
165    
166     // Execute SheepShaver instruction
167     void sheepshaver_cpu::execute_sheep(uint32 opcode)
168     {
169     // D(bug("Extended opcode %08x at %08x (68k pc %08x)\n", opcode, pc(), gpr(24)));
170     assert((((opcode >> 26) & 0x3f) == 6) && OP_MAX <= 64 + 3);
171    
172     switch (opcode & 0x3f) {
173     case 0: // EMUL_RETURN
174     QuitEmulator();
175     break;
176    
177     case 1: // EXEC_RETURN
178     throw sheepshaver_exec_return();
179     break;
180    
181     case 2: // EXEC_NATIVE
182     NativeOp((opcode >> 6) & 0x1f);
183     pc() = lr();
184     break;
185    
186     default: { // EMUL_OP
187     M68kRegisters r68;
188     WriteMacInt32(XLM_68K_R25, gpr(25));
189     WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
190     for (int i = 0; i < 8; i++)
191     r68.d[i] = gpr(8 + i);
192     for (int i = 0; i < 7; i++)
193     r68.a[i] = gpr(16 + i);
194     r68.a[7] = gpr(1);
195     EmulOp(&r68, gpr(24), (opcode & 0x3f) - 3);
196     for (int i = 0; i < 8; i++)
197     gpr(8 + i) = r68.d[i];
198     for (int i = 0; i < 7; i++)
199     gpr(16 + i) = r68.a[i];
200     gpr(1) = r68.a[7];
201     WriteMacInt32(XLM_RUN_MODE, MODE_68K);
202     pc() += 4;
203     break;
204     }
205     }
206     }
207    
208     // Execution loop
209     void sheepshaver_cpu::execute(uint32 entry)
210     {
211     try {
212     pc() = entry;
213     powerpc_cpu::execute();
214     }
215     catch (sheepshaver_exec_return const &) {
216     // Nothing, simply return
217     }
218     catch (...) {
219     printf("ERROR: execute() received an unknown exception!\n");
220     QuitEmulator();
221     }
222     }
223    
224     // Handle MacOS interrupt
225     void sheepshaver_cpu::interrupt(uint32 entry, uint32 sp)
226     {
227     // Create stack frame
228     gpr(1) = sp - 64;
229    
230     // Build trampoline to return from interrupt
231     uint32 trampoline[] = { POWERPC_EMUL_OP | 1 };
232    
233     // Prepare registers for nanokernel interrupt routine
234     kernel_data->v[0x004 >> 2] = gpr(1);
235     kernel_data->v[0x018 >> 2] = gpr(6);
236    
237     gpr(6) = kernel_data->v[0x65c >> 2];
238     WriteMacInt32(gpr(6) + 0x13c, gpr(7));
239     WriteMacInt32(gpr(6) + 0x144, gpr(8));
240     WriteMacInt32(gpr(6) + 0x14c, gpr(9));
241     WriteMacInt32(gpr(6) + 0x154, gpr(10));
242     WriteMacInt32(gpr(6) + 0x15c, gpr(11));
243     WriteMacInt32(gpr(6) + 0x164, gpr(12));
244     WriteMacInt32(gpr(6) + 0x16c, gpr(13));
245    
246     gpr(1) = KernelDataAddr;
247     gpr(7) = kernel_data->v[0x660 >> 2];
248     gpr(8) = 0;
249     gpr(10) = (uint32)trampoline;
250     gpr(12) = (uint32)trampoline;
251     gpr(13) = cr().get();
252    
253     // rlwimi. r7,r7,8,0,0
254     uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7));
255     record_cr0(result);
256     gpr(7) = result;
257    
258     gpr(11) = 0xf072; // MSR (SRR1)
259     cr().set((gpr(11) & 0x0fff0000) | (cr().get() & ~0x0fff0000));
260    
261     // Enter nanokernel
262     execute(entry);
263    
264     // Cleanup stack
265     gpr(1) += 64;
266     }
267    
268     // Execute 68k routine
269     void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r)
270     {
271     #if SAFE_EXEC_68K
272     if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
273     printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
274     #endif
275    
276     // Save program counters and branch registers
277     uint32 saved_pc = pc();
278     uint32 saved_lr = lr();
279     uint32 saved_ctr= ctr();
280    
281     // Create MacOS stack frame
282     uint32 sp = gpr(1);
283     gpr(1) -= 56 + 19*4 + 18*8;
284     WriteMacInt32(gpr(1), sp);
285    
286     // Save PowerPC registers
287     memcpy(Mac2HostAddr(gpr(1)+56), &gpr(13), sizeof(uint32)*(32-13));
288     #if SAVE_FP_EXEC_68K
289     memcpy(Mac2HostAddr(gpr(1)+56+19*4), &fpr(14), sizeof(double)*(32-14));
290     #endif
291    
292     // Setup registers for 68k emulator
293     cr().set(0);
294     cr().set(2, 1); // Supervisor mode
295     for (int i = 0; i < 8; i++) // d[0]..d[7]
296     gpr(8 + i) = r->d[i];
297     for (int i = 0; i < 7; i++) // a[0]..a[6]
298     gpr(16 + i) = r->a[i];
299     gpr(23) = 0;
300     gpr(24) = entry;
301     gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR
302     gpr(26) = 0;
303     gpr(28) = 0; // VBR
304     gpr(29) = kernel_data->ed.v[0x74 >> 2]; // Pointer to opcode table
305     gpr(30) = kernel_data->ed.v[0x78 >> 2]; // Address of emulator
306     gpr(31) = KernelDataAddr + 0x1000;
307    
308     // Push return address (points to EXEC_RETURN opcode) on stack
309     gpr(1) -= 4;
310     WriteMacInt32(gpr(1), XLM_EXEC_RETURN_OPCODE);
311    
312     // Rentering 68k emulator
313     WriteMacInt32(XLM_RUN_MODE, MODE_68K);
314    
315     // Set r0 to 0 for 68k emulator
316     gpr(0) = 0;
317    
318     // Execute 68k opcode
319     uint32 opcode = ReadMacInt16(gpr(24));
320     gpr(27) = (int32)(int16)ReadMacInt16(gpr(24) += 2);
321     gpr(29) += opcode * 8;
322     execute(gpr(29));
323    
324     // Save r25 (contains current 68k interrupt level)
325     WriteMacInt32(XLM_68K_R25, gpr(25));
326    
327     // Reentering EMUL_OP mode
328     WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
329    
330     // Save 68k registers
331     for (int i = 0; i < 8; i++) // d[0]..d[7]
332     r->d[i] = gpr(8 + i);
333     for (int i = 0; i < 7; i++) // a[0]..a[6]
334     r->a[i] = gpr(16 + i);
335    
336     // Restore PowerPC registers
337     memcpy(&gpr(13), Mac2HostAddr(gpr(1)+56), sizeof(uint32)*(32-13));
338     #if SAVE_FP_EXEC_68K
339     memcpy(&fpr(14), Mac2HostAddr(gpr(1)+56+19*4), sizeof(double)*(32-14));
340     #endif
341    
342     // Cleanup stack
343     gpr(1) += 56 + 19*4 + 18*8;
344    
345     // Restore program counters and branch registers
346     pc() = saved_pc;
347     lr() = saved_lr;
348     ctr()= saved_ctr;
349     }
350    
351     // Call MacOS PPC code
352     uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args)
353     {
354     // Save program counters and branch registers
355     uint32 saved_pc = pc();
356     uint32 saved_lr = lr();
357     uint32 saved_ctr= ctr();
358    
359     // Build trampoline with EXEC_RETURN
360     uint32 trampoline[] = { POWERPC_EMUL_OP | 1 };
361     lr() = (uint32)trampoline;
362    
363     gpr(1) -= 64; // Create stack frame
364     uint32 proc = ReadMacInt32(tvect); // Get routine address
365     uint32 toc = ReadMacInt32(tvect + 4); // Get TOC pointer
366    
367     // Save PowerPC registers
368     uint32 regs[8];
369     regs[0] = gpr(2);
370     for (int i = 0; i < nargs; i++)
371     regs[i + 1] = gpr(i + 3);
372    
373     // Prepare and call MacOS routine
374     gpr(2) = toc;
375     for (int i = 0; i < nargs; i++)
376     gpr(i + 3) = args[i];
377     execute(proc);
378     uint32 retval = gpr(3);
379    
380     // Restore PowerPC registers
381     for (int i = 0; i <= nargs; i++)
382     gpr(i + 2) = regs[i];
383    
384     // Cleanup stack
385     gpr(1) += 64;
386    
387     // Restore program counters and branch registers
388     pc() = saved_pc;
389     lr() = saved_lr;
390     ctr()= saved_ctr;
391    
392     return retval;
393     }
394    
395     // Resource Manager thunk
396     inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
397     {
398     printf("ERROR: get_resource() unimplemented\n");
399     QuitEmulator();
400     }
401    
402    
403     /**
404     * SheepShaver CPU engine interface
405     **/
406    
407     static sheepshaver_cpu *main_cpu = NULL; // CPU emulator to handle usual control flow
408     static sheepshaver_cpu *interrupt_cpu = NULL; // CPU emulator to handle interrupts
409     static sheepshaver_cpu *current_cpu = NULL; // Current CPU emulator context
410    
411     // Dump PPC registers
412     static void dump_registers(void)
413     {
414     current_cpu->dump_registers();
415     }
416    
417     // Dump log
418     static void dump_log(void)
419     {
420     current_cpu->dump_log();
421     }
422    
423     /*
424     * Initialize CPU emulation
425     */
426    
427     static struct sigaction sigsegv_action;
428    
429     #if defined(__powerpc__)
430     #include <sys/ucontext.h>
431     #endif
432    
433     static void sigsegv_handler(int sig, siginfo_t *sip, void *scp)
434     {
435     const uintptr addr = (uintptr)sip->si_addr;
436     #if ENABLE_VOSF
437     // Handle screen fault.
438     extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
439     if (Screen_fault_handler((sigsegv_address_t)addr, SIGSEGV_INVALID_PC))
440     return;
441     #endif
442     #if defined(__powerpc__)
443     if (addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) {
444     printf("IGNORE write access to ROM at %08x\n", addr);
445     (((ucontext_t *)scp)->uc_mcontext.regs)->nip += 4;
446     return;
447     }
448     if (addr >= 0xf3012000 && addr < 0xf3014000 && 0) {
449     printf("IGNORE write access to ROM at %08x\n", addr);
450     (((ucontext_t *)scp)->uc_mcontext.regs)->nip += 4;
451     return;
452     }
453     #endif
454     printf("Caught SIGSEGV at address %p\n", sip->si_addr);
455     printf("Native PC: %08x\n", (((ucontext_t *)scp)->uc_mcontext.regs)->nip);
456     printf("Current CPU: %s\n", current_cpu == main_cpu ? "main" : "interrupts");
457     #if 1
458     dump_registers();
459     #else
460     printf("Main CPU context\n");
461     main_cpu->dump_registers();
462     printf("Interrupts CPU context\n");
463     interrupt_cpu->dump_registers();
464     #endif
465     current_cpu->dump_log();
466     WriteMacInt32(XLM_IRQ_NEST, 1);
467     enter_mon();
468     QuitEmulator();
469     }
470    
471     void init_emul_ppc(void)
472     {
473     // Initialize main CPU emulator
474     main_cpu = new sheepshaver_cpu();
475     main_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
476     WriteMacInt32(XLM_RUN_MODE, MODE_68K);
477    
478     // Initialize alternate CPU emulator to handle interrupts
479     interrupt_cpu = new sheepshaver_cpu();
480    
481     // Install SIGSEGV handler
482     sigemptyset(&sigsegv_action.sa_mask);
483     sigsegv_action.sa_sigaction = sigsegv_handler;
484     sigsegv_action.sa_flags = SA_SIGINFO;
485     sigsegv_action.sa_restorer = NULL;
486     sigaction(SIGSEGV, &sigsegv_action, NULL);
487    
488     #if ENABLE_MON
489     // Install "regs" command in cxmon
490     mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n");
491     mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n");
492     #endif
493     }
494    
495     /*
496     * Emulation loop
497     */
498    
499     void emul_ppc(uint32 entry)
500     {
501     current_cpu = main_cpu;
502     current_cpu->start_log();
503     current_cpu->execute(entry);
504     }
505    
506     /*
507     * Handle PowerPC interrupt
508     */
509    
510     // Atomic operations
511     extern int atomic_add(int *var, int v);
512     extern int atomic_and(int *var, int v);
513     extern int atomic_or(int *var, int v);
514    
515     void HandleInterrupt(void)
516     {
517     // Do nothing if interrupts are disabled
518     if (ReadMacInt32(XLM_IRQ_NEST) > 0 || InterruptFlags == 0)
519     return;
520    
521     // Do nothing if CPU objects are not initialized yet
522     if (current_cpu == NULL)
523     return;
524    
525     // Disable MacOS stack sniffer
526     WriteMacInt32(0x110, 0);
527    
528     // Interrupt action depends on current run mode
529     switch (ReadMacInt32(XLM_RUN_MODE)) {
530     case MODE_68K:
531     // 68k emulator active, trigger 68k interrupt level 1
532     assert(current_cpu == main_cpu);
533     WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
534     main_cpu->set_cr(main_cpu->get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
535     break;
536    
537     #if INTERRUPTS_IN_NATIVE_MODE
538     case MODE_NATIVE:
539     // 68k emulator inactive, in nanokernel?
540     assert(current_cpu == main_cpu);
541     if (main_cpu->gpr(1) != KernelDataAddr) {
542     // Prepare for 68k interrupt level 1
543     WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
544     WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
545     ReadMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc)
546     | tswap32(kernel_data->v[0x674 >> 2]));
547    
548     // Execute nanokernel interrupt routine (this will activate the 68k emulator)
549     atomic_add((int32 *)XLM_IRQ_NEST, htonl(1));
550     current_cpu = interrupt_cpu;
551     if (ROMType == ROMTYPE_NEWWORLD)
552     current_cpu->interrupt(ROM_BASE + 0x312b1c, main_cpu->get_sp());
553     else
554     current_cpu->interrupt(ROM_BASE + 0x312a3c, main_cpu->get_sp());
555     current_cpu = main_cpu;
556     }
557     break;
558     #endif
559    
560     #if INTERRUPTS_IN_EMUL_OP_MODE
561     case MODE_EMUL_OP:
562     // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
563     if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
564     #if 1
565     // Execute full 68k interrupt routine
566     M68kRegisters r;
567     uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
568     WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
569     static const uint16 proc[] = {
570     0x3f3c, 0x0000, // move.w #$0000,-(sp) (fake format word)
571     0x487a, 0x000a, // pea @1(pc) (return address)
572     0x40e7, // move sr,-(sp) (saved SR)
573     0x2078, 0x0064, // move.l $64,a0
574     0x4ed0, // jmp (a0)
575     M68K_RTS // @1
576     };
577     Execute68k((uint32)proc, &r);
578     WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
579     #else
580     // Only update cursor
581     if (HasMacStarted()) {
582     if (InterruptFlags & INTFLAG_VIA) {
583     ClearInterruptFlag(INTFLAG_VIA);
584     ADBInterrupt();
585     ExecutePPC(VideoVBL);
586     }
587     }
588     #endif
589     }
590     break;
591     #endif
592     }
593     }
594    
595     /*
596     * Execute NATIVE_OP opcode (called by PowerPC emulator)
597     */
598    
599     #define POWERPC_NATIVE_OP(selector) \
600     { tswap32(POWERPC_EMUL_OP | 2 | (((uint32)selector) << 6)) }
601    
602     // FIXME: Make sure 32-bit relocations are used
603     const uint32 NativeOpTable[NATIVE_OP_MAX] = {
604     POWERPC_NATIVE_OP(NATIVE_PATCH_NAME_REGISTRY),
605     POWERPC_NATIVE_OP(NATIVE_VIDEO_INSTALL_ACCEL),
606     POWERPC_NATIVE_OP(NATIVE_VIDEO_VBL),
607     POWERPC_NATIVE_OP(NATIVE_VIDEO_DO_DRIVER_IO),
608     POWERPC_NATIVE_OP(NATIVE_ETHER_IRQ),
609     POWERPC_NATIVE_OP(NATIVE_ETHER_INIT),
610     POWERPC_NATIVE_OP(NATIVE_ETHER_TERM),
611     POWERPC_NATIVE_OP(NATIVE_ETHER_OPEN),
612     POWERPC_NATIVE_OP(NATIVE_ETHER_CLOSE),
613     POWERPC_NATIVE_OP(NATIVE_ETHER_WPUT),
614     POWERPC_NATIVE_OP(NATIVE_ETHER_RSRV),
615     POWERPC_NATIVE_OP(NATIVE_SERIAL_NOTHING),
616     POWERPC_NATIVE_OP(NATIVE_SERIAL_OPEN),
617     POWERPC_NATIVE_OP(NATIVE_SERIAL_PRIME_IN),
618     POWERPC_NATIVE_OP(NATIVE_SERIAL_PRIME_OUT),
619     POWERPC_NATIVE_OP(NATIVE_SERIAL_CONTROL),
620     POWERPC_NATIVE_OP(NATIVE_SERIAL_STATUS),
621     POWERPC_NATIVE_OP(NATIVE_SERIAL_CLOSE),
622     POWERPC_NATIVE_OP(NATIVE_GET_RESOURCE),
623     POWERPC_NATIVE_OP(NATIVE_GET_1_RESOURCE),
624     POWERPC_NATIVE_OP(NATIVE_GET_IND_RESOURCE),
625     POWERPC_NATIVE_OP(NATIVE_GET_1_IND_RESOURCE),
626     POWERPC_NATIVE_OP(NATIVE_R_GET_RESOURCE),
627     };
628    
629     static void get_resource(void);
630     static void get_1_resource(void);
631     static void get_ind_resource(void);
632     static void get_1_ind_resource(void);
633     static void r_get_resource(void);
634    
635     #define GPR(REG) current_cpu->gpr(REG)
636    
637     static void NativeOp(int selector)
638     {
639     switch (selector) {
640     case NATIVE_PATCH_NAME_REGISTRY:
641     DoPatchNameRegistry();
642     break;
643     case NATIVE_VIDEO_INSTALL_ACCEL:
644     VideoInstallAccel();
645     break;
646     case NATIVE_VIDEO_VBL:
647     VideoVBL();
648     break;
649     case NATIVE_VIDEO_DO_DRIVER_IO:
650     GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4),
651     (void *)GPR(5), GPR(6), GPR(7));
652     break;
653     case NATIVE_GET_RESOURCE:
654     get_resource();
655     break;
656     case NATIVE_GET_1_RESOURCE:
657     get_1_resource();
658     break;
659     case NATIVE_GET_IND_RESOURCE:
660     get_ind_resource();
661     break;
662     case NATIVE_GET_1_IND_RESOURCE:
663     get_1_ind_resource();
664     break;
665     case NATIVE_R_GET_RESOURCE:
666     r_get_resource();
667     break;
668     case NATIVE_SERIAL_NOTHING:
669     case NATIVE_SERIAL_OPEN:
670     case NATIVE_SERIAL_PRIME_IN:
671     case NATIVE_SERIAL_PRIME_OUT:
672     case NATIVE_SERIAL_CONTROL:
673     case NATIVE_SERIAL_STATUS:
674     case NATIVE_SERIAL_CLOSE: {
675     typedef int16 (*SerialCallback)(uint32, uint32);
676     static const SerialCallback serial_callbacks[] = {
677     SerialNothing,
678     SerialOpen,
679     SerialPrimeIn,
680     SerialPrimeOut,
681     SerialControl,
682     SerialStatus,
683     SerialClose
684     };
685     GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4));
686     break;
687     }
688     default:
689     printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
690     QuitEmulator();
691     break;
692     }
693     }
694    
695     /*
696     * Execute native subroutine (LR must contain return address)
697     */
698    
699     void ExecuteNative(int selector)
700     {
701     uint32 tvect[2];
702     tvect[0] = tswap32(POWERPC_NATIVE_OP_FUNC(selector));
703     tvect[1] = 0; // Fake TVECT
704     RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect);
705     M68kRegisters r;
706     Execute68k((uint32)&desc, &r);
707     }
708    
709     /*
710     * Execute 68k subroutine (must be ended with EXEC_RETURN)
711     * This must only be called by the emul_thread when in EMUL_OP mode
712     * r->a[7] is unused, the routine runs on the caller's stack
713     */
714    
715     void Execute68k(uint32 pc, M68kRegisters *r)
716     {
717     current_cpu->execute_68k(pc, r);
718     }
719    
720     /*
721     * Execute 68k A-Trap from EMUL_OP routine
722     * r->a[7] is unused, the routine runs on the caller's stack
723     */
724    
725     void Execute68kTrap(uint16 trap, M68kRegisters *r)
726     {
727     uint16 proc[2] = {trap, M68K_RTS};
728     Execute68k((uint32)proc, r);
729     }
730    
731     /*
732     * Call MacOS PPC code
733     */
734    
735     uint32 call_macos(uint32 tvect)
736     {
737     return current_cpu->execute_macos_code(tvect, 0, NULL);
738     }
739    
740     uint32 call_macos1(uint32 tvect, uint32 arg1)
741     {
742     const uint32 args[] = { arg1 };
743     return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
744     }
745    
746     uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2)
747     {
748     const uint32 args[] = { arg1, arg2 };
749     return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
750     }
751    
752     uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3)
753     {
754     const uint32 args[] = { arg1, arg2, arg3 };
755     return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
756     }
757    
758     uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4)
759     {
760     const uint32 args[] = { arg1, arg2, arg3, arg4 };
761     return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
762     }
763    
764     uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5)
765     {
766     const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 };
767     return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
768     }
769    
770     uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6)
771     {
772     const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
773     return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
774     }
775    
776     uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7)
777     {
778     const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
779     return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
780     }
781    
782     /*
783     * Atomic operations
784     */
785    
786     int atomic_add(int *var, int v)
787     {
788     int ret = *var;
789     *var += v;
790     return ret;
791     }
792    
793     int atomic_and(int *var, int v)
794     {
795     int ret = *var;
796     *var &= v;
797     return ret;
798     }
799    
800     int atomic_or(int *var, int v)
801     {
802     int ret = *var;
803     *var |= v;
804     return ret;
805     }
806    
807     /*
808     * Resource Manager thunks
809     */
810    
811     extern "C" void check_load_invoc(uint32 type, int16 id, uint16 **h);
812    
813     void get_resource(void)
814     {
815     current_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
816     }
817    
818     void get_1_resource(void)
819     {
820     current_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
821     }
822    
823     void get_ind_resource(void)
824     {
825     current_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
826     }
827    
828     void get_1_ind_resource(void)
829     {
830     current_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
831     }
832    
833     void r_get_resource(void)
834     {
835     current_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
836     }