ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.23
Committed: 2003-12-05T13:37:56Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.22: +4 -2 lines
Log Message:
Use an alternate stack base while servicing PowerPC interrupts.

File Contents

# Content
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 "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"
31 #include "cpu/ppc/ppc-cpu.hpp"
32 #include "cpu/ppc/ppc-operations.hpp"
33 #include "cpu/ppc/ppc-instructions.hpp"
34 #include "thunks.h"
35
36 // Used for NativeOp trampolines
37 #include "video.h"
38 #include "name_registry.h"
39 #include "serial.h"
40 #include "ether.h"
41
42 #include <stdio.h>
43
44 #if ENABLE_MON
45 #include "mon.h"
46 #include "mon_disass.h"
47 #endif
48
49 #define DEBUG 0
50 #include "debug.h"
51
52 // Emulation time statistics
53 #define EMUL_TIME_STATS 1
54
55 #if EMUL_TIME_STATS
56 static clock_t emul_start_time;
57 static uint32 interrupt_count = 0;
58 static clock_t interrupt_time = 0;
59 static uint32 exec68k_count = 0;
60 static clock_t exec68k_time = 0;
61 static uint32 native_exec_count = 0;
62 static clock_t native_exec_time = 0;
63 static uint32 macos_exec_count = 0;
64 static clock_t macos_exec_time = 0;
65 #endif
66
67 static void enter_mon(void)
68 {
69 // Start up mon in real-mode
70 #if ENABLE_MON
71 char *arg[4] = {"mon", "-m", "-r", NULL};
72 mon(3, arg);
73 #endif
74 }
75
76 // From main_*.cpp
77 extern uintptr SignalStackBase();
78
79 // PowerPC EmulOp to exit from emulation looop
80 const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1;
81
82 // Enable multicore (main/interrupts) cpu emulation?
83 #define MULTICORE_CPU (ASYNC_IRQ ? 1 : 0)
84
85 // Enable Execute68k() safety checks?
86 #define SAFE_EXEC_68K 1
87
88 // Save FP state in Execute68k()?
89 #define SAVE_FP_EXEC_68K 1
90
91 // Interrupts in EMUL_OP mode?
92 #define INTERRUPTS_IN_EMUL_OP_MODE 1
93
94 // Interrupts in native mode?
95 #define INTERRUPTS_IN_NATIVE_MODE 1
96
97 // Pointer to Kernel Data
98 static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE;
99
100 // SIGSEGV handler
101 static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
102
103 // JIT Compiler enabled?
104 static inline bool enable_jit_p()
105 {
106 return PrefsFindBool("jit");
107 }
108
109
110 /**
111 * PowerPC emulator glue with special 'sheep' opcodes
112 **/
113
114 enum {
115 PPC_I(SHEEP) = PPC_I(MAX),
116 PPC_I(SHEEP_MAX)
117 };
118
119 class sheepshaver_cpu
120 : public powerpc_cpu
121 {
122 void init_decoder();
123 void execute_sheep(uint32 opcode);
124
125 public:
126
127 // Constructor
128 sheepshaver_cpu();
129
130 // Condition Register accessors
131 uint32 get_cr() const { return cr().get(); }
132 void set_cr(uint32 v) { cr().set(v); }
133
134 // Execute 68k routine
135 void execute_68k(uint32 entry, M68kRegisters *r);
136
137 // Execute ppc routine
138 void execute_ppc(uint32 entry);
139
140 // Execute MacOS/PPC code
141 uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args);
142
143 // Resource manager thunk
144 void get_resource(uint32 old_get_resource);
145
146 // Handle MacOS interrupt
147 void interrupt(uint32 entry);
148 void handle_interrupt();
149
150 // Lazy memory allocator (one item at a time)
151 void *operator new(size_t size)
152 { return allocator_helper< sheepshaver_cpu, lazy_allocator >::allocate(); }
153 void operator delete(void *p)
154 { allocator_helper< sheepshaver_cpu, lazy_allocator >::deallocate(p); }
155 // FIXME: really make surre array allocation fail at link time?
156 void *operator new[](size_t);
157 void operator delete[](void *p);
158
159 // Make sure the SIGSEGV handler can access CPU registers
160 friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
161 };
162
163 lazy_allocator< sheepshaver_cpu > allocator_helper< sheepshaver_cpu, lazy_allocator >::allocator;
164
165 sheepshaver_cpu::sheepshaver_cpu()
166 : powerpc_cpu(enable_jit_p())
167 {
168 init_decoder();
169 }
170
171 void sheepshaver_cpu::init_decoder()
172 {
173 #ifndef PPC_NO_STATIC_II_INDEX_TABLE
174 static bool initialized = false;
175 if (initialized)
176 return;
177 initialized = true;
178 #endif
179
180 static const instr_info_t sheep_ii_table[] = {
181 { "sheep",
182 (execute_pmf)&sheepshaver_cpu::execute_sheep,
183 NULL,
184 PPC_I(SHEEP),
185 D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP
186 }
187 };
188
189 const int ii_count = sizeof(sheep_ii_table)/sizeof(sheep_ii_table[0]);
190 D(bug("SheepShaver extra decode table has %d entries\n", ii_count));
191
192 for (int i = 0; i < ii_count; i++) {
193 const instr_info_t * ii = &sheep_ii_table[i];
194 init_decoder_entry(ii);
195 }
196 }
197
198 // Forward declaration for native opcode handler
199 static void NativeOp(int selector);
200
201 /* NativeOp instruction format:
202 +------------+--------------------------+--+----------+------------+
203 | 6 | |FN| OP | 2 |
204 +------------+--------------------------+--+----------+------------+
205 0 5 |6 19 20 21 25 26 31
206 */
207
208 typedef bit_field< 20, 20 > FN_field;
209 typedef bit_field< 21, 25 > NATIVE_OP_field;
210 typedef bit_field< 26, 31 > EMUL_OP_field;
211
212 // Execute SheepShaver instruction
213 void sheepshaver_cpu::execute_sheep(uint32 opcode)
214 {
215 // D(bug("Extended opcode %08x at %08x (68k pc %08x)\n", opcode, pc(), gpr(24)));
216 assert((((opcode >> 26) & 0x3f) == 6) && OP_MAX <= 64 + 3);
217
218 switch (opcode & 0x3f) {
219 case 0: // EMUL_RETURN
220 QuitEmulator();
221 break;
222
223 case 1: // EXEC_RETURN
224 spcflags().set(SPCFLAG_CPU_EXEC_RETURN);
225 break;
226
227 case 2: // EXEC_NATIVE
228 NativeOp(NATIVE_OP_field::extract(opcode));
229 if (FN_field::test(opcode))
230 pc() = lr();
231 else
232 pc() += 4;
233 break;
234
235 default: { // EMUL_OP
236 M68kRegisters r68;
237 WriteMacInt32(XLM_68K_R25, gpr(25));
238 WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
239 for (int i = 0; i < 8; i++)
240 r68.d[i] = gpr(8 + i);
241 for (int i = 0; i < 7; i++)
242 r68.a[i] = gpr(16 + i);
243 r68.a[7] = gpr(1);
244 EmulOp(&r68, gpr(24), EMUL_OP_field::extract(opcode) - 3);
245 for (int i = 0; i < 8; i++)
246 gpr(8 + i) = r68.d[i];
247 for (int i = 0; i < 7; i++)
248 gpr(16 + i) = r68.a[i];
249 gpr(1) = r68.a[7];
250 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
251 pc() += 4;
252 break;
253 }
254 }
255 }
256
257 // Handle MacOS interrupt
258 void sheepshaver_cpu::interrupt(uint32 entry)
259 {
260 #if EMUL_TIME_STATS
261 interrupt_count++;
262 const clock_t interrupt_start = clock();
263 #endif
264
265 #if !MULTICORE_CPU
266 // Save program counters and branch registers
267 uint32 saved_pc = pc();
268 uint32 saved_lr = lr();
269 uint32 saved_ctr= ctr();
270 uint32 saved_sp = gpr(1);
271 #endif
272
273 // Initialize stack pointer to SheepShaver alternate stack base
274 gpr(1) = SignalStackBase() - 64;
275
276 // Build trampoline to return from interrupt
277 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
278
279 // Prepare registers for nanokernel interrupt routine
280 kernel_data->v[0x004 >> 2] = htonl(gpr(1));
281 kernel_data->v[0x018 >> 2] = htonl(gpr(6));
282
283 gpr(6) = ntohl(kernel_data->v[0x65c >> 2]);
284 assert(gpr(6) != 0);
285 WriteMacInt32(gpr(6) + 0x13c, gpr(7));
286 WriteMacInt32(gpr(6) + 0x144, gpr(8));
287 WriteMacInt32(gpr(6) + 0x14c, gpr(9));
288 WriteMacInt32(gpr(6) + 0x154, gpr(10));
289 WriteMacInt32(gpr(6) + 0x15c, gpr(11));
290 WriteMacInt32(gpr(6) + 0x164, gpr(12));
291 WriteMacInt32(gpr(6) + 0x16c, gpr(13));
292
293 gpr(1) = KernelDataAddr;
294 gpr(7) = ntohl(kernel_data->v[0x660 >> 2]);
295 gpr(8) = 0;
296 gpr(10) = trampoline.addr();
297 gpr(12) = trampoline.addr();
298 gpr(13) = get_cr();
299
300 // rlwimi. r7,r7,8,0,0
301 uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7));
302 record_cr0(result);
303 gpr(7) = result;
304
305 gpr(11) = 0xf072; // MSR (SRR1)
306 cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000));
307
308 // Enter nanokernel
309 execute(entry);
310
311 #if !MULTICORE_CPU
312 // Restore program counters and branch registers
313 pc() = saved_pc;
314 lr() = saved_lr;
315 ctr()= saved_ctr;
316 gpr(1) = saved_sp;
317 #endif
318
319 #if EMUL_TIME_STATS
320 interrupt_time += (clock() - interrupt_start);
321 #endif
322 }
323
324 // Execute 68k routine
325 void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r)
326 {
327 #if EMUL_TIME_STATS
328 exec68k_count++;
329 const clock_t exec68k_start = clock();
330 #endif
331
332 #if SAFE_EXEC_68K
333 if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
334 printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
335 #endif
336
337 // Save program counters and branch registers
338 uint32 saved_pc = pc();
339 uint32 saved_lr = lr();
340 uint32 saved_ctr= ctr();
341 uint32 saved_cr = get_cr();
342
343 // Create MacOS stack frame
344 // FIXME: make sure MacOS doesn't expect PPC registers to live on top
345 uint32 sp = gpr(1);
346 gpr(1) -= 56;
347 WriteMacInt32(gpr(1), sp);
348
349 // Save PowerPC registers
350 uint32 saved_GPRs[19];
351 memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13));
352 #if SAVE_FP_EXEC_68K
353 double saved_FPRs[18];
354 memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14));
355 #endif
356
357 // Setup registers for 68k emulator
358 cr().set(CR_SO_field<2>::mask()); // Supervisor mode
359 for (int i = 0; i < 8; i++) // d[0]..d[7]
360 gpr(8 + i) = r->d[i];
361 for (int i = 0; i < 7; i++) // a[0]..a[6]
362 gpr(16 + i) = r->a[i];
363 gpr(23) = 0;
364 gpr(24) = entry;
365 gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR
366 gpr(26) = 0;
367 gpr(28) = 0; // VBR
368 gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]); // Pointer to opcode table
369 gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]); // Address of emulator
370 gpr(31) = KernelDataAddr + 0x1000;
371
372 // Push return address (points to EXEC_RETURN opcode) on stack
373 gpr(1) -= 4;
374 WriteMacInt32(gpr(1), XLM_EXEC_RETURN_OPCODE);
375
376 // Rentering 68k emulator
377 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
378
379 // Set r0 to 0 for 68k emulator
380 gpr(0) = 0;
381
382 // Execute 68k opcode
383 uint32 opcode = ReadMacInt16(gpr(24));
384 gpr(27) = (int32)(int16)ReadMacInt16(gpr(24) += 2);
385 gpr(29) += opcode * 8;
386 execute(gpr(29));
387
388 // Save r25 (contains current 68k interrupt level)
389 WriteMacInt32(XLM_68K_R25, gpr(25));
390
391 // Reentering EMUL_OP mode
392 WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
393
394 // Save 68k registers
395 for (int i = 0; i < 8; i++) // d[0]..d[7]
396 r->d[i] = gpr(8 + i);
397 for (int i = 0; i < 7; i++) // a[0]..a[6]
398 r->a[i] = gpr(16 + i);
399
400 // Restore PowerPC registers
401 memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13));
402 #if SAVE_FP_EXEC_68K
403 memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14));
404 #endif
405
406 // Cleanup stack
407 gpr(1) += 56;
408
409 // Restore program counters and branch registers
410 pc() = saved_pc;
411 lr() = saved_lr;
412 ctr()= saved_ctr;
413 set_cr(saved_cr);
414
415 #if EMUL_TIME_STATS
416 exec68k_time += (clock() - exec68k_start);
417 #endif
418 }
419
420 // Call MacOS PPC code
421 uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args)
422 {
423 #if EMUL_TIME_STATS
424 macos_exec_count++;
425 const clock_t macos_exec_start = clock();
426 #endif
427
428 // Save program counters and branch registers
429 uint32 saved_pc = pc();
430 uint32 saved_lr = lr();
431 uint32 saved_ctr= ctr();
432
433 // Build trampoline with EXEC_RETURN
434 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
435 lr() = trampoline.addr();
436
437 gpr(1) -= 64; // Create stack frame
438 uint32 proc = ReadMacInt32(tvect); // Get routine address
439 uint32 toc = ReadMacInt32(tvect + 4); // Get TOC pointer
440
441 // Save PowerPC registers
442 uint32 regs[8];
443 regs[0] = gpr(2);
444 for (int i = 0; i < nargs; i++)
445 regs[i + 1] = gpr(i + 3);
446
447 // Prepare and call MacOS routine
448 gpr(2) = toc;
449 for (int i = 0; i < nargs; i++)
450 gpr(i + 3) = args[i];
451 execute(proc);
452 uint32 retval = gpr(3);
453
454 // Restore PowerPC registers
455 for (int i = 0; i <= nargs; i++)
456 gpr(i + 2) = regs[i];
457
458 // Cleanup stack
459 gpr(1) += 64;
460
461 // Restore program counters and branch registers
462 pc() = saved_pc;
463 lr() = saved_lr;
464 ctr()= saved_ctr;
465
466 #if EMUL_TIME_STATS
467 macos_exec_time += (clock() - macos_exec_start);
468 #endif
469
470 return retval;
471 }
472
473 // Execute ppc routine
474 inline void sheepshaver_cpu::execute_ppc(uint32 entry)
475 {
476 // Save branch registers
477 uint32 saved_lr = lr();
478
479 SheepVar32 trampoline = POWERPC_EXEC_RETURN;
480 WriteMacInt32(trampoline.addr(), POWERPC_EXEC_RETURN);
481 lr() = trampoline.addr();
482
483 execute(entry);
484
485 // Restore branch registers
486 lr() = saved_lr;
487 }
488
489 // Resource Manager thunk
490 extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
491
492 inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
493 {
494 uint32 type = gpr(3);
495 int16 id = gpr(4);
496
497 // Create stack frame
498 gpr(1) -= 56;
499
500 // Call old routine
501 execute_ppc(old_get_resource);
502
503 // Call CheckLoad()
504 uint32 handle = gpr(3);
505 check_load_invoc(type, id, handle);
506 gpr(3) = handle;
507
508 // Cleanup stack
509 gpr(1) += 56;
510 }
511
512
513 /**
514 * SheepShaver CPU engine interface
515 **/
516
517 static sheepshaver_cpu *main_cpu = NULL; // CPU emulator to handle usual control flow
518 static sheepshaver_cpu *interrupt_cpu = NULL; // CPU emulator to handle interrupts
519 static sheepshaver_cpu *current_cpu = NULL; // Current CPU emulator context
520
521 void FlushCodeCache(uintptr start, uintptr end)
522 {
523 D(bug("FlushCodeCache(%08x, %08x)\n", start, end));
524 main_cpu->invalidate_cache_range(start, end);
525 #if MULTICORE_CPU
526 interrupt_cpu->invalidate_cache_range(start, end);
527 #endif
528 }
529
530 static inline void cpu_push(sheepshaver_cpu *new_cpu)
531 {
532 #if MULTICORE_CPU
533 current_cpu = new_cpu;
534 #endif
535 }
536
537 static inline void cpu_pop()
538 {
539 #if MULTICORE_CPU
540 current_cpu = main_cpu;
541 #endif
542 }
543
544 // Dump PPC registers
545 static void dump_registers(void)
546 {
547 current_cpu->dump_registers();
548 }
549
550 // Dump log
551 static void dump_log(void)
552 {
553 current_cpu->dump_log();
554 }
555
556 /*
557 * Initialize CPU emulation
558 */
559
560 static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
561 {
562 #if ENABLE_VOSF
563 // Handle screen fault
564 extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t);
565 if (Screen_fault_handler(fault_address, fault_instruction))
566 return SIGSEGV_RETURN_SUCCESS;
567 #endif
568
569 const uintptr addr = (uintptr)fault_address;
570 #if HAVE_SIGSEGV_SKIP_INSTRUCTION
571 // Ignore writes to ROM
572 if ((addr - ROM_BASE) < ROM_SIZE)
573 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
574
575 // Get program counter of target CPU
576 sheepshaver_cpu * const cpu = current_cpu;
577 const uint32 pc = cpu->pc();
578
579 // Fault in Mac ROM or RAM?
580 bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize));
581 if (mac_fault) {
582
583 // "VM settings" during MacOS 8 installation
584 if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000)
585 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
586
587 // MacOS 8.5 installation
588 else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000)
589 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
590
591 // MacOS 8 serial drivers on startup
592 else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000))
593 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
594
595 // MacOS 8.1 serial drivers on startup
596 else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
597 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
598 else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
599 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
600
601 // Ignore all other faults, if requested
602 if (PrefsFindBool("ignoresegv"))
603 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
604 }
605 #else
606 #error "FIXME: You don't have the capability to skip instruction within signal handlers"
607 #endif
608
609 printf("SIGSEGV\n");
610 printf(" pc %p\n", fault_instruction);
611 printf(" ea %p\n", fault_address);
612 printf(" cpu %s\n", current_cpu == main_cpu ? "main" : "interrupts");
613 dump_registers();
614 current_cpu->dump_log();
615 enter_mon();
616 QuitEmulator();
617
618 return SIGSEGV_RETURN_FAILURE;
619 }
620
621 void init_emul_ppc(void)
622 {
623 // Initialize main CPU emulator
624 main_cpu = new sheepshaver_cpu();
625 main_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
626 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
627
628 #if MULTICORE_CPU
629 // Initialize alternate CPU emulator to handle interrupts
630 interrupt_cpu = new sheepshaver_cpu();
631 #endif
632
633 // Install the handler for SIGSEGV
634 sigsegv_install_handler(sigsegv_handler);
635
636 #if ENABLE_MON
637 // Install "regs" command in cxmon
638 mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n");
639 mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n");
640 #endif
641
642 #if EMUL_TIME_STATS
643 emul_start_time = clock();
644 #endif
645 }
646
647 /*
648 * Deinitialize emulation
649 */
650
651 void exit_emul_ppc(void)
652 {
653 #if EMUL_TIME_STATS
654 clock_t emul_end_time = clock();
655
656 printf("### Statistics for SheepShaver emulation parts\n");
657 const clock_t emul_time = emul_end_time - emul_start_time;
658 printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC));
659 printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count,
660 (double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time));
661
662 #define PRINT_STATS(LABEL, VAR_PREFIX) do { \
663 printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \
664 printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \
665 double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \
666 100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \
667 } while (0)
668
669 PRINT_STATS("Execute68k[Trap] execution", exec68k);
670 PRINT_STATS("NativeOp execution", native_exec);
671 PRINT_STATS("MacOS routine execution", macos_exec);
672
673 #undef PRINT_STATS
674 printf("\n");
675 #endif
676
677 delete main_cpu;
678 #if MULTICORE_CPU
679 delete interrupt_cpu;
680 #endif
681 }
682
683 /*
684 * Emulation loop
685 */
686
687 void emul_ppc(uint32 entry)
688 {
689 current_cpu = main_cpu;
690 #if DEBUG
691 current_cpu->start_log();
692 #endif
693 // start emulation loop and enable code translation or caching
694 current_cpu->execute(entry);
695 }
696
697 /*
698 * Handle PowerPC interrupt
699 */
700
701 #if ASYNC_IRQ
702 void HandleInterrupt(void)
703 {
704 main_cpu->handle_interrupt();
705 }
706 #else
707 void TriggerInterrupt(void)
708 {
709 #if 0
710 WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
711 #else
712 // Trigger interrupt to main cpu only
713 if (main_cpu)
714 main_cpu->trigger_interrupt();
715 #endif
716 }
717 #endif
718
719 void sheepshaver_cpu::handle_interrupt(void)
720 {
721 // Do nothing if interrupts are disabled
722 if (*(int32 *)XLM_IRQ_NEST > 0)
723 return;
724
725 // Do nothing if there is no interrupt pending
726 if (InterruptFlags == 0)
727 return;
728
729 // Disable MacOS stack sniffer
730 WriteMacInt32(0x110, 0);
731
732 // Interrupt action depends on current run mode
733 switch (ReadMacInt32(XLM_RUN_MODE)) {
734 case MODE_68K:
735 // 68k emulator active, trigger 68k interrupt level 1
736 assert(current_cpu == main_cpu);
737 WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
738 set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
739 break;
740
741 #if INTERRUPTS_IN_NATIVE_MODE
742 case MODE_NATIVE:
743 // 68k emulator inactive, in nanokernel?
744 assert(current_cpu == main_cpu);
745 if (gpr(1) != KernelDataAddr) {
746 // Prepare for 68k interrupt level 1
747 WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
748 WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
749 ReadMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc)
750 | tswap32(kernel_data->v[0x674 >> 2]));
751
752 // Execute nanokernel interrupt routine (this will activate the 68k emulator)
753 DisableInterrupt();
754 cpu_push(interrupt_cpu);
755 if (ROMType == ROMTYPE_NEWWORLD)
756 current_cpu->interrupt(ROM_BASE + 0x312b1c);
757 else
758 current_cpu->interrupt(ROM_BASE + 0x312a3c);
759 cpu_pop();
760 }
761 break;
762 #endif
763
764 #if INTERRUPTS_IN_EMUL_OP_MODE
765 case MODE_EMUL_OP:
766 // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
767 if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
768 #if 1
769 // Execute full 68k interrupt routine
770 M68kRegisters r;
771 uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
772 WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
773 static const uint8 proc[] = {
774 0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word)
775 0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address)
776 0x40, 0xe7, // move sr,-(sp) (saved SR)
777 0x20, 0x78, 0x00, 0x064, // move.l $64,a0
778 0x4e, 0xd0, // jmp (a0)
779 M68K_RTS >> 8, M68K_RTS & 0xff // @1
780 };
781 Execute68k((uint32)proc, &r);
782 WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
783 #else
784 // Only update cursor
785 if (HasMacStarted()) {
786 if (InterruptFlags & INTFLAG_VIA) {
787 ClearInterruptFlag(INTFLAG_VIA);
788 ADBInterrupt();
789 ExecuteNative(NATIVE_VIDEO_VBL);
790 }
791 }
792 #endif
793 }
794 break;
795 #endif
796 }
797 }
798
799 static void get_resource(void);
800 static void get_1_resource(void);
801 static void get_ind_resource(void);
802 static void get_1_ind_resource(void);
803 static void r_get_resource(void);
804
805 #define GPR(REG) current_cpu->gpr(REG)
806
807 static void NativeOp(int selector)
808 {
809 #if EMUL_TIME_STATS
810 native_exec_count++;
811 const clock_t native_exec_start = clock();
812 #endif
813
814 switch (selector) {
815 case NATIVE_PATCH_NAME_REGISTRY:
816 DoPatchNameRegistry();
817 break;
818 case NATIVE_VIDEO_INSTALL_ACCEL:
819 VideoInstallAccel();
820 break;
821 case NATIVE_VIDEO_VBL:
822 VideoVBL();
823 break;
824 case NATIVE_VIDEO_DO_DRIVER_IO:
825 GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4),
826 (void *)GPR(5), GPR(6), GPR(7));
827 break;
828 #ifdef WORDS_BIGENDIAN
829 case NATIVE_ETHER_IRQ:
830 EtherIRQ();
831 break;
832 case NATIVE_ETHER_INIT:
833 GPR(3) = InitStreamModule((void *)GPR(3));
834 break;
835 case NATIVE_ETHER_TERM:
836 TerminateStreamModule();
837 break;
838 case NATIVE_ETHER_OPEN:
839 GPR(3) = ether_open((queue_t *)GPR(3), (void *)GPR(4), GPR(5), GPR(6), (void*)GPR(7));
840 break;
841 case NATIVE_ETHER_CLOSE:
842 GPR(3) = ether_close((queue_t *)GPR(3), GPR(4), (void *)GPR(5));
843 break;
844 case NATIVE_ETHER_WPUT:
845 GPR(3) = ether_wput((queue_t *)GPR(3), (mblk_t *)GPR(4));
846 break;
847 case NATIVE_ETHER_RSRV:
848 GPR(3) = ether_rsrv((queue_t *)GPR(3));
849 break;
850 #else
851 case NATIVE_ETHER_INIT:
852 // FIXME: needs more complicated thunks
853 GPR(3) = false;
854 break;
855 #endif
856 case NATIVE_SERIAL_NOTHING:
857 case NATIVE_SERIAL_OPEN:
858 case NATIVE_SERIAL_PRIME_IN:
859 case NATIVE_SERIAL_PRIME_OUT:
860 case NATIVE_SERIAL_CONTROL:
861 case NATIVE_SERIAL_STATUS:
862 case NATIVE_SERIAL_CLOSE: {
863 typedef int16 (*SerialCallback)(uint32, uint32);
864 static const SerialCallback serial_callbacks[] = {
865 SerialNothing,
866 SerialOpen,
867 SerialPrimeIn,
868 SerialPrimeOut,
869 SerialControl,
870 SerialStatus,
871 SerialClose
872 };
873 GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4));
874 break;
875 }
876 case NATIVE_GET_RESOURCE:
877 case NATIVE_GET_1_RESOURCE:
878 case NATIVE_GET_IND_RESOURCE:
879 case NATIVE_GET_1_IND_RESOURCE:
880 case NATIVE_R_GET_RESOURCE: {
881 typedef void (*GetResourceCallback)(void);
882 static const GetResourceCallback get_resource_callbacks[] = {
883 get_resource,
884 get_1_resource,
885 get_ind_resource,
886 get_1_ind_resource,
887 r_get_resource
888 };
889 get_resource_callbacks[selector - NATIVE_GET_RESOURCE]();
890 break;
891 }
892 case NATIVE_DISABLE_INTERRUPT:
893 DisableInterrupt();
894 break;
895 case NATIVE_ENABLE_INTERRUPT:
896 EnableInterrupt();
897 break;
898 case NATIVE_MAKE_EXECUTABLE:
899 MakeExecutable(0, (void *)GPR(4), GPR(5));
900 break;
901 default:
902 printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
903 QuitEmulator();
904 break;
905 }
906
907 #if EMUL_TIME_STATS
908 native_exec_time += (clock() - native_exec_start);
909 #endif
910 }
911
912 /*
913 * Execute 68k subroutine (must be ended with EXEC_RETURN)
914 * This must only be called by the emul_thread when in EMUL_OP mode
915 * r->a[7] is unused, the routine runs on the caller's stack
916 */
917
918 void Execute68k(uint32 pc, M68kRegisters *r)
919 {
920 current_cpu->execute_68k(pc, r);
921 }
922
923 /*
924 * Execute 68k A-Trap from EMUL_OP routine
925 * r->a[7] is unused, the routine runs on the caller's stack
926 */
927
928 void Execute68kTrap(uint16 trap, M68kRegisters *r)
929 {
930 SheepVar proc_var(4);
931 uint32 proc = proc_var.addr();
932 WriteMacInt16(proc, trap);
933 WriteMacInt16(proc + 2, M68K_RTS);
934 Execute68k(proc, r);
935 }
936
937 /*
938 * Call MacOS PPC code
939 */
940
941 uint32 call_macos(uint32 tvect)
942 {
943 return current_cpu->execute_macos_code(tvect, 0, NULL);
944 }
945
946 uint32 call_macos1(uint32 tvect, uint32 arg1)
947 {
948 const uint32 args[] = { arg1 };
949 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
950 }
951
952 uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2)
953 {
954 const uint32 args[] = { arg1, arg2 };
955 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
956 }
957
958 uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3)
959 {
960 const uint32 args[] = { arg1, arg2, arg3 };
961 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
962 }
963
964 uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4)
965 {
966 const uint32 args[] = { arg1, arg2, arg3, arg4 };
967 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
968 }
969
970 uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5)
971 {
972 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 };
973 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
974 }
975
976 uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6)
977 {
978 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
979 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
980 }
981
982 uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7)
983 {
984 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
985 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
986 }
987
988 /*
989 * Resource Manager thunks
990 */
991
992 void get_resource(void)
993 {
994 current_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
995 }
996
997 void get_1_resource(void)
998 {
999 current_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
1000 }
1001
1002 void get_ind_resource(void)
1003 {
1004 current_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
1005 }
1006
1007 void get_1_ind_resource(void)
1008 {
1009 current_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
1010 }
1011
1012 void r_get_resource(void)
1013 {
1014 current_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
1015 }