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.33 by gbeauche, 2004-04-22T20:57:31Z vs.
Revision 1.53 by gbeauche, 2004-11-22T22:04:38Z

# Line 38 | Line 38
38   #include "name_registry.h"
39   #include "serial.h"
40   #include "ether.h"
41 + #include "timer.h"
42  
43   #include <stdio.h>
44   #include <stdlib.h>
45 + #ifdef HAVE_MALLOC_H
46 + #include <malloc.h>
47 + #endif
48 +
49 + #ifdef USE_SDL_VIDEO
50 + #include <SDL_events.h>
51 + #endif
52  
53   #if ENABLE_MON
54   #include "mon.h"
# Line 51 | Line 59
59   #include "debug.h"
60  
61   // Emulation time statistics
62 < #define EMUL_TIME_STATS 1
62 > #ifndef EMUL_TIME_STATS
63 > #define EMUL_TIME_STATS 0
64 > #endif
65  
66   #if EMUL_TIME_STATS
67   static clock_t emul_start_time;
68 < static uint32 interrupt_count = 0;
68 > static uint32 interrupt_count = 0, ppc_interrupt_count = 0;
69   static clock_t interrupt_time = 0;
70   static uint32 exec68k_count = 0;
71   static clock_t exec68k_time = 0;
# Line 83 | Line 93 | extern "C" void check_load_invoc(uint32
93   // PowerPC EmulOp to exit from emulation looop
94   const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1;
95  
96 < // Enable multicore (main/interrupts) cpu emulation?
97 < #define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0)
96 > // Enable interrupt routine safety checks?
97 > #define SAFE_INTERRUPT_PPC 1
98  
99   // Enable Execute68k() safety checks?
100   #define SAFE_EXEC_68K 1
# Line 99 | Line 109 | const uint32 POWERPC_EXEC_RETURN = POWER
109   #define INTERRUPTS_IN_NATIVE_MODE 1
110  
111   // Pointer to Kernel Data
112 < static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE;
112 > static KernelData * kernel_data;
113  
114   // SIGSEGV handler
115 < static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
115 > sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
116 >
117 > #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   // JIT Compiler enabled?
124   static inline bool enable_jit_p()
# Line 126 | Line 142 | class sheepshaver_cpu
142          void init_decoder();
143          void execute_sheep(uint32 opcode);
144  
145 +        // 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   public:
161  
162          // Constructor
# Line 137 | Line 168 | public:
168          uint32 get_xer() const          { return xer().get(); }
169          void set_xer(uint32 v)          { xer().set(v); }
170  
171 +        // Execute NATIVE_OP routine
172 +        void execute_native_op(uint32 native_op);
173 +
174          // Execute EMUL_OP routine
175          void execute_emul_op(uint32 emul_op);
176  
# Line 149 | Line 183 | public:
183          // Execute MacOS/PPC code
184          uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args);
185  
186 + #if PPC_ENABLE_JIT
187          // Compile one instruction
188 <        virtual bool compile1(codegen_context_t & cg_context);
189 <
188 >        virtual int compile1(codegen_context_t & cg_context);
189 > #endif
190          // Resource manager thunk
191          void get_resource(uint32 old_get_resource);
192  
# Line 221 | Line 256 | void sheepshaver_cpu::init_decoder()
256          }
257   }
258  
224 // Forward declaration for native opcode handler
225 static void NativeOp(int selector);
226
259   /*              NativeOp instruction format:
260 <                +------------+--------------------------+--+----------+------------+
261 <                |      6     |                          |FN|    OP    |      2     |
262 <                +------------+--------------------------+--+----------+------------+
263 <                 0         5 |6                       19 20 21      25 26        31
260 >                +------------+-------------------------+--+-----------+------------+
261 >                |      6     |                         |FN|    OP     |      2     |
262 >                +------------+-------------------------+--+-----------+------------+
263 >                 0         5 |6                      18 19 20      25 26        31
264   */
265  
266 < typedef bit_field< 20, 20 > FN_field;
267 < typedef bit_field< 21, 25 > NATIVE_OP_field;
266 > typedef bit_field< 19, 19 > FN_field;
267 > typedef bit_field< 20, 25 > NATIVE_OP_field;
268   typedef bit_field< 26, 31 > EMUL_OP_field;
269  
270   // Execute EMUL_OP routine
# Line 275 | Line 307 | void sheepshaver_cpu::execute_sheep(uint
307                  break;
308  
309          case 2:         // EXEC_NATIVE
310 <                NativeOp(NATIVE_OP_field::extract(opcode));
310 >                execute_native_op(NATIVE_OP_field::extract(opcode));
311                  if (FN_field::test(opcode))
312                          pc() = lr();
313                  else
# Line 290 | Line 322 | void sheepshaver_cpu::execute_sheep(uint
322   }
323  
324   // Compile one instruction
293 bool sheepshaver_cpu::compile1(codegen_context_t & cg_context)
294 {
325   #if PPC_ENABLE_JIT
326 + int sheepshaver_cpu::compile1(codegen_context_t & cg_context)
327 + {
328          const instr_info_t *ii = cg_context.instr_info;
329          if (ii->mnemo != PPC_I(SHEEP))
330 <                return false;
330 >                return COMPILE_FAILURE;
331  
332 <        bool compiled = false;
332 >        int status = COMPILE_FAILURE;
333          powerpc_dyngen & dg = cg_context.codegen;
334          uint32 opcode = cg_context.opcode;
335  
336          switch (opcode & 0x3f) {
337          case 0:         // EMUL_RETURN
338                  dg.gen_invoke(QuitEmulator);
339 <                compiled = true;
339 >                status = COMPILE_CODE_OK;
340                  break;
341  
342          case 1:         // EXEC_RETURN
343                  dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN);
344 <                compiled = true;
344 >                // Don't check for pending interrupts, we do know we have to
345 >                // get out of this block ASAP
346 >                dg.gen_exec_return();
347 >                status = COMPILE_EPILOGUE_OK;
348                  break;
349  
350          case 2: {       // EXEC_NATIVE
351                  uint32 selector = NATIVE_OP_field::extract(opcode);
352                  switch (selector) {
353 + #if !PPC_REENTRANT_JIT
354 +                // Filter out functions that may invoke Execute68k() or
355 +                // CallMacOS(), this would break reentrancy as they could
356 +                // invalidate the translation cache and even overwrite
357 +                // continuation code when we are done with them.
358                  case NATIVE_PATCH_NAME_REGISTRY:
359                          dg.gen_invoke(DoPatchNameRegistry);
360 <                        compiled = true;
360 >                        status = COMPILE_CODE_OK;
361                          break;
362                  case NATIVE_VIDEO_INSTALL_ACCEL:
363                          dg.gen_invoke(VideoInstallAccel);
364 <                        compiled = true;
364 >                        status = COMPILE_CODE_OK;
365                          break;
366                  case NATIVE_VIDEO_VBL:
367                          dg.gen_invoke(VideoVBL);
368 <                        compiled = true;
368 >                        status = COMPILE_CODE_OK;
369                          break;
370                  case NATIVE_GET_RESOURCE:
371                  case NATIVE_GET_1_RESOURCE:
# Line 343 | Line 383 | bool sheepshaver_cpu::compile1(codegen_c
383                          typedef void (*func_t)(dyngen_cpu_base, uint32);
384                          func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr();
385                          dg.gen_invoke_CPU_im(func, old_get_resource);
386 <                        compiled = true;
386 >                        status = COMPILE_CODE_OK;
387                          break;
388                  }
349                case NATIVE_DISABLE_INTERRUPT:
350                        dg.gen_invoke(DisableInterrupt);
351                        compiled = true;
352                        break;
353                case NATIVE_ENABLE_INTERRUPT:
354                        dg.gen_invoke(EnableInterrupt);
355                        compiled = true;
356                        break;
389                  case NATIVE_CHECK_LOAD_INVOC:
390                          dg.gen_load_T0_GPR(3);
391                          dg.gen_load_T1_GPR(4);
392                          dg.gen_se_16_32_T1();
393                          dg.gen_load_T2_GPR(5);
394                          dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc);
395 <                        compiled = true;
395 >                        status = COMPILE_CODE_OK;
396 >                        break;
397 > #endif
398 >                case NATIVE_BITBLT:
399 >                        dg.gen_load_T0_GPR(3);
400 >                        dg.gen_invoke_T0((void (*)(uint32))NQD_bitblt);
401 >                        status = COMPILE_CODE_OK;
402 >                        break;
403 >                case NATIVE_INVRECT:
404 >                        dg.gen_load_T0_GPR(3);
405 >                        dg.gen_invoke_T0((void (*)(uint32))NQD_invrect);
406 >                        status = COMPILE_CODE_OK;
407 >                        break;
408 >                case NATIVE_FILLRECT:
409 >                        dg.gen_load_T0_GPR(3);
410 >                        dg.gen_invoke_T0((void (*)(uint32))NQD_fillrect);
411 >                        status = COMPILE_CODE_OK;
412                          break;
413                  }
414 <                if (FN_field::test(opcode)) {
415 <                        if (compiled) {
414 >                // Could we fully translate this NativeOp?
415 >                if (status == COMPILE_CODE_OK) {
416 >                        if (!FN_field::test(opcode))
417 >                                cg_context.done_compile = false;
418 >                        else {
419                                  dg.gen_load_A0_LR();
420                                  dg.gen_set_PC_A0();
421 +                                cg_context.done_compile = true;
422                          }
423 <                        cg_context.done_compile = true;
423 >                        break;
424                  }
425 <                else
425 > #if PPC_REENTRANT_JIT
426 >                // Try to execute NativeOp trampoline
427 >                if (!FN_field::test(opcode))
428 >                        dg.gen_set_PC_im(cg_context.pc + 4);
429 >                else {
430 >                        dg.gen_load_A0_LR();
431 >                        dg.gen_set_PC_A0();
432 >                }
433 >                dg.gen_mov_32_T0_im(selector);
434 >                dg.gen_jmp(native_op_trampoline);
435 >                cg_context.done_compile = true;
436 >                status = COMPILE_EPILOGUE_OK;
437 >                break;
438 > #endif
439 >                // Invoke NativeOp handler
440 >                if (!FN_field::test(opcode)) {
441 >                        typedef void (*func_t)(dyngen_cpu_base, uint32);
442 >                        func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr();
443 >                        dg.gen_invoke_CPU_im(func, selector);
444                          cg_context.done_compile = false;
445 +                        status = COMPILE_CODE_OK;
446 +                }
447 +                // Otherwise, let it generate a call to execute_sheep() which
448 +                // will cause necessary updates to the program counter
449                  break;
450          }
451  
452          default: {      // EMUL_OP
453 +                uint32 emul_op = EMUL_OP_field::extract(opcode) - 3;
454 + #if PPC_REENTRANT_JIT
455 +                // Try to execute EmulOp trampoline
456 +                dg.gen_set_PC_im(cg_context.pc + 4);
457 +                dg.gen_mov_32_T0_im(emul_op);
458 +                dg.gen_jmp(emul_op_trampoline);
459 +                cg_context.done_compile = true;
460 +                status = COMPILE_EPILOGUE_OK;
461 +                break;
462 + #endif
463 +                // Invoke EmulOp handler
464                  typedef void (*func_t)(dyngen_cpu_base, uint32);
465                  func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
466 <                dg.gen_invoke_CPU_im(func, EMUL_OP_field::extract(opcode) - 3);
466 >                dg.gen_invoke_CPU_im(func, emul_op);
467                  cg_context.done_compile = false;
468 <                compiled = true;
468 >                status = COMPILE_CODE_OK;
469                  break;
470          }
471          }
472 <        return compiled;
472 >        return status;
473 > }
474 > #endif
475 >
476 > // CPU context to preserve on interrupt
477 > sheepshaver_cpu::interrupt_context::interrupt_context(sheepshaver_cpu *_cpu, const char *_where)
478 > {
479 > #if SAFE_INTERRUPT_PPC >= 2
480 >        cpu = _cpu;
481 >        where = _where;
482 >
483 >        // Save interrupt context
484 >        memcpy(&gpr[0], &cpu->gpr(0), sizeof(gpr));
485 >        pc = cpu->pc();
486 >        lr = cpu->lr();
487 >        ctr = cpu->ctr();
488 >        cr = cpu->get_cr();
489 >        xer = cpu->get_xer();
490 > #endif
491 > }
492 >
493 > sheepshaver_cpu::interrupt_context::~interrupt_context()
494 > {
495 > #if SAFE_INTERRUPT_PPC >= 2
496 >        // Check whether CPU context was preserved by interrupt
497 >        if (memcmp(&gpr[0], &cpu->gpr(0), sizeof(gpr)) != 0) {
498 >                printf("FATAL: %s: interrupt clobbers registers\n", where);
499 >                for (int i = 0; i < 32; i++)
500 >                        if (gpr[i] != cpu->gpr(i))
501 >                                printf(" r%d: %08x -> %08x\n", i, gpr[i], cpu->gpr(i));
502 >        }
503 >        if (pc != cpu->pc())
504 >                printf("FATAL: %s: interrupt clobbers PC\n", where);
505 >        if (lr != cpu->lr())
506 >                printf("FATAL: %s: interrupt clobbers LR\n", where);
507 >        if (ctr != cpu->ctr())
508 >                printf("FATAL: %s: interrupt clobbers CTR\n", where);
509 >        if (cr != cpu->get_cr())
510 >                printf("FATAL: %s: interrupt clobbers CR\n", where);
511 >        if (xer != cpu->get_xer())
512 >                printf("FATAL: %s: interrupt clobbers XER\n", where);
513   #endif
389        return false;
514   }
515  
516   // Handle MacOS interrupt
517   void sheepshaver_cpu::interrupt(uint32 entry)
518   {
519   #if EMUL_TIME_STATS
520 <        interrupt_count++;
520 >        ppc_interrupt_count++;
521          const clock_t interrupt_start = clock();
522   #endif
523  
524 < #if !MULTICORE_CPU
524 > #if SAFE_INTERRUPT_PPC
525 >        static int depth = 0;
526 >        if (depth != 0)
527 >                printf("FATAL: sheepshaver_cpu::interrupt() called more than once: %d\n", depth);
528 >        depth++;
529 > #endif
530 >
531          // Save program counters and branch registers
532          uint32 saved_pc = pc();
533          uint32 saved_lr = lr();
534          uint32 saved_ctr= ctr();
535          uint32 saved_sp = gpr(1);
406 #endif
536  
537          // Initialize stack pointer to SheepShaver alternate stack base
538          gpr(1) = SignalStackBase() - 64;
# Line 443 | Line 572 | void sheepshaver_cpu::interrupt(uint32 e
572          // Enter nanokernel
573          execute(entry);
574  
446 #if !MULTICORE_CPU
575          // Restore program counters and branch registers
576          pc() = saved_pc;
577          lr() = saved_lr;
578          ctr()= saved_ctr;
579          gpr(1) = saved_sp;
452 #endif
580  
581   #if EMUL_TIME_STATS
582          interrupt_time += (clock() - interrupt_start);
583   #endif
584 +
585 + #if SAFE_INTERRUPT_PPC
586 +        depth--;
587 + #endif
588   }
589  
590   // Execute 68k routine
# Line 647 | Line 778 | inline void sheepshaver_cpu::get_resourc
778   *              SheepShaver CPU engine interface
779   **/
780  
781 < static sheepshaver_cpu *main_cpu = NULL;                // CPU emulator to handle usual control flow
782 < static sheepshaver_cpu *interrupt_cpu = NULL;   // CPU emulator to handle interrupts
652 < static sheepshaver_cpu *current_cpu = NULL;             // Current CPU emulator context
781 > // PowerPC CPU emulator
782 > static sheepshaver_cpu *ppc_cpu = NULL;
783  
784   void FlushCodeCache(uintptr start, uintptr end)
785   {
786          D(bug("FlushCodeCache(%08x, %08x)\n", start, end));
787 <        main_cpu->invalidate_cache_range(start, end);
658 < #if MULTICORE_CPU
659 <        interrupt_cpu->invalidate_cache_range(start, end);
660 < #endif
661 < }
662 <
663 < static inline void cpu_push(sheepshaver_cpu *new_cpu)
664 < {
665 < #if MULTICORE_CPU
666 <        current_cpu = new_cpu;
667 < #endif
668 < }
669 <
670 < static inline void cpu_pop()
671 < {
672 < #if MULTICORE_CPU
673 <        current_cpu = main_cpu;
674 < #endif
787 >        ppc_cpu->invalidate_cache_range(start, end);
788   }
789  
790   // Dump PPC registers
791   static void dump_registers(void)
792   {
793 <        current_cpu->dump_registers();
793 >        ppc_cpu->dump_registers();
794   }
795  
796   // Dump log
797   static void dump_log(void)
798   {
799 <        current_cpu->dump_log();
799 >        ppc_cpu->dump_log();
800   }
801  
802   /*
803   *  Initialize CPU emulation
804   */
805  
806 < static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
806 > sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
807   {
808   #if ENABLE_VOSF
809          // Handle screen fault
# Line 702 | Line 815 | static sigsegv_return_t sigsegv_handler(
815          const uintptr addr = (uintptr)fault_address;
816   #if HAVE_SIGSEGV_SKIP_INSTRUCTION
817          // Ignore writes to ROM
818 <        if ((addr - ROM_BASE) < ROM_SIZE)
818 >        if ((addr - (uintptr)ROMBaseHost) < ROM_SIZE)
819                  return SIGSEGV_RETURN_SKIP_INSTRUCTION;
820  
821          // Get program counter of target CPU
822 <        sheepshaver_cpu * const cpu = current_cpu;
822 >        sheepshaver_cpu * const cpu = ppc_cpu;
823          const uint32 pc = cpu->pc();
824          
825          // Fault in Mac ROM or RAM?
826 <        bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize));
826 >        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));
827          if (mac_fault) {
828  
829                  // "VM settings" during MacOS 8 installation
# Line 730 | Line 843 | static sigsegv_return_t sigsegv_handler(
843                          return SIGSEGV_RETURN_SKIP_INSTRUCTION;
844                  else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
845                          return SIGSEGV_RETURN_SKIP_INSTRUCTION;
846 +        
847 +                // MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM)
848 +                else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(16) == 0xf3012002 || cpu->gpr(16) == 0xf3012000))
849 +                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
850 +                else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
851 +                        return SIGSEGV_RETURN_SKIP_INSTRUCTION;
852  
853                  // Ignore writes to the zero page
854                  else if ((uint32)(addr - SheepMem::ZeroPage()) < (uint32)SheepMem::PageSize())
# Line 746 | Line 865 | static sigsegv_return_t sigsegv_handler(
865          printf("SIGSEGV\n");
866          printf("  pc %p\n", fault_instruction);
867          printf("  ea %p\n", fault_address);
749        printf(" cpu %s\n", current_cpu == main_cpu ? "main" : "interrupts");
868          dump_registers();
869 <        current_cpu->dump_log();
869 >        ppc_cpu->dump_log();
870          enter_mon();
871          QuitEmulator();
872  
# Line 757 | Line 875 | static sigsegv_return_t sigsegv_handler(
875  
876   void init_emul_ppc(void)
877   {
878 +        // Get pointer to KernelData in host address space
879 +        kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
880 +
881          // Initialize main CPU emulator
882 <        main_cpu = new sheepshaver_cpu();
883 <        main_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
884 <        main_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000));
882 >        ppc_cpu = new sheepshaver_cpu();
883 >        ppc_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
884 >        ppc_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000));
885          WriteMacInt32(XLM_RUN_MODE, MODE_68K);
886  
766 #if MULTICORE_CPU
767        // Initialize alternate CPU emulator to handle interrupts
768        interrupt_cpu = new sheepshaver_cpu();
769 #endif
770
771        // Install the handler for SIGSEGV
772        sigsegv_install_handler(sigsegv_handler);
773
887   #if ENABLE_MON
888          // Install "regs" command in cxmon
889          mon_add_command("regs", dump_registers, "regs                     Dump PowerPC registers\n");
# Line 796 | Line 909 | void exit_emul_ppc(void)
909          printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC));
910          printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count,
911                     (double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time));
912 +        printf("Total ppc interrupt count: %d (%2.1f %%)\n", ppc_interrupt_count,
913 +                   (double(ppc_interrupt_count) * 100.0) / double(interrupt_count));
914  
915   #define PRINT_STATS(LABEL, VAR_PREFIX) do {                                                             \
916                  printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count);             \
# Line 812 | Line 927 | void exit_emul_ppc(void)
927          printf("\n");
928   #endif
929  
930 <        delete main_cpu;
931 < #if MULTICORE_CPU
932 <        delete interrupt_cpu;
933 < #endif
930 >        delete ppc_cpu;
931 > }
932 >
933 > #if PPC_ENABLE_JIT && PPC_REENTRANT_JIT
934 > // Initialize EmulOp trampolines
935 > void init_emul_op_trampolines(basic_dyngen & dg)
936 > {
937 >        typedef void (*func_t)(dyngen_cpu_base, uint32);
938 >        func_t func;
939 >
940 >        // EmulOp
941 >        emul_op_trampoline = dg.gen_start();
942 >        func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
943 >        dg.gen_invoke_CPU_T0(func);
944 >        dg.gen_exec_return();
945 >        dg.gen_end();
946 >
947 >        // NativeOp
948 >        native_op_trampoline = dg.gen_start();
949 >        func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr();
950 >        dg.gen_invoke_CPU_T0(func);    
951 >        dg.gen_exec_return();
952 >        dg.gen_end();
953 >
954 >        D(bug("EmulOp trampoline:   %p\n", emul_op_trampoline));
955 >        D(bug("NativeOp trampoline: %p\n", native_op_trampoline));
956   }
957 + #endif
958  
959   /*
960   *  Emulation loop
# Line 824 | Line 962 | void exit_emul_ppc(void)
962  
963   void emul_ppc(uint32 entry)
964   {
827        current_cpu = main_cpu;
965   #if 0
966 <        current_cpu->start_log();
966 >        ppc_cpu->start_log();
967   #endif
968          // start emulation loop and enable code translation or caching
969 <        current_cpu->execute(entry);
969 >        ppc_cpu->execute(entry);
970   }
971  
972   /*
973   *  Handle PowerPC interrupt
974   */
975  
839 #if ASYNC_IRQ
840 void HandleInterrupt(void)
841 {
842        main_cpu->handle_interrupt();
843 }
844 #else
976   void TriggerInterrupt(void)
977   {
978   #if 0
979    WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
980   #else
981    // Trigger interrupt to main cpu only
982 <  if (main_cpu)
983 <          main_cpu->trigger_interrupt();
982 >  if (ppc_cpu)
983 >          ppc_cpu->trigger_interrupt();
984   #endif
985   }
855 #endif
986  
987   void sheepshaver_cpu::handle_interrupt(void)
988   {
989 + #ifdef USE_SDL_VIDEO
990 +        // We must fill in the events queue in the same thread that did call SDL_SetVideoMode()
991 +        SDL_PumpEvents();
992 + #endif
993 +
994          // Do nothing if interrupts are disabled
995 <        if (*(int32 *)XLM_IRQ_NEST > 0)
995 >        if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0)
996                  return;
997  
998 <        // Do nothing if there is no interrupt pending
999 <        if (InterruptFlags == 0)
1000 <                return;
998 >        // Current interrupt nest level
999 >        static int interrupt_depth = 0;
1000 >        ++interrupt_depth;
1001 > #if EMUL_TIME_STATS
1002 >        interrupt_count++;
1003 > #endif
1004  
1005          // Disable MacOS stack sniffer
1006          WriteMacInt32(0x110, 0);
# Line 871 | Line 1009 | void sheepshaver_cpu::handle_interrupt(v
1009          switch (ReadMacInt32(XLM_RUN_MODE)) {
1010          case MODE_68K:
1011                  // 68k emulator active, trigger 68k interrupt level 1
874                assert(current_cpu == main_cpu);
1012                  WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
1013                  set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
1014                  break;
# Line 879 | Line 1016 | void sheepshaver_cpu::handle_interrupt(v
1016   #if INTERRUPTS_IN_NATIVE_MODE
1017          case MODE_NATIVE:
1018                  // 68k emulator inactive, in nanokernel?
1019 <                assert(current_cpu == main_cpu);
1020 <                if (gpr(1) != KernelDataAddr) {
1019 >                if (gpr(1) != KernelDataAddr && interrupt_depth == 1) {
1020 >                        interrupt_context ctx(this, "PowerPC mode");
1021 >
1022                          // Prepare for 68k interrupt level 1
1023                          WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
1024                          WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
# Line 889 | Line 1027 | void sheepshaver_cpu::handle_interrupt(v
1027        
1028                          // Execute nanokernel interrupt routine (this will activate the 68k emulator)
1029                          DisableInterrupt();
892                        cpu_push(interrupt_cpu);
1030                          if (ROMType == ROMTYPE_NEWWORLD)
1031 <                                current_cpu->interrupt(ROM_BASE + 0x312b1c);
1031 >                                ppc_cpu->interrupt(ROM_BASE + 0x312b1c);
1032                          else
1033 <                                current_cpu->interrupt(ROM_BASE + 0x312a3c);
897 <                        cpu_pop();
1033 >                                ppc_cpu->interrupt(ROM_BASE + 0x312a3c);
1034                  }
1035                  break;
1036   #endif
# Line 903 | Line 1039 | void sheepshaver_cpu::handle_interrupt(v
1039          case MODE_EMUL_OP:
1040                  // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
1041                  if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
1042 +                        interrupt_context ctx(this, "68k mode");
1043 + #if EMUL_TIME_STATS
1044 +                        const clock_t interrupt_start = clock();
1045 + #endif
1046   #if 1
1047                          // Execute full 68k interrupt routine
1048                          M68kRegisters r;
1049                          uint32 old_r25 = ReadMacInt32(XLM_68K_R25);     // Save interrupt level
1050                          WriteMacInt32(XLM_68K_R25, 0x21);                       // Execute with interrupt level 1
1051 <                        static const uint8 proc[] = {
1051 >                        static const uint8 proc_template[] = {
1052                                  0x3f, 0x3c, 0x00, 0x00,                 // move.w       #$0000,-(sp)    (fake format word)
1053                                  0x48, 0x7a, 0x00, 0x0a,                 // pea          @1(pc)                  (return address)
1054                                  0x40, 0xe7,                                             // move         sr,-(sp)                (saved SR)
# Line 916 | Line 1056 | void sheepshaver_cpu::handle_interrupt(v
1056                                  0x4e, 0xd0,                                             // jmp          (a0)
1057                                  M68K_RTS >> 8, M68K_RTS & 0xff  // @1
1058                          };
1059 <                        Execute68k((uint32)proc, &r);
1059 >                        BUILD_SHEEPSHAVER_PROCEDURE(proc);
1060 >                        Execute68k(proc, &r);
1061                          WriteMacInt32(XLM_68K_R25, old_r25);            // Restore interrupt level
1062   #else
1063                          // Only update cursor
# Line 928 | Line 1069 | void sheepshaver_cpu::handle_interrupt(v
1069                                  }
1070                          }
1071   #endif
1072 + #if EMUL_TIME_STATS
1073 +                        interrupt_time += (clock() - interrupt_start);
1074 + #endif
1075                  }
1076                  break;
1077   #endif
1078          }
1079 +
1080 +        // We are done with this interrupt
1081 +        --interrupt_depth;
1082   }
1083  
1084   static void get_resource(void);
# Line 940 | Line 1087 | static void get_ind_resource(void);
1087   static void get_1_ind_resource(void);
1088   static void r_get_resource(void);
1089  
1090 < #define GPR(REG) current_cpu->gpr(REG)
1091 <
945 < static void NativeOp(int selector)
1090 > // Execute NATIVE_OP routine
1091 > void sheepshaver_cpu::execute_native_op(uint32 selector)
1092   {
1093   #if EMUL_TIME_STATS
1094          native_exec_count++;
# Line 960 | Line 1106 | static void NativeOp(int selector)
1106                  VideoVBL();
1107                  break;
1108          case NATIVE_VIDEO_DO_DRIVER_IO:
1109 <                GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4),
964 <                                                                                           (void *)GPR(5), GPR(6), GPR(7));
1109 >                gpr(3) = (int32)(int16)VideoDoDriverIO(gpr(3), gpr(4), gpr(5), gpr(6), gpr(7));
1110                  break;
1111   #ifdef WORDS_BIGENDIAN
1112          case NATIVE_ETHER_IRQ:
1113                  EtherIRQ();
1114                  break;
1115          case NATIVE_ETHER_INIT:
1116 <                GPR(3) = InitStreamModule((void *)GPR(3));
1116 >                gpr(3) = InitStreamModule((void *)gpr(3));
1117                  break;
1118          case NATIVE_ETHER_TERM:
1119                  TerminateStreamModule();
1120                  break;
1121          case NATIVE_ETHER_OPEN:
1122 <                GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7));
1122 >                gpr(3) = ether_open((queue_t *)gpr(3), (void *)gpr(4), gpr(5), gpr(6), (void*)gpr(7));
1123                  break;
1124          case NATIVE_ETHER_CLOSE:
1125 <                GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5));
1125 >                gpr(3) = ether_close((queue_t *)gpr(3), gpr(4), (void *)gpr(5));
1126                  break;
1127          case NATIVE_ETHER_WPUT:
1128 <                GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4));
1128 >                gpr(3) = ether_wput((queue_t *)gpr(3), (mblk_t *)gpr(4));
1129                  break;
1130          case NATIVE_ETHER_RSRV:
1131 <                GPR(3) = ether_rsrv((queue_t *)GPR(3));
1131 >                gpr(3) = ether_rsrv((queue_t *)gpr(3));
1132                  break;
1133 + #else
1134 +        case NATIVE_ETHER_INIT:
1135 +                // FIXME: needs more complicated thunks
1136 +                gpr(3) = false;
1137 +                break;
1138 + #endif
1139          case NATIVE_SYNC_HOOK:
1140 <                GPR(3) = NQD_sync_hook(GPR(3));
1140 >                gpr(3) = NQD_sync_hook(gpr(3));
1141                  break;
1142          case NATIVE_BITBLT_HOOK:
1143 <                GPR(3) = NQD_bitblt_hook(GPR(3));
1143 >                gpr(3) = NQD_bitblt_hook(gpr(3));
1144                  break;
1145          case NATIVE_BITBLT:
1146 <                NQD_bitblt(GPR(3));
1146 >                NQD_bitblt(gpr(3));
1147                  break;
1148          case NATIVE_FILLRECT_HOOK:
1149 <                GPR(3) = NQD_fillrect_hook(GPR(3));
1149 >                gpr(3) = NQD_fillrect_hook(gpr(3));
1150                  break;
1151          case NATIVE_INVRECT:
1152 <                NQD_invrect(GPR(3));
1152 >                NQD_invrect(gpr(3));
1153                  break;
1154          case NATIVE_FILLRECT:
1155 <                NQD_fillrect(GPR(3));
1005 <                break;
1006 < #else
1007 <        case NATIVE_ETHER_INIT:
1008 <                // FIXME: needs more complicated thunks
1009 <                GPR(3) = false;
1155 >                NQD_fillrect(gpr(3));
1156                  break;
1011 #endif
1157          case NATIVE_SERIAL_NOTHING:
1158          case NATIVE_SERIAL_OPEN:
1159          case NATIVE_SERIAL_PRIME_IN:
# Line 1026 | Line 1171 | static void NativeOp(int selector)
1171                          SerialStatus,
1172                          SerialClose
1173                  };
1174 <                GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4));
1174 >                gpr(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](gpr(3), gpr(4));
1175                  break;
1176          }
1177          case NATIVE_GET_RESOURCE:
# Line 1036 | Line 1181 | static void NativeOp(int selector)
1181          case NATIVE_R_GET_RESOURCE: {
1182                  typedef void (*GetResourceCallback)(void);
1183                  static const GetResourceCallback get_resource_callbacks[] = {
1184 <                        get_resource,
1185 <                        get_1_resource,
1186 <                        get_ind_resource,
1187 <                        get_1_ind_resource,
1188 <                        r_get_resource
1184 >                        ::get_resource,
1185 >                        ::get_1_resource,
1186 >                        ::get_ind_resource,
1187 >                        ::get_1_ind_resource,
1188 >                        ::r_get_resource
1189                  };
1190                  get_resource_callbacks[selector - NATIVE_GET_RESOURCE]();
1191                  break;
1192          }
1048        case NATIVE_DISABLE_INTERRUPT:
1049                DisableInterrupt();
1050                break;
1051        case NATIVE_ENABLE_INTERRUPT:
1052                EnableInterrupt();
1053                break;
1193          case NATIVE_MAKE_EXECUTABLE:
1194 <                MakeExecutable(0, (void *)GPR(4), GPR(5));
1194 >                MakeExecutable(0, gpr(4), gpr(5));
1195                  break;
1196          case NATIVE_CHECK_LOAD_INVOC:
1197 <                check_load_invoc(GPR(3), GPR(4), GPR(5));
1197 >                check_load_invoc(gpr(3), gpr(4), gpr(5));
1198                  break;
1199          default:
1200                  printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
# Line 1076 | Line 1215 | static void NativeOp(int selector)
1215  
1216   void Execute68k(uint32 pc, M68kRegisters *r)
1217   {
1218 <        current_cpu->execute_68k(pc, r);
1218 >        ppc_cpu->execute_68k(pc, r);
1219   }
1220  
1221   /*
# Line 1099 | Line 1238 | void Execute68kTrap(uint16 trap, M68kReg
1238  
1239   uint32 call_macos(uint32 tvect)
1240   {
1241 <        return current_cpu->execute_macos_code(tvect, 0, NULL);
1241 >        return ppc_cpu->execute_macos_code(tvect, 0, NULL);
1242   }
1243  
1244   uint32 call_macos1(uint32 tvect, uint32 arg1)
1245   {
1246          const uint32 args[] = { arg1 };
1247 <        return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1247 >        return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1248   }
1249  
1250   uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2)
1251   {
1252          const uint32 args[] = { arg1, arg2 };
1253 <        return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1253 >        return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1254   }
1255  
1256   uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3)
1257   {
1258          const uint32 args[] = { arg1, arg2, arg3 };
1259 <        return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1259 >        return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1260   }
1261  
1262   uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4)
1263   {
1264          const uint32 args[] = { arg1, arg2, arg3, arg4 };
1265 <        return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1265 >        return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1266   }
1267  
1268   uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5)
1269   {
1270          const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 };
1271 <        return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1271 >        return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1272   }
1273  
1274   uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6)
1275   {
1276          const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
1277 <        return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1277 >        return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1278   }
1279  
1280   uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7)
1281   {
1282          const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
1283 <        return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1283 >        return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
1284   }
1285  
1286   /*
# Line 1150 | Line 1289 | uint32 call_macos7(uint32 tvect, uint32
1289  
1290   void get_resource(void)
1291   {
1292 <        current_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
1292 >        ppc_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
1293   }
1294  
1295   void get_1_resource(void)
1296   {
1297 <        current_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
1297 >        ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
1298   }
1299  
1300   void get_ind_resource(void)
1301   {
1302 <        current_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
1302 >        ppc_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
1303   }
1304  
1305   void get_1_ind_resource(void)
1306   {
1307 <        current_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
1307 >        ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
1308   }
1309  
1310   void r_get_resource(void)
1311   {
1312 <        current_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
1312 >        ppc_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
1313   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines