ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
(Generate patch)

Comparing SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp (file contents):
Revision 1.2 by gbeauche, 2003-09-28T21:27:34Z vs.
Revision 1.18 by gbeauche, 2003-11-24T23:45:41Z

# Line 21 | Line 21
21   #include "sysdeps.h"
22   #include "cpu_emulation.h"
23   #include "main.h"
24 + #include "prefs.h"
25   #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"
30 #include "spcflags.h"
31   #include "cpu/ppc/ppc-cpu.hpp"
32   #include "cpu/ppc/ppc-operations.hpp"
33 + #include "cpu/ppc/ppc-instructions.hpp"
34  
35   // Used for NativeOp trampolines
36   #include "video.h"
37   #include "name_registry.h"
38   #include "serial.h"
39 + #include "ether.h"
40  
41   #include <stdio.h>
42  
# Line 43 | Line 45
45   #include "mon_disass.h"
46   #endif
47  
48 < #define DEBUG 1
48 > #define DEBUG 0
49   #include "debug.h"
50  
51 + // Emulation time statistics
52 + #define EMUL_TIME_STATS 1
53 +
54 + #if EMUL_TIME_STATS
55 + static clock_t emul_start_time;
56 + static uint32 interrupt_count = 0;
57 + static clock_t interrupt_time = 0;
58 + static uint32 exec68k_count = 0;
59 + static clock_t exec68k_time = 0;
60 + static uint32 native_exec_count = 0;
61 + static clock_t native_exec_time = 0;
62 + static uint32 macos_exec_count = 0;
63 + static clock_t macos_exec_time = 0;
64 + #endif
65 +
66   static void enter_mon(void)
67   {
68          // Start up mon in real-mode
# Line 56 | Line 73 | static void enter_mon(void)
73   }
74  
75   // Enable multicore (main/interrupts) cpu emulation?
76 < #define MULTICORE_CPU 0
76 > #define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0)
77  
78   // Enable Execute68k() safety checks?
79   #define SAFE_EXEC_68K 1
# Line 70 | Line 87 | static void enter_mon(void)
87   // Interrupts in native mode?
88   #define INTERRUPTS_IN_NATIVE_MODE 1
89  
73 // 68k Emulator Data
74 struct EmulatorData {
75        uint32  v[0x400];      
76 };
77
78 // Kernel Data
79 struct KernelData {
80        uint32  v[0x400];
81        EmulatorData ed;
82 };
83
90   // Pointer to Kernel Data
91 < static KernelData * const kernel_data = (KernelData *)0x68ffe000;
91 > static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE;
92 >
93 > // SIGSEGV handler
94 > static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
95  
96  
97   /**
98   *              PowerPC emulator glue with special 'sheep' opcodes
99   **/
100  
101 < struct sheepshaver_exec_return { };
101 > enum {
102 >        PPC_I(SHEEP) = PPC_I(MAX),
103 >        PPC_I(SHEEP_MAX)
104 > };
105  
106   class sheepshaver_cpu
107          : public powerpc_cpu
# Line 99 | Line 111 | class sheepshaver_cpu
111  
112   public:
113  
114 <        sheepshaver_cpu()
115 <                : powerpc_cpu()
104 <                { init_decoder(); }
114 >        // Constructor
115 >        sheepshaver_cpu();
116  
117          // Condition Register accessors
118          uint32 get_cr() const           { return cr().get(); }
119          void set_cr(uint32 v)           { cr().set(v); }
120  
121          // Execution loop
122 <        void execute(uint32 pc);
122 >        void execute(uint32 entry, bool enable_cache = false);
123  
124          // Execute 68k routine
125          void execute_68k(uint32 entry, M68kRegisters *r);
# Line 123 | Line 134 | public:
134          void get_resource(uint32 old_get_resource);
135  
136          // Handle MacOS interrupt
137 <        void interrupt(uint32 entry, sheepshaver_cpu *cpu);
138 <
128 <        // spcflags for interrupts handling
129 <        static uint32 spcflags;
137 >        void interrupt(uint32 entry);
138 >        void handle_interrupt();
139  
140          // Lazy memory allocator (one item at a time)
141          void *operator new(size_t size)
# Line 136 | Line 145 | public:
145          // FIXME: really make surre array allocation fail at link time?
146          void *operator new[](size_t);
147          void operator delete[](void *p);
148 +
149 +        // Make sure the SIGSEGV handler can access CPU registers
150 +        friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
151   };
152  
141 uint32 sheepshaver_cpu::spcflags = 0;
153   lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator;
154  
155 + sheepshaver_cpu::sheepshaver_cpu()
156 +        : powerpc_cpu()
157 + {
158 +        init_decoder();
159 + }
160 +
161   void sheepshaver_cpu::init_decoder()
162   {
163   #ifndef PPC_NO_STATIC_II_INDEX_TABLE
# Line 152 | Line 169 | void sheepshaver_cpu::init_decoder()
169  
170          static const instr_info_t sheep_ii_table[] = {
171                  { "sheep",
172 <                  (execute_fn)&sheepshaver_cpu::execute_sheep,
172 >                  (execute_pmf)&sheepshaver_cpu::execute_sheep,
173                    NULL,
174 <                  D_form, 6, 0, CFLOW_TRAP
174 >                  PPC_I(SHEEP),
175 >                  D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP
176                  }
177          };
178  
# Line 191 | Line 209 | void sheepshaver_cpu::execute_sheep(uint
209          case 0:         // EMUL_RETURN
210                  QuitEmulator();
211                  break;
212 <                
212 >
213          case 1:         // EXEC_RETURN
214 <                throw sheepshaver_exec_return();
214 >                spcflags().set(SPCFLAG_CPU_EXEC_RETURN);
215                  break;
216  
217          case 2:         // EXEC_NATIVE
# Line 226 | Line 244 | void sheepshaver_cpu::execute_sheep(uint
244          }
245   }
246  
229 // Checks for pending interrupts
230 struct execute_nothing {
231        static inline void execute(powerpc_cpu *) { }
232 };
233
234 static void HandleInterrupt(void);
235
236 struct execute_spcflags_check {
237        static inline void execute(powerpc_cpu *cpu) {
238                if (SPCFLAGS_TEST(SPCFLAG_ALL_BUT_EXEC_RETURN)) {
239                        if (SPCFLAGS_TEST( SPCFLAG_ENTER_MON )) {
240                                SPCFLAGS_CLEAR( SPCFLAG_ENTER_MON );
241                                enter_mon();
242                        }
243                        if (SPCFLAGS_TEST( SPCFLAG_DOINT )) {
244                                SPCFLAGS_CLEAR( SPCFLAG_DOINT );
245                                HandleInterrupt();
246                        }
247                        if (SPCFLAGS_TEST( SPCFLAG_INT )) {
248                                SPCFLAGS_CLEAR( SPCFLAG_INT );
249                                SPCFLAGS_SET( SPCFLAG_DOINT );
250                        }
251                }
252        }
253 };
254
247   // Execution loop
248 < void sheepshaver_cpu::execute(uint32 entry)
248 > void sheepshaver_cpu::execute(uint32 entry, bool enable_cache)
249   {
250 <        try {
259 <                pc() = entry;
260 <                powerpc_cpu::do_execute<execute_nothing, execute_spcflags_check>();
261 <        }
262 <        catch (sheepshaver_exec_return const &) {
263 <                // Nothing, simply return
264 <        }
265 <        catch (...) {
266 <                printf("ERROR: execute() received an unknown exception!\n");
267 <                QuitEmulator();
268 <        }
250 >        powerpc_cpu::execute(entry, enable_cache);
251   }
252  
253   // Handle MacOS interrupt
254 < void sheepshaver_cpu::interrupt(uint32 entry, sheepshaver_cpu *cpu)
254 > void sheepshaver_cpu::interrupt(uint32 entry)
255   {
256 < #if MULTICORE_CPU
257 <        // Initialize stack pointer from previous CPU running
258 <        gpr(1) = cpu->gpr(1);
259 < #else
256 > #if EMUL_TIME_STATS
257 >        interrupt_count++;
258 >        const clock_t interrupt_start = clock();
259 > #endif
260 >
261 > #if !MULTICORE_CPU
262          // Save program counters and branch registers
263          uint32 saved_pc = pc();
264          uint32 saved_lr = lr();
265          uint32 saved_ctr= ctr();
266 +        uint32 saved_sp = gpr(1);
267   #endif
268  
269 <        // Create stack frame
270 <        gpr(1) -= 64;
269 >        // Initialize stack pointer to SheepShaver alternate stack base
270 >        gpr(1) = SheepStack1Base - 64;
271  
272          // Build trampoline to return from interrupt
273 <        uint32 trampoline[] = { POWERPC_EMUL_OP | 1 };
273 >        uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) };
274  
275          // Prepare registers for nanokernel interrupt routine
276 <        kernel_data->v[0x004 >> 2] = gpr(1);
277 <        kernel_data->v[0x018 >> 2] = gpr(6);
276 >        kernel_data->v[0x004 >> 2] = htonl(gpr(1));
277 >        kernel_data->v[0x018 >> 2] = htonl(gpr(6));
278  
279 <        gpr(6) = kernel_data->v[0x65c >> 2];
279 >        gpr(6) = ntohl(kernel_data->v[0x65c >> 2]);
280          assert(gpr(6) != 0);
281          WriteMacInt32(gpr(6) + 0x13c, gpr(7));
282          WriteMacInt32(gpr(6) + 0x144, gpr(8));
# Line 302 | Line 287 | void sheepshaver_cpu::interrupt(uint32 e
287          WriteMacInt32(gpr(6) + 0x16c, gpr(13));
288  
289          gpr(1)  = KernelDataAddr;
290 <        gpr(7)  = kernel_data->v[0x660 >> 2];
290 >        gpr(7)  = ntohl(kernel_data->v[0x660 >> 2]);
291          gpr(8)  = 0;
292          gpr(10) = (uint32)trampoline;
293          gpr(12) = (uint32)trampoline;
294 <        gpr(13) = cr().get();
294 >        gpr(13) = get_cr();
295  
296          // rlwimi. r7,r7,8,0,0
297          uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7));
# Line 314 | Line 299 | void sheepshaver_cpu::interrupt(uint32 e
299          gpr(7) = result;
300  
301          gpr(11) = 0xf072; // MSR (SRR1)
302 <        cr().set((gpr(11) & 0x0fff0000) | (cr().get() & ~0x0fff0000));
302 >        cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000));
303  
304          // Enter nanokernel
305          execute(entry);
306  
322        // Cleanup stack
323        gpr(1) += 64;
324
307   #if !MULTICORE_CPU
308          // Restore program counters and branch registers
309          pc() = saved_pc;
310          lr() = saved_lr;
311          ctr()= saved_ctr;
312 +        gpr(1) = saved_sp;
313 + #endif
314 +
315 + #if EMUL_TIME_STATS
316 +        interrupt_time += (clock() - interrupt_start);
317   #endif
318   }
319  
320   // Execute 68k routine
321   void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r)
322   {
323 + #if EMUL_TIME_STATS
324 +        exec68k_count++;
325 +        const clock_t exec68k_start = clock();
326 + #endif
327 +
328   #if SAFE_EXEC_68K
329          if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
330                  printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
# Line 342 | Line 334 | void sheepshaver_cpu::execute_68k(uint32
334          uint32 saved_pc = pc();
335          uint32 saved_lr = lr();
336          uint32 saved_ctr= ctr();
337 +        uint32 saved_cr = get_cr();
338  
339          // Create MacOS stack frame
340 +        // FIXME: make sure MacOS doesn't expect PPC registers to live on top
341          uint32 sp = gpr(1);
342 <        gpr(1) -= 56 + 19*4 + 18*8;
342 >        gpr(1) -= 56;
343          WriteMacInt32(gpr(1), sp);
344  
345          // Save PowerPC registers
346 <        memcpy(Mac2HostAddr(gpr(1)+56), &gpr(13), sizeof(uint32)*(32-13));
346 >        uint32 saved_GPRs[19];
347 >        memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13));
348   #if SAVE_FP_EXEC_68K
349 <        memcpy(Mac2HostAddr(gpr(1)+56+19*4), &fpr(14), sizeof(double)*(32-14));
349 >        double saved_FPRs[18];
350 >        memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14));
351   #endif
352  
353          // Setup registers for 68k emulator
# Line 365 | Line 361 | void sheepshaver_cpu::execute_68k(uint32
361          gpr(25) = ReadMacInt32(XLM_68K_R25);            // MSB of SR
362          gpr(26) = 0;
363          gpr(28) = 0;                                                            // VBR
364 <        gpr(29) = kernel_data->ed.v[0x74 >> 2];         // Pointer to opcode table
365 <        gpr(30) = kernel_data->ed.v[0x78 >> 2];         // Address of emulator
364 >        gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]);          // Pointer to opcode table
365 >        gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]);          // Address of emulator
366          gpr(31) = KernelDataAddr + 0x1000;
367  
368          // Push return address (points to EXEC_RETURN opcode) on stack
# Line 398 | Line 394 | void sheepshaver_cpu::execute_68k(uint32
394            r->a[i] = gpr(16 + i);
395  
396          // Restore PowerPC registers
397 <        memcpy(&gpr(13), Mac2HostAddr(gpr(1)+56), sizeof(uint32)*(32-13));
397 >        memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13));
398   #if SAVE_FP_EXEC_68K
399 <        memcpy(&fpr(14), Mac2HostAddr(gpr(1)+56+19*4), sizeof(double)*(32-14));
399 >        memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14));
400   #endif
401  
402          // Cleanup stack
403 <        gpr(1) += 56 + 19*4 + 18*8;
403 >        gpr(1) += 56;
404  
405          // Restore program counters and branch registers
406          pc() = saved_pc;
407          lr() = saved_lr;
408          ctr()= saved_ctr;
409 +        set_cr(saved_cr);
410 +
411 + #if EMUL_TIME_STATS
412 +        exec68k_time += (clock() - exec68k_start);
413 + #endif
414   }
415  
416   // Call MacOS PPC code
417   uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args)
418   {
419 + #if EMUL_TIME_STATS
420 +        macos_exec_count++;
421 +        const clock_t macos_exec_start = clock();
422 + #endif
423 +
424          // Save program counters and branch registers
425          uint32 saved_pc = pc();
426          uint32 saved_lr = lr();
427          uint32 saved_ctr= ctr();
428  
429          // Build trampoline with EXEC_RETURN
430 <        uint32 trampoline[] = { POWERPC_EMUL_OP | 1 };
430 >        uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) };
431          lr() = (uint32)trampoline;
432  
433          gpr(1) -= 64;                                                           // Create stack frame
# Line 453 | Line 459 | uint32 sheepshaver_cpu::execute_macos_co
459          lr() = saved_lr;
460          ctr()= saved_ctr;
461  
462 + #if EMUL_TIME_STATS
463 +        macos_exec_time += (clock() - macos_exec_start);
464 + #endif
465 +
466          return retval;
467   }
468  
# Line 461 | Line 471 | inline void sheepshaver_cpu::execute_ppc
471   {
472          // Save branch registers
473          uint32 saved_lr = lr();
464        uint32 saved_ctr= ctr();
465
466        const uint32 trampoline[] = { POWERPC_EMUL_OP | 1 };
474  
475 +        const uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) };
476          lr() = (uint32)trampoline;
477 <        ctr()= entry;
477 >
478          execute(entry);
479  
480          // Restore branch registers
481          lr() = saved_lr;
474        ctr()= saved_ctr;
482   }
483  
484   // Resource Manager thunk
485 < extern "C" void check_load_invoc(uint32 type, int16 id, uint16 **h);
485 > extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
486  
487   inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
488   {
# Line 487 | Line 494 | inline void sheepshaver_cpu::get_resourc
494  
495          // Call old routine
496          execute_ppc(old_get_resource);
490        uint16 **handle = (uint16 **)gpr(3);
497  
498          // Call CheckLoad()
499 +        uint32 handle = gpr(3);
500          check_load_invoc(type, id, handle);
501 <        gpr(3) = (uint32)handle;
501 >        gpr(3) = handle;
502  
503          // Cleanup stack
504          gpr(1) += 56;
# Line 506 | Line 513 | static sheepshaver_cpu *main_cpu = NULL;
513   static sheepshaver_cpu *interrupt_cpu = NULL;   // CPU emulator to handle interrupts
514   static sheepshaver_cpu *current_cpu = NULL;             // Current CPU emulator context
515  
516 + void FlushCodeCache(uintptr start, uintptr end)
517 + {
518 +        D(bug("FlushCodeCache(%08x, %08x)\n", start, end));
519 +        main_cpu->invalidate_cache_range(start, end);
520 + #if MULTICORE_CPU
521 +        interrupt_cpu->invalidate_cache_range(start, end);
522 + #endif
523 + }
524 +
525   static inline void cpu_push(sheepshaver_cpu *new_cpu)
526   {
527   #if MULTICORE_CPU
# Line 536 | Line 552 | static void dump_log(void)
552   *  Initialize CPU emulation
553   */
554  
555 < static struct sigaction sigsegv_action;
540 <
541 < #if defined(__powerpc__)
542 < #include <sys/ucontext.h>
543 < #endif
544 <
545 < static void sigsegv_handler(int sig, siginfo_t *sip, void *scp)
555 > static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
556   {
547        const uintptr addr = (uintptr)sip->si_addr;
557   #if ENABLE_VOSF
558 <        // Handle screen fault.
559 <        extern bool Screen_fault_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction);
560 <        if (Screen_fault_handler((sigsegv_address_t)addr, SIGSEGV_INVALID_PC))
561 <                return;
558 >        // Handle screen fault
559 >        extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t);
560 >        if (Screen_fault_handler(fault_address, fault_instruction))
561 >                return SIGSEGV_RETURN_SUCCESS;
562   #endif
563 < #if defined(__powerpc__)
564 <        if (addr >= ROM_BASE && addr < ROM_BASE + ROM_SIZE) {
565 <                printf("IGNORE write access to ROM at %08x\n", addr);
566 <                (((ucontext_t *)scp)->uc_mcontext.regs)->nip += 4;
567 <                return;
568 <        }
569 <        if (addr >= 0xf3012000 && addr < 0xf3014000 && 0) {
570 <                printf("IGNORE write access to ROM at %08x\n", addr);
571 <                (((ucontext_t *)scp)->uc_mcontext.regs)->nip += 4;
572 <                return;
563 >
564 >        const uintptr addr = (uintptr)fault_address;
565 > #if HAVE_SIGSEGV_SKIP_INSTRUCTION
566 >        // Ignore writes to ROM
567 >        if ((addr - ROM_BASE) < ROM_SIZE)
568 >                return SIGSEGV_RETURN_SKIP_INSTRUCTION;
569 >
570 >        // Get program counter of target CPU
571 >        sheepshaver_cpu * const cpu = current_cpu;
572 >        const uint32 pc = cpu->pc();
573 >        
574 >        // Fault in Mac ROM or RAM?
575 >        bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize));
576 >        if (mac_fault) {
577 >
578 >                // "VM settings" during MacOS 8 installation
579 >                if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000)
580 >                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
581 >        
582 >                // MacOS 8.5 installation
583 >                else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000)
584 >                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
585 >        
586 >                // MacOS 8 serial drivers on startup
587 >                else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000))
588 >                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
589 >        
590 >                // MacOS 8.1 serial drivers on startup
591 >                else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
592 >                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
593 >                else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
594 >                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
595 >
596 >                // Ignore all other faults, if requested
597 >                if (PrefsFindBool("ignoresegv"))
598 >                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
599          }
565 #endif
566        printf("Caught SIGSEGV at address %p\n", sip->si_addr);
567        printf("Native PC: %08x\n", (((ucontext_t *)scp)->uc_mcontext.regs)->nip);
568        printf("Current CPU: %s\n", current_cpu == main_cpu ? "main" : "interrupts");
569 #if 1
570        dump_registers();
600   #else
601 <        printf("Main CPU context\n");
573 <        main_cpu->dump_registers();
574 <        printf("Interrupts CPU context\n");
575 <        interrupt_cpu->dump_registers();
601 > #error "FIXME: You don't have the capability to skip instruction within signal handlers"
602   #endif
603 +
604 +        printf("SIGSEGV\n");
605 +        printf("  pc %p\n", fault_instruction);
606 +        printf("  ea %p\n", fault_address);
607 +        printf(" cpu %s\n", current_cpu == main_cpu ? "main" : "interrupts");
608 +        dump_registers();
609          current_cpu->dump_log();
610          enter_mon();
611          QuitEmulator();
612 +
613 +        return SIGSEGV_RETURN_FAILURE;
614   }
615  
616   void init_emul_ppc(void)
# Line 591 | Line 625 | void init_emul_ppc(void)
625          interrupt_cpu = new sheepshaver_cpu();
626   #endif
627  
628 <        // Install SIGSEGV handler
629 <        sigemptyset(&sigsegv_action.sa_mask);
596 <        sigsegv_action.sa_sigaction = sigsegv_handler;
597 <        sigsegv_action.sa_flags = SA_SIGINFO;
598 <        sigsegv_action.sa_restorer = NULL;
599 <        sigaction(SIGSEGV, &sigsegv_action, NULL);
628 >        // Install the handler for SIGSEGV
629 >        sigsegv_install_handler(sigsegv_handler);
630  
631   #if ENABLE_MON
632          // Install "regs" command in cxmon
633          mon_add_command("regs", dump_registers, "regs                     Dump PowerPC registers\n");
634          mon_add_command("log", dump_log, "log                      Dump PowerPC emulation log\n");
635   #endif
636 +
637 + #if EMUL_TIME_STATS
638 +        emul_start_time = clock();
639 + #endif
640 + }
641 +
642 + /*
643 + *  Deinitialize emulation
644 + */
645 +
646 + void exit_emul_ppc(void)
647 + {
648 + #if EMUL_TIME_STATS
649 +        clock_t emul_end_time = clock();
650 +
651 +        printf("### Statistics for SheepShaver emulation parts\n");
652 +        const clock_t emul_time = emul_end_time - emul_start_time;
653 +        printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC));
654 +        printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count,
655 +                   (double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time));
656 +
657 + #define PRINT_STATS(LABEL, VAR_PREFIX) do {                                                             \
658 +                printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count);             \
659 +                printf("Total " LABEL " time  : %.1f sec (%.1f%%)\n",                   \
660 +                           double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC),          \
661 +                           100.0 * double(VAR_PREFIX##_time) / double(emul_time));      \
662 +        } while (0)
663 +
664 +        PRINT_STATS("Execute68k[Trap] execution", exec68k);
665 +        PRINT_STATS("NativeOp execution", native_exec);
666 +        PRINT_STATS("MacOS routine execution", macos_exec);
667 +
668 + #undef PRINT_STATS
669 +        printf("\n");
670 + #endif
671 +
672 +        delete main_cpu;
673 + #if MULTICORE_CPU
674 +        delete interrupt_cpu;
675 + #endif
676   }
677  
678   /*
# Line 612 | Line 682 | void init_emul_ppc(void)
682   void emul_ppc(uint32 entry)
683   {
684          current_cpu = main_cpu;
685 + #if DEBUG
686          current_cpu->start_log();
687 <        current_cpu->execute(entry);
687 > #endif
688 >        // start emulation loop and enable code translation or caching
689 >        current_cpu->execute(entry, true);
690   }
691  
692   /*
693   *  Handle PowerPC interrupt
694   */
695  
696 < // Atomic operations
697 < extern int atomic_add(int *var, int v);
698 < extern int atomic_and(int *var, int v);
699 < extern int atomic_or(int *var, int v);
700 <
696 > #if ASYNC_IRQ
697 > void HandleInterrupt(void)
698 > {
699 >        main_cpu->handle_interrupt();
700 > }
701 > #else
702   void TriggerInterrupt(void)
703   {
704   #if 0
705    WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
706   #else
707 <  SPCFLAGS_SET( SPCFLAG_INT );
707 >  // Trigger interrupt to main cpu only
708 >  if (main_cpu)
709 >          main_cpu->trigger_interrupt();
710   #endif
711   }
712 + #endif
713  
714 < static void HandleInterrupt(void)
714 > void sheepshaver_cpu::handle_interrupt(void)
715   {
716          // Do nothing if interrupts are disabled
717 <        if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0)
717 >        if (*(int32 *)XLM_IRQ_NEST > 0)
718                  return;
719  
720          // Do nothing if there is no interrupt pending
# Line 653 | Line 730 | static void HandleInterrupt(void)
730                  // 68k emulator active, trigger 68k interrupt level 1
731                  assert(current_cpu == main_cpu);
732                  WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
733 <                main_cpu->set_cr(main_cpu->get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
733 >                set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
734                  break;
735      
736   #if INTERRUPTS_IN_NATIVE_MODE
737          case MODE_NATIVE:
738                  // 68k emulator inactive, in nanokernel?
739                  assert(current_cpu == main_cpu);
740 <                if (main_cpu->gpr(1) != KernelDataAddr) {
740 >                if (gpr(1) != KernelDataAddr) {
741                          // Prepare for 68k interrupt level 1
742                          WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
743                          WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
# Line 671 | Line 748 | static void HandleInterrupt(void)
748                          DisableInterrupt();
749                          cpu_push(interrupt_cpu);
750                          if (ROMType == ROMTYPE_NEWWORLD)
751 <                                current_cpu->interrupt(ROM_BASE + 0x312b1c, main_cpu);
751 >                                current_cpu->interrupt(ROM_BASE + 0x312b1c);
752                          else
753 <                                current_cpu->interrupt(ROM_BASE + 0x312a3c, main_cpu);
753 >                                current_cpu->interrupt(ROM_BASE + 0x312a3c);
754                          cpu_pop();
755                  }
756                  break;
# Line 748 | Line 825 | const uint32 NativeOpTable[NATIVE_OP_MAX
825          POWERPC_NATIVE_OP_INIT(1, NATIVE_R_GET_RESOURCE),
826          POWERPC_NATIVE_OP_INIT(0, NATIVE_DISABLE_INTERRUPT),
827          POWERPC_NATIVE_OP_INIT(0, NATIVE_ENABLE_INTERRUPT),
828 +        POWERPC_NATIVE_OP_INIT(1, NATIVE_MAKE_EXECUTABLE),
829   };
830  
831   static void get_resource(void);
# Line 760 | Line 838 | static void r_get_resource(void);
838  
839   static void NativeOp(int selector)
840   {
841 + #if EMUL_TIME_STATS
842 +        native_exec_count++;
843 +        const clock_t native_exec_start = clock();
844 + #endif
845 +
846          switch (selector) {
847          case NATIVE_PATCH_NAME_REGISTRY:
848                  DoPatchNameRegistry();
# Line 774 | Line 857 | static void NativeOp(int selector)
857                  GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4),
858                                                                                             (void *)GPR(5), GPR(6), GPR(7));
859                  break;
860 <        case NATIVE_GET_RESOURCE:
861 <                get_resource();
860 > #ifdef WORDS_BIGENDIAN
861 >        case NATIVE_ETHER_IRQ:
862 >                EtherIRQ();
863                  break;
864 <        case NATIVE_GET_1_RESOURCE:
865 <                get_1_resource();
864 >        case NATIVE_ETHER_INIT:
865 >                GPR(3) = InitStreamModule((void *)GPR(3));
866                  break;
867 <        case NATIVE_GET_IND_RESOURCE:
868 <                get_ind_resource();
867 >        case NATIVE_ETHER_TERM:
868 >                TerminateStreamModule();
869                  break;
870 <        case NATIVE_GET_1_IND_RESOURCE:
871 <                get_1_ind_resource();
870 >        case NATIVE_ETHER_OPEN:
871 >                GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7));
872 >                break;
873 >        case NATIVE_ETHER_CLOSE:
874 >                GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5));
875                  break;
876 <        case NATIVE_R_GET_RESOURCE:
877 <                r_get_resource();
876 >        case NATIVE_ETHER_WPUT:
877 >                GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4));
878                  break;
879 +        case NATIVE_ETHER_RSRV:
880 +                GPR(3) = ether_rsrv((queue_t *)GPR(3));
881 +                break;
882 + #else
883 +        case NATIVE_ETHER_INIT:
884 +                // FIXME: needs more complicated thunks
885 +                GPR(3) = false;
886 +                break;
887 + #endif
888          case NATIVE_SERIAL_NOTHING:
889          case NATIVE_SERIAL_OPEN:
890          case NATIVE_SERIAL_PRIME_IN:
# Line 809 | Line 905 | static void NativeOp(int selector)
905                  GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4));
906                  break;
907          }
908 +        case NATIVE_GET_RESOURCE:
909 +        case NATIVE_GET_1_RESOURCE:
910 +        case NATIVE_GET_IND_RESOURCE:
911 +        case NATIVE_GET_1_IND_RESOURCE:
912 +        case NATIVE_R_GET_RESOURCE: {
913 +                typedef void (*GetResourceCallback)(void);
914 +                static const GetResourceCallback get_resource_callbacks[] = {
915 +                        get_resource,
916 +                        get_1_resource,
917 +                        get_ind_resource,
918 +                        get_1_ind_resource,
919 +                        r_get_resource
920 +                };
921 +                get_resource_callbacks[selector - NATIVE_GET_RESOURCE]();
922 +                break;
923 +        }
924          case NATIVE_DISABLE_INTERRUPT:
925                  DisableInterrupt();
926                  break;
927          case NATIVE_ENABLE_INTERRUPT:
928                  EnableInterrupt();
929                  break;
930 +        case NATIVE_MAKE_EXECUTABLE:
931 +                MakeExecutable(0, (void *)GPR(4), GPR(5));
932 +                break;
933          default:
934                  printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
935                  QuitEmulator();
936                  break;
937          }
938 +
939 + #if EMUL_TIME_STATS
940 +        native_exec_time += (clock() - native_exec_start);
941 + #endif
942   }
943  
944   /*
# Line 854 | Line 973 | void Execute68k(uint32 pc, M68kRegisters
973  
974   void Execute68kTrap(uint16 trap, M68kRegisters *r)
975   {
976 <        uint16 proc[2] = {trap, M68K_RTS};
976 >        uint16 proc[2];
977 >        proc[0] = htons(trap);
978 >        proc[1] = htons(M68K_RTS);
979          Execute68k((uint32)proc, r);
980   }
981  
# Line 910 | Line 1031 | uint32 call_macos7(uint32 tvect, uint32
1031   }
1032  
1033   /*
913 *  Atomic operations
914 */
915
916 int atomic_add(int *var, int v)
917 {
918        int ret = *var;
919        *var += v;
920        return ret;
921 }
922
923 int atomic_and(int *var, int v)
924 {
925        int ret = *var;
926        *var &= v;
927        return ret;
928 }
929
930 int atomic_or(int *var, int v)
931 {
932        int ret = *var;
933        *var |= v;
934        return ret;
935 }
936
937 /*
1034   *  Resource Manager thunks
1035   */
1036  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines