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