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