2003-09-07 14:25:05 +00:00
|
|
|
/*
|
|
|
|
* sheepshaver_glue.cpp - Glue Kheperix CPU to SheepShaver CPU engine interface
|
|
|
|
*
|
2004-01-12 15:37:24 +00:00
|
|
|
* SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig
|
2003-09-07 14:25:05 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "cpu_emulation.h"
|
|
|
|
#include "main.h"
|
2003-09-29 07:05:15 +00:00
|
|
|
#include "prefs.h"
|
2003-09-07 14:25:05 +00:00
|
|
|
#include "xlowmem.h"
|
|
|
|
#include "emul_op.h"
|
|
|
|
#include "rom_patches.h"
|
|
|
|
#include "macos_util.h"
|
|
|
|
#include "block-alloc.hpp"
|
|
|
|
#include "sigsegv.h"
|
|
|
|
#include "cpu/ppc/ppc-cpu.hpp"
|
|
|
|
#include "cpu/ppc/ppc-operations.hpp"
|
2003-11-24 23:45:52 +00:00
|
|
|
#include "cpu/ppc/ppc-instructions.hpp"
|
2003-12-04 17:26:38 +00:00
|
|
|
#include "thunks.h"
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
// Used for NativeOp trampolines
|
|
|
|
#include "video.h"
|
|
|
|
#include "name_registry.h"
|
|
|
|
#include "serial.h"
|
2003-11-10 15:11:44 +00:00
|
|
|
#include "ether.h"
|
2004-05-15 17:26:28 +00:00
|
|
|
#include "timer.h"
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-02-24 14:09:12 +00:00
|
|
|
#include <stdlib.h>
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
#if ENABLE_MON
|
|
|
|
#include "mon.h"
|
|
|
|
#include "mon_disass.h"
|
|
|
|
#endif
|
|
|
|
|
2003-10-26 13:59:04 +00:00
|
|
|
#define DEBUG 0
|
2003-09-07 14:25:05 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2003-11-04 20:48:29 +00:00
|
|
|
// Emulation time statistics
|
2004-06-05 07:09:38 +00:00
|
|
|
#ifndef EMUL_TIME_STATS
|
|
|
|
#define EMUL_TIME_STATS 0
|
|
|
|
#endif
|
2003-11-04 20:48:29 +00:00
|
|
|
|
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
static clock_t emul_start_time;
|
2004-06-05 07:09:38 +00:00
|
|
|
static uint32 interrupt_count = 0, ppc_interrupt_count = 0;
|
2003-11-04 20:48:29 +00:00
|
|
|
static clock_t interrupt_time = 0;
|
|
|
|
static uint32 exec68k_count = 0;
|
|
|
|
static clock_t exec68k_time = 0;
|
|
|
|
static uint32 native_exec_count = 0;
|
|
|
|
static clock_t native_exec_time = 0;
|
|
|
|
static uint32 macos_exec_count = 0;
|
|
|
|
static clock_t macos_exec_time = 0;
|
|
|
|
#endif
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
static void enter_mon(void)
|
|
|
|
{
|
|
|
|
// Start up mon in real-mode
|
|
|
|
#if ENABLE_MON
|
|
|
|
char *arg[4] = {"mon", "-m", "-r", NULL};
|
|
|
|
mon(3, arg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-12-05 13:37:56 +00:00
|
|
|
// From main_*.cpp
|
|
|
|
extern uintptr SignalStackBase();
|
|
|
|
|
2004-01-24 11:28:06 +00:00
|
|
|
// From rsrc_patches.cpp
|
|
|
|
extern "C" void check_load_invoc(uint32 type, int16 id, uint32 h);
|
|
|
|
|
2003-12-04 17:26:38 +00:00
|
|
|
// PowerPC EmulOp to exit from emulation looop
|
|
|
|
const uint32 POWERPC_EXEC_RETURN = POWERPC_EMUL_OP | 1;
|
|
|
|
|
2004-05-12 15:54:23 +00:00
|
|
|
// Enable interrupt routine safety checks?
|
|
|
|
#define SAFE_INTERRUPT_PPC 1
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Enable Execute68k() safety checks?
|
|
|
|
#define SAFE_EXEC_68K 1
|
|
|
|
|
|
|
|
// Save FP state in Execute68k()?
|
|
|
|
#define SAVE_FP_EXEC_68K 1
|
|
|
|
|
|
|
|
// Interrupts in EMUL_OP mode?
|
|
|
|
#define INTERRUPTS_IN_EMUL_OP_MODE 1
|
|
|
|
|
|
|
|
// Interrupts in native mode?
|
|
|
|
#define INTERRUPTS_IN_NATIVE_MODE 1
|
|
|
|
|
2004-05-15 17:26:28 +00:00
|
|
|
// Enable native EMUL_OPs to be run without a mode switch
|
|
|
|
#define ENABLE_NATIVE_EMUL_OP 1
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Pointer to Kernel Data
|
2003-09-29 15:46:09 +00:00
|
|
|
static KernelData * const kernel_data = (KernelData *)KERNEL_DATA_BASE;
|
2003-09-07 14:25:05 +00:00
|
|
|
|
2003-11-10 16:23:58 +00:00
|
|
|
// SIGSEGV handler
|
|
|
|
static sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
|
|
|
|
|
2004-05-19 21:23:17 +00:00
|
|
|
#if PPC_ENABLE_JIT && PPC_REENTRANT_JIT
|
|
|
|
// Special trampolines for EmulOp and NativeOp
|
|
|
|
static uint8 *emul_op_trampoline;
|
|
|
|
static uint8 *native_op_trampoline;
|
|
|
|
#endif
|
|
|
|
|
2003-12-03 10:52:50 +00:00
|
|
|
// JIT Compiler enabled?
|
|
|
|
static inline bool enable_jit_p()
|
|
|
|
{
|
|
|
|
return PrefsFindBool("jit");
|
|
|
|
}
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PowerPC emulator glue with special 'sheep' opcodes
|
|
|
|
**/
|
|
|
|
|
2003-11-24 23:45:52 +00:00
|
|
|
enum {
|
|
|
|
PPC_I(SHEEP) = PPC_I(MAX),
|
|
|
|
PPC_I(SHEEP_MAX)
|
|
|
|
};
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
class sheepshaver_cpu
|
|
|
|
: public powerpc_cpu
|
|
|
|
{
|
|
|
|
void init_decoder();
|
|
|
|
void execute_sheep(uint32 opcode);
|
|
|
|
|
2004-05-15 17:26:28 +00:00
|
|
|
// Filter out EMUL_OP routines that only call native code
|
|
|
|
bool filter_execute_emul_op(uint32 emul_op);
|
|
|
|
|
|
|
|
// "Native" EMUL_OP routines
|
|
|
|
void execute_emul_op_microseconds();
|
|
|
|
void execute_emul_op_idle_time_1();
|
|
|
|
void execute_emul_op_idle_time_2();
|
|
|
|
|
2004-05-20 11:05:30 +00:00
|
|
|
// CPU context to preserve on interrupt
|
|
|
|
class interrupt_context {
|
|
|
|
uint32 gpr[32];
|
|
|
|
uint32 pc;
|
|
|
|
uint32 lr;
|
|
|
|
uint32 ctr;
|
|
|
|
uint32 cr;
|
|
|
|
uint32 xer;
|
|
|
|
sheepshaver_cpu *cpu;
|
|
|
|
const char *where;
|
|
|
|
public:
|
|
|
|
interrupt_context(sheepshaver_cpu *_cpu, const char *_where);
|
|
|
|
~interrupt_context();
|
|
|
|
};
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
public:
|
|
|
|
|
2003-10-26 13:59:04 +00:00
|
|
|
// Constructor
|
|
|
|
sheepshaver_cpu();
|
2003-09-07 14:25:05 +00:00
|
|
|
|
2003-12-25 23:54:36 +00:00
|
|
|
// CR & XER accessors
|
2003-09-07 14:25:05 +00:00
|
|
|
uint32 get_cr() const { return cr().get(); }
|
|
|
|
void set_cr(uint32 v) { cr().set(v); }
|
2003-12-25 23:54:36 +00:00
|
|
|
uint32 get_xer() const { return xer().get(); }
|
|
|
|
void set_xer(uint32 v) { xer().set(v); }
|
2003-09-07 14:25:05 +00:00
|
|
|
|
2004-05-19 21:23:17 +00:00
|
|
|
// Execute NATIVE_OP routine
|
|
|
|
void execute_native_op(uint32 native_op);
|
|
|
|
|
2004-01-24 11:28:06 +00:00
|
|
|
// Execute EMUL_OP routine
|
|
|
|
void execute_emul_op(uint32 emul_op);
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Execute 68k routine
|
|
|
|
void execute_68k(uint32 entry, M68kRegisters *r);
|
|
|
|
|
2003-09-28 21:27:34 +00:00
|
|
|
// Execute ppc routine
|
|
|
|
void execute_ppc(uint32 entry);
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Execute MacOS/PPC code
|
|
|
|
uint32 execute_macos_code(uint32 tvect, int nargs, uint32 const *args);
|
|
|
|
|
2004-01-24 11:28:06 +00:00
|
|
|
// Compile one instruction
|
2004-05-19 21:23:17 +00:00
|
|
|
virtual int compile1(codegen_context_t & cg_context);
|
2004-01-24 11:28:06 +00:00
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Resource manager thunk
|
|
|
|
void get_resource(uint32 old_get_resource);
|
|
|
|
|
|
|
|
// Handle MacOS interrupt
|
2003-09-29 15:46:09 +00:00
|
|
|
void interrupt(uint32 entry);
|
2003-10-26 13:59:04 +00:00
|
|
|
void handle_interrupt();
|
2003-09-28 21:27:34 +00:00
|
|
|
|
2003-11-10 16:23:58 +00:00
|
|
|
// Make sure the SIGSEGV handler can access CPU registers
|
|
|
|
friend sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
|
2003-09-07 14:25:05 +00:00
|
|
|
};
|
|
|
|
|
2004-02-20 17:20:15 +00:00
|
|
|
// Memory allocator returning areas aligned on 16-byte boundaries
|
|
|
|
void *operator new(size_t size)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2004-02-24 14:09:12 +00:00
|
|
|
#if defined(HAVE_POSIX_MEMALIGN)
|
2004-02-20 17:20:15 +00:00
|
|
|
if (posix_memalign(&p, 16, size) != 0)
|
|
|
|
throw std::bad_alloc();
|
2004-02-24 14:09:12 +00:00
|
|
|
#elif defined(HAVE_MEMALIGN)
|
|
|
|
p = memalign(16, size);
|
|
|
|
#elif defined(HAVE_VALLOC)
|
|
|
|
p = valloc(size); // page-aligned!
|
|
|
|
#else
|
|
|
|
/* XXX: handle padding ourselves */
|
|
|
|
p = malloc(size);
|
|
|
|
#endif
|
2004-02-20 17:20:15 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void *p)
|
|
|
|
{
|
2004-02-24 14:09:12 +00:00
|
|
|
#if defined(HAVE_MEMALIGN) || defined(HAVE_VALLOC)
|
|
|
|
#if defined(__GLIBC__)
|
|
|
|
// this is known to work only with GNU libc
|
|
|
|
free(p);
|
|
|
|
#endif
|
|
|
|
#else
|
2004-02-20 17:20:15 +00:00
|
|
|
free(p);
|
2004-02-24 14:09:12 +00:00
|
|
|
#endif
|
2004-02-20 17:20:15 +00:00
|
|
|
}
|
2003-09-07 14:25:05 +00:00
|
|
|
|
2003-10-26 13:59:04 +00:00
|
|
|
sheepshaver_cpu::sheepshaver_cpu()
|
2003-12-03 10:52:50 +00:00
|
|
|
: powerpc_cpu(enable_jit_p())
|
2003-10-26 13:59:04 +00:00
|
|
|
{
|
|
|
|
init_decoder();
|
|
|
|
}
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
void sheepshaver_cpu::init_decoder()
|
|
|
|
{
|
|
|
|
static const instr_info_t sheep_ii_table[] = {
|
|
|
|
{ "sheep",
|
2003-11-02 14:48:20 +00:00
|
|
|
(execute_pmf)&sheepshaver_cpu::execute_sheep,
|
2003-09-07 14:25:05 +00:00
|
|
|
NULL,
|
2003-11-24 23:45:52 +00:00
|
|
|
PPC_I(SHEEP),
|
2003-10-12 05:44:17 +00:00
|
|
|
D_form, 6, 0, CFLOW_JUMP | CFLOW_TRAP
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const int ii_count = sizeof(sheep_ii_table)/sizeof(sheep_ii_table[0]);
|
|
|
|
D(bug("SheepShaver extra decode table has %d entries\n", ii_count));
|
|
|
|
|
|
|
|
for (int i = 0; i < ii_count; i++) {
|
|
|
|
const instr_info_t * ii = &sheep_ii_table[i];
|
|
|
|
init_decoder_entry(ii);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-28 21:27:34 +00:00
|
|
|
/* NativeOp instruction format:
|
2004-04-22 22:54:47 +00:00
|
|
|
+------------+-------------------------+--+-----------+------------+
|
|
|
|
| 6 | |FN| OP | 2 |
|
|
|
|
+------------+-------------------------+--+-----------+------------+
|
|
|
|
0 5 |6 18 19 20 25 26 31
|
2003-09-28 21:27:34 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-22 22:54:47 +00:00
|
|
|
typedef bit_field< 19, 19 > FN_field;
|
|
|
|
typedef bit_field< 20, 25 > NATIVE_OP_field;
|
2003-09-28 21:27:34 +00:00
|
|
|
typedef bit_field< 26, 31 > EMUL_OP_field;
|
|
|
|
|
2004-05-15 17:26:28 +00:00
|
|
|
// "Native" EMUL_OP routines
|
|
|
|
#define GPR_A(REG) gpr(16 + (REG))
|
|
|
|
#define GPR_D(REG) gpr( 8 + (REG))
|
|
|
|
|
|
|
|
void sheepshaver_cpu::execute_emul_op_microseconds()
|
|
|
|
{
|
|
|
|
Microseconds(GPR_A(0), GPR_D(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void sheepshaver_cpu::execute_emul_op_idle_time_1()
|
|
|
|
{
|
|
|
|
// Sleep if no events pending
|
|
|
|
if (ReadMacInt32(0x14c) == 0)
|
|
|
|
Delay_usec(16667);
|
|
|
|
GPR_A(0) = ReadMacInt32(0x2b6);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sheepshaver_cpu::execute_emul_op_idle_time_2()
|
|
|
|
{
|
|
|
|
// Sleep if no events pending
|
|
|
|
if (ReadMacInt32(0x14c) == 0)
|
|
|
|
Delay_usec(16667);
|
|
|
|
GPR_D(0) = (uint32)-2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out EMUL_OP routines that only call native code
|
|
|
|
bool sheepshaver_cpu::filter_execute_emul_op(uint32 emul_op)
|
|
|
|
{
|
|
|
|
switch (emul_op) {
|
|
|
|
case OP_MICROSECONDS:
|
|
|
|
execute_emul_op_microseconds();
|
|
|
|
return true;
|
|
|
|
case OP_IDLE_TIME:
|
|
|
|
execute_emul_op_idle_time_1();
|
|
|
|
return true;
|
|
|
|
case OP_IDLE_TIME_2:
|
|
|
|
execute_emul_op_idle_time_2();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-01-24 11:28:06 +00:00
|
|
|
// Execute EMUL_OP routine
|
|
|
|
void sheepshaver_cpu::execute_emul_op(uint32 emul_op)
|
|
|
|
{
|
2004-05-15 17:26:28 +00:00
|
|
|
#if ENABLE_NATIVE_EMUL_OP
|
|
|
|
// First, filter out EMUL_OPs that can be executed without a mode switch
|
|
|
|
if (filter_execute_emul_op(emul_op))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2004-01-24 11:28:06 +00:00
|
|
|
M68kRegisters r68;
|
|
|
|
WriteMacInt32(XLM_68K_R25, gpr(25));
|
|
|
|
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
r68.d[i] = gpr(8 + i);
|
|
|
|
for (int i = 0; i < 7; i++)
|
|
|
|
r68.a[i] = gpr(16 + i);
|
|
|
|
r68.a[7] = gpr(1);
|
|
|
|
uint32 saved_cr = get_cr() & CR_field<2>::mask();
|
|
|
|
uint32 saved_xer = get_xer();
|
|
|
|
EmulOp(&r68, gpr(24), emul_op);
|
|
|
|
set_cr(saved_cr);
|
|
|
|
set_xer(saved_xer);
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
gpr(8 + i) = r68.d[i];
|
|
|
|
for (int i = 0; i < 7; i++)
|
|
|
|
gpr(16 + i) = r68.a[i];
|
|
|
|
gpr(1) = r68.a[7];
|
|
|
|
WriteMacInt32(XLM_RUN_MODE, MODE_68K);
|
|
|
|
}
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Execute SheepShaver instruction
|
|
|
|
void sheepshaver_cpu::execute_sheep(uint32 opcode)
|
|
|
|
{
|
|
|
|
// D(bug("Extended opcode %08x at %08x (68k pc %08x)\n", opcode, pc(), gpr(24)));
|
|
|
|
assert((((opcode >> 26) & 0x3f) == 6) && OP_MAX <= 64 + 3);
|
|
|
|
|
|
|
|
switch (opcode & 0x3f) {
|
|
|
|
case 0: // EMUL_RETURN
|
|
|
|
QuitEmulator();
|
|
|
|
break;
|
2003-10-19 21:37:43 +00:00
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
case 1: // EXEC_RETURN
|
2003-11-01 15:15:31 +00:00
|
|
|
spcflags().set(SPCFLAG_CPU_EXEC_RETURN);
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // EXEC_NATIVE
|
2004-05-19 21:23:17 +00:00
|
|
|
execute_native_op(NATIVE_OP_field::extract(opcode));
|
2003-09-28 21:27:34 +00:00
|
|
|
if (FN_field::test(opcode))
|
|
|
|
pc() = lr();
|
|
|
|
else
|
|
|
|
pc() += 4;
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
|
|
|
|
2004-01-24 11:28:06 +00:00
|
|
|
default: // EMUL_OP
|
|
|
|
execute_emul_op(EMUL_OP_field::extract(opcode) - 3);
|
2003-09-07 14:25:05 +00:00
|
|
|
pc() += 4;
|
|
|
|
break;
|
|
|
|
}
|
2004-01-24 11:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile one instruction
|
2004-05-19 21:23:17 +00:00
|
|
|
int sheepshaver_cpu::compile1(codegen_context_t & cg_context)
|
2004-01-24 11:28:06 +00:00
|
|
|
{
|
|
|
|
#if PPC_ENABLE_JIT
|
|
|
|
const instr_info_t *ii = cg_context.instr_info;
|
|
|
|
if (ii->mnemo != PPC_I(SHEEP))
|
2004-05-19 21:23:17 +00:00
|
|
|
return COMPILE_FAILURE;
|
2004-01-24 11:28:06 +00:00
|
|
|
|
2004-05-19 21:23:17 +00:00
|
|
|
int status = COMPILE_FAILURE;
|
2004-01-24 11:28:06 +00:00
|
|
|
powerpc_dyngen & dg = cg_context.codegen;
|
|
|
|
uint32 opcode = cg_context.opcode;
|
|
|
|
|
|
|
|
switch (opcode & 0x3f) {
|
|
|
|
case 0: // EMUL_RETURN
|
|
|
|
dg.gen_invoke(QuitEmulator);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // EXEC_RETURN
|
|
|
|
dg.gen_spcflags_set(SPCFLAG_CPU_EXEC_RETURN);
|
2004-05-19 21:23:17 +00:00
|
|
|
// Don't check for pending interrupts, we do know we have to
|
|
|
|
// get out of this block ASAP
|
|
|
|
dg.gen_exec_return();
|
|
|
|
status = COMPILE_EPILOGUE_OK;
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: { // EXEC_NATIVE
|
|
|
|
uint32 selector = NATIVE_OP_field::extract(opcode);
|
|
|
|
switch (selector) {
|
2004-05-19 21:23:17 +00:00
|
|
|
#if !PPC_REENTRANT_JIT
|
|
|
|
// Filter out functions that may invoke Execute68k() or
|
|
|
|
// CallMacOS(), this would break reentrancy as they could
|
|
|
|
// invalidate the translation cache and even overwrite
|
|
|
|
// continuation code when we are done with them.
|
2004-01-24 11:28:06 +00:00
|
|
|
case NATIVE_PATCH_NAME_REGISTRY:
|
|
|
|
dg.gen_invoke(DoPatchNameRegistry);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_VIDEO_INSTALL_ACCEL:
|
|
|
|
dg.gen_invoke(VideoInstallAccel);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_VIDEO_VBL:
|
|
|
|
dg.gen_invoke(VideoVBL);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_GET_RESOURCE:
|
|
|
|
case NATIVE_GET_1_RESOURCE:
|
|
|
|
case NATIVE_GET_IND_RESOURCE:
|
|
|
|
case NATIVE_GET_1_IND_RESOURCE:
|
|
|
|
case NATIVE_R_GET_RESOURCE: {
|
|
|
|
static const uint32 get_resource_ptr[] = {
|
|
|
|
XLM_GET_RESOURCE,
|
|
|
|
XLM_GET_1_RESOURCE,
|
|
|
|
XLM_GET_IND_RESOURCE,
|
|
|
|
XLM_GET_1_IND_RESOURCE,
|
|
|
|
XLM_R_GET_RESOURCE
|
|
|
|
};
|
|
|
|
uint32 old_get_resource = ReadMacInt32(get_resource_ptr[selector - NATIVE_GET_RESOURCE]);
|
|
|
|
typedef void (*func_t)(dyngen_cpu_base, uint32);
|
|
|
|
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::get_resource).ptr();
|
|
|
|
dg.gen_invoke_CPU_im(func, old_get_resource);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NATIVE_CHECK_LOAD_INVOC:
|
|
|
|
dg.gen_load_T0_GPR(3);
|
|
|
|
dg.gen_load_T1_GPR(4);
|
|
|
|
dg.gen_se_16_32_T1();
|
|
|
|
dg.gen_load_T2_GPR(5);
|
|
|
|
dg.gen_invoke_T0_T1_T2((void (*)(uint32, uint32, uint32))check_load_invoc);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
|
|
|
break;
|
|
|
|
#endif
|
2004-04-22 22:54:47 +00:00
|
|
|
case NATIVE_BITBLT:
|
|
|
|
dg.gen_load_T0_GPR(3);
|
|
|
|
dg.gen_invoke_T0((void (*)(uint32))NQD_bitblt);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-04-22 22:54:47 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_INVRECT:
|
|
|
|
dg.gen_load_T0_GPR(3);
|
|
|
|
dg.gen_invoke_T0((void (*)(uint32))NQD_invrect);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-04-22 22:54:47 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_FILLRECT:
|
|
|
|
dg.gen_load_T0_GPR(3);
|
|
|
|
dg.gen_invoke_T0((void (*)(uint32))NQD_fillrect);
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-04-22 22:54:47 +00:00
|
|
|
break;
|
2004-01-24 11:28:06 +00:00
|
|
|
}
|
2004-05-19 21:23:17 +00:00
|
|
|
// Could we fully translate this NativeOp?
|
2004-05-23 05:28:12 +00:00
|
|
|
if (status == COMPILE_CODE_OK) {
|
|
|
|
if (!FN_field::test(opcode))
|
|
|
|
cg_context.done_compile = false;
|
|
|
|
else {
|
2004-01-24 11:28:06 +00:00
|
|
|
dg.gen_load_A0_LR();
|
|
|
|
dg.gen_set_PC_A0();
|
2004-05-23 05:28:12 +00:00
|
|
|
cg_context.done_compile = true;
|
2004-01-24 11:28:06 +00:00
|
|
|
}
|
2004-05-19 21:23:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if PPC_REENTRANT_JIT
|
|
|
|
// Try to execute NativeOp trampoline
|
2004-05-23 05:28:12 +00:00
|
|
|
if (!FN_field::test(opcode))
|
|
|
|
dg.gen_set_PC_im(cg_context.pc + 4);
|
|
|
|
else {
|
|
|
|
dg.gen_load_A0_LR();
|
|
|
|
dg.gen_set_PC_A0();
|
|
|
|
}
|
2004-05-19 21:23:17 +00:00
|
|
|
dg.gen_mov_32_T0_im(selector);
|
|
|
|
dg.gen_jmp(native_op_trampoline);
|
|
|
|
cg_context.done_compile = true;
|
|
|
|
status = COMPILE_EPILOGUE_OK;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
// Invoke NativeOp handler
|
2004-05-23 05:28:12 +00:00
|
|
|
if (!FN_field::test(opcode)) {
|
|
|
|
typedef void (*func_t)(dyngen_cpu_base, uint32);
|
|
|
|
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr();
|
|
|
|
dg.gen_invoke_CPU_im(func, selector);
|
|
|
|
cg_context.done_compile = false;
|
|
|
|
status = COMPILE_CODE_OK;
|
|
|
|
}
|
|
|
|
// Otherwise, let it generate a call to execute_sheep() which
|
|
|
|
// will cause necessary updates to the program counter
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: { // EMUL_OP
|
2004-05-15 17:26:28 +00:00
|
|
|
uint32 emul_op = EMUL_OP_field::extract(opcode) - 3;
|
|
|
|
#if ENABLE_NATIVE_EMUL_OP
|
|
|
|
typedef void (*emul_op_func_t)(dyngen_cpu_base);
|
|
|
|
emul_op_func_t emul_op_func = 0;
|
|
|
|
switch (emul_op) {
|
|
|
|
case OP_MICROSECONDS:
|
|
|
|
emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_microseconds).ptr();
|
|
|
|
break;
|
|
|
|
case OP_IDLE_TIME:
|
|
|
|
emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_1).ptr();
|
|
|
|
break;
|
|
|
|
case OP_IDLE_TIME_2:
|
|
|
|
emul_op_func = (emul_op_func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op_idle_time_2).ptr();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (emul_op_func) {
|
|
|
|
dg.gen_invoke_CPU(emul_op_func);
|
|
|
|
cg_context.done_compile = false;
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-05-15 17:26:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2004-05-19 21:23:17 +00:00
|
|
|
#if PPC_REENTRANT_JIT
|
|
|
|
// Try to execute EmulOp trampoline
|
|
|
|
dg.gen_set_PC_im(cg_context.pc + 4);
|
|
|
|
dg.gen_mov_32_T0_im(emul_op);
|
|
|
|
dg.gen_jmp(emul_op_trampoline);
|
|
|
|
cg_context.done_compile = true;
|
|
|
|
status = COMPILE_EPILOGUE_OK;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
// Invoke EmulOp handler
|
2004-01-24 11:28:06 +00:00
|
|
|
typedef void (*func_t)(dyngen_cpu_base, uint32);
|
|
|
|
func_t func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
|
2004-05-15 17:26:28 +00:00
|
|
|
dg.gen_invoke_CPU_im(func, emul_op);
|
2004-01-24 11:28:06 +00:00
|
|
|
cg_context.done_compile = false;
|
2004-05-19 21:23:17 +00:00
|
|
|
status = COMPILE_CODE_OK;
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
2004-05-19 21:23:17 +00:00
|
|
|
return status;
|
2004-01-24 11:28:06 +00:00
|
|
|
#endif
|
2004-05-19 21:23:17 +00:00
|
|
|
return COMPILE_FAILURE;
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
2004-05-20 11:05:30 +00:00
|
|
|
// CPU context to preserve on interrupt
|
|
|
|
sheepshaver_cpu::interrupt_context::interrupt_context(sheepshaver_cpu *_cpu, const char *_where)
|
|
|
|
{
|
|
|
|
#if SAFE_INTERRUPT_PPC >= 2
|
|
|
|
cpu = _cpu;
|
|
|
|
where = _where;
|
|
|
|
|
|
|
|
// Save interrupt context
|
|
|
|
memcpy(&gpr[0], &cpu->gpr(0), sizeof(gpr));
|
|
|
|
pc = cpu->pc();
|
|
|
|
lr = cpu->lr();
|
|
|
|
ctr = cpu->ctr();
|
|
|
|
cr = cpu->get_cr();
|
|
|
|
xer = cpu->get_xer();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
sheepshaver_cpu::interrupt_context::~interrupt_context()
|
|
|
|
{
|
|
|
|
#if SAFE_INTERRUPT_PPC >= 2
|
|
|
|
// Check whether CPU context was preserved by interrupt
|
|
|
|
if (memcmp(&gpr[0], &cpu->gpr(0), sizeof(gpr)) != 0) {
|
|
|
|
printf("FATAL: %s: interrupt clobbers registers\n", where);
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
if (gpr[i] != cpu->gpr(i))
|
|
|
|
printf(" r%d: %08x -> %08x\n", i, gpr[i], cpu->gpr(i));
|
|
|
|
}
|
|
|
|
if (pc != cpu->pc())
|
|
|
|
printf("FATAL: %s: interrupt clobbers PC\n", where);
|
|
|
|
if (lr != cpu->lr())
|
|
|
|
printf("FATAL: %s: interrupt clobbers LR\n", where);
|
|
|
|
if (ctr != cpu->ctr())
|
|
|
|
printf("FATAL: %s: interrupt clobbers CTR\n", where);
|
|
|
|
if (cr != cpu->get_cr())
|
|
|
|
printf("FATAL: %s: interrupt clobbers CR\n", where);
|
|
|
|
if (xer != cpu->get_xer())
|
|
|
|
printf("FATAL: %s: interrupt clobbers XER\n", where);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Handle MacOS interrupt
|
2003-09-29 15:46:09 +00:00
|
|
|
void sheepshaver_cpu::interrupt(uint32 entry)
|
2003-09-07 14:25:05 +00:00
|
|
|
{
|
2003-11-04 20:48:29 +00:00
|
|
|
#if EMUL_TIME_STATS
|
2004-06-05 07:09:38 +00:00
|
|
|
ppc_interrupt_count++;
|
2003-11-04 20:48:29 +00:00
|
|
|
const clock_t interrupt_start = clock();
|
|
|
|
#endif
|
|
|
|
|
2004-05-12 15:54:23 +00:00
|
|
|
#if SAFE_INTERRUPT_PPC
|
|
|
|
static int depth = 0;
|
|
|
|
if (depth != 0)
|
|
|
|
printf("FATAL: sheepshaver_cpu::interrupt() called more than once: %d\n", depth);
|
|
|
|
depth++;
|
|
|
|
#endif
|
|
|
|
|
2003-09-28 21:27:34 +00:00
|
|
|
// Save program counters and branch registers
|
|
|
|
uint32 saved_pc = pc();
|
|
|
|
uint32 saved_lr = lr();
|
|
|
|
uint32 saved_ctr= ctr();
|
2003-09-29 15:46:09 +00:00
|
|
|
uint32 saved_sp = gpr(1);
|
2003-09-28 21:27:34 +00:00
|
|
|
|
2003-09-29 15:46:09 +00:00
|
|
|
// Initialize stack pointer to SheepShaver alternate stack base
|
2003-12-05 13:37:56 +00:00
|
|
|
gpr(1) = SignalStackBase() - 64;
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
// Build trampoline to return from interrupt
|
2003-12-04 17:26:38 +00:00
|
|
|
SheepVar32 trampoline = POWERPC_EXEC_RETURN;
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
// Prepare registers for nanokernel interrupt routine
|
2003-09-29 22:50:31 +00:00
|
|
|
kernel_data->v[0x004 >> 2] = htonl(gpr(1));
|
|
|
|
kernel_data->v[0x018 >> 2] = htonl(gpr(6));
|
2003-09-07 14:25:05 +00:00
|
|
|
|
2003-09-29 22:50:31 +00:00
|
|
|
gpr(6) = ntohl(kernel_data->v[0x65c >> 2]);
|
2003-09-28 21:27:34 +00:00
|
|
|
assert(gpr(6) != 0);
|
2003-09-07 14:25:05 +00:00
|
|
|
WriteMacInt32(gpr(6) + 0x13c, gpr(7));
|
|
|
|
WriteMacInt32(gpr(6) + 0x144, gpr(8));
|
|
|
|
WriteMacInt32(gpr(6) + 0x14c, gpr(9));
|
|
|
|
WriteMacInt32(gpr(6) + 0x154, gpr(10));
|
|
|
|
WriteMacInt32(gpr(6) + 0x15c, gpr(11));
|
|
|
|
WriteMacInt32(gpr(6) + 0x164, gpr(12));
|
|
|
|
WriteMacInt32(gpr(6) + 0x16c, gpr(13));
|
|
|
|
|
|
|
|
gpr(1) = KernelDataAddr;
|
2003-09-29 22:50:31 +00:00
|
|
|
gpr(7) = ntohl(kernel_data->v[0x660 >> 2]);
|
2003-09-07 14:25:05 +00:00
|
|
|
gpr(8) = 0;
|
2003-12-04 17:26:38 +00:00
|
|
|
gpr(10) = trampoline.addr();
|
|
|
|
gpr(12) = trampoline.addr();
|
2003-10-19 21:37:43 +00:00
|
|
|
gpr(13) = get_cr();
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
// rlwimi. r7,r7,8,0,0
|
|
|
|
uint32 result = op_ppc_rlwimi::apply(gpr(7), 8, 0x80000000, gpr(7));
|
|
|
|
record_cr0(result);
|
|
|
|
gpr(7) = result;
|
|
|
|
|
|
|
|
gpr(11) = 0xf072; // MSR (SRR1)
|
2003-10-19 21:37:43 +00:00
|
|
|
cr().set((gpr(11) & 0x0fff0000) | (get_cr() & ~0x0fff0000));
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
// Enter nanokernel
|
|
|
|
execute(entry);
|
|
|
|
|
2003-09-28 21:27:34 +00:00
|
|
|
// Restore program counters and branch registers
|
|
|
|
pc() = saved_pc;
|
|
|
|
lr() = saved_lr;
|
|
|
|
ctr()= saved_ctr;
|
2003-09-29 15:46:09 +00:00
|
|
|
gpr(1) = saved_sp;
|
2003-11-04 20:48:29 +00:00
|
|
|
|
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
interrupt_time += (clock() - interrupt_start);
|
|
|
|
#endif
|
2004-05-12 15:54:23 +00:00
|
|
|
|
|
|
|
#if SAFE_INTERRUPT_PPC
|
|
|
|
depth--;
|
|
|
|
#endif
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute 68k routine
|
|
|
|
void sheepshaver_cpu::execute_68k(uint32 entry, M68kRegisters *r)
|
|
|
|
{
|
2003-11-04 20:48:29 +00:00
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
exec68k_count++;
|
|
|
|
const clock_t exec68k_start = clock();
|
|
|
|
#endif
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
#if SAFE_EXEC_68K
|
|
|
|
if (ReadMacInt32(XLM_RUN_MODE) != MODE_EMUL_OP)
|
|
|
|
printf("FATAL: Execute68k() not called from EMUL_OP mode\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Save program counters and branch registers
|
|
|
|
uint32 saved_pc = pc();
|
|
|
|
uint32 saved_lr = lr();
|
|
|
|
uint32 saved_ctr= ctr();
|
2003-10-19 21:37:43 +00:00
|
|
|
uint32 saved_cr = get_cr();
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
// Create MacOS stack frame
|
2003-10-11 09:33:27 +00:00
|
|
|
// FIXME: make sure MacOS doesn't expect PPC registers to live on top
|
2003-09-07 14:25:05 +00:00
|
|
|
uint32 sp = gpr(1);
|
2003-10-11 09:33:27 +00:00
|
|
|
gpr(1) -= 56;
|
2003-09-07 14:25:05 +00:00
|
|
|
WriteMacInt32(gpr(1), sp);
|
|
|
|
|
|
|
|
// Save PowerPC registers
|
2003-10-11 09:33:27 +00:00
|
|
|
uint32 saved_GPRs[19];
|
|
|
|
memcpy(&saved_GPRs[0], &gpr(13), sizeof(uint32)*(32-13));
|
2003-09-07 14:25:05 +00:00
|
|
|
#if SAVE_FP_EXEC_68K
|
2003-10-11 09:33:27 +00:00
|
|
|
double saved_FPRs[18];
|
|
|
|
memcpy(&saved_FPRs[0], &fpr(14), sizeof(double)*(32-14));
|
2003-09-07 14:25:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Setup registers for 68k emulator
|
2003-09-28 21:27:34 +00:00
|
|
|
cr().set(CR_SO_field<2>::mask()); // Supervisor mode
|
2003-09-07 14:25:05 +00:00
|
|
|
for (int i = 0; i < 8; i++) // d[0]..d[7]
|
|
|
|
gpr(8 + i) = r->d[i];
|
|
|
|
for (int i = 0; i < 7; i++) // a[0]..a[6]
|
|
|
|
gpr(16 + i) = r->a[i];
|
|
|
|
gpr(23) = 0;
|
|
|
|
gpr(24) = entry;
|
|
|
|
gpr(25) = ReadMacInt32(XLM_68K_R25); // MSB of SR
|
|
|
|
gpr(26) = 0;
|
|
|
|
gpr(28) = 0; // VBR
|
2003-09-29 22:50:31 +00:00
|
|
|
gpr(29) = ntohl(kernel_data->ed.v[0x74 >> 2]); // Pointer to opcode table
|
|
|
|
gpr(30) = ntohl(kernel_data->ed.v[0x78 >> 2]); // Address of emulator
|
2003-09-07 14:25:05 +00:00
|
|
|
gpr(31) = KernelDataAddr + 0x1000;
|
|
|
|
|
|
|
|
// Push return address (points to EXEC_RETURN opcode) on stack
|
|
|
|
gpr(1) -= 4;
|
|
|
|
WriteMacInt32(gpr(1), XLM_EXEC_RETURN_OPCODE);
|
|
|
|
|
|
|
|
// Rentering 68k emulator
|
|
|
|
WriteMacInt32(XLM_RUN_MODE, MODE_68K);
|
|
|
|
|
|
|
|
// Set r0 to 0 for 68k emulator
|
|
|
|
gpr(0) = 0;
|
|
|
|
|
|
|
|
// Execute 68k opcode
|
|
|
|
uint32 opcode = ReadMacInt16(gpr(24));
|
|
|
|
gpr(27) = (int32)(int16)ReadMacInt16(gpr(24) += 2);
|
|
|
|
gpr(29) += opcode * 8;
|
|
|
|
execute(gpr(29));
|
|
|
|
|
|
|
|
// Save r25 (contains current 68k interrupt level)
|
|
|
|
WriteMacInt32(XLM_68K_R25, gpr(25));
|
|
|
|
|
|
|
|
// Reentering EMUL_OP mode
|
|
|
|
WriteMacInt32(XLM_RUN_MODE, MODE_EMUL_OP);
|
|
|
|
|
|
|
|
// Save 68k registers
|
|
|
|
for (int i = 0; i < 8; i++) // d[0]..d[7]
|
|
|
|
r->d[i] = gpr(8 + i);
|
|
|
|
for (int i = 0; i < 7; i++) // a[0]..a[6]
|
|
|
|
r->a[i] = gpr(16 + i);
|
|
|
|
|
|
|
|
// Restore PowerPC registers
|
2003-10-11 09:33:27 +00:00
|
|
|
memcpy(&gpr(13), &saved_GPRs[0], sizeof(uint32)*(32-13));
|
2003-09-07 14:25:05 +00:00
|
|
|
#if SAVE_FP_EXEC_68K
|
2003-10-11 09:33:27 +00:00
|
|
|
memcpy(&fpr(14), &saved_FPRs[0], sizeof(double)*(32-14));
|
2003-09-07 14:25:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Cleanup stack
|
2003-10-11 09:33:27 +00:00
|
|
|
gpr(1) += 56;
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
// Restore program counters and branch registers
|
|
|
|
pc() = saved_pc;
|
|
|
|
lr() = saved_lr;
|
|
|
|
ctr()= saved_ctr;
|
2003-10-19 21:37:43 +00:00
|
|
|
set_cr(saved_cr);
|
2003-11-04 20:48:29 +00:00
|
|
|
|
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
exec68k_time += (clock() - exec68k_start);
|
|
|
|
#endif
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call MacOS PPC code
|
|
|
|
uint32 sheepshaver_cpu::execute_macos_code(uint32 tvect, int nargs, uint32 const *args)
|
|
|
|
{
|
2003-11-04 20:48:29 +00:00
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
macos_exec_count++;
|
|
|
|
const clock_t macos_exec_start = clock();
|
|
|
|
#endif
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Save program counters and branch registers
|
|
|
|
uint32 saved_pc = pc();
|
|
|
|
uint32 saved_lr = lr();
|
|
|
|
uint32 saved_ctr= ctr();
|
|
|
|
|
|
|
|
// Build trampoline with EXEC_RETURN
|
2003-12-04 17:26:38 +00:00
|
|
|
SheepVar32 trampoline = POWERPC_EXEC_RETURN;
|
|
|
|
lr() = trampoline.addr();
|
2003-09-07 14:25:05 +00:00
|
|
|
|
|
|
|
gpr(1) -= 64; // Create stack frame
|
|
|
|
uint32 proc = ReadMacInt32(tvect); // Get routine address
|
|
|
|
uint32 toc = ReadMacInt32(tvect + 4); // Get TOC pointer
|
|
|
|
|
|
|
|
// Save PowerPC registers
|
|
|
|
uint32 regs[8];
|
|
|
|
regs[0] = gpr(2);
|
|
|
|
for (int i = 0; i < nargs; i++)
|
|
|
|
regs[i + 1] = gpr(i + 3);
|
|
|
|
|
|
|
|
// Prepare and call MacOS routine
|
|
|
|
gpr(2) = toc;
|
|
|
|
for (int i = 0; i < nargs; i++)
|
|
|
|
gpr(i + 3) = args[i];
|
|
|
|
execute(proc);
|
|
|
|
uint32 retval = gpr(3);
|
|
|
|
|
|
|
|
// Restore PowerPC registers
|
|
|
|
for (int i = 0; i <= nargs; i++)
|
|
|
|
gpr(i + 2) = regs[i];
|
|
|
|
|
|
|
|
// Cleanup stack
|
|
|
|
gpr(1) += 64;
|
|
|
|
|
|
|
|
// Restore program counters and branch registers
|
|
|
|
pc() = saved_pc;
|
|
|
|
lr() = saved_lr;
|
|
|
|
ctr()= saved_ctr;
|
|
|
|
|
2003-11-04 20:48:29 +00:00
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
macos_exec_time += (clock() - macos_exec_start);
|
|
|
|
#endif
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-09-28 21:27:34 +00:00
|
|
|
// Execute ppc routine
|
|
|
|
inline void sheepshaver_cpu::execute_ppc(uint32 entry)
|
|
|
|
{
|
|
|
|
// Save branch registers
|
|
|
|
uint32 saved_lr = lr();
|
|
|
|
|
2003-12-04 17:26:38 +00:00
|
|
|
SheepVar32 trampoline = POWERPC_EXEC_RETURN;
|
|
|
|
WriteMacInt32(trampoline.addr(), POWERPC_EXEC_RETURN);
|
|
|
|
lr() = trampoline.addr();
|
2003-10-11 09:33:27 +00:00
|
|
|
|
2003-09-28 21:27:34 +00:00
|
|
|
execute(entry);
|
|
|
|
|
|
|
|
// Restore branch registers
|
|
|
|
lr() = saved_lr;
|
|
|
|
}
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Resource Manager thunk
|
|
|
|
inline void sheepshaver_cpu::get_resource(uint32 old_get_resource)
|
|
|
|
{
|
2003-09-28 21:27:34 +00:00
|
|
|
uint32 type = gpr(3);
|
|
|
|
int16 id = gpr(4);
|
|
|
|
|
|
|
|
// Create stack frame
|
|
|
|
gpr(1) -= 56;
|
|
|
|
|
|
|
|
// Call old routine
|
|
|
|
execute_ppc(old_get_resource);
|
|
|
|
|
|
|
|
// Call CheckLoad()
|
2003-09-29 22:50:31 +00:00
|
|
|
uint32 handle = gpr(3);
|
2003-09-28 21:27:34 +00:00
|
|
|
check_load_invoc(type, id, handle);
|
2003-09-29 22:50:31 +00:00
|
|
|
gpr(3) = handle;
|
2003-09-28 21:27:34 +00:00
|
|
|
|
|
|
|
// Cleanup stack
|
|
|
|
gpr(1) += 56;
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SheepShaver CPU engine interface
|
|
|
|
**/
|
|
|
|
|
2004-05-20 12:33:58 +00:00
|
|
|
// PowerPC CPU emulator
|
|
|
|
static sheepshaver_cpu *ppc_cpu = NULL;
|
2003-09-07 14:25:05 +00:00
|
|
|
|
2003-10-12 05:44:17 +00:00
|
|
|
void FlushCodeCache(uintptr start, uintptr end)
|
|
|
|
{
|
|
|
|
D(bug("FlushCodeCache(%08x, %08x)\n", start, end));
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->invalidate_cache_range(start, end);
|
2003-09-28 21:27:34 +00:00
|
|
|
}
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Dump PPC registers
|
|
|
|
static void dump_registers(void)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->dump_registers();
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump log
|
|
|
|
static void dump_log(void)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->dump_log();
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize CPU emulation
|
|
|
|
*/
|
|
|
|
|
2003-09-29 07:05:15 +00:00
|
|
|
static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
|
2003-09-07 14:25:05 +00:00
|
|
|
{
|
|
|
|
#if ENABLE_VOSF
|
2003-09-29 07:05:15 +00:00
|
|
|
// Handle screen fault
|
|
|
|
extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t);
|
|
|
|
if (Screen_fault_handler(fault_address, fault_instruction))
|
|
|
|
return SIGSEGV_RETURN_SUCCESS;
|
2003-09-07 14:25:05 +00:00
|
|
|
#endif
|
2003-09-29 07:05:15 +00:00
|
|
|
|
|
|
|
const uintptr addr = (uintptr)fault_address;
|
|
|
|
#if HAVE_SIGSEGV_SKIP_INSTRUCTION
|
|
|
|
// Ignore writes to ROM
|
|
|
|
if ((addr - ROM_BASE) < ROM_SIZE)
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
|
2003-11-10 16:23:58 +00:00
|
|
|
// Get program counter of target CPU
|
2004-05-20 12:33:58 +00:00
|
|
|
sheepshaver_cpu * const cpu = ppc_cpu;
|
2003-11-10 16:23:58 +00:00
|
|
|
const uint32 pc = cpu->pc();
|
|
|
|
|
|
|
|
// Fault in Mac ROM or RAM?
|
2004-05-31 10:08:31 +00:00
|
|
|
bool mac_fault = (pc >= ROM_BASE) && (pc < (ROM_BASE + ROM_AREA_SIZE)) || (pc >= RAMBase) && (pc < (RAMBase + RAMSize)) || (pc >= DR_CACHE_BASE && pc < (DR_CACHE_BASE + DR_CACHE_SIZE));
|
2003-11-10 16:23:58 +00:00
|
|
|
if (mac_fault) {
|
|
|
|
|
|
|
|
// "VM settings" during MacOS 8 installation
|
|
|
|
if (pc == ROM_BASE + 0x488160 && cpu->gpr(20) == 0xf8000000)
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
|
|
|
|
// MacOS 8.5 installation
|
|
|
|
else if (pc == ROM_BASE + 0x488140 && cpu->gpr(16) == 0xf8000000)
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
|
|
|
|
// MacOS 8 serial drivers on startup
|
|
|
|
else if (pc == ROM_BASE + 0x48e080 && (cpu->gpr(8) == 0xf3012002 || cpu->gpr(8) == 0xf3012000))
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
|
|
|
|
// MacOS 8.1 serial drivers on startup
|
|
|
|
else if (pc == ROM_BASE + 0x48c5e0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
else if (pc == ROM_BASE + 0x4a10a0 && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
2004-05-31 10:08:31 +00:00
|
|
|
|
|
|
|
// MacOS 8.6 serial drivers on startup (with DR Cache and OldWorld ROM)
|
|
|
|
else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(16) == 0xf3012002 || cpu->gpr(16) == 0xf3012000))
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
else if ((pc - DR_CACHE_BASE) < DR_CACHE_SIZE && (cpu->gpr(20) == 0xf3012002 || cpu->gpr(20) == 0xf3012000))
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
2003-11-10 16:23:58 +00:00
|
|
|
|
2004-02-24 11:12:54 +00:00
|
|
|
// Ignore writes to the zero page
|
|
|
|
else if ((uint32)(addr - SheepMem::ZeroPage()) < (uint32)SheepMem::PageSize())
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
|
2003-11-10 16:23:58 +00:00
|
|
|
// Ignore all other faults, if requested
|
|
|
|
if (PrefsFindBool("ignoresegv"))
|
|
|
|
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
|
|
|
|
}
|
2003-09-07 14:25:05 +00:00
|
|
|
#else
|
2003-09-29 07:05:15 +00:00
|
|
|
#error "FIXME: You don't have the capability to skip instruction within signal handlers"
|
2003-09-07 14:25:05 +00:00
|
|
|
#endif
|
2003-09-29 07:05:15 +00:00
|
|
|
|
|
|
|
printf("SIGSEGV\n");
|
|
|
|
printf(" pc %p\n", fault_instruction);
|
|
|
|
printf(" ea %p\n", fault_address);
|
|
|
|
dump_registers();
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->dump_log();
|
2003-09-07 14:25:05 +00:00
|
|
|
enter_mon();
|
|
|
|
QuitEmulator();
|
2003-09-29 07:05:15 +00:00
|
|
|
|
|
|
|
return SIGSEGV_RETURN_FAILURE;
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_emul_ppc(void)
|
|
|
|
{
|
|
|
|
// Initialize main CPU emulator
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu = new sheepshaver_cpu();
|
|
|
|
ppc_cpu->set_register(powerpc_registers::GPR(3), any_register((uint32)ROM_BASE + 0x30d000));
|
|
|
|
ppc_cpu->set_register(powerpc_registers::GPR(4), any_register(KernelDataAddr + 0x1000));
|
2003-09-07 14:25:05 +00:00
|
|
|
WriteMacInt32(XLM_RUN_MODE, MODE_68K);
|
|
|
|
|
2003-09-29 07:05:15 +00:00
|
|
|
// Install the handler for SIGSEGV
|
|
|
|
sigsegv_install_handler(sigsegv_handler);
|
2003-09-29 15:46:09 +00:00
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
#if ENABLE_MON
|
|
|
|
// Install "regs" command in cxmon
|
|
|
|
mon_add_command("regs", dump_registers, "regs Dump PowerPC registers\n");
|
|
|
|
mon_add_command("log", dump_log, "log Dump PowerPC emulation log\n");
|
|
|
|
#endif
|
2003-11-04 20:48:29 +00:00
|
|
|
|
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
emul_start_time = clock();
|
|
|
|
#endif
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
2003-11-03 21:28:32 +00:00
|
|
|
/*
|
|
|
|
* Deinitialize emulation
|
|
|
|
*/
|
|
|
|
|
|
|
|
void exit_emul_ppc(void)
|
|
|
|
{
|
2003-11-04 20:48:29 +00:00
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
clock_t emul_end_time = clock();
|
|
|
|
|
|
|
|
printf("### Statistics for SheepShaver emulation parts\n");
|
|
|
|
const clock_t emul_time = emul_end_time - emul_start_time;
|
|
|
|
printf("Total emulation time : %.1f sec\n", double(emul_time) / double(CLOCKS_PER_SEC));
|
|
|
|
printf("Total interrupt count: %d (%2.1f Hz)\n", interrupt_count,
|
|
|
|
(double(interrupt_count) * CLOCKS_PER_SEC) / double(emul_time));
|
2004-06-05 07:09:38 +00:00
|
|
|
printf("Total ppc interrupt count: %d (%2.1f %%)\n", ppc_interrupt_count,
|
|
|
|
(double(ppc_interrupt_count) * 100.0) / double(interrupt_count));
|
2003-11-04 20:48:29 +00:00
|
|
|
|
|
|
|
#define PRINT_STATS(LABEL, VAR_PREFIX) do { \
|
|
|
|
printf("Total " LABEL " count : %d\n", VAR_PREFIX##_count); \
|
|
|
|
printf("Total " LABEL " time : %.1f sec (%.1f%%)\n", \
|
|
|
|
double(VAR_PREFIX##_time) / double(CLOCKS_PER_SEC), \
|
|
|
|
100.0 * double(VAR_PREFIX##_time) / double(emul_time)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
PRINT_STATS("Execute68k[Trap] execution", exec68k);
|
|
|
|
PRINT_STATS("NativeOp execution", native_exec);
|
|
|
|
PRINT_STATS("MacOS routine execution", macos_exec);
|
|
|
|
|
|
|
|
#undef PRINT_STATS
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
2004-05-20 12:33:58 +00:00
|
|
|
delete ppc_cpu;
|
2003-11-03 21:28:32 +00:00
|
|
|
}
|
|
|
|
|
2004-05-19 21:23:17 +00:00
|
|
|
#if PPC_ENABLE_JIT && PPC_REENTRANT_JIT
|
|
|
|
// Initialize EmulOp trampolines
|
|
|
|
void init_emul_op_trampolines(basic_dyngen & dg)
|
|
|
|
{
|
|
|
|
typedef void (*func_t)(dyngen_cpu_base, uint32);
|
|
|
|
func_t func;
|
|
|
|
|
|
|
|
// EmulOp
|
|
|
|
emul_op_trampoline = dg.gen_start();
|
|
|
|
func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_emul_op).ptr();
|
|
|
|
dg.gen_invoke_CPU_T0(func);
|
|
|
|
dg.gen_exec_return();
|
|
|
|
dg.gen_end();
|
|
|
|
|
|
|
|
// NativeOp
|
|
|
|
native_op_trampoline = dg.gen_start();
|
|
|
|
func = (func_t)nv_mem_fun(&sheepshaver_cpu::execute_native_op).ptr();
|
|
|
|
dg.gen_invoke_CPU_T0(func);
|
|
|
|
dg.gen_exec_return();
|
|
|
|
dg.gen_end();
|
|
|
|
|
|
|
|
D(bug("EmulOp trampoline: %p\n", emul_op_trampoline));
|
|
|
|
D(bug("NativeOp trampoline: %p\n", native_op_trampoline));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
/*
|
|
|
|
* Emulation loop
|
|
|
|
*/
|
|
|
|
|
|
|
|
void emul_ppc(uint32 entry)
|
|
|
|
{
|
2003-12-25 23:54:36 +00:00
|
|
|
#if 0
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->start_log();
|
2003-10-26 13:59:04 +00:00
|
|
|
#endif
|
|
|
|
// start emulation loop and enable code translation or caching
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->execute(entry);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle PowerPC interrupt
|
|
|
|
*/
|
|
|
|
|
2003-09-28 21:27:34 +00:00
|
|
|
void TriggerInterrupt(void)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
WriteMacInt32(0x16a, ReadMacInt32(0x16a) + 1);
|
|
|
|
#else
|
2003-10-26 13:59:04 +00:00
|
|
|
// Trigger interrupt to main cpu only
|
2004-05-20 12:33:58 +00:00
|
|
|
if (ppc_cpu)
|
|
|
|
ppc_cpu->trigger_interrupt();
|
2003-09-28 21:27:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-10-26 13:59:04 +00:00
|
|
|
void sheepshaver_cpu::handle_interrupt(void)
|
2003-09-07 14:25:05 +00:00
|
|
|
{
|
|
|
|
// Do nothing if interrupts are disabled
|
2004-06-22 17:10:08 +00:00
|
|
|
if (int32(ReadMacInt32(XLM_IRQ_NEST)) > 0)
|
2003-09-07 14:25:05 +00:00
|
|
|
return;
|
|
|
|
|
2004-05-20 11:47:27 +00:00
|
|
|
// Current interrupt nest level
|
|
|
|
static int interrupt_depth = 0;
|
|
|
|
++interrupt_depth;
|
2004-06-05 07:09:38 +00:00
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
interrupt_count++;
|
|
|
|
#endif
|
2004-05-20 11:47:27 +00:00
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Disable MacOS stack sniffer
|
|
|
|
WriteMacInt32(0x110, 0);
|
|
|
|
|
|
|
|
// Interrupt action depends on current run mode
|
|
|
|
switch (ReadMacInt32(XLM_RUN_MODE)) {
|
|
|
|
case MODE_68K:
|
|
|
|
// 68k emulator active, trigger 68k interrupt level 1
|
|
|
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
|
2003-10-26 13:59:04 +00:00
|
|
|
set_cr(get_cr() | tswap32(kernel_data->v[0x674 >> 2]));
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
#if INTERRUPTS_IN_NATIVE_MODE
|
|
|
|
case MODE_NATIVE:
|
|
|
|
// 68k emulator inactive, in nanokernel?
|
2004-05-20 11:47:27 +00:00
|
|
|
if (gpr(1) != KernelDataAddr && interrupt_depth == 1) {
|
2004-05-20 11:05:30 +00:00
|
|
|
interrupt_context ctx(this, "PowerPC mode");
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
// Prepare for 68k interrupt level 1
|
|
|
|
WriteMacInt16(tswap32(kernel_data->v[0x67c >> 2]), 1);
|
|
|
|
WriteMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc,
|
|
|
|
ReadMacInt32(tswap32(kernel_data->v[0x658 >> 2]) + 0xdc)
|
|
|
|
| tswap32(kernel_data->v[0x674 >> 2]));
|
|
|
|
|
|
|
|
// Execute nanokernel interrupt routine (this will activate the 68k emulator)
|
2003-09-28 21:27:34 +00:00
|
|
|
DisableInterrupt();
|
2003-09-07 14:25:05 +00:00
|
|
|
if (ROMType == ROMTYPE_NEWWORLD)
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->interrupt(ROM_BASE + 0x312b1c);
|
2003-09-07 14:25:05 +00:00
|
|
|
else
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->interrupt(ROM_BASE + 0x312a3c);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if INTERRUPTS_IN_EMUL_OP_MODE
|
|
|
|
case MODE_EMUL_OP:
|
|
|
|
// 68k emulator active, within EMUL_OP routine, execute 68k interrupt routine directly when interrupt level is 0
|
|
|
|
if ((ReadMacInt32(XLM_68K_R25) & 7) == 0) {
|
2004-05-20 11:05:30 +00:00
|
|
|
interrupt_context ctx(this, "68k mode");
|
2004-06-05 07:09:38 +00:00
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
const clock_t interrupt_start = clock();
|
|
|
|
#endif
|
2003-09-07 14:25:05 +00:00
|
|
|
#if 1
|
|
|
|
// Execute full 68k interrupt routine
|
|
|
|
M68kRegisters r;
|
|
|
|
uint32 old_r25 = ReadMacInt32(XLM_68K_R25); // Save interrupt level
|
|
|
|
WriteMacInt32(XLM_68K_R25, 0x21); // Execute with interrupt level 1
|
2003-09-28 21:27:34 +00:00
|
|
|
static const uint8 proc[] = {
|
|
|
|
0x3f, 0x3c, 0x00, 0x00, // move.w #$0000,-(sp) (fake format word)
|
|
|
|
0x48, 0x7a, 0x00, 0x0a, // pea @1(pc) (return address)
|
|
|
|
0x40, 0xe7, // move sr,-(sp) (saved SR)
|
|
|
|
0x20, 0x78, 0x00, 0x064, // move.l $64,a0
|
|
|
|
0x4e, 0xd0, // jmp (a0)
|
|
|
|
M68K_RTS >> 8, M68K_RTS & 0xff // @1
|
2003-09-07 14:25:05 +00:00
|
|
|
};
|
|
|
|
Execute68k((uint32)proc, &r);
|
|
|
|
WriteMacInt32(XLM_68K_R25, old_r25); // Restore interrupt level
|
|
|
|
#else
|
|
|
|
// Only update cursor
|
|
|
|
if (HasMacStarted()) {
|
|
|
|
if (InterruptFlags & INTFLAG_VIA) {
|
|
|
|
ClearInterruptFlag(INTFLAG_VIA);
|
|
|
|
ADBInterrupt();
|
2003-12-04 23:37:38 +00:00
|
|
|
ExecuteNative(NATIVE_VIDEO_VBL);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
2004-06-05 07:09:38 +00:00
|
|
|
#endif
|
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
interrupt_time += (clock() - interrupt_start);
|
2003-09-07 14:25:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2004-05-20 11:47:27 +00:00
|
|
|
|
|
|
|
// We are done with this interrupt
|
|
|
|
--interrupt_depth;
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_resource(void);
|
|
|
|
static void get_1_resource(void);
|
|
|
|
static void get_ind_resource(void);
|
|
|
|
static void get_1_ind_resource(void);
|
|
|
|
static void r_get_resource(void);
|
|
|
|
|
2004-05-19 21:23:17 +00:00
|
|
|
// Execute NATIVE_OP routine
|
|
|
|
void sheepshaver_cpu::execute_native_op(uint32 selector)
|
2003-09-07 14:25:05 +00:00
|
|
|
{
|
2003-11-04 20:48:29 +00:00
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
native_exec_count++;
|
|
|
|
const clock_t native_exec_start = clock();
|
|
|
|
#endif
|
|
|
|
|
2003-09-07 14:25:05 +00:00
|
|
|
switch (selector) {
|
|
|
|
case NATIVE_PATCH_NAME_REGISTRY:
|
|
|
|
DoPatchNameRegistry();
|
|
|
|
break;
|
|
|
|
case NATIVE_VIDEO_INSTALL_ACCEL:
|
|
|
|
VideoInstallAccel();
|
|
|
|
break;
|
|
|
|
case NATIVE_VIDEO_VBL:
|
|
|
|
VideoVBL();
|
|
|
|
break;
|
|
|
|
case NATIVE_VIDEO_DO_DRIVER_IO:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = (int32)(int16)VideoDoDriverIO((void *)gpr(3), (void *)gpr(4),
|
|
|
|
(void *)gpr(5), gpr(6), gpr(7));
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
2003-11-10 15:11:44 +00:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
case NATIVE_ETHER_IRQ:
|
|
|
|
EtherIRQ();
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
2003-11-10 15:11:44 +00:00
|
|
|
case NATIVE_ETHER_INIT:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = InitStreamModule((void *)gpr(3));
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
2003-11-10 15:11:44 +00:00
|
|
|
case NATIVE_ETHER_TERM:
|
|
|
|
TerminateStreamModule();
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
2003-11-10 15:11:44 +00:00
|
|
|
case NATIVE_ETHER_OPEN:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = ether_open((queue_t *)gpr(3), (void *)gpr(4), gpr(5), gpr(6), (void*)gpr(7));
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
2003-11-10 15:11:44 +00:00
|
|
|
case NATIVE_ETHER_CLOSE:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = ether_close((queue_t *)gpr(3), gpr(4), (void *)gpr(5));
|
2003-11-10 15:11:44 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_ETHER_WPUT:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = ether_wput((queue_t *)gpr(3), (mblk_t *)gpr(4));
|
2003-11-10 15:11:44 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_ETHER_RSRV:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = ether_rsrv((queue_t *)gpr(3));
|
2003-11-10 15:11:44 +00:00
|
|
|
break;
|
2004-04-22 21:45:18 +00:00
|
|
|
#else
|
|
|
|
case NATIVE_ETHER_INIT:
|
|
|
|
// FIXME: needs more complicated thunks
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = false;
|
2004-04-22 21:45:18 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2004-04-18 23:03:53 +00:00
|
|
|
case NATIVE_SYNC_HOOK:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = NQD_sync_hook(gpr(3));
|
2004-04-18 23:03:53 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_BITBLT_HOOK:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = NQD_bitblt_hook(gpr(3));
|
2004-04-18 23:03:53 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_BITBLT:
|
2004-05-19 21:23:17 +00:00
|
|
|
NQD_bitblt(gpr(3));
|
2004-04-18 23:03:53 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_FILLRECT_HOOK:
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = NQD_fillrect_hook(gpr(3));
|
2004-04-18 23:03:53 +00:00
|
|
|
break;
|
|
|
|
case NATIVE_INVRECT:
|
2004-05-19 21:23:17 +00:00
|
|
|
NQD_invrect(gpr(3));
|
2004-04-18 23:03:53 +00:00
|
|
|
break;
|
2004-04-22 20:57:31 +00:00
|
|
|
case NATIVE_FILLRECT:
|
2004-05-19 21:23:17 +00:00
|
|
|
NQD_fillrect(gpr(3));
|
2004-04-18 23:03:53 +00:00
|
|
|
break;
|
2003-09-07 14:25:05 +00:00
|
|
|
case NATIVE_SERIAL_NOTHING:
|
|
|
|
case NATIVE_SERIAL_OPEN:
|
|
|
|
case NATIVE_SERIAL_PRIME_IN:
|
|
|
|
case NATIVE_SERIAL_PRIME_OUT:
|
|
|
|
case NATIVE_SERIAL_CONTROL:
|
|
|
|
case NATIVE_SERIAL_STATUS:
|
|
|
|
case NATIVE_SERIAL_CLOSE: {
|
|
|
|
typedef int16 (*SerialCallback)(uint32, uint32);
|
|
|
|
static const SerialCallback serial_callbacks[] = {
|
|
|
|
SerialNothing,
|
|
|
|
SerialOpen,
|
|
|
|
SerialPrimeIn,
|
|
|
|
SerialPrimeOut,
|
|
|
|
SerialControl,
|
|
|
|
SerialStatus,
|
|
|
|
SerialClose
|
|
|
|
};
|
2004-05-19 21:23:17 +00:00
|
|
|
gpr(3) = serial_callbacks[selector - NATIVE_SERIAL_NOTHING](gpr(3), gpr(4));
|
2003-09-07 14:25:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-11-10 15:11:44 +00:00
|
|
|
case NATIVE_GET_RESOURCE:
|
|
|
|
case NATIVE_GET_1_RESOURCE:
|
|
|
|
case NATIVE_GET_IND_RESOURCE:
|
|
|
|
case NATIVE_GET_1_IND_RESOURCE:
|
|
|
|
case NATIVE_R_GET_RESOURCE: {
|
|
|
|
typedef void (*GetResourceCallback)(void);
|
|
|
|
static const GetResourceCallback get_resource_callbacks[] = {
|
2004-05-19 21:23:17 +00:00
|
|
|
::get_resource,
|
|
|
|
::get_1_resource,
|
|
|
|
::get_ind_resource,
|
|
|
|
::get_1_ind_resource,
|
|
|
|
::r_get_resource
|
2003-11-10 15:11:44 +00:00
|
|
|
};
|
|
|
|
get_resource_callbacks[selector - NATIVE_GET_RESOURCE]();
|
|
|
|
break;
|
|
|
|
}
|
2003-10-12 05:44:17 +00:00
|
|
|
case NATIVE_MAKE_EXECUTABLE:
|
2004-05-19 21:23:17 +00:00
|
|
|
MakeExecutable(0, (void *)gpr(4), gpr(5));
|
2003-10-12 05:44:17 +00:00
|
|
|
break;
|
2004-01-24 11:28:06 +00:00
|
|
|
case NATIVE_CHECK_LOAD_INVOC:
|
2004-05-19 21:23:17 +00:00
|
|
|
check_load_invoc(gpr(3), gpr(4), gpr(5));
|
2004-01-24 11:28:06 +00:00
|
|
|
break;
|
2003-09-07 14:25:05 +00:00
|
|
|
default:
|
|
|
|
printf("FATAL: NATIVE_OP called with bogus selector %d\n", selector);
|
|
|
|
QuitEmulator();
|
|
|
|
break;
|
|
|
|
}
|
2003-11-04 20:48:29 +00:00
|
|
|
|
|
|
|
#if EMUL_TIME_STATS
|
|
|
|
native_exec_time += (clock() - native_exec_start);
|
|
|
|
#endif
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute 68k subroutine (must be ended with EXEC_RETURN)
|
|
|
|
* This must only be called by the emul_thread when in EMUL_OP mode
|
|
|
|
* r->a[7] is unused, the routine runs on the caller's stack
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Execute68k(uint32 pc, M68kRegisters *r)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->execute_68k(pc, r);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute 68k A-Trap from EMUL_OP routine
|
|
|
|
* r->a[7] is unused, the routine runs on the caller's stack
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Execute68kTrap(uint16 trap, M68kRegisters *r)
|
|
|
|
{
|
2003-12-04 17:26:38 +00:00
|
|
|
SheepVar proc_var(4);
|
|
|
|
uint32 proc = proc_var.addr();
|
|
|
|
WriteMacInt16(proc, trap);
|
|
|
|
WriteMacInt16(proc + 2, M68K_RTS);
|
|
|
|
Execute68k(proc, r);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call MacOS PPC code
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint32 call_macos(uint32 tvect)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, 0, NULL);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 call_macos1(uint32 tvect, uint32 arg1)
|
|
|
|
{
|
|
|
|
const uint32 args[] = { arg1 };
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2)
|
|
|
|
{
|
|
|
|
const uint32 args[] = { arg1, arg2 };
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3)
|
|
|
|
{
|
|
|
|
const uint32 args[] = { arg1, arg2, arg3 };
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4)
|
|
|
|
{
|
|
|
|
const uint32 args[] = { arg1, arg2, arg3, arg4 };
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5)
|
|
|
|
{
|
|
|
|
const uint32 args[] = { arg1, arg2, arg3, arg4, arg5 };
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6)
|
|
|
|
{
|
|
|
|
const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6 };
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7)
|
|
|
|
{
|
|
|
|
const uint32 args[] = { arg1, arg2, arg3, arg4, arg5, arg6, arg7 };
|
2004-05-20 12:33:58 +00:00
|
|
|
return ppc_cpu->execute_macos_code(tvect, sizeof(args)/sizeof(args[0]), args);
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resource Manager thunks
|
|
|
|
*/
|
|
|
|
|
|
|
|
void get_resource(void)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_RESOURCE));
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_1_resource(void)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_RESOURCE));
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_ind_resource(void)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_IND_RESOURCE));
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_1_ind_resource(void)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->get_resource(ReadMacInt32(XLM_GET_1_IND_RESOURCE));
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void r_get_resource(void)
|
|
|
|
{
|
2004-05-20 12:33:58 +00:00
|
|
|
ppc_cpu->get_resource(ReadMacInt32(XLM_R_GET_RESOURCE));
|
2003-09-07 14:25:05 +00:00
|
|
|
}
|