ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.7
Committed: 2003-10-12T05:44:15Z (20 years, 7 months ago) by gbeauche
Branch: MAIN
Changes since 1.6: +14 -1 lines
Log Message:
- Handle MakeExecutable() replacement
- Disable predecode cache in CVS for now
- Fix flight recorder ordering in predecode cache mode

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