ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/kpx_cpu/sheepshaver_glue.cpp
Revision: 1.8
Committed: 2003-10-19T21:37:43Z (20 years, 7 months ago) by gbeauche
Branch: MAIN
Changes since 1.7: +5 -3 lines
Log Message:
Preserve CR in execute_68k(). This enables MacOS 8.6 to work. ;-)

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) = get_cr();
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) | (get_cr() & ~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 uint32 saved_cr = get_cr();
332
333 // Create MacOS stack frame
334 // FIXME: make sure MacOS doesn't expect PPC registers to live on top
335 uint32 sp = gpr(1);
336 gpr(1) -= 56;
337 WriteMacInt32(gpr(1), sp);
338
339 // Save PowerPC registers
340 uint32 saved_GPRs[19];
341 memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13));
342 #if SAVE_FP_EXEC_68K
343 double saved_FPRs[18];
344 memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14));
345 #endif
346
347 // Setup registers for 68k emulator
348 cr().set(CR_SO_field<2>::mask()); // Supervisor mode
349 for (int i = 0; i < 8; i++) // d[0]..d[7]
350 gpr(8 + i) = r->d[i];
351 for (int i = 0; i < 7; i++) // a[0]..a[6]
352 gpr(16 + i) = r->a[i];
353 gpr(23) = 0;
354 gpr(24) = entry;
355 gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR
356 gpr(26) = 0;
357 gpr(28) = 0; // VBR
358 gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]); // Pointer to opcode table
359 gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]); // Address of emulator
360 gpr(31) = KernelDataAddr + 0x1000;
361
362 // Push return address (points to EXEC_RETURN opcode) on stack
363 gpr(1) -= 4;
364 WriteMacInt32(gpr(1), XLM_EXEC_RETURN_OPCODE);
365
366 // Rentering 68k emulator
367 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
368
369 // Set r0 to 0 for 68k emulator
370 gpr(0) = 0;
371
372 // Execute 68k opcode
373 uint32 opcode = ReadMacInt16(gpr(24));
374 gpr(27) = (int32)(int16)ReadMacInt16(gpr(24) += 2);
375 gpr(29) += opcode * 8;
376 execute(gpr(29));
377
378 // Save r25 (contains current 68k interrupt level)
379 WriteMacInt32(XLM_68K_R25, gpr(25));
380
381 // Reentering EMUL_OP mode
382 WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
383
384 // Save 68k registers
385 for (int i = 0; i < 8; i++) // d[0]..d[7]
386 r->d[i] = gpr(8 + i);
387 for (int i = 0; i < 7; i++) // a[0]..a[6]
388 r->a[i] = gpr(16 + i);
389
390 // Restore PowerPC registers
391 memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13));
392 #if SAVE_FP_EXEC_68K
393 memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14));
394 #endif
395
396 // Cleanup stack
397 gpr(1) += 56;
398
399 // Restore program counters and branch registers
400 pc() = saved_pc;
401 lr() = saved_lr;
402 ctr()= saved_ctr;
403 set_cr(saved_cr);
404 }
405
406 // Call MacOS PPC code
407 uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args)
408 {
409 // Save program counters and branch registers
410 uint32 saved_pc = pc();
411 uint32 saved_lr = lr();
412 uint32 saved_ctr= ctr();
413
414 // Build trampoline with EXEC_RETURN
415 uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) };
416 lr() = (uint32)trampoline;
417
418 gpr(1) -= 64; // Create stack frame
419 uint32 proc = ReadMacInt32(tvect); // Get routine address
420 uint32 toc = ReadMacInt32(tvect + 4); // Get TOC pointer
421
422 // Save PowerPC registers
423 uint32 regs[8];
424 regs[0] = gpr(2);
425 for (int i = 0; i < nargs; i++)
426 regs[i + 1] = gpr(i + 3);
427
428 // Prepare and call MacOS routine
429 gpr(2) = toc;
430 for (int i = 0; i < nargs; i++)
431 gpr(i + 3) = args[i];
432 execute(proc);
433 uint32 retval = gpr(3);
434
435 // Restore PowerPC registers
436 for (int i = 0; i <= nargs; i++)
437 gpr(i + 2) = regs[i];
438
439 // Cleanup stack
440 gpr(1) += 64;
441
442 // Restore program counters and branch registers
443 pc() = saved_pc;
444 lr() = saved_lr;
445 ctr()= saved_ctr;
446
447 return retval;
448 }
449
450 // Execute ppc routine
451 inline void sheepshaver_cpu::execute_ppc(uint32 entry)
452 {
453 // Save branch registers
454 uint32 saved_lr = lr();
455
456 const uint32 trampoline[] = { htonl(POWERPC_EMUL_OP | 1) };
457 lr() = (uint32)trampoline;
458
459 execute(entry);
460
461 // Restore branch registers
462 lr() = saved_lr;
463 }
464
465 // Resource Manager thunk
466 extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
467
468 inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
469 {
470 uint32 type = gpr(3);
471 int16 id = gpr(4);
472
473 // Create stack frame
474 gpr(1) -= 56;
475
476 // Call old routine
477 execute_ppc(old_get_resource);
478
479 // Call CheckLoad()
480 uint32 handle = gpr(3);
481 check_load_invoc(type, id, handle);
482 gpr(3) = handle;
483
484 // Cleanup stack
485 gpr(1) += 56;
486 }
487
488
489 /**
490 * SheepShaver CPU engine interface
491 **/
492
493 static sheepshaver_cpu *main_cpu = NULL; // CPU emulator to handle usual control flow
494 static sheepshaver_cpu *interrupt_cpu = NULL; // CPU emulator to handle interrupts
495 static sheepshaver_cpu *current_cpu = NULL; // Current CPU emulator context
496
497 void FlushCodeCache(uintptr start, uintptr end)
498 {
499 D(bug("FlushCodeCache(%08x, %08x)\n", start, end));
500 main_cpu->invalidate_cache_range(start, end);
501 #if MULTICORE_CPU
502 interrupt_cpu->invalidate_cache_range(start, end);
503 #endif
504 }
505
506 static inline void cpu_push(sheepshaver_cpu *new_cpu)
507 {
508 #if MULTICORE_CPU
509 current_cpu = new_cpu;
510 #endif
511 }
512
513 static inline void cpu_pop()
514 {
515 #if MULTICORE_CPU
516 current_cpu = main_cpu;
517 #endif
518 }
519
520 // Dump PPC registers
521 static void dump_registers(void)
522 {
523 current_cpu->dump_registers();
524 }
525
526 // Dump log
527 static void dump_log(void)
528 {
529 current_cpu->dump_log();
530 }
531
532 /*
533 * Initialize CPU emulation
534 */
535
536 static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
537 {
538 #if ENABLE_VOSF
539 // Handle screen fault
540 extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t);
541 if (Screen_fault_handler(fault_address, fault_instruction))
542 return SIGSEGV_RETURN_SUCCESS;
543 #endif
544
545 const uintptr addr = (uintptr)fault_address;
546 #if HAVE_SIGSEGV_SKIP_INSTRUCTION
547 // Ignore writes to ROM
548 if ((addr - ROM_BASE) < ROM_SIZE)
549 return SIGSEGV_RETURN_SKIP_INSTRUCTION;
550
551 // Ignore all other faults, if requested
552 if (PrefsFindBool("ignoresegv"))
553 return SIGSEGV_RETURN_FAILURE;
554 #else
555 #error "FIXME: You don't have the capability to skip instruction within signal handlers"
556 #endif
557
558 printf("SIGSEGV\n");
559 printf(" pc %p\n", fault_instruction);
560 printf(" ea %p\n", fault_address);
561 printf(" cpu %s\n", current_cpu == main_cpu ? "main" : "interrupts");
562 dump_registers();
563 current_cpu->dump_log();
564 enter_mon();
565 QuitEmulator();
566
567 return SIGSEGV_RETURN_FAILURE;
568 }
569
570 void init_emul_ppc(void)
571 {
572 // Initialize main CPU emulator
573 main_cpu = new sheepshaver_cpu();
574 main_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
575 WriteMacInt32(XLM_RUN_MODE, MODE_68K);
576
577 #if MULTICORE_CPU
578 // Initialize alternate CPU emulator to handle interrupts
579 interrupt_cpu = new sheepshaver_cpu();
580 #endif
581
582 // Install the handler for SIGSEGV
583 sigsegv_install_handler(sigsegv_handler);
584
585 #if ENABLE_MON
586 // Install "regs" command in cxmon
587 mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n");
588 mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n");
589 #endif
590 }
591
592 /*
593 * Emulation loop
594 */
595
596 void emul_ppc(uint32 entry)
597 {
598 current_cpu = main_cpu;
599 current_cpu->start_log();
600 current_cpu->execute(entry);
601 }
602
603 /*
604 * Handle PowerPC interrupt
605 */
606
607 // Atomic operations
608 extern int atomic_add(int *var, int v);
609 extern int atomic_and(int *var, int v);
610 extern int atomic_or(int *var, int v);
611
612 #if !ASYNC_IRQ
613 void TriggerInterrupt(void)
614 {
615 #if 0
616 WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
617 #else
618 SPCFLAGS_SET( SPCFLAG_INT );
619 #endif
620 }
621 #endif
622
623 void HandleInterrupt(void)
624 {
625 // Do nothing if interrupts are disabled
626 if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0)
627 return;
628
629 // Do nothing if there is no interrupt pending
630 if (InterruptFlags == 0)
631 return;
632
633 // Disable MacOS stack sniffer
634 WriteMacInt32(0x110, 0);
635
636 // Interrupt action depends on current run mode
637 switch (ReadMacInt32(XLM_RUN_MODE)) {
638 case MODE_68K:
639 // 68k emulator active, trigger 68k interrupt level 1
640 assert(current_cpu == main_cpu);
641 WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
642 main_cpu->set_cr(main_cpu->get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
643 break;
644
645 #if INTERRUPTS_IN_NATIVE_MODE
646 case MODE_NATIVE:
647 // 68k emulator inactive, in nanokernel?
648 assert(current_cpu == main_cpu);
649 if (main_cpu->gpr(1) != KernelDataAddr) {
650 // Prepare for 68k interrupt level 1
651 WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
652 WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
653 ReadMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc)
654 | tswap32(kernel_data->v[0x674 >> 2]));
655
656 // Execute nanokernel interrupt routine (this will activate the 68k emulator)
657 DisableInterrupt();
658 cpu_push(interrupt_cpu);
659 if (ROMType == ROMTYPE_NEWWORLD)
660 current_cpu->interrupt(ROM_BASE + 0x312b1c);
661 else
662 current_cpu->interrupt(ROM_BASE + 0x312a3c);
663 cpu_pop();
664 }
665 break;
666 #endif
667
668 #if INTERRUPTS_IN_EMUL_OP_MODE
669 case MODE_EMUL_OP:
670 // 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
671 if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
672 #if 1
673 // Execute full 68k interrupt routine
674 M68kRegisters r;
675 uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
676 WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
677 static const uint8 proc[] = {
678 0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word)
679 0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address)
680 0x40, 0xe7, // move sr,-(sp) (saved SR)
681 0x20, 0x78, 0x00, 0x064, // move.l $64,a0
682 0x4e, 0xd0, // jmp (a0)
683 M68K_RTS >> 8, M68K_RTS & 0xff // @1
684 };
685 Execute68k((uint32)proc, &r);
686 WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
687 #else
688 // Only update cursor
689 if (HasMacStarted()) {
690 if (InterruptFlags & INTFLAG_VIA) {
691 ClearInterruptFlag(INTFLAG_VIA);
692 ADBInterrupt();
693 ExecutePPC(VideoVBL);
694 }
695 }
696 #endif
697 }
698 break;
699 #endif
700 }
701 }
702
703 /*
704 * Execute NATIVE_OP opcode (called by PowerPC emulator)
705 */
706
707 #define POWERPC_NATIVE_OP_INIT(LR, OP) \
708 tswap32(POWERPC_EMUL_OP | ((LR) << 11) | (((uint32)OP) << 6) | 2)
709
710 // FIXME: Make sure 32-bit relocations are used
711 const uint32 NativeOpTable[NATIVE_OP_MAX] = {
712 POWERPC_NATIVE_OP_INIT(1, NATIVE_PATCH_NAME_REGISTRY),
713 POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_INSTALL_ACCEL),
714 POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_VBL),
715 POWERPC_NATIVE_OP_INIT(1, NATIVE_VIDEO_DO_DRIVER_IO),
716 POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_IRQ),
717 POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_INIT),
718 POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_TERM),
719 POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_OPEN),
720 POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_CLOSE),
721 POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_WPUT),
722 POWERPC_NATIVE_OP_INIT(1, NATIVE_ETHER_RSRV),
723 POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_NOTHING),
724 POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_OPEN),
725 POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_IN),
726 POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_PRIME_OUT),
727 POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CONTROL),
728 POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_STATUS),
729 POWERPC_NATIVE_OP_INIT(1, NATIVE_SERIAL_CLOSE),
730 POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_RESOURCE),
731 POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_RESOURCE),
732 POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_IND_RESOURCE),
733 POWERPC_NATIVE_OP_INIT(1, NATIVE_GET_1_IND_RESOURCE),
734 POWERPC_NATIVE_OP_INIT(1, NATIVE_R_GET_RESOURCE),
735 POWERPC_NATIVE_OP_INIT(0, NATIVE_DISABLE_INTERRUPT),
736 POWERPC_NATIVE_OP_INIT(0, NATIVE_ENABLE_INTERRUPT),
737 POWERPC_NATIVE_OP_INIT(1, NATIVE_MAKE_EXECUTABLE),
738 };
739
740 static void get_resource(void);
741 static void get_1_resource(void);
742 static void get_ind_resource(void);
743 static void get_1_ind_resource(void);
744 static void r_get_resource(void);
745
746 #define GPR(REG) current_cpu->gpr(REG)
747
748 static void NativeOp(int selector)
749 {
750 switch (selector) {
751 case NATIVE_PATCH_NAME_REGISTRY:
752 DoPatchNameRegistry();
753 break;
754 case NATIVE_VIDEO_INSTALL_ACCEL:
755 VideoInstallAccel();
756 break;
757 case NATIVE_VIDEO_VBL:
758 VideoVBL();
759 break;
760 case NATIVE_VIDEO_DO_DRIVER_IO:
761 GPR(3) = (int32)(int16)VideoDoDriverIO((void *)GPR(3), (void *)GPR(4),
762 (void *)GPR(5), GPR(6), GPR(7));
763 break;
764 case NATIVE_GET_RESOURCE:
765 get_resource();
766 break;
767 case NATIVE_GET_1_RESOURCE:
768 get_1_resource();
769 break;
770 case NATIVE_GET_IND_RESOURCE:
771 get_ind_resource();
772 break;
773 case NATIVE_GET_1_IND_RESOURCE:
774 get_1_ind_resource();
775 break;
776 case NATIVE_R_GET_RESOURCE:
777 r_get_resource();
778 break;
779 case NATIVE_SERIAL_NOTHING:
780 case NATIVE_SERIAL_OPEN:
781 case NATIVE_SERIAL_PRIME_IN:
782 case NATIVE_SERIAL_PRIME_OUT:
783 case NATIVE_SERIAL_CONTROL:
784 case NATIVE_SERIAL_STATUS:
785 case NATIVE_SERIAL_CLOSE: {
786 typedef int16 (*SerialCallback)(uint32, uint32);
787 static const SerialCallback serial_callbacks[] = {
788 SerialNothing,
789 SerialOpen,
790 SerialPrimeIn,
791 SerialPrimeOut,
792 SerialControl,
793 SerialStatus,
794 SerialClose
795 };
796 GPR(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](GPR(3), GPR(4));
797 break;
798 }
799 case NATIVE_DISABLE_INTERRUPT:
800 DisableInterrupt();
801 break;
802 case NATIVE_ENABLE_INTERRUPT:
803 EnableInterrupt();
804 break;
805 case NATIVE_MAKE_EXECUTABLE:
806 MakeExecutable(0, (void *)GPR(4), GPR(5));
807 break;
808 default:
809 printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
810 QuitEmulator();
811 break;
812 }
813 }
814
815 /*
816 * Execute native subroutine (LR must contain return address)
817 */
818
819 void ExecuteNative(int selector)
820 {
821 uint32 tvect[2];
822 tvect[0] = tswap32(POWERPC_NATIVE_OP_FUNC(selector));
823 tvect[1] = 0; // Fake TVECT
824 RoutineDescriptor desc = BUILD_PPC_ROUTINE_DESCRIPTOR(0, tvect);
825 M68kRegisters r;
826 Execute68k((uint32)&desc, &r);
827 }
828
829 /*
830 * Execute 68k subroutine (must be ended with EXEC_RETURN)
831 * This must only be called by the emul_thread when in EMUL_OP mode
832 * r->a[7] is unused, the routine runs on the caller's stack
833 */
834
835 void Execute68k(uint32 pc, M68kRegisters *r)
836 {
837 current_cpu->execute_68k(pc, r);
838 }
839
840 /*
841 * Execute 68k A-Trap from EMUL_OP routine
842 * r->a[7] is unused, the routine runs on the caller's stack
843 */
844
845 void Execute68kTrap(uint16 trap, M68kRegisters *r)
846 {
847 uint16 proc[2];
848 proc[0] = htons(trap);
849 proc[1] = htons(M68K_RTS);
850 Execute68k((uint32)proc, r);
851 }
852
853 /*
854 * Call MacOS PPC code
855 */
856
857 uint32 call_macos(uint32 tvect)
858 {
859 return current_cpu->execute_macos_code(tvect, 0, NULL);
860 }
861
862 uint32 call_macos1(uint32 tvect, uint32 arg1)
863 {
864 const uint32 args[] = { arg1 };
865 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
866 }
867
868 uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2)
869 {
870 const uint32 args[] = { arg1, arg2 };
871 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
872 }
873
874 uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3)
875 {
876 const uint32 args[] = { arg1, arg2, arg3 };
877 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
878 }
879
880 uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4)
881 {
882 const uint32 args[] = { arg1, arg2, arg3, arg4 };
883 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
884 }
885
886 uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5)
887 {
888 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 };
889 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
890 }
891
892 uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6)
893 {
894 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
895 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
896 }
897
898 uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7)
899 {
900 const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
901 return current_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
902 }
903
904 /*
905 * Atomic operations
906 */
907
908 int atomic_add(int *var, int v)
909 {
910 int ret = *var;
911 *var += v;
912 return ret;
913 }
914
915 int atomic_and(int *var, int v)
916 {
917 int ret = *var;
918 *var &= v;
919 return ret;
920 }
921
922 int atomic_or(int *var, int v)
923 {
924 int ret = *var;
925 *var |= v;
926 return ret;
927 }
928
929 /*
930 * Resource Manager thunks
931 */
932
933 void get_resource(void)
934 {
935 current_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
936 }
937
938 void get_1_resource(void)
939 {
940 current_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
941 }
942
943 void get_ind_resource(void)
944 {
945 current_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
946 }
947
948 void get_1_ind_resource(void)
949 {
950 current_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
951 }
952
953 void r_get_resource(void)
954 {
955 current_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
956 }