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