Compare commits

..
32 changed files with 3926 additions and 1682 deletions

No files matched your search

-4
View File
@@ -1,7 +1,3 @@
[submodule "thirdparty/cubeb"]
path = thirdparty/cubeb
url = https://github.com/DingusDevOrg/cubeb.git
[submodule "thirdparty/capstone"]
path = thirdparty/capstone
url = https://github.com/maximumspatium/capstone.git
branch = next
+30 -88
View File
@@ -16,44 +16,6 @@ if (UNIX AND NOT APPLE)
endif()
endif()
option(DPPC_BUILD_PPC_TESTS "Build PowerPC tests" OFF)
option(DPPC_BUILD_BENCHMARKS "Build benchmarking programs" OFF)
option(ENABLE_68K_DEBUGGER "Enable 68k debugging" OFF)
if (ENABLE_68K_DEBUGGER)
# Build capstone as static library.
set(CAPSTONE_BUILD_STATIC ON CACHE BOOL "")
# Turn off anything unnecessary.
set(CAPSTONE_BUILD_SHARED OFF CACHE BOOL "")
set(CAPSTONE_BUILD_TESTS OFF CACHE BOOL "")
set(CAPSTONE_BUILD_CSTOOL OFF CACHE BOOL "")
set(CAPSTONE_BUILD_DIET OFF CACHE BOOL "")
set(CAPSTONE_OSXKERNEL_SUPPORT OFF CACHE BOOL "")
# Disable unused Capstone architectures.
set(CAPSTONE_ARM_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_ARM64_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_MIPS_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_PPC_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_SPARC_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_SYSZ_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_XCORE_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_X86_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_TMS320C64X_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_M680X_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_EVM_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_MOS65XX_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_WASM_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_BPF_SUPPORT OFF CACHE BOOL "")
set(CAPSTONE_RISCV_SUPPORT OFF CACHE BOOL "")
ADD_DEFINITIONS(-DENABLE_68K_DEBUGGER)
add_subdirectory(thirdparty/capstone EXCLUDE_FROM_ALL)
endif()
add_subdirectory("${PROJECT_SOURCE_DIR}/cpu/ppc/")
add_subdirectory("${PROJECT_SOURCE_DIR}/debugger/")
add_subdirectory("${PROJECT_SOURCE_DIR}/devices/")
@@ -110,58 +72,38 @@ target_link_libraries(dingusppc cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE
target_include_directories(dingusppc PRIVATE ${CLI11_ROOT})
endif()
if (ENABLE_68K_DEBUGGER)
target_link_libraries(dingusppc capstone-static)
add_executable(testppc ${TEST_SOURCES} $<TARGET_OBJECTS:debugger>
$<TARGET_OBJECTS:cpu_ppc>
$<TARGET_OBJECTS:devices>
$<TARGET_OBJECTS:execution>
$<TARGET_OBJECTS:machines>
$<TARGET_OBJECTS:loguru>)
if (WIN32)
target_link_libraries(testppc "${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2.lib"
"${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2main.lib"
cubeb)
else()
#target_link_libraries(testppc libsoundio_static ${LIBSOUNDIO_LIBS} ${SDL2_LIBRARIES})
target_link_libraries(testppc cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT})
endif()
if (DPPC_BUILD_PPC_TESTS)
add_executable(testppc ${TEST_SOURCES} $<TARGET_OBJECTS:debugger>
$<TARGET_OBJECTS:cpu_ppc>
$<TARGET_OBJECTS:devices>
$<TARGET_OBJECTS:execution>
$<TARGET_OBJECTS:machines>
$<TARGET_OBJECTS:loguru>)
file(GLOB BENCH_SOURCES "${PROJECT_SOURCE_DIR}/benchmark/*.cpp")
add_executable(bench1 ${BENCH_SOURCES} $<TARGET_OBJECTS:debugger>
$<TARGET_OBJECTS:cpu_ppc>
$<TARGET_OBJECTS:devices>
$<TARGET_OBJECTS:execution>
$<TARGET_OBJECTS:machines>
$<TARGET_OBJECTS:loguru>)
if (WIN32)
target_link_libraries(testppc
"${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2.lib"
"${PROJECT_SOURCE_DIR}/thirdparty/SDL2/lib/x64/SDL2main.lib"
cubeb)
else()
#target_link_libraries(testppc libsoundio_static ${LIBSOUNDIO_LIBS} ${SDL2_LIBRARIES})
target_link_libraries(testppc cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS}
${CMAKE_THREAD_LIBS_INIT})
endif()
target_link_libraries(bench1 cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT})
if (ENABLE_68K_DEBUGGER)
target_link_libraries(testppc capstone-static)
endif()
endif()
add_custom_command(
TARGET testppc POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${PROJECT_SOURCE_DIR}/cpu/ppc/test/ppcinttests.csv"
"${PROJECT_SOURCE_DIR}/cpu/ppc/test/ppcdisasmtest.csv"
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
if (DPPC_BUILD_BENCHMARKS)
file(GLOB BENCH_SOURCES "${PROJECT_SOURCE_DIR}/benchmark/*.cpp")
add_executable(bench1 ${BENCH_SOURCES} $<TARGET_OBJECTS:debugger>
$<TARGET_OBJECTS:cpu_ppc>
$<TARGET_OBJECTS:devices>
$<TARGET_OBJECTS:execution>
$<TARGET_OBJECTS:machines>
$<TARGET_OBJECTS:loguru>)
target_link_libraries(bench1 cubeb ${SDL2_LIBRARIES} ${CMAKE_DL_LIBS}
${CMAKE_THREAD_LIBS_INIT})
if (ENABLE_68K_DEBUGGER)
target_link_libraries(bench1 capstone-static)
endif()
endif()
if (DPPC_BUILD_PPC_TESTS)
add_custom_command(
TARGET testppc POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${PROJECT_SOURCE_DIR}/cpu/ppc/test/ppcinttests.csv"
"${PROJECT_SOURCE_DIR}/cpu/ppc/test/ppcdisasmtest.csv"
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
endif()
install (TARGETS dingusppc DESTINATION ${PROJECT_SOURCE_DIR}/build)
install (TARGETS dingusppc DESTINATION ${EXECUTABLE_OUTPUT_PATH})
+3 -8
View File
@@ -44,17 +44,17 @@ Specifies the Boot ROM path (optional; looks for bootrom.bin by default)
Specify machine ID (optional; will attempt to determine machine ID from the boot rom otherwise)
As of now, only the Power Macintosh G3 Beige is implemented.
As of now, only the Beige G3 is implemented.
## How to Compile
You need to install development tools first.
At least, a C++11 compliant compiler and [CMake](https://cmake.org) are required.
At least, a C++ compiler and [CMake](https://cmake.org) are required.
You will also have to recursive clone or run
```
git submodule update --init --recursive
git submodules --update
```
This is because the CubeB module is not included by default. All other components are already included in the thirdparty folder and compiled along with the rest of DingusPPC.
@@ -71,11 +71,6 @@ You may specify another build type using the variable CMAKE_BUILD_TYPE.
Future versions may drop SDL 2 as a requirement.
For Raspbian, you may also need the following command:
```
sudo apt install doxygen graphviz
```
## Testing
DingusPPC includes a test suite for verifying the correctness of its PowerPC CPU
+41 -9
View File
@@ -3,6 +3,8 @@
#include "cpu/ppc/ppcemu.h"
#include "cpu/ppc/ppcmmu.h"
#include "devices/mpc106.h"
#include "jittables.h"
#include "nuinterpreter.h"
#include <thirdparty/loguru/loguru.hpp>
uint32_t cs_code[] = {
@@ -17,6 +19,14 @@ uint32_t cs_code[] = {
0x7C650194, 0x4E800020
};
/* set up vCPU registers for benchmark code execution */
void setup_regs() {
ppc_state.pc = 0;
ppc_state.gpr[3] = 0x1000; // buf
ppc_state.gpr[4] = 0x8000; // len
ppc_state.gpr[5] = 0; // sum
}
int main(int argc, char** argv) {
int i;
@@ -38,6 +48,8 @@ int main(int argc, char** argv) {
return -1;
}
init_jit_tables();
ppc_cpu_init(grackle_obj, PPC_VER::MPC750);
/* load executable code into RAM at address 0 */
@@ -51,16 +63,22 @@ int main(int argc, char** argv) {
mem_write_byte(0x1000+i, rand() % 256);
}
/* prepare benchmark code execution */
ppc_state.pc = 0;
ppc_state.gpr[3] = 0x1000; // buf
ppc_state.gpr[4] = 0x8000; // len
ppc_state.gpr[5] = 0; // sum
setup_regs();
ppc_exec_until(0xC4);
LOG_F(INFO, "Checksum: 0x%08X", ppc_state.gpr[3]);
LOG_F(INFO, "\nRunning with the NuInterpreter");
setup_regs();
NuInterpExec(0);
LOG_F(INFO, "Checksum: 0x%08X", ppc_state.gpr[3]);
LOG_F(INFO, "\nBenchmarking the old Interpreter:");
// run the clock once for cache fill etc.
auto start_time = std::chrono::steady_clock::now();
auto end_time = std::chrono::steady_clock::now();
@@ -68,10 +86,7 @@ int main(int argc, char** argv) {
LOG_F(INFO, "Time elapsed (dry run): %lld ns", time_elapsed.count());
for (i = 0; i < 5; i++) {
ppc_state.pc = 0;
ppc_state.gpr[3] = 0x1000; // buf
ppc_state.gpr[4] = 0x8000; // len
ppc_state.gpr[5] = 0; // sum
setup_regs();
auto start_time = std::chrono::steady_clock::now();
@@ -85,6 +100,23 @@ int main(int argc, char** argv) {
LOG_F(INFO, "Time elapsed (run #%d): %lld ns", i, time_elapsed.count());
}
LOG_F(INFO, "\nBenchmarking the NuInterpreter:");
for (i = 0; i < 5; i++) {
setup_regs();
auto start_time = std::chrono::steady_clock::now();
NuInterpExec(0);
auto end_time = std::chrono::steady_clock::now();
LOG_F(INFO, "Checksum: 0x%08X", ppc_state.gpr[3]);
auto time_elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time);
LOG_F(INFO, "Time elapsed (run #%d): %lld ns", i, time_elapsed.count());
}
delete(grackle_obj);
return 0;
+420
View File
@@ -0,0 +1,420 @@
#include <cinttypes>
#include <thirdparty/loguru/loguru.hpp>
#include "ppcemu.h"
#include "ppcmmu.h"
#include "ppcdefs.h"
#include "jittables.h"
#include "nuinterpreter.h"
#if defined(USE_DTC)
#define CAST(name) &&lab_##name
struct InterpInstr {
void* emu_fn;
InstrInfo info;
};
#define REG_D(opcode) ((opcode >> 21) & 0x1F)
#define REG_A(opcode) ((opcode >> 16) & 0x1F)
#define REG_B(opcode) ((opcode >> 11) & 0x1F)
#define REG_S REG_D
#define BR_BO REG_D
#define BR_BI REG_A
#define SIMM(opcode) ((int32_t)((int16_t)(opcode & 0xFFFF)))
#define UIMM(opcode) ((uint32_t)((uint16_t)(opcode & 0xFFFF)))
#define REG_TO(opcode) ((opcode >> 21) & 0x1F)
#define REG_CRFD(opcode) (((opcode >> 23) & 7) << 2)
#define REG_CRFS(opcode) (((opcode >> 18) & 7) << 2)
#define REG_SR(opcode) (opcode >> 16) & 15
#define LI(opcode) (opcode >> 2) & 0xFFFFFF
/** mask generator for rotate and shift instructions (§ 4.2.1.4 PowerpC PEM) */
static inline uint32_t rot_mask(unsigned rot_mb, unsigned rot_me) {
uint32_t m1 = 0xFFFFFFFFUL >> rot_mb;
uint32_t m2 = 0xFFFFFFFFUL << (31 - rot_me);
return ((rot_mb <= rot_me) ? m2 & m1 : m1 | m2);
}
CachedInstr code_block[256];
void NuInterpExec(uint32_t next_pc) {
/* WARNING: entries in this table MUST be in the same order as constants in PPCInstr! */
InterpInstr interp_tab[] = {
{nullptr, {InstrOps::opNone, CFlowType::CFL_NONE, 0}},
{CAST(addi), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(adde), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addze), {InstrOps::opDA, CFlowType::CFL_NONE, 1}},
{CAST(andidot), {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{CAST(lwz), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lwzu), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lbz), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lhz), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(rlwinm), {InstrOps::opRot, CFlowType::CFL_NONE, 1}},
{CAST(srawidot), {InstrOps::opSASh, CFlowType::CFL_NONE, 1}},
{CAST(bc), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(bdnz), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(bdz), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(bclr), {InstrOps::opBrLink, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(mtspr), {InstrOps::opSSpr, CFlowType::CFL_NONE, 2}},
{CAST(bexit), {InstrOps::opNone, CFlowType::CFL_UNCOND_BRANCH, 0}},
{CAST(twi), {InstrOps::opTOASimm, CFlowType::CFL_NONE, 1}},
{CAST(mulli), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(cmpli), {InstrOps::opCrfDASimm, CFlowType::CFL_NONE, 1}},
{CAST(cmpi), {InstrOps::opCrfDAUimm, CFlowType::CFL_NONE, 1}},
{CAST(addic), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(addicdot), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(sc), {InstrOps::opNone, CFlowType::CFL_NONE, 1}},
{CAST(rlwimix), {InstrOps::opRot, CFlowType::CFL_NONE, 1}},
{CAST(rlwnmx), {InstrOps::opRot, CFlowType::CFL_NONE, 1}},
{CAST(ori), {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{CAST(oris), {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{CAST(xori), {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{CAST(xoris), {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{CAST(andisdot), {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{CAST(lbzu), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(stw), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(stwu), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(stb), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(stbu), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(lhzu), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lha), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lhau), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(sth), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(sthu), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(lmw), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(stmw), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(lfs), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lfsu), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lfd), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(lfdu), {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{CAST(stfs), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(stfsu), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(stfd), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(stfdu), {InstrOps::opSASimm, CFlowType::CFL_NONE, 1}},
{CAST(bcl), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(bca), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(bcla), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(b), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(bl), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(ba), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(bla), {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{CAST(crnor), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(rfi), {InstrOps::opNone, CFlowType::CFL_COND_BRANCH, 1}},
{CAST(crandc), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(isync), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(crxor), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(crnand), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(crand), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(creqv), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(crorc), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(cror), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(bcctr), {InstrOps::opBrRel, CFlowType::CFL_NONE, 1}},
{CAST(bcctrl), {InstrOps::opBrRel, CFlowType::CFL_NONE, 1}},
{CAST(cmp), {InstrOps::opCrfDAB, CFlowType::CFL_NONE, 1}},
{CAST(tw), {InstrOps::opTOAB, CFlowType::CFL_NONE, 1}},
{CAST(subfc), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(subfcdot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addc), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addcdot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(mulhwu), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(mulhwudot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(mfcr), {InstrOps::opD, CFlowType::CFL_NONE, 1}},
{CAST(lwarx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lwzx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(slw), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(slwdot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(cntlzw), {InstrOps::opSA, CFlowType::CFL_NONE, 1}},
{CAST(cntlzwdot), {InstrOps::opSA, CFlowType::CFL_NONE, 1}},
{CAST(ppc_and), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(anddot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(cmpl), {InstrOps::opCrfDAB, CFlowType::CFL_NONE, 1}},
{CAST(subf), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(subfdot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(dcbst), {InstrOps::opAB, CFlowType::CFL_NONE, 1}},
{CAST(lwzux), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(andc), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(andcdot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(mulhw), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(mulhwdot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
/*
{CAST(lbzux), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lbzx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lfdux), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lfdx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lfsux), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lfsx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lhaux), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lhax), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lhbrx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lhzux), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lhzx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lswi), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lswx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(lwbrx), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(add), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(adddot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addc), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addcdot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addedot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addme), {InstrOps::opDA, CFlowType::CFL_NONE, 1}},
{CAST(addmedot), {InstrOps::opDA, CFlowType::CFL_NONE, 1}},
{CAST(addze), {InstrOps::opDA, CFlowType::CFL_NONE, 1}},
{CAST(addzedot), {InstrOps::opDA, CFlowType::CFL_NONE, 1}},
{CAST(and), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(anddot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(divw), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(divwdot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(divwu), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(divwudot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(eieio), {InstrOps::opNone, CFlowType::CFL_NONE, 1}},
{CAST(cmpl), {InstrOps::opCrfDAB, CFlowType::CFL_NONE, 1}},
{CAST(eqv), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(eqvdot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(extsb), {InstrOps::opSA, CFlowType::CFL_NONE, 1}},
{CAST(extsbdot), {InstrOps::opSA, CFlowType::CFL_NONE, 1}},
{CAST(extsh), {InstrOps::opSA, CFlowType::CFL_NONE, 1}},
{CAST(extshdot), {InstrOps::opSA, CFlowType::CFL_NONE, 1}},
{CAST(fmr), {InstrOps::opDB, CFlowType::CFL_NONE, 1}},
{CAST(icbi), {InstrOps::opAB, CFlowType::CFL_NONE, 1}},
{CAST(isync), {InstrOps::opNone, CFlowType::CFL_NONE, 1}},
{CAST(mtcrf), {InstrOps::opNone, CFlowType::CFL_NONE, 1}},
{CAST(mulhwu), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(mullw), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(nand), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(nanddot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(neg), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(negdot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(nor), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(nordot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(or), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(ordot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(orc), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(orcdot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(slw), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(slwdot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(sraw), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(srawdot), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(srawi), {InstrOps::opSASh, CFlowType::CFL_NONE, 1}},
{CAST(stbux), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stbx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stfdux), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stfdx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stfiwx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stfsux), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stfsx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(sthbrx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(sthux), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(sthx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stswi), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stswx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stwbrx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stwcx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stwux), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(stwx), {InstrOps::opSAB, CFlowType::CFL_NONE, 1}},
{CAST(addedot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addeo), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{CAST(addeodot), {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
*/
};
CachedInstr* c_instr;
uint32_t opcode, main_opcode;
unsigned instr_index;
uint8_t* pc_real;
bool done;
pc_real = quickinstruction_translate(next_pc);
c_instr = &code_block[0];
done = false;
while (!done) {
opcode = ppc_cur_instruction;
main_opcode = opcode >> 26;
instr_index = main_index_tab[main_opcode];
if (!instr_index) {
switch (main_opcode) {
case 16:
instr_index = subgrp16_index_tab[opcode & 3];
break;
case 18:
instr_index = subgrp18_index_tab[opcode & 3];
break;
case 19:
instr_index = subgrp19_index_tab[opcode & 0x7FF];
break;
case 31:
instr_index = subgrp31_index_tab[opcode & 0x7FF];
break;
case 59:
instr_index = subgrp59_index_tab[opcode & 0x3F];
break;
case 63:
instr_index = subgrp63_index_tab[opcode & 0x7FF];
break;
default:
instr_index = 0;
}
if (!instr_index) {
LOG_F(INFO, "Illegal opcode 0x%08X - Program exception!", opcode);
return;
}
}
const InterpInstr* p_instr = &interp_tab[instr_index];
c_instr->call_me = p_instr->emu_fn;
/* pre-decode operands, immediate values etc. */
switch (p_instr->info.ops_fmt) {
case InstrOps::opDA:
case InstrOps::opSA:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_A(opcode);
break;
case InstrOps::opDAB:
case InstrOps::opSAB:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = REG_B(opcode);
break;
case InstrOps::opAB:
c_instr->d2 = REG_A(opcode);
c_instr->d3 = REG_B(opcode);
break;
case InstrOps::opTOAB:
c_instr->d1 = REG_TO(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = REG_B(opcode);
break;
case InstrOps::opCrfDAB:
c_instr->d1 = REG_CRFD(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = REG_B(opcode);
break;
case InstrOps::opDASimm:
case InstrOps::opSASimm:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = SIMM(opcode);
break;
case InstrOps::opSAUimm:
c_instr->d1 = REG_S(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->uimm = UIMM(opcode);
break;
case InstrOps::opSASh:
c_instr->d1 = REG_S(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = (opcode >> 11) & 0x1F; // shift
c_instr->uimm = (1 << c_instr->d3) - 1; // mask
break;
case InstrOps::opRot:
c_instr->d1 = REG_S(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = (opcode >> 11) & 0x1F; // shift
c_instr->d4 = opcode & 1; // Rc bit
c_instr->uimm = rot_mask((opcode >> 6) & 31, (opcode >> 1) & 31); //mask
break;
case InstrOps::opSSpr:
c_instr->d1 = REG_S(opcode);
c_instr->uimm = (REG_B(opcode) << 5) | REG_A(opcode);
break;
case InstrOps::opD:
c_instr->d1 = REG_D(opcode);
break;
case InstrOps::opTOASimm:
c_instr->d1 = REG_TO(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = SIMM(opcode);
break;
case InstrOps::opTOB:
c_instr->d1 = REG_TO(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = REG_B(opcode);
break;
case InstrOps::opCrfDASimm:
c_instr->d1 = REG_CRFD(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = SIMM(opcode);
break;
case InstrOps::opCrfDAUimm:
c_instr->d1 = REG_CRFD(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = UIMM(opcode);
break;
case InstrOps::opDSR:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_SR(opcode);
case InstrOps::opDB:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_B(opcode);
case InstrOps::opNone:
break;
case InstrOps::opBrRel:
c_instr->bt = SIMM(opcode) >> 2;
switch (BR_BO(opcode) & 0x1E) {
case 12:
case 14:
c_instr->call_me = interp_tab[PPCInstr::bc].emu_fn;
c_instr->uimm = 0x80000000UL >> BR_BI(opcode);
break;
case 16:
c_instr->call_me = interp_tab[PPCInstr::bdnz].emu_fn;
break;
case 18:
c_instr->call_me = interp_tab[PPCInstr::bdz].emu_fn;
break;
default:
LOG_F(ERROR, "Unsupported opcode 0x%08X - Program exception!", opcode);
return;
}
break;
case InstrOps::opBrLink:
switch (BR_BO(opcode) & 0x1E) {
case 20: // blr
c_instr->call_me = interp_tab[PPCInstr::bexit].emu_fn;
c_instr->uimm = next_pc;
done = true;
continue;
default:
LOG_F(ERROR, "Unsupported opcode 0x%08X - Program exception!", opcode);
return;
}
break;
default:
LOG_F(ERROR, "Unknown opcode format %d!", p_instr->info.ops_fmt);
return;
}
c_instr++;
next_pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
} // while
//LOG_F(INFO, "PreDecode completed!");
CachedInstr* code = &code_block[0];
goto *(code->call_me); // begin code execution
#define GEN_OP(name,body) lab_##name: { body; }
#define NEXT goto *((++code)->call_me)
#include "interpops.h" // auto-generate labeled emulation blocks
}
#endif
+1613
View File
File diff suppressed because it is too large. Load diff
+185
View File
@@ -0,0 +1,185 @@
#include <cinttypes>
#include <map>
#include "jittables.h"
#include "ppcdefs.h"
uint16_t main_index_tab[64] = {
PPCInstr::illegal, PPCInstr::illegal, PPCInstr::illegal,
PPCInstr::twi, PPCInstr::illegal, PPCInstr::illegal,
PPCInstr::illegal, PPCInstr::mulli, PPCInstr::subfic,
PPCInstr::dozi, PPCInstr::cmpli, PPCInstr::cmpi,
PPCInstr::addic, PPCInstr::addicdot, PPCInstr::addi,
PPCInstr::addis, PPCInstr::illegal, PPCInstr::sc,
PPCInstr::illegal, PPCInstr::illegal, PPCInstr::rlwimix,
PPCInstr::rlwinmx, PPCInstr::rlmi, PPCInstr::rlwnmx,
PPCInstr::ori, PPCInstr::oris, PPCInstr::xori,
PPCInstr::xoris, PPCInstr::andidot, PPCInstr::andisdot,
PPCInstr::illegal, PPCInstr::illegal, PPCInstr::lwz,
PPCInstr::lwzu, PPCInstr::lbz, PPCInstr::lbzu,
PPCInstr::stw, PPCInstr::stwu, PPCInstr::stb,
PPCInstr::stbu, PPCInstr::lhz, PPCInstr::lhzu,
PPCInstr::lha, PPCInstr::lhau, PPCInstr::sth,
PPCInstr::sthu, PPCInstr::lmw, PPCInstr::stmw,
PPCInstr::lfs, PPCInstr::lfsu, PPCInstr::lfd,
PPCInstr::lfdu, PPCInstr::stfs, PPCInstr::stfsu,
PPCInstr::stfd, PPCInstr::stfdu, PPCInstr::illegal,
PPCInstr::illegal, PPCInstr::illegal, PPCInstr::illegal,
PPCInstr::illegal, PPCInstr::illegal, PPCInstr::illegal,
PPCInstr::illegal
};
uint16_t subgrp16_index_tab[4] = {
PPCInstr::bc, PPCInstr::bcl, PPCInstr::bca, PPCInstr::bcla
};
uint16_t subgrp18_index_tab[4] = {
PPCInstr::b, PPCInstr::bl, PPCInstr::ba, PPCInstr::bla
};
uint16_t subgrp19_index_tab[2048] = { PPCInstr::illegal };
uint16_t subgrp31_index_tab[2048] = { PPCInstr::illegal };
uint16_t subgrp59_index_tab[64] = { PPCInstr::illegal };
uint16_t subgrp63_index_tab[2048] = { PPCInstr::illegal };
static std::map<uint16_t, PPCInstr> Subop19Init = {
{ 32, PPCInstr::bclr },
{ 33, PPCInstr::bclrl },
{ 66, PPCInstr::crnor },
{ 100, PPCInstr::rfi },
{ 258, PPCInstr::crandc },
{ 300, PPCInstr::isync },
{ 386, PPCInstr::crxor },
{ 450, PPCInstr::crnand },
{ 514, PPCInstr::crand },
{ 578, PPCInstr::creqv },
{ 834, PPCInstr::crorc },
{ 898, PPCInstr::cror },
{ 1056, PPCInstr::bcctr },
{ 1057, PPCInstr::bcctrl }
};
static std::map<uint16_t, PPCInstr> Subop31Init = {
{ 0, PPCInstr::cmp}, { 8, PPCInstr::tw},
{ 16, PPCInstr::subfc}, { 17, PPCInstr::subfcdot},
{ 20, PPCInstr::addc}, { 21, PPCInstr::addcdot},
{ 22, PPCInstr::mulhwu}, { 23, PPCInstr::mulhwudot},
{ 38, PPCInstr::mfcr}, { 40, PPCInstr::lwarx},
{ 46, PPCInstr::lwzx}, { 48, PPCInstr::slw},
{ 49, PPCInstr::slwdot}, { 52, PPCInstr::cntlzw},
{ 53, PPCInstr::cntlzwdot}, { 56, PPCInstr::ppc_and},
{ 57, PPCInstr::anddot}, { 58, PPCInstr::maskg},
{ 59, PPCInstr::maskgdot}, { 64, PPCInstr::cmpl},
{ 80, PPCInstr::subf}, { 81, PPCInstr::subfdot},
{ 108, PPCInstr::dcbst}, { 110, PPCInstr::lwzux},
{ 120, PPCInstr::andc}, { 121, PPCInstr::andcdot},
{ 150, PPCInstr::mulhw}, { 151, PPCInstr::mulhwdot},
{ 166, PPCInstr::mfmsr}, { 172, PPCInstr::dcbf},
{ 174, PPCInstr::lbzx}, { 208, PPCInstr::neg},
{ 209, PPCInstr::negdot}, { 214, PPCInstr::mul},
{ 215, PPCInstr::muldot}, { 238, PPCInstr::lbzux},
{ 248, PPCInstr::nor}, { 249, PPCInstr::nordot},
{ 272, PPCInstr::subfe}, { 273, PPCInstr::subfedot},
{ 276, PPCInstr::adde}, { 277, PPCInstr::addedot},
{ 288, PPCInstr::mtcrf}, { 292, PPCInstr::mtmsr},
{ 301, PPCInstr::stwcx}, { 302, PPCInstr::stwx},
{ 304, PPCInstr::slq}, { 305, PPCInstr::slqdot},
{ 306, PPCInstr::sle}, { 307, PPCInstr::sledot},
{ 366, PPCInstr::stwux}, { 368, PPCInstr::sliq},
{ 400, PPCInstr::subfze}, { 401, PPCInstr::subfzedot},
{ 404, PPCInstr::addze}, { 405, PPCInstr::addzedot},
{ 420, PPCInstr::mtsr}, { 430, PPCInstr::stbx},
{ 432, PPCInstr::sllq}, { 433, PPCInstr::sllqdot},
{ 434, PPCInstr::sleq}, { 436, PPCInstr::sleqdot},
{ 464, PPCInstr::subfme}, { 465, PPCInstr::subfmedot},
{ 468, PPCInstr::addme}, { 469, PPCInstr::addmedot},
{ 470, PPCInstr::mullw}, { 471, PPCInstr::mullwdot},
{ 484, PPCInstr::mtsrin}, { 492, PPCInstr::dcbtst},
{ 494, PPCInstr::stbux}, { 496, PPCInstr::slliq},
{ 497, PPCInstr::slliqdot}, { 528, PPCInstr::doz},
{ 529, PPCInstr::dozdot}, { 532, PPCInstr::add},
{ 533, PPCInstr::adddot}, { 554, PPCInstr::lscbx},
{ 555, PPCInstr::lscbxdot}, { 556, PPCInstr::dcbt},
{ 558, PPCInstr::lhzx}, { 568, PPCInstr::eqv},
{ 569, PPCInstr::eqvdot}, { 612, PPCInstr::tlbie},
{ 622, PPCInstr::lhzux}, { 632, PPCInstr::ppc_xor},
{ 633, PPCInstr::xordot}, { 662, PPCInstr::ppc_div},
{ 663, PPCInstr::divdot}, { 678, PPCInstr::mfspr},
{ 686, PPCInstr::lhax}, { 720, PPCInstr::ppc_abs},
{ 721, PPCInstr::absdot}, { 726, PPCInstr::divs},
{ 727, PPCInstr::divsdot}, { 740, PPCInstr::tlbia},
{ 742, PPCInstr::mftb}, { 750, PPCInstr::lhaux},
{ 814, PPCInstr::sthx}, { 824, PPCInstr::orc},
{ 825, PPCInstr::orcdot}, { 878, PPCInstr::sthux},
{ 888, PPCInstr::ppc_or}, { 889, PPCInstr::ordot},
{ 918, PPCInstr::divwu}, { 919, PPCInstr::divwudot},
{ 934, PPCInstr::mtspr}, { 940, PPCInstr::dcbi},
{ 952, PPCInstr::nand}, { 953, PPCInstr::nanddot},
{ 976, PPCInstr::nabs}, { 977, PPCInstr::nabsdot},
{ 982, PPCInstr::divw}, { 983, PPCInstr::divwdot},
{ 1024, PPCInstr::mcrxr}, { 1040, PPCInstr::subfco},
{ 1041, PPCInstr::subfcodot}, { 1044, PPCInstr::addco},
{ 1045, PPCInstr::addcodot}, { 1062, PPCInstr::clcs},
{ 1063, PPCInstr::clcsdot}, { 1066, PPCInstr::lswx},
{ 1068, PPCInstr::lwbrx}, { 1070, PPCInstr::lfsx},
{ 1072, PPCInstr::srw}, { 1073, PPCInstr::srwdot},
{ 1074, PPCInstr::rrib}, { 1075, PPCInstr::rribdot},
{ 1082, PPCInstr::maskir}, { 1083, PPCInstr::maskirdot},
{ 1104, PPCInstr::subfo}, { 1105, PPCInstr::subfodot},
{ 1132, PPCInstr::tlbsync}, { 1134, PPCInstr::lfsux},
{ 1190, PPCInstr::mfsr}, { 1194, PPCInstr::lswi},
{ 1196, PPCInstr::ppc_sync}, { 1198, PPCInstr::lfdx},
{ 1232, PPCInstr::nego}, { 1233, PPCInstr::negodot},
{ 1238, PPCInstr::mulo}, { 1239, PPCInstr::mulodot},
{ 1262, PPCInstr::lfdux}, { 1296, PPCInstr::subfeo},
{ 1297, PPCInstr::subfeodot}, { 1300, PPCInstr::addeo},
{ 1301, PPCInstr::addeodot}, { 1318, PPCInstr::mfsrin},
{ 1322, PPCInstr::stswx}, { 1324, PPCInstr::stwbrx},
{ 1326, PPCInstr::stfsx}, { 1328, PPCInstr::srq},
{ 1329, PPCInstr::srqdot}, { 1330, PPCInstr::sre},
{ 1331, PPCInstr::sredot}, { 1390, PPCInstr::stfsux},
{ 1392, PPCInstr::sriq}, { 1393, PPCInstr::sriqdot},
{ 1424, PPCInstr::subfzeo}, { 1425, PPCInstr::subfzeodot},
{ 1428, PPCInstr::addzeo}, { 1429, PPCInstr::addzeodot},
{ 1450, PPCInstr::stswi}, { 1454, PPCInstr::stfdx},
{ 1456, PPCInstr::srlq}, { 1457, PPCInstr::srlqdot},
{ 1458, PPCInstr::sreq }, { 1459, PPCInstr::sreqdot},
{ 1488, PPCInstr::subfmeo }, { 1489, PPCInstr::subfmeodot},
{ 1492, PPCInstr::addmeo }, { 1493, PPCInstr::addmeodot},
{ 1494, PPCInstr::mullwo }, { 1495, PPCInstr::mullwodot},
{ 1518, PPCInstr::stfdux }, { 1520, PPCInstr::srliq},
{ 1521, PPCInstr::srliqdot }, { 1552, PPCInstr::dozo},
{ 1553, PPCInstr::dozodot }, { 1556, PPCInstr::addo},
{ 1557, PPCInstr::addodot }, { 1580, PPCInstr::lhbrx},
{ 1584, PPCInstr::sraw }, { 1585, PPCInstr::srawdot},
{ 1648, PPCInstr::srawi }, { 1649, PPCInstr::srawidot},
{ 1686, PPCInstr::divo }, { 1687, PPCInstr::divodot},
{ 1708, PPCInstr::eieio }, { 1744, PPCInstr::abso},
{ 1745, PPCInstr::absodot }, { 1750, PPCInstr::divso},
{ 1751, PPCInstr::divsodot }, { 1836, PPCInstr::sthbrx},
{ 1840, PPCInstr::sraq }, { 1841, PPCInstr::sraqdot},
{ 1842, PPCInstr::srea }, { 1843, PPCInstr::sreadot},
{ 1844, PPCInstr::extsh }, { 1845, PPCInstr::extshdot},
{ 1904, PPCInstr::sraiq }, { 1905, PPCInstr::sraiqdot},
{ 1908, PPCInstr::extsb }, { 1909, PPCInstr::extsbdot},
{ 1942, PPCInstr::divwuo }, { 1943, PPCInstr::divwuodot},
{ 1956, PPCInstr::tlbld }, { 1964, PPCInstr::icbi},
{ 1966, PPCInstr::stfiwx }, { 2000, PPCInstr::nabso},
{ 2001, PPCInstr::nabsodot }, { 2006, PPCInstr::divwo},
{ 2007, PPCInstr::divwodot }, { 2020, PPCInstr::tlbli},
{ 2028, PPCInstr::dcbz }
};
void init_jit_tables() {
std::map<uint16_t, PPCInstr>::iterator it;
for (it = Subop19Init.begin(); it != Subop19Init.end(); it++ ) {
subgrp19_index_tab[it->first] = it->second;
}
for (it = Subop31Init.begin(); it != Subop31Init.end(); it++ ) {
subgrp31_index_tab[it->first] = it->second;
}
}
+61
View File
@@ -0,0 +1,61 @@
#ifndef JIT_TABLES_H
#define JIT_TABLES_H
#include <cinttypes>
#include "ppcemu.h"
/** Instruction operands formats. */
enum class InstrOps {
opNone,
opDA,
opDAB,
opAB,
opDASimm,
opSAUimm,
opSASh,
opRot,
opSSpr,
opBrRel,
opBrLink,
opD,
opTOASimm,
opTOB,
opCrfDAB,
opCrfDASimm,
opCrfDAUimm,
opDSR,
opDB,
opSASimm,
opSA,
opSAB,
opTOAB
};
/* Control flow kind. */
enum class CFlowType {
CFL_NONE,
CFL_COND_BRANCH,
CFL_UNCOND_BRANCH,
CFL_TRAP,
};
struct InstrInfo {
InstrOps ops_fmt; // describes operands format
CFlowType cflow_type; // control flow type
int num_cycles;
// Required by JIT
//int flags; // flags updated by this instruction
};
extern uint16_t main_index_tab[];
extern uint16_t subgrp16_index_tab[];
extern uint16_t subgrp18_index_tab[];
extern uint16_t subgrp19_index_tab[];
extern uint16_t subgrp31_index_tab[];
extern uint16_t subgrp59_index_tab[];
extern uint16_t subgrp63_index_tab[];
void init_jit_tables(void);
#endif /* JIT_TABLES_H */
+272
View File
@@ -0,0 +1,272 @@
#include <cinttypes>
#include <thirdparty/loguru/loguru.hpp>
#include "ppcemu.h"
#include "ppcmmu.h"
#include "ppcdefs.h"
#include "jittables.h"
#include "nuinterpreter.h"
#if !defined(USE_DTC)
CachedInstr* interp_tpc;
bool interp_running;
#define GEN_OP(name,body) void impl_##name(const CachedInstr* code) {body}
#define NEXT interp_tpc++
#include "interpops.h"
struct InterpInstr {
ImplSubr emu_fn; // pointer to the instruction emulation routine
InstrInfo info;
};
/* WARNING: entries in this table MUST be in the same order as constants in PPCInstr! */
InterpInstr interp_tab[] = {
{nullptr, {InstrOps::opNone, CFlowType::CFL_NONE, 0}},
{impl_addi, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_adde, {InstrOps::opDAB, CFlowType::CFL_NONE, 1}},
{impl_addze, {InstrOps::opDA, CFlowType::CFL_NONE, 1}},
{impl_andidot, {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{impl_lwz, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_lwzu, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_lbz, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_lhz, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_rlwinm, {InstrOps::opRot, CFlowType::CFL_NONE, 1}},
{impl_srawidot, {InstrOps::opSASh, CFlowType::CFL_NONE, 1}},
{impl_bc, {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{impl_bdnz, {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{impl_bdz, {InstrOps::opBrRel, CFlowType::CFL_COND_BRANCH, 0}},
{impl_bclr, {InstrOps::opBrLink, CFlowType::CFL_COND_BRANCH, 0}},
{impl_mtspr, {InstrOps::opSSpr, CFlowType::CFL_NONE, 2}},
{impl_bexit, {InstrOps::opNone, CFlowType::CFL_UNCOND_BRANCH, 0}},
{impl_twi, {InstrOps::opTOASimm, CFlowType::CFL_NONE, 1}},
{impl_mulli, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_cmpli, {InstrOps::opCrfDASimm, CFlowType::CFL_NONE, 1}},
{impl_cmpi, {InstrOps::opCrfDAUimm, CFlowType::CFL_NONE, 1}},
{impl_addic, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_addicdot, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_sc, {InstrOps::opSC, CFlowType::CFL_NONE, 1}},
{impl_rlwimix, {InstrOps::opRot, CFlowType::CFL_NONE, 1}},
{impl_rlwnmx, {InstrOps::opRot, CFlowType::CFL_NONE, 1}},
{impl_ori, {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{impl_oris, {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{impl_xori, {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{impl_xoris, {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{impl_andisdot, {InstrOps::opSAUimm, CFlowType::CFL_NONE, 1}},
{impl_lbzu, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_stw, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_stwu, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_stb, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_stbu, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_lhzu, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_lha, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_lhau, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_sth, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
{impl_sthu, {InstrOps::opDASimm, CFlowType::CFL_NONE, 1}},
};
CachedInstr code_block[256];
#define REG_D(opcode) ((opcode >> 21) & 0x1F)
#define REG_A(opcode) ((opcode >> 16) & 0x1F)
#define REG_B(opcode) ((opcode >> 11) & 0x1F)
#define REG_S REG_D
#define BR_BO REG_D
#define BR_BI REG_A
#define SIMM(opcode) ((int32_t)((int16_t)(opcode & 0xFFFF)))
#define UIMM(opcode) ((uint32_t)((uint16_t)(opcode & 0xFFFF)))
#define REG_TO(opcode) REG_D
#define REG_CRFD(opcode) (((opcode >> 23) & 7) << 2)
#define REG_SR(opcode) (opcode >> 16) & 15
/** mask generator for rotate and shift instructions (§ 4.2.1.4 PowerpC PEM) */
static inline uint32_t rot_mask(unsigned rot_mb, unsigned rot_me) {
uint32_t m1 = 0xFFFFFFFFUL >> rot_mb;
uint32_t m2 = 0xFFFFFFFFUL << (31 - rot_me);
return ((rot_mb <= rot_me) ? m2 & m1 : m1 | m2);
}
bool PreDecode(uint32_t next_pc, CachedInstr* c_instr)
{
uint32_t opcode, main_opcode;
unsigned instr_index;
uint8_t* pc_real;
bool done;
pc_real = quickinstruction_translate(next_pc);
//CachedInstr* c_instr = &code[0];
done = false;
while (!done) {
opcode = ppc_cur_instruction;
main_opcode = opcode >> 26;
instr_index = main_index_tab[main_opcode];
if (!instr_index) {
switch (main_opcode) {
case 16:
instr_index = subgrp16_index_tab[opcode & 3];
break;
case 18:
instr_index = subgrp18_index_tab[opcode & 3];
break;
case 19:
instr_index = subgrp19_index_tab[opcode & 0x7FF];
break;
case 31:
instr_index = subgrp31_index_tab[opcode & 0x7FF];
break;
case 59:
instr_index = subgrp59_index_tab[opcode & 0x3F];
break;
case 63:
instr_index = subgrp63_index_tab[opcode & 0x7FF];
break;
default:
instr_index = 0;
}
if (!instr_index) {
LOG_F(ERROR, "Illegal opcode 0x%08X - Program exception!", opcode);
return false;
}
}
const InterpInstr* p_instr = &interp_tab[instr_index];
c_instr->call_me = p_instr->emu_fn;
/* pre-decode operands, immediate values etc. */
switch (p_instr->info.ops_fmt) {
case InstrOps::opDA:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_A(opcode);
break;
case InstrOps::opDAB:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = REG_B(opcode);
break;
case InstrOps::opDASimm:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = SIMM(opcode);
break;
case InstrOps::opSAUimm:
c_instr->d1 = REG_S(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->uimm = UIMM(opcode);
break;
case InstrOps::opSASh:
c_instr->d1 = REG_S(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = (opcode >> 11) & 0x1F; // shift
c_instr->uimm = (1 << c_instr->d3) - 1; // mask
break;
case InstrOps::opRot:
c_instr->d1 = REG_S(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = (opcode >> 11) & 0x1F; // shift
c_instr->d4 = opcode & 1; // Rc bit
c_instr->uimm = rot_mask((opcode >> 6) & 31, (opcode >> 1) & 31); //mask
break;
case InstrOps::opSSpr:
c_instr->d1 = REG_S(opcode);
c_instr->uimm = (REG_B(opcode) << 5) | REG_A(opcode);
break;
case InstrOps::opD:
c_instr->d1 = REG_D(opcode);
break;
case InstrOps::opTOASimm:
c_instr->d1 = REG_TO(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = SIMM(opcode);
break;
case InstrOps::opTOB:
c_instr->d1 = REG_TO(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->d3 = REG_B(opcode);
break;
case InstrOps::opCrfDASimm:
c_instr->d1 = REG_CRFD(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = SIMM(opcode);
break;
case InstrOps::opCrfDAUimm:
c_instr->d1 = REG_CRFD(opcode);
c_instr->d2 = REG_A(opcode);
c_instr->simm = UIMM(opcode);
break;
case InstrOps::opDSR:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_SR(opcode);
case InstrOps::opDB:
c_instr->d1 = REG_D(opcode);
c_instr->d2 = REG_B(opcode);
case InstrOps::opSC:
break;
case InstrOps::opBrRel:
c_instr->bt = SIMM(opcode) >> 2;
switch (BR_BO(opcode) & 0x1E) {
case 12:
case 14:
c_instr->call_me = interp_tab[PPCInstr::bc].emu_fn;
c_instr->uimm = 0x80000000UL >> BR_BI(opcode);
break;
case 16:
c_instr->call_me = interp_tab[PPCInstr::bdnz].emu_fn;
break;
case 18:
c_instr->call_me = interp_tab[PPCInstr::bdz].emu_fn;
break;
default:
LOG_F(ERROR, "Unsupported opcode 0x%08X - Program exception!", opcode);
return false;
}
break;
case InstrOps::opBrLink:
switch (BR_BO(opcode) & 0x1E) {
case 20: // blr
c_instr->call_me = interp_tab[PPCInstr::bexit].emu_fn;
c_instr->uimm = next_pc;
done = true;
continue;
default:
LOG_F(ERROR, "Unsupported opcode 0x%08X - Program exception!", opcode);
return false;
}
break;
default:
LOG_F(ERROR, "Unknown opcode format %d!", p_instr->info.ops_fmt);
return false;
}
c_instr++;
next_pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
} // while
return true;
}
void NuInterpExec(uint32_t start_addr) {
interp_tpc = &code_block[0];
if (!PreDecode(start_addr, interp_tpc)) {
return;
}
interp_running = true;
while(interp_running) {
interp_tpc->call_me(interp_tpc);
}
}
#endif // if !defined(USE_DTC)
+38
View File
@@ -0,0 +1,38 @@
#ifndef NU_INTERPRETER_H
#define NU_INTERPRETER_H
#include "ppcdefs.h"
#define USE_DTC
struct CachedInstr; // forward declaration
typedef void (*ImplSubr)(const CachedInstr *);
struct CachedInstr {
#if defined(USE_DTC)
void* call_me;
#else
ImplSubr call_me;
#endif
union {
struct {
uint8_t d1;
uint8_t d2;
uint8_t d3;
uint8_t d4;
};
int32_t bt; // branch target
};
union {
int32_t simm;
uint32_t uimm;
};
};
bool PreDecode(uint32_t next_pc, CachedInstr* c_instr);
void NuInterpExec(uint32_t start_addr);
#endif // NU_INTERPRETER_H
+297
View File
@@ -0,0 +1,297 @@
#ifndef PPC_DEFS_H
#define PPC_DEFS_H
/* Enumerate all supported PowerPC instructions. */
enum PPCInstr : int {
illegal = 0,
addi,
adde,
addze,
andidot,
lwz,
lwzu,
lbz,
lhz,
rlwinmx,
srawidot,
bc,
bdnz,
bdz,
bclr,
mtspr,
bexit, // arificial block exit opcode
twi,
mulli,
subfic,
cmpli,
cmpi,
addic,
addicdot,
addis,
sc,
rlwimix,
rlwnmx,
ori,
oris,
xori,
xoris,
andisdot,
lbzu,
stw,
stwu,
stb,
stbu,
lhzu,
lha,
lhau,
sth,
sthu,
lmw,
stmw,
lfs,
lfsu,
lfd,
lfdu,
stfs,
stfsu,
stfd,
stfdu,
bcl,
bca,
bcla,
b,
bl,
ba,
bla,
bclrl,
crnor,
rfi,
crandc,
isync,
crxor,
crnand,
crand,
creqv,
crorc,
cror,
bcctr,
bcctrl,
cmp,
tw,
subfc,
subfcdot,
addc,
addcdot,
mulhwu,
mulhwudot,
mfcr,
lwarx,
lwzx,
slw,
slwdot,
cntlzw,
cntlzwdot,
ppc_and,
anddot,
cmpl,
subf,
subfdot,
dcbst,
lwzux,
andc,
andcdot,
mulhw,
mulhwdot,
mfmsr,
dcbf,
lbzx,
neg,
negdot,
mul,
muldot,
lbzux,
nor,
nordot,
subfe,
subfedot,
addedot,
mtcrf,
mtmsr,
stwcx,
stwx,
stwux,
subfze,
subfzedot,
addzedot,
mtsr,
stbx,
subfme,
subfmedot,
addme,
addmedot,
mullw,
mullwdot,
mtsrin,
dcbtst,
stbux,
add,
adddot,
lscbx,
lscbxdot,
dcbt,
lhzx,
eqv,
eqvdot,
tlbie,
lhzux,
ppc_xor,
xordot,
ppc_div,
divdot,
mfspr,
lhax,
ppc_abs,
absdot,
divs,
divsdot,
tlbia,
mftb,
lhaux,
sthx,
orc,
orcdot,
sthux,
ppc_or,
ordot,
divwu,
divwudot,
dcbi,
nand,
nanddot,
divw,
divwdot,
mcrxr,
subfco,
subfcodot,
addco,
addcodot,
lswx,
lwbrx,
lfsx,
srw,
srwdot,
subfo,
subfodot,
tlbsync,
lfsux,
mfsr,
lswi,
ppc_sync,
lfdx,
nego,
negodot,
mulo,
mulodot,
lfdux,
subfeo,
subfeodot,
addeo,
addeodot,
mfsrin,
stswx,
stwbrx,
stfsx,
stfsux,
subfzeo,
subfzeodot,
addzeo,
addzeodot,
stswi,
stfdx,
subfmeo,
subfmeodot,
addmeo,
addmeodot,
mullwo,
mullwodot,
stfdux,
addo,
addodot,
lhbrx,
sraw,
srawdot,
srawi,
eieio,
sthbrx,
extsh,
extshdot,
extsb,
extsbdot,
divwuo,
divwuodot,
tlbld,
icbi,
stfiwx,
divwo,
divwodot,
tlbli,
dcbz,
// POWER-specific
dozi,
rlmi,
maskg,
maskgdot,
slq,
slqdot,
sle,
sledot,
sliq,
sllq,
sllqdot,
sleq,
sleqdot,
slliq,
slliqdot,
doz,
dozdot,
nabs,
nabsdot,
clcs,
clcsdot,
rrib,
rribdot,
maskir,
maskirdot,
srq,
srqdot,
sre,
sredot,
sriq,
sriqdot,
srlq,
srlqdot,
sreq,
sreqdot,
srliq,
srliqdot,
dozo,
dozodot,
divo,
divodot,
abso,
absodot,
divso,
divsodot,
sraq,
sraqdot,
srea,
sreadot,
sraiq,
sraiqdot,
nabso,
nabsodot,
};
#endif /* PPC_DEFS_H */
+37 -75
View File
@@ -209,8 +209,8 @@ const char* opc_flt_ldst[8] = {/* integer load and store instructions */
"lfdu",
"stfs",
"stfsu",
"stfd",
"stfdu"};
"sftd",
"sftdu"};
const char* opc_flt_ext_arith[32] = {
/* integer load and store instructions */
@@ -347,10 +347,10 @@ void opc_ar_im(PPCDisasmContext* ctx) {
int32_t imm = SIGNEXT(ctx->instr_code & 0xFFFF, 15);
if (ctx->simplified) {
if (((ctx->instr_code >> 26) == 0xE) && !ra) {
if (((ctx->instr_code >> 26) == 0xE) & !ra) {
fmt_twoop_simm(ctx->instr_str, "li", rd, imm);
return;
} else if (((ctx->instr_code >> 26) == 0xF) && !ra) {
} else if (((ctx->instr_code >> 26) == 0xF) & !ra) {
fmt_twoop_simm(ctx->instr_str, "lis", rd, imm);
return;
}
@@ -954,12 +954,12 @@ void opc_group31(PPCDisasmContext* ctx) {
} else if (index == 11) { /* tlbia */
ctx->instr_str = my_sprintf("%-8s", "tlbia");
} else if (index == 30) { /* tlbld - 603 only */
if (!rs && !ra)
if (!rs & !ra)
opc_illegal(ctx);
else
ctx->instr_str = my_sprintf("%-8sr%s", "tlbld", rb);
} else if (index == 30) { /* tlbli - 603 only */
if (!rs && !ra)
if (!rs & !ra)
opc_illegal(ctx);
else
ctx->instr_str = my_sprintf("%-8sr%s", "tlbli", rb);
@@ -973,10 +973,10 @@ void opc_group31(PPCDisasmContext* ctx) {
strcat(opcode, ".");
if ((index == 0) || (index == 4) || (index == 6) || (index == 16) || (index == 20) ||
(index == 22) || (index == 24) || (index == 28)) {
if ((index == 0) | (index == 4) | (index == 6) | (index == 16) | (index == 20) |
(index == 22) | (index == 24) | (index == 28)) {
fmt_threeop(ctx->instr_str, opcode, ra, rs, rb);
} else if ((index == 5) || (index == 7) || (index == 21) || (index == 23) || (index == 25) || (index == 29)) {
} else if ((index == 5) | (index == 7) | (index == 21) | (index == 23) | (index == 25) | (index == 29)) {
fmt_threeop_simm(ctx->instr_str, opcode, ra, rs, rb);
} else {
opc_illegal(ctx);
@@ -990,7 +990,7 @@ void opc_group31(PPCDisasmContext* ctx) {
if (rc_set)
strcat(opcode, ".");
if ((index == 4) || (index == 6) || (index == 16) || (index == 20) || (index == 22) ||
if ((index == 4) | (index == 6) | (index == 16) | (index == 20) | (index == 22) |
(index == 28)) {
fmt_threeop(ctx->instr_str, opcode, ra, rs, rb);
} else {
@@ -1077,7 +1077,7 @@ void opc_group31(PPCDisasmContext* ctx) {
}
}
/* eciwx, ecowx, lhbrx, lwbrx, stwbrx, sthbrx */
else if ((index == 9) || (index == 13) || (index == 16) || (index == 20) || (index == 24) || (index == 28)) {
else if ((index == 9) | (index == 13) | (index == 16) | (index == 20) | (index == 24) | (index == 28)) {
if (rc_set) {
opc_illegal(ctx);
return;
@@ -1088,15 +1088,15 @@ void opc_group31(PPCDisasmContext* ctx) {
fmt_threeop(ctx->instr_str, opcode, rs, ra, rb);
return;
}
} else if ((index == 18) || (index == 26)) { /* sync, eieio */
} else if ((index == 18) | (index == 26)) { /* sync, eieio */
ctx->instr_str = my_sprintf("%-8s", opcode);
return;
}
/* dcba, dcbf, dcbi, dcbst, dcbt, dcbz, icbi */
else if (
(index == 1) || (index == 2) || (index == 7) || (index == 8) || (index == 14) ||
(index == 23) || (index == 30) || (index == 31)) {
if (rc_set || (rs != 0)) {
(index == 1) | (index == 2) | (index == 7) | (index == 8) | (index == 14) |
(index == 23) | (index == 30) | (index == 31)) {
if (rc_set | (rs != 0)) {
opc_illegal(ctx);
return;
} else {
@@ -1449,7 +1449,7 @@ void opc_group59(PPCDisasmContext* ctx) {
if (rc_set)
strcat(opcode, ".");
if ((rc != 0) || (ra != 0))
if ((rc != 0) | (ra != 0))
opc_illegal(ctx);
else
fmt_twoop_flt(ctx->instr_str, "fsqrts", rs, rb);
@@ -1462,7 +1462,7 @@ void opc_group59(PPCDisasmContext* ctx) {
if (rc_set)
strcat(opcode, ".");
if ((rc != 0) || (ra != 0))
if ((rc != 0) | (ra != 0))
opc_illegal(ctx);
else
fmt_twoop_flt(ctx->instr_str, opcode, rs, rb);
@@ -1590,7 +1590,7 @@ void opc_group63(PPCDisasmContext* ctx) {
if (rc_set)
strcat(opcode, ".");
if ((rc != 0) || (ra != 0))
if ((rc != 0) | (ra != 0))
opc_illegal(ctx);
else
fmt_threeop_flt(ctx->instr_str, opcode, rs, ra, rb);
@@ -1627,7 +1627,7 @@ void opc_group63(PPCDisasmContext* ctx) {
if (rc_set)
strcat(opcode, ".");
if ((rc != 0) || (ra != 0))
if ((rc != 0) | (ra != 0))
opc_illegal(ctx);
else
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
@@ -1687,32 +1687,20 @@ void opc_group63(PPCDisasmContext* ctx) {
case 12: /* frsp */
if (ra != 0)
opc_illegal(ctx);
else {
strcpy(opcode, "frsp");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
}
else
ctx->instr_str = my_sprintf("%-8s%d, r%d, r%d", "frsp", rs, rb);
break;
case 14: /* fctiw */
if (ra != 0)
opc_illegal(ctx);
else {
strcpy(opcode, "fctiw");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
}
else
ctx->instr_str = my_sprintf("%-8s%d, r%d, r%d", "fctiw", rs, rb);
break;
case 15: /* fctiwz */
if (ra != 0)
opc_illegal(ctx);
else {
strcpy(opcode, "fctiwz");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
}
else
ctx->instr_str = my_sprintf("%-8s%d, r%d, r%d", "fctiwz", rs, rb);
break;
case 32: /* fcmpo */
if (rs & 3)
@@ -1737,7 +1725,7 @@ void opc_group63(PPCDisasmContext* ctx) {
if (ra != 0)
opc_illegal(ctx);
else
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
ctx->instr_str = my_sprintf("%-8sr%d, r%d", opcode, rs, rb);
break;
case 64:
strcpy(opcode, "mcrfs");
@@ -1750,52 +1738,31 @@ void opc_group63(PPCDisasmContext* ctx) {
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8s%d", opcode, rs);
ctx->instr_str = my_sprintf("%-8scrb%d", opcode, rs);
break;
case 72: /* fmr */
if (ra != 0)
opc_illegal(ctx);
else {
strcpy(opcode, "fmr");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
}
else
ctx->instr_str = my_sprintf("%-8sr%d, r%d", "fmr", rs, rb);
break;
case 134: /* mtfsfi */
if (ra != 0)
opc_illegal(ctx);
else {
strcpy(opcode, "mtfsfi");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8scr%d, %d", opcode, (rs >> 2), (rb >> 1));
}
else
ctx->instr_str = my_sprintf("%-8scr%d, r%d", "mtfsfi", (rs >> 2), (rb >> 1));
break;
case 136: /* fnabs */
if (ra != 0)
opc_illegal(ctx);
else {
strcpy(opcode, "fnabs");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
}
else
ctx->instr_str = my_sprintf("%-8sf%d, f%d", "fnabs", rs, rb);
break;
case 264: /* fabs */
if (ra != 0)
opc_illegal(ctx);
else {
strcpy(opcode, "fabs");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8sf%d, f%d", opcode, rs, rb);
}
else
ctx->instr_str = my_sprintf("%-8s%d, f%d, f%d", "fabs", rs, rb);
break;
case 583: /* mffs */
strcpy(opcode, "mffs");
@@ -1803,18 +1770,13 @@ void opc_group63(PPCDisasmContext* ctx) {
if (rc_set)
strcat(opcode, ".");
if ((ra != 0) || (rb != 0))
if ((ra != 0) | (rb != 0))
opc_illegal(ctx);
else
ctx->instr_str = my_sprintf("%-8sf%d", opcode, rs);
break;
case 711: /* mtfsf */
strcpy(opcode, "mtfsf");
if (rc_set)
strcat(opcode, ".");
ctx->instr_str = my_sprintf("%-8s%d, f%d", opcode, fm, rb);
ctx->instr_str = my_sprintf("%-8sfm%d, r%d", "mtfsf", fm, rb);
break;
default:
opc_illegal(ctx);
+22 -16
View File
@@ -35,7 +35,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
//#define ILLEGAL_OP_SAFE 1
// Uncomment this to use GCC built-in functions.
#define USE_GCC_BUILTINS 1
//#define USE_GCC_BUILTINS 1
// Uncomment this to use Visual Studio built-in functions.
//#define USE_VS_BUILTINS 1
@@ -153,6 +153,9 @@ extern int32_t simult_result;
extern uint32_t uiadd_result;
extern uint32_t uimult_result;
extern int32_t word_copy_location;
extern uint32_t strwrd_replace_value;
extern uint32_t crf_d;
extern uint32_t crf_s;
extern uint32_t reg_s;
@@ -212,14 +215,6 @@ enum class Except_Type {
EXC_TRACE = 13
};
/** Programm Exception subclasses. */
enum Exc_Cause : uint32_t {
FPU_OFF = 1 << (31 - 11),
ILLEGAL_OP = 1 << (31 - 12),
NOT_ALLOWED = 1 << (31 - 13),
TRAP = 1 << (31 - 14),
};
// extern bool bb_end;
extern BB_end_kind bb_kind;
@@ -251,10 +246,13 @@ extern uint32_t supervisor_inst_num;
extern void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t proc_version);
extern void ppc_mmu_init();
[[noreturn]] void ppc_illegalop();
[[noreturn]] void ppc_fpu_off();
void ppc_illegalop();
void ppc_illegalsubop19();
void ppc_illegalsubop31();
void ppc_illegalsubop59();
void ppc_illegalsubop63();
//void ppc_opcode4();
void ppc_opcode4();
void ppc_opcode16();
void ppc_opcode18();
void ppc_opcode19();
@@ -282,8 +280,17 @@ extern void ppc_grab_regsdauimm();
extern void ppc_grab_regsasimm();
extern void ppc_grab_regssauimm();
extern void ppc_grab_regsfpdb(bool int_rep);
extern void ppc_grab_regsfpdab(bool int_rep);
extern void ppc_grab_regsfpdia(bool int_rep);
extern void ppc_grab_regsfpdiab(bool int_rep);
extern void ppc_grab_regsfpsia(bool int_rep);
extern void ppc_grab_regsfpsiab(bool int_rep);
extern void ppc_store_result_regd();
extern void ppc_store_result_rega();
extern void ppc_store_sfpresult(bool int_rep);
extern void ppc_store_dfpresult(bool int_rep);
void ppc_changecrf0(uint32_t set_result);
void ppc_fp_changecrf1();
@@ -394,7 +401,6 @@ extern void ppc_mfsrin();
extern void ppc_mtsr();
extern void ppc_mtsrin();
extern void ppc_mcrf();
extern void ppc_mtcrf();
extern void ppc_mfmsr();
extern void ppc_mfspr();
@@ -478,11 +484,11 @@ extern void ppc_stfdux();
extern void ppc_fadd();
extern void ppc_fsub();
extern void ppc_fmul();
extern void ppc_fmult();
extern void ppc_fdiv();
extern void ppc_fadds();
extern void ppc_fsubs();
extern void ppc_fmuls();
extern void ppc_fmults();
extern void ppc_fdivs();
extern void ppc_fmadd();
extern void ppc_fmsub();
@@ -548,9 +554,9 @@ extern void ppc_main_opcode(void);
extern void ppc_exec(void);
extern void ppc_exec_single(void);
extern void ppc_exec_until(uint32_t goal_addr);
extern void ppc_exec_dbg(uint32_t start_addr, uint32_t size);
/* debugging support API */
void print_gprs(void); /* print content of the general purpose registers */
void print_fprs(void); /* print content of the floating-point registers */
uint64_t get_reg(std::string& reg_name); /* get content of the register reg_name */
void set_reg(std::string& reg_name, uint64_t val); /* set reg_name to val */
+89 -104
View File
@@ -73,7 +73,7 @@ uint8_t tbr_factor; /* cycles_count to TBR freq ratio in 2^x units */
/** Primary opcode (bits 0...5) lookup table. */
static PPCOpcode OpcodeGrabber[] = {
ppc_illegalop, ppc_illegalop, ppc_illegalop, ppc_twi, ppc_illegalop, ppc_illegalop,
ppc_illegalop, ppc_illegalop, ppc_illegalop, ppc_twi, ppc_opcode4, ppc_illegalop,
ppc_illegalop, ppc_mulli, ppc_subfic, power_dozi, ppc_cmpli, ppc_cmpi,
ppc_addic, ppc_addicdot, ppc_addi, ppc_addis, ppc_opcode16, ppc_sc,
ppc_opcode18, ppc_opcode19, ppc_rlwimi, ppc_rlwinm, power_rlmi, ppc_rlwnm,
@@ -82,28 +82,20 @@ static PPCOpcode OpcodeGrabber[] = {
ppc_stw, ppc_stwu, ppc_stb, ppc_stbu, ppc_lhz, ppc_lhzu,
ppc_lha, ppc_lhau, ppc_sth, ppc_sthu, ppc_lmw, ppc_stmw,
ppc_lfs, ppc_lfsu, ppc_lfd, ppc_lfdu, ppc_stfs, ppc_stfsu,
ppc_stfd, ppc_stfdu, ppc_illegalop, ppc_illegalop, ppc_illegalop, ppc_opcode59,
ppc_stfd, ppc_stfdu, ppc_illegalop, ppc_illegalop, ppc_illegalop, ppc_illegalop,
ppc_illegalop, ppc_illegalop, ppc_illegalop, ppc_opcode63};
/** Lookup tables for branch instructions. */
static PPCOpcode SubOpcode16Grabber[] = {
dppc_interpreter::ppc_bc,
dppc_interpreter::ppc_bcl,
dppc_interpreter::ppc_bca,
dppc_interpreter::ppc_bcla};
static PPCOpcode SubOpcode16Grabber[] = {ppc_bc, ppc_bcl, ppc_bca, ppc_bcla};
static PPCOpcode SubOpcode18Grabber[] = {
dppc_interpreter::ppc_b,
dppc_interpreter::ppc_bl,
dppc_interpreter::ppc_ba,
dppc_interpreter::ppc_bla};
static PPCOpcode SubOpcode18Grabber[] = {ppc_b, ppc_bl, ppc_ba, ppc_bla};
/** Instructions decoding tables for integer,
single floating-point, and double-floating point ops respectively */
PPCOpcode SubOpcode31Grabber[1024] = { ppc_illegalop };
PPCOpcode SubOpcode59Grabber[32] = { ppc_illegalop };
PPCOpcode SubOpcode63Grabber[1024] = { ppc_illegalop };
PPCOpcode SubOpcode31Grabber[1024] = {ppc_illegalsubop19};
PPCOpcode SubOpcode59Grabber[1024] = {ppc_illegalsubop19};
PPCOpcode SubOpcode63Grabber[1024] = {ppc_illegalsubop19};
#define UPDATE_TBR_DEC \
@@ -117,18 +109,50 @@ PPCOpcode SubOpcode63Grabber[1024] = { ppc_illegalop };
old_cycles_count += delta << tbr_factor; \
}
/** Exception helpers. */
[[noreturn]] void ppc_illegalop() {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
}
[[noreturn]] void ppc_fpu_off() {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::FPU_OFF);
}
/** Opcode decoding functions. */
void ppc_illegalop() {
LOG_F(ERROR, "Illegal opcode reported: 0x%X Report this! \n", ppc_cur_instruction);
exit(-1);
}
void ppc_illegalsubop19() {
uint16_t illegal_subcode = ppc_cur_instruction & 0x7FF;
uint32_t grab_it = (uint32_t)illegal_subcode;
LOG_F(ERROR, "Illegal subopcode for 19 reported: %d Report this! \n", grab_it);
exit(-1);
}
void ppc_illegalsubop31() {
uint16_t illegal_subcode = ppc_cur_instruction & 0x7FF;
uint32_t grab_it = (uint32_t)illegal_subcode;
LOG_F(ERROR, "Illegal subopcode for 31 reported: %d Report this! \n", grab_it);
exit(-1);
}
void ppc_illegalsubop59() {
uint16_t illegal_subcode = ppc_cur_instruction & 0x7FF;
uint32_t grab_it = (uint32_t)illegal_subcode;
LOG_F(ERROR, "Illegal subopcode for 59 reported: %d Report this! \n", grab_it);
exit(-1);
}
void ppc_illegalsubop63() {
uint16_t illegal_subcode = ppc_cur_instruction & 0x7FF;
uint32_t grab_it = (uint32_t)illegal_subcode;
LOG_F(ERROR, "Illegal subopcode for 63 reported: %d Report this! \n", grab_it);
exit(-1);
}
void ppc_opcode4() {
LOG_F(INFO, "Reading from Opcode 4 table \n");
uint8_t subop_grab = ppc_cur_instruction & 3;
uint32_t regrab = (uint32_t)subop_grab;
LOG_F(ERROR, "Executing subopcode entry %d \n"
".. or would if I bothered to implement it. SORRY!", regrab);
exit(-1);
}
void ppc_opcode16() {
SubOpcode16Grabber[ppc_cur_instruction & 3]();
}
@@ -156,9 +180,6 @@ void ppc_opcode19() {
} else {
switch (subop_grab) {
case 0:
ppc_mcrf();
break;
case 66:
ppc_crnor();
break;
@@ -189,8 +210,8 @@ void ppc_opcode19() {
case 898:
ppc_cror();
break;
default:
ppc_illegalop();
default: // Illegal opcode - should never happen
ppc_illegalsubop19();
}
}
}
@@ -210,8 +231,8 @@ void ppc_opcode31() {
}
void ppc_opcode59() {
uint16_t subop_grab = (ppc_cur_instruction >> 1) & 0x1F;
rc_flag = ppc_cur_instruction & 1;
uint16_t subop_grab = (ppc_cur_instruction & 0x7FF) >> 1;
rc_flag = subop_grab & 1;
#ifdef EXHAUSTIVE_DEBUG
uint32_t regrab = (uint32_t)subop_grab;
LOG_F(INFO, "Executing Opcode 59 table subopcode entry \n", regrab);
@@ -220,8 +241,8 @@ void ppc_opcode59() {
}
void ppc_opcode63() {
uint16_t subop_grab = (ppc_cur_instruction >> 1) & 0x3FF;
rc_flag = ppc_cur_instruction & 1;
uint16_t subop_grab = (ppc_cur_instruction & 0x7FF) >> 1;
rc_flag = subop_grab & 1;
#ifdef EXHAUSTIVE_DEBUG
uint32_t regrab = (uint32_t)subop_grab;
LOG_F(INFO, "Executing Opcode 63 table subopcode entry \n", regrab);
@@ -331,7 +352,7 @@ void ppc_exec_single()
}
/** Execute PPC code until goal_addr is reached. */
void ppc_exec_until(volatile uint32_t goal_addr)
void ppc_exec_until(uint32_t goal_addr)
{
uint32_t bb_start_la, page_start, delta;
uint8_t* pc_real;
@@ -390,68 +411,6 @@ again:
}
}
/** Execute PPC code until control is reached the specified region. */
void ppc_exec_dbg(volatile uint32_t start_addr, volatile uint32_t size)
{
uint32_t bb_start_la, page_start, delta;
uint8_t* pc_real;
/* start new basic block */
glob_bb_start_la = bb_start_la = ppc_state.pc;
bb_kind = BB_end_kind::BB_NONE;
if (setjmp(exc_env)) {
/* reaching here means we got a low-level exception */
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - glob_bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
pc_real = quickinstruction_translate(bb_start_la);
page_start = bb_start_la & 0xFFFFF000;
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
//printf("DBG Exec: got exception, continue at %X\n", ppc_state.pc);
goto again;
}
/* initial MMU translation for the current code page. */
pc_real = quickinstruction_translate(bb_start_la);
/* set current code page limits */
page_start = bb_start_la & 0xFFFFF000;
again:
while (ppc_state.pc < start_addr || ppc_state.pc >= start_addr + size) {
ppc_main_opcode();
if (bb_kind != BB_end_kind::BB_NONE) {
#ifdef NEW_TBR_UPDATE_ALGO
cycles_count += ((ppc_state.pc - bb_start_la) >> 2) + 1;
UPDATE_TBR_DEC
#else
timebase_counter += ((ppc_state.pc - bb_start_la) >> 2) + 1;
#endif
glob_bb_start_la = bb_start_la = ppc_next_instruction_address;
if ((ppc_next_instruction_address & 0xFFFFF000) != page_start) {
page_start = bb_start_la & 0xFFFFF000;
pc_real = quickinstruction_translate(bb_start_la);
} else {
pc_real += (int)bb_start_la - (int)ppc_state.pc;
ppc_set_cur_instruction(pc_real);
}
ppc_state.pc = bb_start_la;
bb_kind = BB_end_kind::BB_NONE;
//printf("DBG Exec: new basic block at %X, start_addr=%X\n", ppc_state.pc, start_addr);
} else {
ppc_state.pc += 4;
pc_real += 4;
ppc_set_cur_instruction(pc_real);
}
}
}
uint64_t instr_count, old_instr_count;
@@ -621,14 +580,28 @@ void initialize_ppc_opcode_tables() {
SubOpcode59Grabber[18] = ppc_fdivs;
SubOpcode59Grabber[20] = ppc_fsubs;
SubOpcode59Grabber[21] = ppc_fadds;
SubOpcode59Grabber[22] = ppc_fsqrts;
SubOpcode59Grabber[24] = ppc_fres;
SubOpcode59Grabber[25] = ppc_fmuls;
SubOpcode59Grabber[28] = ppc_fmsubs;
SubOpcode59Grabber[29] = ppc_fmadds;
SubOpcode59Grabber[30] = ppc_fnmsubs;
SubOpcode59Grabber[31] = ppc_fnmadds;
for (int i = 25; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmults;
}
for (int i = 28; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmsubs;
}
for (int i = 29; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fmadds;
}
for (int i = 30; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fnmsubs;
}
for (int i = 31; i < 1024; i += 32) {
SubOpcode59Grabber[i] = ppc_fnmadds;
}
SubOpcode63Grabber[0] = ppc_fcmpu;
SubOpcode63Grabber[12] = ppc_frsp;
@@ -656,7 +629,7 @@ void initialize_ppc_opcode_tables() {
}
for (int i = 25; i < 1024; i += 32) {
SubOpcode63Grabber[i] = ppc_fmul;
SubOpcode63Grabber[i] = ppc_fmult;
}
for (int i = 28; i < 1024; i += 32) {
@@ -743,6 +716,18 @@ void ppc_cpu_init(MemCtrlBase* mem_ctrl, uint32_t proc_version) {
ppc_state.pc = 0xFFF00100;
}
void print_gprs() {
for (int i = 0; i < 32; i++)
cout << "GPR " << dec << i << " : " << uppercase << hex << ppc_state.gpr[i] << endl;
cout << "PC: " << uppercase << hex << ppc_state.pc << endl;
cout << "LR: " << uppercase << hex << ppc_state.spr[SPR::LR] << endl;
cout << "CR: " << uppercase << hex << ppc_state.cr << endl;
cout << "CTR: " << uppercase << hex << ppc_state.spr[SPR::CTR] << endl;
cout << "XER: " << uppercase << hex << ppc_state.spr[SPR::XER] << endl;
cout << "MSR: " << uppercase << hex << ppc_state.msr << endl;
}
void print_fprs() {
for (int i = 0; i < 32; i++)
cout << "FPR " << dec << i << " : " << ppc_state.fpr[i].dbl64_r << endl;
+325 -279
View File
File diff suppressed because it is too large. Load diff
+10 -50
View File
@@ -30,7 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "ppcmmu.h"
#include "devices/memctrlbase.h"
#include "memaccess.h"
#include "memreadwrite.h"
#include "ppcemu.h"
#include <array>
#include <cinttypes>
@@ -55,8 +55,6 @@ AddressMapEntry last_ptab_area = {0xFFFFFFFF, 0xFFFFFFFF};
AddressMapEntry last_dma_area = {0xFFFFFFFF, 0xFFFFFFFF};
#define WRITE_BYTE(addr, val) (*(addr) = val)
/* macro for generating code reading from physical memory */
#define READ_PHYS_MEM(ENTRY, ADDR, OP, SIZE, UNVAL) \
{ \
@@ -373,32 +371,13 @@ static uint32_t ppc_mmu_addr_translate(uint32_t la, int is_write) {
}
static void mem_write_unaligned(uint32_t addr, uint32_t value, uint32_t size) {
#ifdef MMU_DEBUG
LOG_F(WARNING, "Attempt to write unaligned %d bytes to 0x%08X\n", size, addr);
#endif
//LOG_F(WARNING, "Attempt to write unaligned %d bytes to 0x%08X\n", size, addr);
if (((addr & 0xFFF) + size) > 0x1000) {
// Special case: unaligned cross-page writes
LOG_F(WARNING, "Cross-page unaligned write, addr=%08X, size=%d\n",
addr, size);
uint32_t phys_addr;
uint32_t shift = (size - 1) * 8;
// Break misaligned memory accesses into multiple, smaller accesses
// and retranslate on page boundary.
// Because such accesses suffer a performance penalty, they will be
// presumably very rare so don't care much about performance.
for (int i = 0; i < size; shift -= 8, addr++, phys_addr++, i++) {
if ((ppc_state.msr & 0x10) && (!i || !(addr & 0xFFF))) {
phys_addr = ppc_mmu_addr_translate(addr, 0);
}
WRITE_PHYS_MEM(last_write_area, phys_addr, WRITE_BYTE,
(value >> shift) & 0xFF, 1);
}
LOG_F(ERROR, "SOS! Cross-page unaligned write, addr=%08X, size=%d\n", addr, size);
exit(-1); // FIXME!
} else {
// data address translation if enabled
/* data address translation if enabled */
if (ppc_state.msr & 0x10) {
addr = ppc_mmu_addr_translate(addr, 0);
}
@@ -417,6 +396,8 @@ void mem_write_byte(uint32_t addr, uint8_t value) {
addr = ppc_mmu_addr_translate(addr, 1);
}
#define WRITE_BYTE(addr, val) (*(addr) = val)
WRITE_PHYS_MEM(last_write_area, addr, WRITE_BYTE, value, 1);
}
@@ -463,32 +444,11 @@ void mem_write_qword(uint32_t addr, uint64_t value) {
static uint32_t mem_grab_unaligned(uint32_t addr, uint32_t size) {
uint32_t ret = 0;
#ifdef MMU_DEBUG
LOG_F(WARNING, "Attempt to read unaligned %d bytes from 0x%08X\n", size, addr);
#endif
//LOG_F(WARNING, "Attempt to read unaligned %d bytes from 0x%08X\n", size, addr);
if (((addr & 0xFFF) + size) > 0x1000) {
// Special case: misaligned cross-page reads
LOG_F(WARNING, "Cross-page unaligned read, addr=%08X, size=%d\n",
addr, size);
uint32_t phys_addr;
uint32_t res = 0;
// Break misaligned memory accesses into multiple, smaller accesses
// and retranslate on page boundary.
// Because such accesses suffer a performance penalty, they will be
// presumably very rare so don't care much about performance.
for (int i = 0; i < size; addr++, phys_addr++, i++) {
if ((ppc_state.msr & 0x10) && (!i || !(addr & 0xFFF))) {
phys_addr = ppc_mmu_addr_translate(addr, 0);
}
READ_PHYS_MEM(last_read_area, phys_addr, *, 1, 0xFFU);
res = (res << 8) | ret;
}
return res;
LOG_F(ERROR, "SOS! Cross-page unaligned read, addr=%08X, size=%d\n", addr, size);
exit(-1); // FIXME!
} else {
/* data address translation if enabled */
if (ppc_state.msr & 0x10) {
+186 -163
View File
@@ -56,6 +56,8 @@ uint32_t ppc_result_b = 0;
uint32_t ppc_result_c = 0;
uint32_t ppc_result_d = 0;
uint32_t strwrd_replace_value;
/**
Extract the registers desired and the values of the registers
This also takes the MSR into account, mainly to determine
@@ -71,27 +73,27 @@ void ppc_store_result_rega() {
ppc_state.gpr[reg_a] = ppc_result_a;
}
inline void ppc_grab_regsdasimm() {
void ppc_grab_regsdasimm() {
reg_d = (ppc_cur_instruction >> 21) & 31;
reg_a = (ppc_cur_instruction >> 16) & 31;
simm = (int32_t)((int16_t)((ppc_cur_instruction)&65535));
ppc_result_a = ppc_state.gpr[reg_a];
}
inline void ppc_grab_regsdauimm() {
void ppc_grab_regsdauimm() {
reg_d = (ppc_cur_instruction >> 21) & 31;
reg_a = (ppc_cur_instruction >> 16) & 31;
uimm = (uint32_t)((uint16_t)((ppc_cur_instruction)&65535));
ppc_result_a = ppc_state.gpr[reg_a];
}
inline void ppc_grab_regsasimm() {
void ppc_grab_regsasimm() {
reg_a = (ppc_cur_instruction >> 16) & 31;
simm = (int32_t)((int16_t)(ppc_cur_instruction & 65535));
ppc_result_a = ppc_state.gpr[reg_a];
}
inline void ppc_grab_regssauimm() {
void ppc_grab_regssauimm() {
reg_s = (ppc_cur_instruction >> 21) & 31;
reg_a = (ppc_cur_instruction >> 16) & 31;
uimm = (uint32_t)((uint16_t)((ppc_cur_instruction)&65535));
@@ -123,7 +125,7 @@ void ppc_grab_regssa() {
ppc_result_a = ppc_state.gpr[reg_a];
}
inline void ppc_grab_regssb() {
void ppc_grab_regssb() {
reg_s = (ppc_cur_instruction >> 21) & 31;
reg_b = (ppc_cur_instruction >> 11) & 31;
ppc_result_d = ppc_state.gpr[reg_s];
@@ -136,7 +138,7 @@ void ppc_grab_regsda() {
ppc_result_a = ppc_state.gpr[reg_a];
}
inline void ppc_grab_regsdb() {
void ppc_grab_regsdb() {
reg_d = (ppc_cur_instruction >> 21) & 31;
reg_b = (ppc_cur_instruction >> 11) & 31;
ppc_result_b = ppc_state.gpr[reg_b];
@@ -269,7 +271,7 @@ void dppc_interpreter::ppc_addme() {
uint32_t xer_ca = !!(ppc_state.spr[SPR::XER] & 0x20000000);
ppc_result_d = ppc_result_a + xer_ca - 1;
if (((xer_ca - 1) < 0xFFFFFFFFUL) || (ppc_result_d < ppc_result_a)) {
if (((xer_ca - 1) < 0xFFFFFFFFUL) | (ppc_result_d < ppc_result_a)) {
ppc_state.spr[SPR::XER] |= 0x20000000UL;
} else {
ppc_state.spr[SPR::XER] &= 0xDFFFFFFFUL;
@@ -293,7 +295,7 @@ void dppc_interpreter::ppc_addze() {
} else {
ppc_state.spr[SPR::XER] &= 0xDFFFFFFFUL;
}
if (oe_flag)
ppc_setsoov(ppc_result_a, 0xFFFFFFFFUL, ppc_result_d);
if (rc_flag)
@@ -353,7 +355,7 @@ void dppc_interpreter::ppc_subfme() {
uint32_t grab_xer = !!(ppc_state.spr[SPR::XER] & 0x20000000);
ppc_result_d = ~ppc_result_a + grab_xer - 1;
ppc_carry(~ppc_result_a, ppc_result_d);
if (oe_flag)
ppc_setsoov(0xFFFFFFFF, ppc_result_a, ppc_result_d);
if (rc_flag)
@@ -504,7 +506,7 @@ void dppc_interpreter::ppc_neg() {
else
ppc_state.spr[SPR::XER] &= 0xBFFFFFFF;
}
if (rc_flag)
ppc_changecrf0(ppc_result_d);
@@ -514,16 +516,15 @@ void dppc_interpreter::ppc_neg() {
void dppc_interpreter::ppc_cntlzw() {
ppc_grab_regssa();
uint32_t lead = 0;
uint32_t bit_check = ppc_result_d;
#ifdef USE_GCC_BUILTINS
uint32_t lead = !bit_check ? 32 : __builtin_clz(bit_check);
lead = __builtin_clz(bit_check);
#elif defined USE_VS_BUILTINS
uint32_t lead = __lzcnt(bit_check);
lead = __lzcnt(bit_check);
#else
uint32_t lead, mask;
for (mask = 0x80000000UL, lead = 0; mask != 0; lead++, mask >>= 1) {
for (uint32_t mask = 0x80000000UL; mask; lead++, mask >>= 1) {
if (bit_check & mask)
break;
}
@@ -571,7 +572,7 @@ void dppc_interpreter::ppc_mullw() {
}
}
ppc_result_d = (uint32_t)product;
ppc_result_d = (uint32_t)product;
if (rc_flag)
ppc_changecrf0(ppc_result_d);
@@ -601,7 +602,8 @@ void dppc_interpreter::ppc_divw() {
if (oe_flag)
ppc_state.spr[SPR::XER] |= 0xC0000000;
} else { /* normal signed devision */
}
else { /* normal signed devision */
ppc_result_d = (int32_t)ppc_result_a / (int32_t)ppc_result_b;
if (oe_flag)
@@ -809,7 +811,7 @@ void dppc_interpreter::ppc_mfmsr() {
supervisor_inst_num++;
#endif
if (ppc_state.msr & 0x4000) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::NOT_ALLOWED);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x00040000);
}
reg_d = (ppc_cur_instruction >> 21) & 31;
ppc_state.gpr[reg_d] = ppc_state.msr;
@@ -820,7 +822,7 @@ void dppc_interpreter::ppc_mtmsr() {
supervisor_inst_num++;
#endif
if (ppc_state.msr & 0x4000) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::NOT_ALLOWED);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x00040000);
}
reg_s = (ppc_cur_instruction >> 21) & 31;
ppc_state.msr = ppc_state.gpr[reg_s];
@@ -915,7 +917,8 @@ void dppc_interpreter::ppc_mtcrf() {
}
void dppc_interpreter::ppc_mcrxr() {
int crf_d = (ppc_cur_instruction >> 21) & 0x1C;
crf_d = (ppc_cur_instruction >> 23) & 7;
crf_d = crf_d << 2;
ppc_state.cr = (ppc_state.cr & ~(0xF0000000UL >> crf_d)) |
((ppc_state.spr[SPR::XER] & 0xF0000000UL) >> crf_d);
ppc_state.spr[SPR::XER] &= 0x0FFFFFFF;
@@ -954,30 +957,30 @@ void dppc_interpreter::ppc_b() {
uint32_t quick_test = (ppc_cur_instruction & 0x03FFFFFC);
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = (uint32_t)(ppc_state.pc + adr_li);
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
void dppc_interpreter::ppc_bl() {
uint32_t quick_test = (ppc_cur_instruction & 0x03FFFFFC);
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = (uint32_t)(ppc_state.pc + adr_li);
ppc_state.spr[SPR::LR] = (uint32_t)(ppc_state.pc + 4);
bb_kind = BB_end_kind::BB_BRANCH;
ppc_state.spr[SPR::LR] = (uint32_t)(ppc_state.pc + 4);
bb_kind = BB_end_kind::BB_BRANCH;
}
void dppc_interpreter::ppc_ba() {
uint32_t quick_test = (ppc_cur_instruction & 0x03FFFFFC);
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = adr_li;
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
void dppc_interpreter::ppc_bla() {
uint32_t quick_test = (ppc_cur_instruction & 0x03FFFFFC);
adr_li = (quick_test < 0x2000000) ? quick_test : (0xFC000000UL + quick_test);
ppc_next_instruction_address = adr_li;
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
bb_kind = BB_end_kind::BB_BRANCH;
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
bb_kind = BB_end_kind::BB_BRANCH;
}
void dppc_interpreter::ppc_bc() {
@@ -995,7 +998,7 @@ void dppc_interpreter::ppc_bc() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.pc + br_bd);
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
}
@@ -1014,7 +1017,7 @@ void dppc_interpreter::ppc_bca() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = br_bd;
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
}
@@ -1033,7 +1036,7 @@ void dppc_interpreter::ppc_bcl() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.pc + br_bd);
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1053,7 +1056,7 @@ void dppc_interpreter::ppc_bcla() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = br_bd;
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1067,7 +1070,7 @@ void dppc_interpreter::ppc_bcctr() {
if (cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::CTR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
}
@@ -1080,7 +1083,7 @@ void dppc_interpreter::ppc_bcctrl() {
if (cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::CTR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1099,7 +1102,7 @@ void dppc_interpreter::ppc_bclr() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::LR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
}
@@ -1117,7 +1120,7 @@ void dppc_interpreter::ppc_bclrl() {
if (ctr_ok && cnd_ok) {
ppc_next_instruction_address = (ppc_state.spr[SPR::LR] & 0xFFFFFFFCUL);
bb_kind = BB_end_kind::BB_BRANCH;
bb_kind = BB_end_kind::BB_BRANCH;
}
ppc_state.spr[SPR::LR] = ppc_state.pc + 4;
}
@@ -1131,7 +1134,8 @@ void dppc_interpreter::ppc_cmp() {
}
#endif
int crf_d = (ppc_cur_instruction >> 21) & 0x1C;
crf_d = (ppc_cur_instruction >> 23) & 7;
crf_d = crf_d << 2;
ppc_grab_regssab();
xercon = (ppc_state.spr[SPR::XER] & 0x80000000UL) >> 3;
cmp_c = (((int32_t)ppc_result_a) == ((int32_t)ppc_result_b))
@@ -1148,7 +1152,8 @@ void dppc_interpreter::ppc_cmpi() {
}
#endif
int crf_d = (ppc_cur_instruction >> 21) & 0x1C;
crf_d = (ppc_cur_instruction >> 23) & 7;
crf_d = crf_d << 2;
ppc_grab_regsasimm();
xercon = (ppc_state.spr[SPR::XER] & 0x80000000UL) >> 3;
cmp_c = (((int32_t)ppc_result_a) == simm)
@@ -1165,7 +1170,8 @@ void dppc_interpreter::ppc_cmpl() {
}
#endif
int crf_d = (ppc_cur_instruction >> 21) & 0x1C;
crf_d = (ppc_cur_instruction >> 23) & 7;
crf_d = crf_d << 2;
ppc_grab_regssab();
xercon = (ppc_state.spr[SPR::XER] & 0x80000000UL) >> 3;
cmp_c = (ppc_result_a == ppc_result_b)
@@ -1182,7 +1188,8 @@ void dppc_interpreter::ppc_cmpli() {
}
#endif
int crf_d = (ppc_cur_instruction >> 21) & 0x1C;
crf_d = (ppc_cur_instruction >> 23) & 7;
crf_d = crf_d << 2;
ppc_grab_regssauimm();
xercon = (ppc_state.spr[SPR::XER] & 0x80000000UL) >> 3;
cmp_c = (ppc_result_a == uimm) ? 0x20000000UL
@@ -1192,14 +1199,6 @@ void dppc_interpreter::ppc_cmpli() {
// Condition Register Changes
void dppc_interpreter::ppc_mcrf() {
int crf_d = (ppc_cur_instruction >> 21) & 0x1C;
int crf_s = (ppc_cur_instruction >> 16) & 0x1C;
uint32_t grab_s = ppc_state.cr & (0xf0000000UL >> crf_s);
ppc_state.cr = ((ppc_state.cr & ~(0xf0000000UL >> crf_d)) | (grab_s >> crf_d));
}
void dppc_interpreter::ppc_crand() {
ppc_grab_regsdab();
if ((ppc_state.cr & (0x80000000UL >> reg_a)) && (ppc_state.cr & (0x80000000UL >> reg_b))) {
@@ -1208,7 +1207,6 @@ void dppc_interpreter::ppc_crand() {
ppc_state.cr &= ~(0x80000000UL >> reg_d);
}
}
void dppc_interpreter::ppc_crandc() {
ppc_grab_regsdab();
if ((ppc_state.cr & (0x80000000UL >> reg_a)) && !(ppc_state.cr & (0x80000000UL >> reg_b))) {
@@ -1295,7 +1293,7 @@ void dppc_interpreter::ppc_tw() {
(((int32_t)ppc_state.gpr[reg_a] == (int32_t)ppc_state.gpr[reg_b]) && (ppc_to & 0x04)) ||
((ppc_state.gpr[reg_a] < ppc_state.gpr[reg_b]) && (ppc_to & 0x02)) ||
((ppc_state.gpr[reg_a] > ppc_state.gpr[reg_b]) && (ppc_to & 0x01))) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::TRAP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1308,7 +1306,7 @@ void dppc_interpreter::ppc_twi() {
(((int32_t)ppc_state.gpr[reg_a] == simm) && (ppc_to & 0x04)) ||
((ppc_state.gpr[reg_a] < (uint32_t)simm) && (ppc_to & 0x02)) ||
((ppc_state.gpr[reg_a] > (uint32_t)simm) && (ppc_to & 0x01))) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::TRAP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1356,13 +1354,16 @@ void dppc_interpreter::ppc_dcbtst() {
void dppc_interpreter::ppc_dcbz() {
ppc_grab_regsdab();
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
ppc_effective_address &= 0xFFFFFFE0; // align EA on a 32-byte boundary
mem_write_qword(ppc_effective_address, 0);
mem_write_qword((ppc_effective_address + 8), 0);
mem_write_qword((ppc_effective_address + 16), 0);
mem_write_qword((ppc_effective_address + 24), 0);
if (!(ppc_state.pc & 32) && (ppc_state.pc < 0xFFFFFFE0UL)) {
ppc_grab_regsdab();
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
mem_write_qword(ppc_effective_address, 0);
mem_write_qword((ppc_effective_address + 8), 0);
mem_write_qword((ppc_effective_address + 16), 0);
mem_write_qword((ppc_effective_address + 24), 0);
} else {
ppc_exception_handler(Except_Type::EXC_ALIGNMENT, 0x00000);
}
}
@@ -1371,13 +1372,13 @@ void dppc_interpreter::ppc_dcbz() {
void dppc_interpreter::ppc_stb() {
ppc_grab_regssa();
ppc_effective_address = (int32_t)((int16_t)(ppc_cur_instruction & 0xFFFF));
ppc_effective_address += reg_a ? ppc_result_a : 0;
ppc_effective_address += (reg_a > 0) ? ppc_result_a : 0;
mem_write_byte(ppc_effective_address, ppc_result_d);
}
void dppc_interpreter::ppc_stbx() {
ppc_grab_regssab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
mem_write_byte(ppc_effective_address, ppc_result_d);
}
@@ -1389,7 +1390,7 @@ void dppc_interpreter::ppc_stbu() {
mem_write_byte(ppc_effective_address, ppc_result_d);
ppc_state.gpr[reg_a] = ppc_effective_address;
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1400,7 +1401,7 @@ void dppc_interpreter::ppc_stbux() {
mem_write_byte(ppc_effective_address, ppc_result_d);
ppc_state.gpr[reg_a] = ppc_effective_address;
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1419,7 +1420,7 @@ void dppc_interpreter::ppc_sthu() {
mem_write_word(ppc_effective_address, ppc_result_d);
ppc_state.gpr[reg_a] = ppc_effective_address;
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1430,13 +1431,13 @@ void dppc_interpreter::ppc_sthux() {
mem_write_word(ppc_effective_address, ppc_result_d);
ppc_state.gpr[reg_a] = ppc_effective_address;
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
void dppc_interpreter::ppc_sthx() {
ppc_grab_regssab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
mem_write_word(ppc_effective_address, ppc_result_d);
}
@@ -1449,20 +1450,20 @@ void dppc_interpreter::ppc_sthbrx() {
void dppc_interpreter::ppc_stw() {
ppc_grab_regssa();
ppc_effective_address = (int32_t)((int16_t)(ppc_cur_instruction & 0xFFFF));
ppc_effective_address += reg_a ? ppc_result_a : 0;
ppc_effective_address += (reg_a > 0) ? ppc_result_a : 0;
mem_write_dword(ppc_effective_address, ppc_result_d);
}
void dppc_interpreter::ppc_stwx() {
ppc_grab_regssab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
mem_write_dword(ppc_effective_address, ppc_result_d);
}
void dppc_interpreter::ppc_stwcx() {
// PLACEHOLDER CODE FOR STWCX - We need to check for reserve memory
if (rc_flag == 0) {
ppc_illegalop();
ppc_illegalsubop31();
} else {
ppc_grab_regssab();
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
@@ -1484,7 +1485,7 @@ void dppc_interpreter::ppc_stwu() {
mem_write_dword(ppc_effective_address, ppc_result_d);
ppc_state.gpr[reg_a] = ppc_effective_address;
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1495,13 +1496,13 @@ void dppc_interpreter::ppc_stwux() {
mem_write_dword(ppc_effective_address, ppc_result_d);
ppc_state.gpr[reg_a] = ppc_effective_address;
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
void dppc_interpreter::ppc_stwbrx() {
ppc_grab_regssab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
ppc_result_d = BYTESWAP_32(ppc_result_d);
mem_write_dword(ppc_effective_address, ppc_result_d);
}
@@ -1540,13 +1541,13 @@ void dppc_interpreter::ppc_lbzu() {
ppc_store_result_regd();
ppc_store_result_rega();
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
void dppc_interpreter::ppc_lbzx() {
ppc_grab_regsdab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
ppc_result_d = mem_grab_byte(ppc_effective_address);
ppc_store_result_regd();
}
@@ -1560,7 +1561,7 @@ void dppc_interpreter::ppc_lbzux() {
ppc_store_result_regd();
ppc_store_result_rega();
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1568,7 +1569,7 @@ void dppc_interpreter::ppc_lbzux() {
void dppc_interpreter::ppc_lhz() {
ppc_grab_regsda();
ppc_effective_address = (int32_t)((int16_t)(ppc_cur_instruction & 0xFFFF));
ppc_effective_address += reg_a ? ppc_result_a : 0;
ppc_effective_address += (reg_a > 0) ? ppc_result_a : 0;
ppc_result_d = mem_grab_word(ppc_effective_address);
ppc_store_result_regd();
}
@@ -1583,13 +1584,13 @@ void dppc_interpreter::ppc_lhzu() {
ppc_store_result_regd();
ppc_store_result_rega();
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
void dppc_interpreter::ppc_lhzx() {
ppc_grab_regsdab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
ppc_result_d = mem_grab_word(ppc_effective_address);
ppc_store_result_regd();
}
@@ -1603,7 +1604,7 @@ void dppc_interpreter::ppc_lhzux() {
ppc_store_result_regd();
ppc_store_result_rega();
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
@@ -1635,13 +1636,13 @@ void dppc_interpreter::ppc_lhau() {
ppc_result_a = ppc_effective_address;
ppc_store_result_rega();
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
void dppc_interpreter::ppc_lhaux() {
ppc_grab_regsdab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
uint16_t val = mem_grab_word(ppc_effective_address);
if (val & 0x8000) {
ppc_result_d = 0xFFFF0000UL | (uint32_t)val;
@@ -1655,7 +1656,7 @@ void dppc_interpreter::ppc_lhaux() {
void dppc_interpreter::ppc_lhax() {
ppc_grab_regsdab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
uint16_t val = mem_grab_word(ppc_effective_address);
if (val & 0x8000) {
ppc_result_d = 0xFFFF0000UL | (uint32_t)val;
@@ -1667,7 +1668,7 @@ void dppc_interpreter::ppc_lhax() {
void dppc_interpreter::ppc_lhbrx() {
ppc_grab_regsdab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
ppc_result_d = (uint32_t)(BYTESWAP_16(mem_grab_word(ppc_effective_address)));
ppc_store_result_regd();
}
@@ -1682,7 +1683,7 @@ void dppc_interpreter::ppc_lwz() {
void dppc_interpreter::ppc_lwbrx() {
ppc_grab_regsdab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
ppc_result_d = BYTESWAP_32(mem_grab_dword(ppc_effective_address));
ppc_store_result_regd();
}
@@ -1697,13 +1698,13 @@ void dppc_interpreter::ppc_lwzu() {
ppc_result_a = ppc_effective_address;
ppc_store_result_rega();
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
}
void dppc_interpreter::ppc_lwzx() {
ppc_grab_regsdab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
ppc_result_d = mem_grab_dword(ppc_effective_address);
ppc_store_result_regd();
}
@@ -1713,7 +1714,7 @@ void dppc_interpreter::ppc_lwzux() {
if ((reg_a != reg_d) || reg_a != 0) {
ppc_effective_address = ppc_result_a + ppc_result_b;
} else {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x20000);
}
ppc_result_d = mem_grab_dword(ppc_effective_address);
ppc_result_a = ppc_effective_address;
@@ -1744,144 +1745,166 @@ void dppc_interpreter::ppc_lmw() {
void dppc_interpreter::ppc_lswi() {
ppc_grab_regsda();
ppc_effective_address = reg_a ? ppc_result_a : 0;
grab_inb = (ppc_cur_instruction >> 11) & 31;
grab_inb = grab_inb ? grab_inb : 32;
uint32_t stringed_word = 0;
ppc_effective_address = ppc_result_a;
grab_inb = (ppc_cur_instruction >> 11) & 31;
grab_inb = (grab_inb & 32) == 0 ? 32 : grab_inb;
uint32_t shift_times = 0;
while (grab_inb > 0) {
switch (grab_inb) {
switch (shift_times) {
case 0:
ppc_state.gpr[reg_d] = (ppc_result_d & 0x00FFFFFFUL) |
(mem_grab_byte(ppc_effective_address) << 24);
ppc_store_result_regd();
break;
case 1:
stringed_word = mem_grab_byte(ppc_effective_address) << 24;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
ppc_result_d = (ppc_result_d & 0xFF00FFFFUL) |
(mem_grab_byte(ppc_effective_address) << 16);
ppc_store_result_regd();
break;
case 2:
stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
ppc_result_d = (ppc_result_d & 0xFFFF00FFUL) |
(mem_grab_byte(ppc_effective_address) << 8);
ppc_store_result_regd();
break;
case 3:
stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
stringed_word += mem_grab_byte(ppc_effective_address + 2) << 8;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
ppc_result_d = (ppc_result_d & 0xFFFFFF00UL) | mem_grab_byte(ppc_effective_address);
ppc_store_result_regd();
break;
default:
ppc_state.gpr[reg_d] = mem_grab_word(ppc_effective_address);
reg_d++;
ppc_effective_address += 4;
grab_inb -= 4;
LOG_F(ERROR, "Something really horrible happened with lswi. \n");
}
if (shift_times == 3) {
shift_times = 0;
reg_d = (reg_d + 1) & 0x1F;
} else {
shift_times++;
}
ppc_effective_address++;
grab_inb--;
}
}
void dppc_interpreter::ppc_lswx() {
ppc_grab_regsdab();
// Invalid instruction forms
if ((ppc_result_d == 0) && (ppc_result_a == 0)) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x100000);
}
if ((ppc_result_d == ppc_result_a) || (ppc_result_a == ppc_result_b)) {
ppc_exception_handler(Except_Type::EXC_PROGRAM, Exc_Cause::ILLEGAL_OP);
ppc_exception_handler(Except_Type::EXC_PROGRAM, 0x100000);
}
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
grab_inb = ppc_state.spr[SPR::XER] & 127;
uint32_t stringed_word = 0;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
grab_inb = ppc_state.spr[SPR::XER] & 127;
uint32_t shift_times = 0;
while (grab_inb > 0) {
switch (grab_inb) {
switch (shift_times) {
case 0:
ppc_result_d = (ppc_result_d & 0x00FFFFFFUL) |
(mem_grab_byte(ppc_effective_address) << 24);
ppc_store_result_regd();
break;
case 1:
stringed_word = mem_grab_byte(ppc_effective_address) << 24;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
ppc_result_d = (ppc_result_d & 0xFF00FFFFUL) |
(mem_grab_byte(ppc_effective_address) << 16);
ppc_store_result_regd();
break;
case 2:
stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
ppc_result_d = (ppc_result_d & 0xFFFF00FFUL) |
(mem_grab_byte(ppc_effective_address) << 8);
ppc_store_result_regd();
break;
case 3:
stringed_word = mem_grab_byte(ppc_effective_address) << 24;
stringed_word += mem_grab_byte(ppc_effective_address + 1) << 16;
stringed_word += mem_grab_byte(ppc_effective_address + 2) << 8;
ppc_state.gpr[reg_d] = stringed_word;
grab_inb = 0;
ppc_result_d = (ppc_result_d & 0xFFFFFF00UL) | mem_grab_byte(ppc_effective_address);
ppc_store_result_regd();
break;
default:
ppc_state.gpr[reg_d] = mem_grab_word(ppc_effective_address);
reg_d++;
ppc_effective_address += 4;
grab_inb -= 4;
LOG_F(ERROR, "Something really horrible happened with lswx. \n");
}
if (shift_times == 3) {
shift_times = 0;
reg_d = (reg_d + 1) & 0x1F;
} else {
shift_times++;
}
ppc_effective_address++;
grab_inb--;
}
}
void dppc_interpreter::ppc_stswi() {
ppc_grab_regssa();
ppc_effective_address = reg_a ? ppc_result_a : 0;
ppc_effective_address = (reg_a == 0) ? 0 : ppc_result_a;
grab_inb = (ppc_cur_instruction >> 11) & 31;
grab_inb = (grab_inb & 32) == 0 ? 32 : grab_inb;
grab_inb = (grab_inb & 32) == 0 ? 32 : grab_inb;
uint32_t shift_times = 0;
while (grab_inb > 0) {
switch (grab_inb) {
switch (shift_times) {
case 0:
strwrd_replace_value = (ppc_result_d >> 24);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
case 1:
mem_write_byte(ppc_effective_address, (ppc_result_d >> 24));
grab_inb = 0;
strwrd_replace_value = (ppc_result_d >> 16);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
case 2:
mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
grab_inb = 0;
strwrd_replace_value = (ppc_result_d >> 8);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
case 3:
mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
mem_write_byte((ppc_effective_address + 2), ((ppc_result_d >> 8) & 0xFF));
grab_inb = 0;
strwrd_replace_value = (ppc_result_d);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
default:
mem_write_dword(ppc_effective_address, ppc_result_d);
reg_s++;
ppc_effective_address += 4;
grab_inb -= 4;
LOG_F(ERROR, "Something really horrible happened with stswi. \n");
}
if (shift_times == 3) {
shift_times = 0;
reg_s = (reg_s + 1) & 0x1F;
} else {
shift_times++;
}
ppc_effective_address++;
grab_inb--;
}
}
void dppc_interpreter::ppc_stswx() {
ppc_grab_regssab();
ppc_effective_address = reg_a ? (ppc_result_a + ppc_result_b) : ppc_result_b;
ppc_effective_address = (reg_a == 0) ? ppc_result_b : (ppc_result_a + ppc_result_b);
grab_inb = ppc_state.spr[SPR::XER] & 127;
uint32_t shift_times = 0;
while (grab_inb > 0) {
switch (grab_inb) {
switch (shift_times) {
case 0:
strwrd_replace_value = (ppc_result_d >> 24);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
case 1:
mem_write_byte(ppc_effective_address, (ppc_result_d >> 24));
grab_inb = 0;
strwrd_replace_value = (ppc_result_d >> 16);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
case 2:
mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
grab_inb = 0;
strwrd_replace_value = (ppc_result_d >> 8);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
case 3:
mem_write_byte(ppc_effective_address, ((ppc_result_d >> 24) & 0xFF));
mem_write_byte((ppc_effective_address + 1), ((ppc_result_d >> 16) & 0xFF));
mem_write_byte((ppc_effective_address + 2), ((ppc_result_d >> 8) & 0xFF));
grab_inb = 0;
strwrd_replace_value = (ppc_result_d);
mem_write_byte(ppc_effective_address, strwrd_replace_value);
break;
default:
mem_write_dword(ppc_effective_address, ppc_result_d);
reg_s++;
ppc_effective_address += 4;
grab_inb -= 4;
LOG_F(ERROR, "Something really horrible happened with stswx. \n");
}
if (shift_times == 3) {
shift_times = 0;
reg_s = (reg_s + 1) & 0x1F;
} else {
shift_times++;
}
ppc_effective_address++;
grab_inb--;
}
}
+3 -16
View File
@@ -331,9 +331,9 @@
0xFFF00100,0xCC1F0008,lfdu,f0,0x8(r31)
0xFFF00100,0xC1628398,lfs,f11,-0x7C68(r2)
0xFFF00100,0xC5BE0004,lfsu,f13,0x4(r30)
0xFFF00100,0xDBC1FFF0,stfd,f30,-0x10(r1)
0xFFF00100,0xD8010860,stfd,f0,0x860(r1)
0xFFF00100,0xDC180008,stfdu,f0,0x8(r24)
0xFFF00100,0xDBC1FFF0,sftd,f30,-0x10(r1)
0xFFF00100,0xD8010860,sftd,f0,0x860(r1)
0xFFF00100,0xDC180008,sftdu,f0,0x8(r24)
0xFFF00100,0xD01E0110,stfs,f0,0x110(r30)
0xFFF00100,0xD58B0004,stfsu,f12,0x4(r11)
0xFFF00100,0x7C1C5CAE,lfdx,f0,r28,r11
@@ -362,21 +362,9 @@
0xFFF00100,0xEDA66279,fmsubs.,f13,f6,f9,f12
0xFFF00100,0xEC00637C,fnmsubs,f0,f0,f13,f12
0xFFF00100,0xFE0820AF,fsel.,f16,f8,f2,f4
0xFFF00100,0xFDA06050,fneg,f13,f12
0xFFF00100,0xFD80EA10,fabs,f12,f29
0xFFF00100,0xFD600110,fnabs,f11,f0
0xFFF00100,0xFD002034,frsqrte,f8,f4
0xFFF00100,0x7FF957AE,stfiwx,f31,r25,r10
0xFFF00100,0xFC40F818,frsp,f2,f31
0xFFF00100,0xFC201019,frsp.,f1,f2
0xFFF00100,0xFDA0F81E,fctiwz,f13,f31
0xFFF00100,0xFCA0501D,fctiw.,f5,f10
0xFFF00100,0xFD9C0080,mcrfs,cr3,cr7
0xFFF00100,0xFDFE058E,mtfsf,255,f0
0xFFF00100,0xFF80F10C,mtfsfi,cr7,15
0xFFF00100,0xFF80F10D,mtfsfi.,cr7,15
0xFFF00100,0xFC406890,fmr,f2,f13
0xFFF00100,0xFC20E891,fmr.,f1,f29
# compare instructions
0xFFF00100,0x7C15A000,cmpw,r21,r20
@@ -396,7 +384,6 @@
0xFFF00100,0x7CBC04AA,lswi,r5,r28,0x20
0xFFF00100,0x7FEF2C2A,lswx,r31,r15,r5
0xFFF00100,0x7E000400,mcrxr,cr4
0xFFF00100,0xFDC0008C,mtfsb0,14
0xFFF00100,0xFFE0004C,mtfsb1,31
0xFFF00100,0xFFE0048F,mffs.,f31
0xFFF00100,0x7C2000A6,mfmsr,r1
1 # Test data for PowerPC disassembler supplied as comma-separated values
331 0xFFF00100,0x7C4325EE,stfdux,f2,r3,r4
332 # floating point operations
333 0xFFF00100,0xFC03282A,fadd,f0,f3,f5
334 0xFFF00100,0xFDAD682B,fadd.,f13,f13,f13
335 0xFFF00100,0xFC0D6028,fsub,f0,f13,f12
336 0xFFF00100,0xFC2107F2,fmul,f1,f1,f31
337 0xFFF00100,0xFF2C07F3,fmul.,f25,f12,f31
338 0xFFF00100,0xFC0D0024,fdiv,f0,f13,f0
339 0xFFF00100,0xFC2B0025,fdiv.,f1,f11,f0
362 0xFFF00100,0x7FBFB800,cmp,cr7,r31,r23 0xFFF00100,0x7CBC04AA,lswi,r5,r28,0x20
363 0xFFF00100,0x7C053040,cmplw,r5,r6 0xFFF00100,0x7FEF2C2A,lswx,r31,r15,r5
364 0xFFF00100,0x7F804840,cmplw,cr7,r0,r9 0xFFF00100,0x7E000400,mcrxr,cr4
0xFFF00100,0x2F800000,cmpwi,cr7,r0,0x0
0xFFF00100,0x298E0022,cmplwi,cr3,r14,0x22
365 0xFFF00100,0xFE17C840,fcmpo,cr4,f23,f25 0xFFF00100,0xFFE0004C,mtfsb1,31
366 0xFFF00100,0xFF0C6800,fcmpu,cr6,f12,f13 0xFFF00100,0xFFE0048F,mffs.,f31
367 # misc instructions 0xFFF00100,0x7C2000A6,mfmsr,r1
0xFFF00100,0x7D290034,cntlzw,r9,r9
0xFFF00100,0x7FFF0035,cntlzw.,r31,r31
0xFFF00100,0x7C00D7AC,icbi,0,r26
0xFFF00100,0x7D604828,lwarx,r11,0,r9
0xFFF00100,0x7CBC04AA,lswi,r5,r28,0x20
0xFFF00100,0x7FEF2C2A,lswx,r31,r15,r5
0xFFF00100,0x7E000400,mcrxr,cr4
0xFFF00100,0xFDC0008C,mtfsb0,14
0xFFF00100,0xFFE0004C,mtfsb1,31
0xFFF00100,0xFFE0048F,mffs.,f31
368 0xFFF00100,0x7C2000A6,mfmsr,r1 0xFFF00100,0x7C000124,mtmsr,r0
369 0xFFF00100,0x7C000124,mtmsr,r0 0xFFF00100,0x7FEF01A4,mtsr,15,r31
370 0xFFF00100,0x7FEF01A4,mtsr,15,r31 0xFFF00100,0x7C6021E4,mtsrin,r3,r4
384 0xFFF00100,0x7D49F02F,dc.l,0x7D49F02F 0xFFF00100,0x7F800009,dc.l,0x7F800009
385 0xFFF00100,0x7F800009,dc.l,0x7F800009 0xFFF00100,0x7C6B0CD0,dc.l,0x7C6B0CD0
386 0xFFF00100,0x7C6B0CD0,dc.l,0x7C6B0CD0 0xFFF00100,0x7C642D90,dc.l,0x7C642D90
0xFFF00100,0x7C642D90,dc.l,0x7C642D90
387 # POWER/PPC601 specific instructions
388 0xFFF00100,0x7C440426,clcs,r2,r4
389 0xFFF00100,0x24000800,dozi,r0,r0,0x800
+2 -3
View File
@@ -1,6 +1,5 @@
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/thirdparty/capstone/include")
include_directories("${PROJECT_SOURCE_DIR}")
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
add_library(debugger OBJECT ${SOURCES})
add_library(debugger OBJECT ${SOURCES})
+27 -287
View File
@@ -19,7 +19,9 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <array>
#include "../cpu/ppc/ppcdisasm.h"
#include "../cpu/ppc/ppcemu.h"
#include "../cpu/ppc/ppcmmu.h"
#include <fstream>
#include <iomanip>
#include <iostream>
@@ -28,13 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdio.h>
#include <string>
#include <thirdparty/loguru/loguru.hpp>
#include "../cpu/ppc/ppcdisasm.h"
#include "../cpu/ppc/ppcemu.h"
#include "../cpu/ppc/ppcmmu.h"
#ifdef ENABLE_68K_DEBUGGER // optionally defined in CMakeLists.txt
#include <capstone/capstone.h>
#endif
using namespace std;
@@ -56,9 +52,8 @@ static uint32_t str2num(string& num_str) {
static void show_help() {
cout << "Debugger commands:" << endl;
cout << " step [N] -- execute single instruction" << endl;
cout << " N is an optional step count" << endl;
cout << " si [N] -- shortcut for step" << endl;
cout << " step -- execute single instruction" << endl;
cout << " si -- shortcut for step" << endl;
cout << " next -- same as step but treats subroutine calls" << endl;
cout << " as single instructions." << endl;
cout << " ni -- shortcut for next" << endl;
@@ -76,154 +71,23 @@ static void show_help() {
cout << " disas N,X -- disassemble N instructions starting at address X" << endl;
cout << " X can be any number or a known register name" << endl;
cout << " disas with no arguments defaults to disas 1,pc" << endl;
cout << " da N,X -- shortcut for disas" << endl;
#ifdef ENABLE_68K_DEBUGGER
cout << " context X -- switch to the debugging context X." << endl;
cout << " X can be either 'ppc' (default) or '68k'" << endl;
cout << " Use 68k for debugging emulated 68k code only." << endl;
#endif
cout << " quit -- quit the debugger" << endl << endl;
cout << "Pressing ENTER will repeat last command." << endl;
}
#ifdef ENABLE_68K_DEBUGGER
static void disasm(uint32_t count, uint32_t address) {
PPCDisasmContext ctx;
static void disasm_68k(uint32_t count, uint32_t address) {
csh cs_handle;
uint8_t code[10];
size_t code_size;
uint64_t dis_addr;
ctx.instr_addr = address;
ctx.simplified = true;
if (cs_open(CS_ARCH_M68K, CS_MODE_M68K_040, &cs_handle) != CS_ERR_OK) {
cout << "Capstone initialization error" << endl;
return;
}
cs_insn* insn = cs_malloc(cs_handle);
for (; count > 0; count--) {
/* prefetch opcode bytes (a 68k instruction can occupy 2...10 bytes) */
for (int i = 0; i < sizeof(code); i++) {
code[i] = mem_read_dbg(address + i, 1);
}
const uint8_t *code_ptr = code;
code_size = sizeof(code);
dis_addr = address;
if (cs_disasm_iter(cs_handle, &code_ptr, &code_size, &dis_addr, insn)) {
cout << uppercase << hex << insn->address << " ";
cout << setfill(' ');
cout << setw(10) << left << insn->mnemonic << insn->op_str << endl;
address = dis_addr;
} else {
cout << "DS.W " << hex << ((code[0] << 8) | code[1]) << endl;
address += 2;
}
}
cs_free(insn, 1);
cs_close(&cs_handle);
}
/* emulator opcode table size --> 512 KB */
#define EMU_68K_TABLE_SIZE 0x80000
/** Execute one emulated 68k instruction. */
void exec_single_68k()
{
string reg;
uint32_t emu_table_virt, cur_68k_pc, cur_instr_tab_entry, ppc_pc;
/* PPC r24 contains 68k PC advanced by two bytes
as part of instruction prefetching */
reg = "R24";
cur_68k_pc = get_reg(reg) - 2;
/* PPC r29 contains base address of the emulator opcode table */
reg = "R29";
emu_table_virt = get_reg(reg) & 0xFFF80000;
/* calculate address of the current opcode table entry as follows:
get_word(68k_PC) * entry_size + table_base */
cur_instr_tab_entry = mem_grab_word(cur_68k_pc) * 8 + emu_table_virt;
/* grab the PPC PC too */
reg = "PC";
ppc_pc = get_reg(reg);
//printf("cur_instr_tab_entry = %X\n", cur_instr_tab_entry);
/* because the first two PPC instructions for each emulated 68k once
are resided in the emulator opcode table, we need to execute them
one by one until the execution goes outside the opcode table. */
while (ppc_pc >= cur_instr_tab_entry && ppc_pc < cur_instr_tab_entry + 8) {
ppc_exec_single();
reg = "PC";
ppc_pc = get_reg(reg);
LOG_F(9, "Tracing within emulator table, PC = %X\n", ppc_pc);
}
/* Getting here means we're outside the emualtor opcode table.
Execute PPC code until we hit the opcode table again. */
LOG_F(9, "Tracing outside the emulator table, PC = %X\n", ppc_pc);
ppc_exec_dbg(emu_table_virt, EMU_68K_TABLE_SIZE - 1);
}
/** Execute emulated 68k code until target_addr is reached. */
void exec_until_68k(uint32_t target_addr)
{
string reg;
uint32_t emu_table_virt, ppc_pc;
reg = "R29";
emu_table_virt = get_reg(reg) & 0xFFF80000;
reg = "R24";
while (target_addr != (get_reg(reg) - 2)) {
reg = "PC";
ppc_pc = get_reg(reg);
if (ppc_pc >= emu_table_virt && ppc_pc < (emu_table_virt + EMU_68K_TABLE_SIZE - 1)) {
LOG_F(9, "Tracing within emulator table, PC = %X\n", ppc_pc);
ppc_exec_single();
} else {
LOG_F(9, "Tracing outside the emulator table, PC = %X\n", ppc_pc);
ppc_exec_dbg(emu_table_virt, EMU_68K_TABLE_SIZE - 1);
}
reg = "R24";
for (int i = 0; i < count; i++) {
ctx.instr_code = mem_read_dbg(ctx.instr_addr, 4);
cout << uppercase << hex << ctx.instr_addr;
cout << " " << disassemble_single(&ctx) << endl;
}
}
void print_68k_regs()
{
int i;
string reg;
for (i = 0; i < 8; i++) {
reg = "R" + to_string(i + 8);
cout << "D" << dec << i << " : " << uppercase << hex << get_reg(reg) << endl;
}
for (i = 0; i < 7; i++) {
reg = "R" + to_string(i + 16);
cout << "A" << dec << i << " : " << uppercase << hex << get_reg(reg) << endl;
}
reg = "R1";
cout << "A7 : " << uppercase << hex << get_reg(reg) << endl;
reg = "R24";
cout << "PC: " << uppercase << hex << get_reg(reg) - 2 << endl;
reg = "R25";
cout << "SR: " << uppercase << hex << ((get_reg(reg) & 0xFF) << 8) << endl;
reg = "R26";
cout << "CCR: " << uppercase << hex << get_reg(reg) << endl;
}
#endif // ENABLE_68K_DEBUGGER
static void dump_mem(string& params) {
int cell_size, chars_per_line;
bool is_char;
@@ -305,8 +169,7 @@ static void dump_mem(string& params) {
cout << (char)val;
chars_per_line += cell_size;
} else {
cout << setw(cell_size * 2) << setfill('0') << right;
cout << uppercase << hex << val << " ";
cout << setw(cell_size * 2) << setfill('0') << uppercase << hex << val << " ";
chars_per_line += cell_size * 2 + 2;
}
}
@@ -318,66 +181,18 @@ static void dump_mem(string& params) {
cout << endl << endl;
}
static void disasm(uint32_t count, uint32_t address) {
PPCDisasmContext ctx;
ctx.instr_addr = address;
ctx.simplified = true;
for (int i = 0; i < count; i++) {
ctx.instr_code = mem_read_dbg(ctx.instr_addr, 4);
cout << uppercase << hex << ctx.instr_addr;
cout << " " << disassemble_single(&ctx) << endl;
}
}
static void print_gprs() {
string reg_name;
int i;
for (i = 0; i < 32; i++) {
reg_name = "R" + to_string(i);
cout << right << std::setw(3) << setfill(' ') << reg_name << " : " <<
setw(8) << setfill('0') << right << uppercase << hex << get_reg(reg_name);
if (i & 1) {
cout << endl;
} else {
cout << "\t\t";
}
}
array<string,6> sprs = {"PC", "LR", "CR", "CTR", "XER", "MSR"};
for (auto &spr : sprs) {
cout << right << std::setw(3) << setfill(' ') << spr << " : " <<
setw(8) << setfill('0') << uppercase << hex << get_reg(spr);
if (i & 1) {
cout << endl;
} else {
cout << "\t\t";
}
i++;
}
}
void enter_debugger() {
string inp, cmd, addr_str, expr_str, reg_expr, last_cmd, reg_value_str, inst_string, inst_num_str;
uint32_t addr, inst_grab;
std::stringstream ss;
int log_level, context;
int log_level;
size_t separator_pos;
context = 1; /* start with the PowerPC context */
cout << "Welcome to the DingusPPC command line debugger." << endl;
cout << "Please enter a command or 'help'." << endl << endl;
while (1) {
cout << "dingusdbg> ";
cout << "ppcdbg> ";
/* reset string stream */
ss.str("");
@@ -405,13 +220,7 @@ void enter_debugger() {
}
#endif
else if (cmd == "regs") {
if (context == 2) {
#ifdef ENABLE_68K_DEBUGGER
print_68k_regs();
#endif
} else {
print_gprs();
}
print_gprs();
} else if (cmd == "set") {
ss >> expr_str;
@@ -439,32 +248,7 @@ void enter_debugger() {
cout << exc.what() << endl;
}
} else if (cmd == "step" || cmd == "si") {
int count;
expr_str = "";
ss >> expr_str;
if (expr_str.length() > 0) {
try {
count = str2num(expr_str);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
count = 1;
}
} else {
count = 1;
}
if (context == 2) {
#ifdef ENABLE_68K_DEBUGGER
for (; --count >= 0;) {
exec_single_68k();
}
#endif
} else {
for (; --count >= 0;) {
ppc_exec_single();
}
}
ppc_exec_single();
} else if (cmd == "next" || cmd == "ni") {
addr_str = "PC";
addr = get_reg(addr_str) + 4;
@@ -473,17 +257,11 @@ void enter_debugger() {
ss >> addr_str;
try {
addr = str2addr(addr_str);
if (context == 2) {
#ifdef ENABLE_68K_DEBUGGER
exec_until_68k(addr);
#endif
} else {
ppc_exec_until(addr);
}
ppc_exec_until(addr);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
}
} else if (cmd == "disas" || cmd == "da") {
} else if (cmd == "disas") {
expr_str = "";
ss >> expr_str;
if (expr_str.length() > 0) {
@@ -492,73 +270,35 @@ void enter_debugger() {
cout << "disas: not enough arguments specified." << endl;
continue;
}
inst_num_str = expr_str.substr(0, expr_str.find_first_of(","));
try {
inst_grab = str2num(inst_num_str);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
continue;
}
addr_str = expr_str.substr(expr_str.find_first_of(",") + 1);
inst_grab = stol(inst_num_str, NULL, 0);
addr_str = expr_str.substr(expr_str.find_first_of(",") + 1);
try {
addr = str2addr(addr_str);
} catch (invalid_argument& exc) {
try {
/* number conversion failed, trying reg name */
if (context == 2 && (addr_str == "pc" || addr_str == "PC")) {
addr_str = "R24";
addr = get_reg(addr_str) - 2;
} else {
addr = get_reg(addr_str);
}
addr = get_reg(addr_str);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
continue;
}
}
try {
if (context == 2) {
#ifdef ENABLE_68K_DEBUGGER
disasm_68k(inst_grab, addr);
#endif
} else {
disasm(inst_grab, addr);
}
disasm(inst_grab, addr);
} catch (invalid_argument& exc) {
cout << exc.what() << endl;
}
} else {
/* disas without arguments defaults to disas 1,pc */
if (context == 2) {
#ifdef ENABLE_68K_DEBUGGER
addr_str = "R24";
addr = get_reg(addr_str);
disasm_68k(1, addr - 2);
#endif
} else {
addr_str = "PC";
addr = get_reg(addr_str);
disasm(1, addr);
}
addr_str = "PC";
addr = get_reg(addr_str);
disasm(1, addr);
}
} else if (cmd == "dump") {
expr_str = "";
ss >> expr_str;
dump_mem(expr_str);
#ifdef ENABLE_68K_DEBUGGER
} else if (cmd == "context") {
expr_str = "";
ss >> expr_str;
if (expr_str == "ppc" || expr_str == "PPC") {
context = 1;
} else if (expr_str == "68k" || expr_str == "68K") {
context = 2;
} else {
cout << "Unknown debugging context: " << expr_str << endl;
}
#endif
} else {
cout << "Unknown command: " << cmd << endl;
continue;
+132 -233
View File
@@ -21,90 +21,23 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "displayid.h"
#include "endianswap.h"
#include "memaccess.h"
#include "memreadwrite.h"
#include "pcidevice.h"
#include <atirage.h>
#include <cstdint>
#include <map>
#include <thirdparty/loguru/loguru.hpp>
/* Mach64 post dividers. */
static const int mach64_post_div[8] = {
1, 2, 4, 8, // standard post dividers
3, 5, 6, 12 // alternate post dividers
};
/* Human readable Mach64 HW register names for easier debugging. */
static const std::map<uint16_t, std::string> mach64_reg_names = {
{0x0000, "CRTC_H_TOTAL_DISP"},
{0x0004, "CRTC_H_SYNC_STRT_WID"},
{0x0008, "CRTC_V_TOTAL_DISP"},
{0x000C, "CRTC_V_SYNC_STRT_WID"},
{0x0010, "CRTC_VLINE_CRNT_VLINE"},
{0x0014, "CRTC_OFF_PITCH"},
{0x0018, "CRTC_INT_CNTL"},
{0x001C, "CRTC_GEN_CNTL"},
{0x0020, "DSP_CONFIG"},
{0x0024, "DSP_ON_OFF"},
{0x002C, "MEM_BUF_CNTL"},
{0x0034, "MEM_ADDR_CFG"},
{0x0040, "OVR_CLR"},
{0x0044, "OVR_WID_LEFT_RIGHT"},
{0x0048, "OVR_WID_TOP_BOTTOM"},
{0x0060, "CUR_CLR0"},
{0x0064, "CUR_CLR1"},
{0x0068, "CUR_OFFSET"},
{0x006C, "CUR_HORZ_VERT_POSN"},
{0x0078, "GP_IO"},
{0x007C, "HW_DEBUG"},
{0x0080, "SCRATCH_REG0"},
{0x0084, "SCRATCH_REG1"},
{0x0088, "SCRATCH_REG2"},
{0x008C, "SCRATCH_REG3"},
{0x0090, "CLOCK_CNTL"},
{0x00A0, "BUS_CNTL"},
{0x00AC, "EXT_MEM_CNTL"},
{0x00B0, "MEM_CNTL"},
{0x00C0, "DAC_REGS"},
{0x00C4, "DAC_CNTL"},
{0x00D0, "GEN_TEST_CNTL"},
{0x00D4, "CUSTOM_MACRO_CNTL"},
{0x00E0, "CONFIG_CHIP_ID"},
{0x00E4, "CONFIG_STAT0"},
{0x01B4, "SRC_CNTL"},
{0x01FC, "SCALE_3D_CNTL"},
{0x0310, "FIFO_STAT"},
{0x0338, "GUI_STAT"},
};
ATIRage::ATIRage(uint16_t dev_id, uint32_t mem_amount) : PCIDevice("ati-rage") {
uint8_t asic_id;
this->vram_size = mem_amount << 20;
/* allocate video RAM */
/*allocate video RAM */
this->vram_ptr = new uint8_t[this->vram_size];
/* ATI Rage driver needs to know ASIC ID (manufacturer's internal chip code)
to operate properly */
switch (dev_id) {
case ATI_RAGE_PRO_DEV_ID:
asic_id = 0x5C; // R3B/D/P-A4 fabricated by UMC
break;
default:
asic_id = 0xDD;
LOG_F(WARNING, "ATI Rage: bogus ASIC ID assigned!");
}
/* set up PCI configuration space header */
WRITE_DWORD_LE_A(&this->pci_cfg[0], (dev_id << 16) | ATI_PCI_VENDOR_ID);
WRITE_DWORD_LE_A(&this->pci_cfg[8], (0x030000 << 8) | asic_id);
WRITE_DWORD_LE_A(&this->pci_cfg[0x3C], 0x00080100);
/* stuff default values into chip registers */
WRITE_DWORD_LE_A(&this->block_io_regs[ATI_CONFIG_CHIP_ID],
(asic_id << 24) | dev_id);
/* configure PCI parameters */
WRITE_DWORD_BE_A(&this->pci_cfg[0], (dev_id << 16) | ATI_PCI_VENDOR_ID);
WRITE_DWORD_BE_A(&this->pci_cfg[8], 0x0300005C);
WRITE_DWORD_BE_A(&this->pci_cfg[0x3C], 0x00080100);
/* initialize display identification */
this->disp_id = new DisplayID();
@@ -119,28 +52,125 @@ ATIRage::~ATIRage()
delete (this->disp_id);
}
const char* ATIRage::get_reg_name(uint32_t reg_offset) {
auto iter = mach64_reg_names.find(reg_offset & ~3);
if (iter != mach64_reg_names.end()) {
return iter->second.c_str();
} else {
return "unknown Mach64 register";
uint32_t ATIRage::size_dep_read(uint8_t* buf, uint32_t size) {
switch (size) {
case 4:
return READ_DWORD_LE_A(buf);
break;
case 2:
return READ_WORD_LE_A(buf);
break;
case 1:
return *buf;
break;
default:
LOG_F(WARNING, "ATI Rage read: invalid size %d", size);
return 0;
}
}
void ATIRage::size_dep_write(uint8_t* buf, uint32_t value, uint32_t size) {
switch (size) {
case 4:
WRITE_DWORD_BE_A(buf, value);
break;
case 2:
WRITE_WORD_BE_A(buf, value & 0xFFFFU);
break;
case 1:
*buf = value & 0xFF;
}
}
const char* ATIRage::get_reg_name(uint32_t reg_offset) {
const char* reg_name;
switch (reg_offset & ~3) {
case ATI_CRTC_H_TOTAL_DISP:
reg_name = "CRTC_H_TOTAL_DISP";
break;
case ATI_CRTC_H_SYNC_STRT_WID:
reg_name = "CRTC_H_SYNC_STRT_WID";
break;
case ATI_CRTC_V_TOTAL_DISP:
reg_name = "CRTC_V_TOTAL_DISP";
break;
case ATI_CRTC_V_SYNC_STRT_WID:
reg_name = "CRTC_V_SYNC_STRT_WID";
break;
case ATI_CRTC_OFF_PITCH:
reg_name = "CRTC_OFF_PITCH";
break;
case ATI_CRTC_INT_CNTL:
reg_name = "CRTC_INT_CNTL";
break;
case ATI_CRTC_GEN_CNTL:
reg_name = "CRTC_GEN_CNTL";
break;
case ATI_DSP_CONFIG:
reg_name = "DSP_CONFIG";
break;
case ATI_DSP_ON_OFF:
reg_name = "DSP_ON_OFF";
break;
case ATI_MEM_BUF_CNTL:
reg_name = "MEM_BUF_CNTL";
break;
case ATI_MEM_ADDR_CFG:
reg_name = "MEM_ADDR_CFG";
break;
case ATI_OVR_CLR:
reg_name = "OVR_CLR";
break;
case ATI_OVR_WID_LEFT_RIGHT:
reg_name = "OVR_WID_LEFT_RIGHT";
break;
case ATI_OVR_WID_TOP_BOTTOM:
reg_name = "OVR_WID_TOP_BOTTOM";
break;
case ATI_GP_IO:
reg_name = "GP_IO";
break;
case ATI_CLOCK_CNTL:
reg_name = "CLOCK_CNTL";
break;
case ATI_BUS_CNTL:
reg_name = "BUS_CNTL";
break;
case ATI_EXT_MEM_CNTL:
reg_name = "EXT_MEM_CNTL";
break;
case ATI_MEM_CNTL:
reg_name = "MEM_CNTL";
break;
case ATI_DAC_REGS:
reg_name = "DAC_REGS";
break;
case ATI_DAC_CNTL:
reg_name = "DAC_CNTL";
break;
case ATI_GEN_TEST_CNTL:
reg_name = "GEN_TEST_CNTL";
break;
case ATI_CFG_CHIP_ID:
reg_name = "CONFIG_CHIP_ID";
break;
case ATI_CFG_STAT0:
reg_name = "CONFIG_STAT0";
break;
default:
reg_name = "unknown";
}
return reg_name;
}
uint32_t ATIRage::read_reg(uint32_t offset, uint32_t size) {
uint32_t res;
switch (offset & ~3) {
case ATI_GP_IO:
break;
case ATI_CLOCK_CNTL:
/* reading from internal PLL registers */
if (offset == ATI_CLOCK_CNTL+2 && size == 1 &&
!(this->block_io_regs[ATI_CLOCK_CNTL+1] & 0x2)) {
return this->plls[this->block_io_regs[ATI_CLOCK_CNTL+1] >> 2];
}
break;
case ATI_DAC_REGS:
if (offset == ATI_DAC_DATA) {
this->block_io_regs[ATI_DAC_DATA] =
@@ -160,15 +190,10 @@ uint32_t ATIRage::read_reg(uint32_t offset, uint32_t size) {
get_reg_name(offset),
offset,
size,
read_mem(&this->block_io_regs[offset], size));
size_dep_read(&this->block_io_regs[offset], size));
}
if (offset > sizeof(this->block_io_regs)) {
LOG_F(WARNING, "ATI Rage: register offset 0x%04X out of bounds!", offset);
return 0;
}
res = read_mem(&this->block_io_regs[offset], size);
res = size_dep_read(&this->block_io_regs[offset], size);
return res;
}
@@ -177,22 +202,10 @@ void ATIRage::write_reg(uint32_t offset, uint32_t value, uint32_t size) {
uint32_t gpio_val;
uint16_t gpio_dir;
if (offset > sizeof(this->block_io_regs)) {
LOG_F(WARNING, "ATI Rage: register offset 0x%04X out of bounds!", offset);
return;
}
/* size-dependent endian conversion */
write_mem(&this->block_io_regs[offset], value, size);
size_dep_write(&this->block_io_regs[offset], value, size);
switch (offset & ~3) {
case ATI_CRTC_GEN_CNTL:
if (this->block_io_regs[ATI_CRTC_GEN_CNTL+3] & 2) {
this->crtc_enable();
} else {
this->crtc_on = false;
}
break;
case ATI_GP_IO:
if (offset < (ATI_GP_IO + 2)) {
gpio_val = READ_DWORD_LE_A(&this->block_io_regs[ATI_GP_IO]);
@@ -202,19 +215,6 @@ void ATIRage::write_reg(uint32_t offset, uint32_t value, uint32_t size) {
this->disp_id->read_monitor_sense(gpio_val, gpio_dir));
}
break;
case ATI_CLOCK_CNTL:
/* writing to internal PLL registers */
if (offset == ATI_CLOCK_CNTL+2 && size == 1 &&
(this->block_io_regs[ATI_CLOCK_CNTL+1] & 0x2)) {
int pll_addr = this->block_io_regs[ATI_CLOCK_CNTL+1] >> 2;
uint8_t pll_data = this->block_io_regs[ATI_CLOCK_CNTL+2];
this->plls[pll_addr] = pll_data;
LOG_F(INFO, "ATI Rage: PLL #%d set to 0x%02X", pll_addr, pll_data);
} else if (offset == ATI_CLOCK_CNTL && size == 1) {
LOG_F(INFO, "ATI Rage: CLOCK_SEL = 0x%02X",
this->block_io_regs[ATI_CLOCK_CNTL] & 3);
}
break;
case ATI_DAC_REGS:
switch (offset) {
/* writing to read/write index registers resets color component index */
@@ -255,7 +255,7 @@ uint32_t ATIRage::pci_cfg_read(uint32_t reg_offs, uint32_t size) {
LOG_F(INFO, "Reading ATI Rage config space, offset = 0x%X, size=%d", reg_offs, size);
res = read_mem(&this->pci_cfg[reg_offs], size);
res = size_dep_read(&this->pci_cfg[reg_offs], size);
LOG_F(INFO, "Return value: 0x%X", res);
return res;
@@ -277,42 +277,34 @@ void ATIRage::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size) {
else {
this->aperture_base = BYTESWAP_32(value);
LOG_F(INFO, "ATI Rage aperture address set to 0x%08X", this->aperture_base);
WRITE_DWORD_BE_A(&this->pci_cfg[CFG_REG_BAR0], value);
WRITE_DWORD_LE_A(&this->pci_cfg[CFG_REG_BAR0], value);
this->host_instance->pci_register_mmio_region(this->aperture_base,
APERTURE_SIZE, this);
}
break;
case 0x14: /* BAR 1: I/O space base, 256 bytes wide */
if (value == 0xFFFFFFFFUL) {
WRITE_DWORD_BE_A(&this->pci_cfg[CFG_REG_BAR1], 0xFFFFFF01UL);
WRITE_DWORD_LE_A(&this->pci_cfg[CFG_REG_BAR1], 0xFFFFFF01UL);
}
else {
WRITE_DWORD_BE_A(&this->pci_cfg[CFG_REG_BAR1], value);
WRITE_DWORD_LE_A(&this->pci_cfg[CFG_REG_BAR1], value);
}
break;
case 0x18: /* BAR 2 */
if (value == 0xFFFFFFFFUL) {
WRITE_DWORD_BE_A(&this->pci_cfg[CFG_REG_BAR2], 0xFFFFF000UL);
WRITE_DWORD_LE_A(&this->pci_cfg[CFG_REG_BAR2], 0xFFFFF000UL);
}
else {
WRITE_DWORD_BE_A(&this->pci_cfg[CFG_REG_BAR2], value);
WRITE_DWORD_LE_A(&this->pci_cfg[CFG_REG_BAR2], value);
}
break;
case CFG_REG_BAR3: /* unimplemented */
case CFG_REG_BAR4: /* unimplemented */
case CFG_REG_BAR5: /* unimplemented */
WRITE_DWORD_BE_A(&this->pci_cfg[reg_offs], 0);
break;
case CFG_EXP_BASE: /* no expansion ROM */
if (value == 0x00F8FFFFUL) {
// return 0 (not implemented) when attempting to size the expansion ROM
WRITE_DWORD_BE_A(&this->pci_cfg[reg_offs], 0);
} else {
WRITE_DWORD_BE_A(&this->pci_cfg[reg_offs], value);
}
WRITE_DWORD_LE_A(&this->pci_cfg[reg_offs], 0);
break;
default:
write_mem(&this->pci_cfg[reg_offs], value, size);
size_dep_write(&this->pci_cfg[reg_offs], value, size);
}
}
@@ -323,7 +315,7 @@ bool ATIRage::io_access_allowed(uint32_t offset, uint32_t* p_io_base) {
return false;
}
uint32_t io_base = READ_DWORD_LE_A(&this->pci_cfg[CFG_REG_BAR1]) & ~3;
uint32_t io_base = READ_DWORD_BE_A(&this->pci_cfg[CFG_REG_BAR1]) & ~3;
if (offset < io_base || offset > (io_base + 0x100)) {
LOG_F(WARNING, "Rage: I/O out of range, base=0x%X, offset=0x%X", io_base, offset);
@@ -362,7 +354,7 @@ bool ATIRage::pci_io_write(uint32_t offset, uint32_t value, uint32_t size) {
uint32_t ATIRage::read(uint32_t reg_start, uint32_t offset, int size)
{
LOG_F(8, "Reading ATI Rage PCI memory: region=%X, offset=%X, size %d", reg_start, offset, size);
//LOG_F(INFO, "Reading ATI Rage PCI memory: region=%X, offset=%X, size %d", reg_start, offset, size);
if (reg_start < this->aperture_base || offset > APERTURE_SIZE) {
LOG_F(WARNING, "ATI Rage: attempt to read outside the aperture!");
@@ -371,7 +363,7 @@ uint32_t ATIRage::read(uint32_t reg_start, uint32_t offset, int size)
if (offset < this->vram_size) {
/* read from little-endian VRAM region */
return read_mem(this->vram_ptr + offset, size);
return size_dep_read(this->vram_ptr + offset, size);
}
else if (offset >= MEMMAP_OFFSET) {
/* read from memory-mapped registers */
@@ -386,7 +378,7 @@ uint32_t ATIRage::read(uint32_t reg_start, uint32_t offset, int size)
void ATIRage::write(uint32_t reg_start, uint32_t offset, uint32_t value, int size)
{
LOG_F(8, "Writing reg=%X, offset=%X, value=%X, size %d", reg_start, offset, value, size);
//LOG_F(INFO, "Writing reg=%X, offset=%X, value=%X, size %d", reg_start, offset, value, size);
if (reg_start < this->aperture_base || offset > APERTURE_SIZE) {
LOG_F(WARNING, "ATI Rage: attempt to write outside the aperture!");
@@ -395,7 +387,7 @@ void ATIRage::write(uint32_t reg_start, uint32_t offset, uint32_t value, int siz
if (offset < this->vram_size) {
/* write to little-endian VRAM region */
write_mem(this->vram_ptr + offset, value, size);
size_dep_write(this->vram_ptr + offset, value, size);
} else if (offset >= MEMMAP_OFFSET) {
/* write to memory-mapped registers */
this->write_reg(offset - MEMMAP_OFFSET, value, size);
@@ -404,96 +396,3 @@ void ATIRage::write(uint32_t reg_start, uint32_t offset, uint32_t value, int siz
LOG_F(WARNING, "ATI Rage: write attempt to unmapped aperture region at 0x%08X", offset);
}
}
float ATIRage::calc_pll_freq(int scale, int fb_div) {
return (ATI_XTAL * scale * fb_div) / this->plls[PLL_REF_DIV];
}
void ATIRage::verbose_pixel_format(int crtc_index) {
if (crtc_index) {
LOG_F(ERROR, "CRTC2 not supported yet");
return;
}
const char* what = "Pixel format:";
switch (this->block_io_regs[ATI_CRTC_GEN_CNTL+1] & 7) {
case 1:
LOG_F(INFO, "%s 4 bpp with DAC palette", what);
break;
case 2:
// check the undocumented DAC_DIRECT bit
if (this->block_io_regs[ATI_DAC_CNTL+1] & 4) {
LOG_F(INFO, "%s 8 bpp direct color (RGB322)", what);
} else {
LOG_F(INFO, "%s 8 bpp with DAC palette", what);
}
break;
case 3:
LOG_F(INFO, "%s 15 bpp direct color (RGB555)", what);
break;
case 4:
LOG_F(INFO, "%s 16 bpp direct color (RGB565)", what);
break;
case 5:
LOG_F(INFO, "%s 24 bpp direct color (RGB888)", what);
break;
case 6:
LOG_F(INFO, "%s 32 bpp direct color (ARGB8888)", what);
break;
default:
LOG_F(ERROR, "ATI Rage: CRTC pixel format %d not supported",
this->block_io_regs[ATI_CRTC_GEN_CNTL+2] & 7);
}
}
void ATIRage::crtc_enable() {
/* active (visible) width is specified in characters (8 px) - 1 */
this->active_width = (this->block_io_regs[ATI_CRTC_H_TOTAL_DISP+2] + 1) * 8;
/* active (visible) height is specified in lines - 1 */
this->active_height =
(READ_WORD_LE_A(&this->block_io_regs[ATI_CRTC_V_TOTAL_DISP+2]) & 0x7FFUL) + 1;
if ((this->plls[PLL_VCLK_CNTL] & 3) == 3) {
/* look up which VPLL ouput is requested */
int clock_sel = this->block_io_regs[ATI_CLOCK_CNTL] & 3;
/* calculate VPLL output frequency */
float vpll_freq = calc_pll_freq(2, this->plls[VCLK0_FB_DIV + clock_sel]);
/* calculate post divider's index */
/* NOTE: post divider's index has been extended by an additional
bit in Rage Pro. This bit is resided in PLL_EXT_CNTL register.
*/
int post_div_idx = ((this->plls[PLL_EXT_CNTL] >> (clock_sel + 2)) & 4) |
((this->plls[VCLK_POST_DIV] >> (clock_sel * 2)) & 3);
/* pixel clock = source_freq / post_div */
this->pixel_clock = vpll_freq / mach64_post_div[post_div_idx];
/* calculate display refresh rate */
int hori_total =
((READ_WORD_LE_A(&this->block_io_regs[ATI_CRTC_H_TOTAL_DISP])
& 0x1FFUL) + 1) * 8;
int vert_total =
(READ_WORD_LE_A(&this->block_io_regs[ATI_CRTC_V_TOTAL_DISP]) & 0x7FFUL) + 1;
this->refresh_rate = pixel_clock / hori_total / vert_total;
LOG_F(INFO, "ATI Rage: primary CRT controller enabled:");
LOG_F(INFO, "Video mode: %s",
(this->block_io_regs[ATI_CRTC_GEN_CNTL+3] & 1) ? "extended" : "VGA");
LOG_F(INFO, "Video width: %d px", this->active_width);
LOG_F(INFO, "Video height: %d px", this->active_height);
verbose_pixel_format(0);
LOG_F(INFO, "VPLL frequency: %f MHz", vpll_freq * 1e-6);
LOG_F(INFO, "Pixel (dot) clock: %f MHz", this->pixel_clock * 1e-6);
LOG_F(INFO, "Refresh rate: %f Hz", this->refresh_rate);
} else {
LOG_F(WARNING, "ATI Rage: VLCK source != VPLL!");
}
this->crtc_on = true;
}
+77 -161
View File
@@ -26,6 +26,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "pcidevice.h"
#include <cinttypes>
using namespace std;
/* PCI related definitions. */
enum {
ATI_PCI_VENDOR_ID = 0x1002,
@@ -35,159 +37,78 @@ enum {
/** Mach registers offsets. */
enum {
ATI_CRTC_H_TOTAL_DISP = 0x0000,
ATI_CRTC_H_SYNC_STRT_WID = 0x0004,
ATI_CRTC_V_TOTAL_DISP = 0x0008,
ATI_CRTC_V_SYNC_STRT_WID = 0x000C,
ATI_CRTC_VLINE_CRNT_VLINE = 0x0010,
ATI_CRTC_OFF_PITCH = 0x0014,
ATI_CRTC_INT_CNTL = 0x0018,
ATI_CRTC_GEN_CNTL = 0x001C,
ATI_DSP_CONFIG = 0x0020,
ATI_DSP_ON_OFF = 0x0024,
ATI_TIMER_CFG = 0x0028,
ATI_MEM_BUF_CNTL = 0x002C,
ATI_MEM_ADDR_CFG = 0x0034,
ATI_CRT_TRAP = 0x0038,
ATI_I2C_CNTL_0 = 0x003C,
ATI_OVR_CLR = 0x0040,
ATI_OVR_WID_LEFT_RIGHT = 0x0044,
ATI_OVR_WID_TOP_BOTTOM = 0x0048,
ATI_VGA_DSP_CFG = 0x004C,
ATI_VGA_DSP_TGL = 0x0050,
ATI_DSP2_CONFIG = 0x0054,
ATI_DSP2_TOGGLE = 0x0058,
ATI_CRTC2_OFF_PITCH = 0x005C,
ATI_CUR_CLR0 = 0x0060,
ATI_CUR_CLR1 = 0x0064,
ATI_CUR_OFFSET = 0x0068,
ATI_CUR_HORZ_VERT_POSN = 0x006C,
ATI_CUR_HORZ_VERT_OFF = 0x0070,
ATI_GP_IO = 0x0078,
ATI_HW_DEBUG = 0x007C,
ATI_SCRATCH_REG0 = 0x0080,
ATI_SCRATCH_REG1 = 0x0084,
ATI_SCRATCH_REG2 = 0x0088,
ATI_SCRATCH_REG3 = 0x008C,
ATI_CLOCK_CNTL = 0x0090,
ATI_CONFIG_STAT1 = 0x0094,
ATI_CONFIG_STAT2 = 0x0098,
ATI_BUS_CNTL = 0x00A0,
ATI_EXT_MEM_CNTL = 0x00AC,
ATI_MEM_CNTL = 0x00B0,
ATI_MEM_VGA_WP_SEL = 0x00B4,
ATI_MEM_VGA_RP_SEL = 0x00B8,
ATI_I2C_CNTL_1 = 0x00BC,
ATI_DAC_REGS = 0x00C0,
ATI_DAC_W_INDEX = 0x00C0,
ATI_DAC_DATA = 0x00C1,
ATI_DAC_MASK = 0x00C2,
ATI_DAC_R_INDEX = 0x00C3,
ATI_DAC_CNTL = 0x00C4,
ATI_GEN_TEST_CNTL = 0x00D0,
ATI_CUSTOM_MACRO_CNTL = 0x00D4,
ATI_CONFIG_CNTL = 0x00DC,
ATI_CONFIG_CHIP_ID = 0x00E0,
ATI_CONFIG_STAT0 = 0x00E4,
ATI_CRC_SIG = 0x00E8,
ATI_DST_Y_X_ALIAS1 = 0x00F4,
ATI_DST_OFF_PITCH = 0x0100,
ATI_DST_X = 0x0104,
ATI_DST_Y = 0x0108,
ATI_DST_Y_X = 0x010C,
ATI_DST_WIDTH = 0x0110,
ATI_DST_HEIGHT = 0x0114,
ATI_DST_HEIGHT_WIDTH = 0x0118,
ATI_DST_X_WIDTH = 0x011C,
ATI_DST_BRES_LNTH = 0x0120,
ATI_DST_BRES_ERR = 0x0124,
ATI_DST_BRES_INC = 0x0128,
ATI_DST_BRES_DEC = 0x012C,
ATI_DST_CNTL = 0x0130,
ATI_DST_Y_X_ALIAS2 = 0x0134,
ATI_TRAIL_BRES_ERR = 0x0138,
ATI_TRAIL_BRES_INC = 0x013C,
ATI_TRAIL_BRES_DEC = 0x0140,
ATI_DST_BRES_LNTH_ALIAS1 = 0x0144,
ATI_Z_OFF_PITCH = 0x0148,
ATI_Z_CNTL = 0x014C,
ATI_ALPHA_TST_CNTL = 0x0150,
ATI_SRC_OFF_PITCH = 0x0180,
ATI_SRC_X = 0x0184,
ATI_SRC_Y = 0x0188,
ATI_SRC_Y_X = 0x018C,
ATI_SRC_WIDTH1 = 0x0190,
ATI_SRC_HEIGHT1 = 0x0194,
ATI_SRC_HEIGHT1_WIDTH1 = 0x0198,
ATI_SRC_X_START = 0x019C,
ATI_SRC_Y_START = 0x01A0,
ATI_SRC_Y_X_START = 0x01A4,
ATI_SRC_WIDTH2 = 0x01A8,
ATI_SRC_HEIGHT2 = 0x01AC,
ATI_SRC_HEIGHT2_WIDTH2 = 0x01B0,
ATI_SRC_CNTL = 0x01B4,
ATI_SCALE_OFF = 0x01C0,
ATI_SCALE_WIDTH = 0x01DC,
ATI_SCALE_HEIGHT = 0x01E0,
ATI_SCALE_PITCH = 0x01EC,
ATI_SCALE_X_INC = 0x01F0,
ATI_SCALE_Y_INC = 0x01F4,
ATI_SCALE_VACC = 0x01F8,
ATI_SCALE_3D_CNTL = 0x01FC,
ATI_HOST_CNTL = 0x0240,
ATI_PAT_REG0 = 0x0280,
ATI_PAT_REG1 = 0x0284,
ATI_PAT_CNTL = 0x0288,
ATI_SC_LEFT = 0x02A0,
ATI_SC_RIGHT = 0x02A4,
ATI_SC_LEFT_RIGHT = 0x02A8,
ATI_SC_TOP = 0x02AC,
ATI_SC_BOTTOM = 0x02B0,
ATI_SC_TOP_BOTTOM = 0x02B4,
ATI_DP_BKGD_CLR = 0x02C0,
ATI_DP_FOG_CLR = 0x02C0,
ATI_DP_WRITE_MSK = 0x02C8,
ATI_DP_PIX_WIDTH = 0x02D0,
ATI_DP_MIX = 0x02D4,
ATI_DP_SRC = 0x02D8,
ATI_FRGD_CLR_MIX = 0x02DC,
ATI_FRGD_BKGD_CLR = 0x02E0,
ATI_DST_X_Y = 0x02E8,
ATI_DST_WIDTH_HEIGHT = 0x02EC,
ATI_USR_DST_PITCH = 0x02F0,
ATI_DP_SET_GUI_ENGINE2 = 0x02F4,
ATI_DP_SET_GUI_ENGINE = 0x02F8,
ATI_CLR_CMP_CLR = 0x0300,
ATI_CLR_CMP_MSK = 0x0304,
ATI_CLR_CMP_CNTL = 0x0308,
ATI_FIFO_STAT = 0x0310,
ATI_CONTEXT_MASK = 0x0320,
ATI_GUI_TRAJ_CNTL = 0x0330,
ATI_GUI_STAT = 0x0338,
ATI_MPP_CONFIG = 0x04C0,
ATI_MPP_STROBE_SEQ = 0x04C4,
ATI_MPP_ADDR = 0x04C8,
ATI_MPP_DATA = 0x04CC,
ATI_TVO_CNTL = 0x0500,
};
/* Mach64 PLL register indices. */
enum {
PLL_REF_DIV = 2, // reference divider, same for all Mach64 clocks
PLL_VCLK_CNTL = 5,
VCLK_POST_DIV = 6,
VCLK0_FB_DIV = 7, // feedback divider for VCLK0
VCLK1_FB_DIV = 8, // feedback divider for VCLK1
VCLK2_FB_DIV = 9, // feedback divider for VCLK2
VCLK3_FB_DIV = 10, // feedback divider for VCLK3
PLL_EXT_CNTL = 11,
ATI_CRTC_H_TOTAL_DISP = 0x0000,
ATI_CRTC_H_SYNC_STRT_WID = 0x0004,
ATI_CRTC_V_TOTAL_DISP = 0x0008,
ATI_CRTC_V_SYNC_STRT_WID = 0x000C,
ATI_CRTC_OFF_PITCH = 0x0014,
ATI_CRTC_INT_CNTL = 0x0018,
ATI_CRTC_GEN_CNTL = 0x001C,
ATI_DSP_CONFIG = 0x0020,
ATI_DSP_ON_OFF = 0x0024,
ATI_TIMER_CFG = 0x0028,
ATI_MEM_BUF_CNTL = 0x002C,
ATI_MEM_ADDR_CFG = 0x0034,
ATI_CRT_TRAP = 0x0038,
ATI_I2C_CNTL_0 = 0x003C,
ATI_OVR_CLR = 0x0040,
ATI_OVR_WID_LEFT_RIGHT = 0x0044,
ATI_OVR_WID_TOP_BOTTOM = 0x0048,
ATI_VGA_DSP_CFG = 0x004C,
ATI_VGA_DSP_TGL = 0x0050,
ATI_DSP2_CONFIG = 0x0054,
ATI_DSP2_TOGGLE = 0x0058,
ATI_CRTC2_OFF_PITCH = 0x005C,
ATI_CUR_CLR0 = 0x0060,
ATI_CUR_CLR1 = 0x0064,
ATI_CUR_OFFSET = 0x0068,
ATI_CUR_HORZ_VERT_POSN = 0x006C,
ATI_CUR_HORZ_VERT_OFF = 0x0070,
ATI_GP_IO = 0x0078,
ATI_HW_DEBUG = 0x007C,
ATI_SCRATCH_REG0 = 0x0080,
ATI_SCRATCH_REG1 = 0x0084,
ATI_SCRATCH_REG2 = 0x0088,
ATI_SCRATCH_REG3 = 0x008C,
ATI_CLOCK_CNTL = 0x0090,
ATI_CLONFIG_STAT1 = 0x0094,
ATI_CLONFIG_STAT2 = 0x0098,
ATI_BUS_CNTL = 0x00A0,
ATI_EXT_MEM_CNTL = 0x00AC,
ATI_MEM_CNTL = 0x00B0,
ATI_VGA_WP_SEL = 0x00B4,
ATI_VGA_RP_SEL = 0x00B8,
ATI_I2C_CNTL_1 = 0x00BC,
ATI_DAC_REGS = 0x00C0,
ATI_DAC_W_INDEX = 0x00C0,
ATI_DAC_DATA = 0x00C1,
ATI_DAC_MASK = 0x00C2,
ATI_DAC_R_INDEX = 0x00C3,
ATI_DAC_CNTL = 0x00C4,
ATI_GEN_TEST_CNTL = 0x00D0,
ATI_CUSTOM_MACRO_CNTL = 0x00D4,
ATI_CONFIG_CNTL = 0x00DC,
ATI_CFG_CHIP_ID = 0x00E0,
ATI_CFG_STAT0 = 0x00E4,
ATI_CRC_SIG = 0x00E8,
ATI_DST_OFF_PITCH = 0x0100,
ATI_SRC_OFF_PITCH = 0x0180,
ATI_HOST_CNTL = 0x0240,
ATI_DP_WRITE_MSK = 0x02C8,
ATI_DP_PIX_WIDTH = 0x02D0,
ATI_DST_X_Y = 0x02E8,
ATI_DST_WIDTH_HEIGHT = 0x02EC,
ATI_CONTEXT_MASK = 0x0320,
ATI_MPP_CONFIG = 0x04C0,
ATI_MPP_STROBE_SEQ = 0x04C4,
ATI_MPP_ADDR = 0x04C8,
ATI_MPP_DATA = 0x04CC,
ATI_TVO_CNTL = 0x0500,
};
constexpr auto APERTURE_SIZE = 0x01000000UL; /* Mach64 aperture size */
constexpr auto MEMMAP_OFFSET = 0x007FFC00UL; /* offset to memory mapped registers */
constexpr auto ATI_XTAL = 14318180.0f; // external crystal oscillator frequency
class ATIRage : public PCIDevice {
public:
ATIRage(uint16_t dev_id, uint32_t mem_amount);
@@ -214,28 +135,23 @@ public:
bool pci_io_write(uint32_t offset, uint32_t value, uint32_t size);
protected:
uint32_t size_dep_read(uint8_t* buf, uint32_t size);
void size_dep_write(uint8_t* buf, uint32_t val, uint32_t size);
const char* get_reg_name(uint32_t reg_offset);
bool io_access_allowed(uint32_t offset, uint32_t* p_io_base);
uint32_t read_reg(uint32_t offset, uint32_t size);
void write_reg(uint32_t offset, uint32_t value, uint32_t size);
float calc_pll_freq(int scale, int fb_div);
void verbose_pixel_format(int crtc_index);
void crtc_enable();
private:
uint8_t block_io_regs[512] = {0};
// uint32_t atirage_membuf_regs[9]; /* ATI Rage Memory Buffer Registers */
// uint32_t atirage_scratch_regs[4]; /* ATI Rage Scratch Registers */
// uint32_t atirage_cmdfifo_regs[3]; /* ATI Rage Command FIFO Registers */
// uint32_t atirage_datapath_regs[12]; /* ATI Rage Data Path Registers*/
uint8_t block_io_regs[256] = {0};
uint8_t pci_cfg[256] = {0}; /* PCI configuration space */
uint8_t plls[64] = {0}; // internal PLL registers
/* CRT controller parameters */
bool crtc_on = false;
int active_width; // width of the visible display area
int active_height; // height of the visible display area
float pixel_clock;
float refresh_rate;
/* Video RAM variables */
uint32_t vram_size;
uint8_t* vram_ptr;
+9 -18
View File
@@ -27,7 +27,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cinttypes>
#include <iostream>
#include <thirdparty/loguru/loguru.hpp>
#include <cpu/ppc/ppcemu.h>
/** Heathrow Mac I/O device emulation.
@@ -171,7 +170,7 @@ void HeathrowIC::write(uint32_t reg_start, uint32_t offset, uint32_t value, int
uint32_t HeathrowIC::mio_ctrl_read(uint32_t offset, int size) {
uint32_t res = 0;
switch (offset & 0xFC) {
switch (offset & 0xFF) {
case 0x14:
LOG_F(9, "read from MIO:Int_Mask2 register \n");
res = this->int_mask2;
@@ -197,15 +196,14 @@ uint32_t HeathrowIC::mio_ctrl_read(uint32_t offset, int size) {
res = this->int_levels1;
break;
case 0x34: /* heathrowIDs / HEATHROW_MBCR (Linux): media bay config reg? */
LOG_F(9, "read from MIO:ID register at Address %x \n", ppc_state.pc);
res = this->macio_id;
res = 0xF0700000UL;
break;
case 0x38:
LOG_F(9, "read from MIO:Feat_Ctrl register \n");
res = this->feat_ctrl;
break;
default:
LOG_F(WARNING, "read from unknown MIO register at %x \n", offset);
LOG_F(WARNING, "unknown MIO register at %x \n", offset);
break;
}
@@ -213,17 +211,17 @@ uint32_t HeathrowIC::mio_ctrl_read(uint32_t offset, int size) {
}
void HeathrowIC::mio_ctrl_write(uint32_t offset, uint32_t value, int size) {
switch (offset & 0xFC) {
switch (offset & 0xFF) {
case 0x14:
LOG_F(9, "write %x to MIO:Int_Mask2 register \n", value);
LOG_F(9, "read from MIO:Int_Mask2 register \n");
this->int_mask2 = value;
break;
case 0x18:
LOG_F(9, "write %x to MIO:Int_Clear2 register \n", value);
LOG_F(9, "read from MIO:Int_Clear2 register \n");
this->int_clear2 = value;
break;
case 0x1C:
LOG_F(9, "write %x to MIO:Int_Levels2 register \n", value);
LOG_F(9, "read from MIO:Int_Levels2 register \n");
this->int_levels2 = value;
break;
case 0x24:
@@ -235,22 +233,15 @@ void HeathrowIC::mio_ctrl_write(uint32_t offset, uint32_t value, int size) {
this->int_clear1 = value;
break;
case 0x2C:
LOG_F(9, "write %x to MIO:Int_Levels1 register \n", value);
LOG_F(9, "read from MIO:Int_Levels1 register \n");
this->int_levels1 = value;
break;
case 0x34:
LOG_F(WARNING, "Attempted to write %x to MIO:ID at %x; Address : %x \n", value, offset, ppc_state.pc);
break;
case 0x38:
LOG_F(9, "write %x to MIO:Feat_Ctrl register \n", value);
this->feat_ctrl = value;
break;
case 0x3C:
LOG_F(9, "write %x to MIO:Aux_Ctrl register \n", value);
this->aux_ctrl = value;
break;
default:
LOG_F(WARNING, "write %x to unknown MIO register at %x \n", value, offset);
LOG_F(WARNING, "unknown MIO register at %x \n", offset);
break;
}
}
-2
View File
@@ -141,9 +141,7 @@ private:
uint32_t int_mask1 = 0;
uint32_t int_clear1 = 0;
uint32_t int_levels1 = 0;
uint32_t macio_id = 0xF0700008UL;
uint32_t feat_ctrl = 0; // features control register
uint32_t aux_ctrl = 0; // aux features control register
/* device cells */
ViaCuda* viacuda; /* VIA cell with Cuda MCU attached to it */
+35 -6
View File
@@ -31,7 +31,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "hwcomponent.h"
#include "memctrlbase.h"
#include "memaccess.h"
#include "memreadwrite.h"
#include "mmiodevice.h"
#include "mpc106.h"
@@ -178,7 +178,21 @@ uint32_t MPC106::pci_cfg_read(uint32_t reg_offs, uint32_t size) {
LOG_F(9, "read from Grackle register %08X\n", reg_offs);
#endif
return read_mem(&this->my_pci_cfg_hdr[reg_offs], size);
switch (size) {
case 1:
return this->my_pci_cfg_hdr[reg_offs];
break;
case 2:
return READ_WORD_BE_A(&this->my_pci_cfg_hdr[reg_offs]);
break;
case 4:
return READ_DWORD_BE_A(&this->my_pci_cfg_hdr[reg_offs]);
break;
default:
LOG_F(ERROR, "MPC106 read error: invalid size parameter %d \n", size);
}
return 0;
}
void MPC106::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size) {
@@ -187,8 +201,23 @@ void MPC106::pci_cfg_write(uint32_t reg_offs, uint32_t value, uint32_t size) {
#endif
// FIXME: implement write-protection for read-only registers
write_mem(&this->my_pci_cfg_hdr[reg_offs], value, size);
switch (size) {
case 1:
this->my_pci_cfg_hdr[reg_offs] = value & 0xFF;
break;
case 2:
this->my_pci_cfg_hdr[reg_offs] = (value >> 8) & 0xFF;
this->my_pci_cfg_hdr[reg_offs + 1] = value & 0xFF;
break;
case 4:
this->my_pci_cfg_hdr[reg_offs] = (value >> 24) & 0xFF;
this->my_pci_cfg_hdr[reg_offs + 1] = (value >> 16) & 0xFF;
this->my_pci_cfg_hdr[reg_offs + 2] = (value >> 8) & 0xFF;
this->my_pci_cfg_hdr[reg_offs + 3] = value & 0xFF;
break;
default:
LOG_F(ERROR, "MPC106 read error: invalid size parameter %d \n", size);
}
if (this->my_pci_cfg_hdr[0xF2] & 8) {
#ifdef MPC106_DEBUG
@@ -242,8 +271,8 @@ void MPC106::setup_ram() {
bank_end = (((ext_mem_end >> bank * 8) & 3) << 30) |
(((mem_end >> bank * 8) & 0xFF) << 20) | 0xFFFFFUL;
if (bank && bank_start != ram_size)
LOG_F(WARNING, "MPC106: RAM not contiguous! \n");
ram_size += bank_end - bank_start + 1;
LOG_F(ERROR, "MPC106 error: RAM not contiguous! \n");
ram_size += bank_end + 1;
}
}
+3 -55
View File
@@ -26,6 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "viacuda.h"
#include "adb.h"
//#include "cpu/ppc/ppcemu.h"
#include <cinttypes>
#include <thirdparty/loguru/loguru.hpp>
@@ -316,50 +317,14 @@ void ViaCuda::pseudo_command(int cmd, int data_count) {
}
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_GET_6805_ADDRESS:
response_header(CUDA_PKT_PSEUDO, 0);
this->out_buf[2] = (uint8_t)((this->m6805_address >> 8) & 0xFF);
this->out_buf[3] = (uint8_t)((this->m6805_address) & 0xFF);
break;
case CUDA_GET_REAL_TIME:
response_header(CUDA_PKT_PSEUDO, 0);
this->out_buf[2] = (uint8_t)((this->real_time >> 24) & 0xFF);
this->out_buf[3] = (uint8_t)((this->real_time >> 16) & 0xFF);
this->out_buf[4] = (uint8_t)((this->real_time >> 8) & 0xFF);
this->out_buf[5] = (uint8_t)((this->real_time) & 0xFF);
break;
case CUDA_READ_PRAM:
response_header(CUDA_PKT_PSEUDO, 0);
this->pram_obj->read_byte(this->in_buf[2]);
break;
case CUDA_SET_6805_ADDRESS:
response_header(CUDA_PKT_PSEUDO, 0);
this->m6805_address = ((uint16_t)in_buf[2]) >> 8;
this->m6805_address += ((uint16_t)in_buf[3]);
LOG_F(INFO, "6805 address set to: %x", this->m6805_address);
break;
case CUDA_SET_REAL_TIME:
response_header(CUDA_PKT_PSEUDO, 0);
this->real_time = ((uint32_t)in_buf[2]) >> 24;
this->real_time += ((uint32_t)in_buf[3]) >> 16;
this->real_time += ((uint32_t)in_buf[4]) >> 8;
this->real_time += ((uint32_t)in_buf[5]);
break;
case CUDA_WRITE_PRAM:
response_header(CUDA_PKT_PSEUDO, 0);
this->pram_obj->write_byte(this->in_buf[2], this->in_buf[3]);
break;
case CUDA_FILE_SERVER_FLAG:
response_header(CUDA_PKT_PSEUDO, 0);
if (this->in_buf[2]) {
LOG_F(INFO, "Cuda: File server flag on");
this->file_server = true;
}
else {
LOG_F(INFO, "Cuda: File server flag off");
this->file_server = false;
}
break;
case CUDA_SET_AUTOPOLL_RATE:
this->poll_rate = this->in_buf[2];
LOG_F(INFO, "Cuda: autopoll rate set to: %d", this->poll_rate);
@@ -370,16 +335,6 @@ void ViaCuda::pseudo_command(int cmd, int data_count) {
this->out_buf[3] = this->poll_rate;
this->out_count++;
break;
case CUDA_SET_DEVICE_LIST:
response_header(CUDA_PKT_PSEUDO, 0);
this->device_mask = ((uint16_t)in_buf[2]) >> 8;
this->device_mask += ((uint16_t)in_buf[3]);
break;
case CUDA_GET_DEVICE_LIST:
response_header(CUDA_PKT_PSEUDO, 0);
this->out_buf[2] = (uint8_t)((this->device_mask >> 8) & 0xFF);
this->out_buf[3] = (uint8_t)((this->device_mask) & 0xFF);
break;
case CUDA_READ_WRITE_I2C:
response_header(CUDA_PKT_PSEUDO, 0);
i2c_simple_transaction(this->in_buf[2], &this->in_buf[3], this->in_count - 3);
@@ -395,16 +350,9 @@ void ViaCuda::pseudo_command(int cmd, int data_count) {
LOG_F(INFO, "Cuda: send %d to PB0 \n", (int)(this->in_buf[2]));
response_header(CUDA_PKT_PSEUDO, 0);
break;
case CUDA_WARM_START:
case CUDA_POWER_DOWN:
case CUDA_MONO_STABLE_RESET:
case CUDA_RESTART_SYSTEM:
/* really kludge temp code */
LOG_F(INFO, "Cuda: Restart/Shutdown signal sent with command 0x%x! \n", cmd);
exit(0);
break;
default:
LOG_F(ERROR, "Cuda: unsupported pseudo command 0x%x \n", cmd);
//LOG_F(ERROR, "Cuda: unsupported pseudo command 0x%x - Address: 0x%x \n", cmd, ppc_state.pc);
error_response(CUDA_ERR_BAD_CMD);
}
}
@@ -485,4 +433,4 @@ void ViaCuda::i2c_comb_transaction(
this->next_out_handler = &ViaCuda::i2c_handler;
this->is_open_ended = true;
}
}
}
+1 -16
View File
@@ -88,22 +88,11 @@ enum {
/** Cuda pseudo commands. */
enum {
CUDA_WARM_START = 0x00, /* warm start */
CUDA_START_STOP_AUTOPOLL = 0x01, /* start/stop device auto-polling */
CUDA_GET_6805_ADDRESS = 0x02, /* get 6805 address */
CUDA_GET_REAL_TIME = 0x03, /* get real time */
CUDA_READ_PRAM = 0x07, /* read parameter RAM */
CUDA_SET_6805_ADDRESS = 0x08, /* set 6805 address */
CUDA_SET_REAL_TIME = 0x09, /* set real time */
CUDA_POWER_DOWN = 0x0A, /* power down system */
CUDA_WRITE_PRAM = 0x0C, /* write parameter RAM */
CUDA_MONO_STABLE_RESET = 0x0D, /* mono stable reset */
CUDA_RESTART_SYSTEM = 0x11, /* restart system */
CUDA_FILE_SERVER_FLAG = 0x13, /* set file server flag */
CUDA_SET_AUTOPOLL_RATE = 0x14, /* set auto-polling rate */
CUDA_GET_AUTOPOLL_RATE = 0x16, /* get auto-polling rate */
CUDA_SET_DEVICE_LIST = 0x19, /* set device list */
CUDA_GET_DEVICE_LIST = 0x1A, /* get device list */
CUDA_READ_WRITE_I2C = 0x22, /* read/write I2C device */
CUDA_COMB_FMT_I2C = 0x25, /* combined format I2C transaction */
CUDA_OUT_PB0 = 0x26, /* output one bit to Cuda's PB0 line */
@@ -143,10 +132,6 @@ private:
int32_t out_count;
int32_t out_pos;
uint8_t poll_rate;
int32_t real_time = 0;
bool file_server;
uint16_t m6805_address = 0x1100;
uint16_t device_mask = 0;
bool is_open_ended;
uint8_t curr_i2c_addr;
@@ -180,4 +165,4 @@ private:
uint8_t dev_addr, uint8_t sub_addr, uint8_t dev_addr1, const uint8_t* in_buf, int in_bytes);
};
#endif /* VIACUDA_H */
#endif /* VIACUDA_H */
+2 -1
View File
@@ -26,7 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "machinefactory.h"
#include "devices/memctrlbase.h"
#include "memaccess.h"
#include "memreadwrite.h"
#include "machineproperties.h"
#include <cinttypes>
#include <cstring>
@@ -61,6 +61,7 @@ static const map<uint32_t, std::tuple<string, const char*>> rom_identity = {
{0x50444D20, {"pm6100", "Nubus Power Mac"}}, // Piltdown Man (6100/7100/8100)
{0x50697020, {"pippin", "Bandai Pippin"}}, // Pippin
//{"Powe", "Generic Power Mac"}, // PowerMac?
//{"Spar", "20th Anniversay Mac"}, // Spartacus
{0x544E5420, {"pm7200", "Power Mac 7xxxx/8xxx series"}}, // Trinitrotoluene :-)
{0x5A616E7A, {"pm4400", "Power Mac 4400/7220"}}, // Zanzibar
};
-2
View File
@@ -1,11 +1,9 @@
#include "endianswap.h"
#include <thirdparty/loguru/loguru.hpp>
#include <algorithm>
#include <cinttypes>
#include <string>
#include <map>
#include <memory>
#include <limits>
#include <sstream>
#include <utility>
#include <vector>
+1 -1
View File
@@ -64,7 +64,7 @@ int main(int argc, char** argv) {
app.allow_windows_style_options(); /* we want Windows-style options */
app.allow_extras();
bool realtime_enabled, debugger_enabled = false;
bool realtime_enabled, debugger_enabled;
string machine_str;
string bootrom_path("bootrom.bin");
+5 -84
View File
@@ -1,13 +1,12 @@
/** @file Set of macros for accessing host memory units of various sizes
and endianness.
/** @file Set of macros for accessing integers of various sizes and endianness
in host memory.
*/
#ifndef MEM_ACCESS_H
#define MEM_ACCESS_H
#ifndef MEM_READ_WRITE_H
#define MEM_READ_WRITE_H
#include "endianswap.h"
#include <cinttypes>
#include <thirdparty/loguru/loguru.hpp>
/* read an aligned big-endian WORD (16bit) */
#define READ_WORD_BE_A(addr) (BYTESWAP_16(*((uint16_t*)((addr)))))
@@ -100,82 +99,4 @@
(addr)[3] = ((val) >> 24) & 0xFF; \
} while (0)
/* read value of the specified size from memory starting at addr,
perform byte swapping when necessary so that the source
byte order remains unchanged. */
inline uint32_t read_mem(const uint8_t* buf, uint32_t size) {
switch (size) {
case 4:
return READ_DWORD_BE_A(buf);
break;
case 2:
return READ_WORD_BE_A(buf);
break;
case 1:
return *buf;
break;
default:
LOG_F(WARNING, "READ_MEM: invalid size %d!", size);
return 0;
}
}
/* read value of the specified size from memory starting at addr,
perform byte swapping when necessary so that the destination data
will be in the reversed byte order. */
inline uint32_t read_mem_rev(const uint8_t* buf, uint32_t size) {
switch (size) {
case 4:
return READ_DWORD_LE_A(buf);
break;
case 2:
return READ_WORD_LE_A(buf);
break;
case 1:
return *buf;
break;
default:
LOG_F(WARNING, "READ_MEM_REV: invalid size %d!", size);
return 0;
}
}
/* write the specified value of the specified size to memory pointed
to by addr, perform necessary byte swapping so that the byte order
of the destination remains unchanged. */
inline void write_mem(uint8_t* buf, uint32_t value, uint32_t size) {
switch (size) {
case 4:
WRITE_DWORD_BE_A(buf, value);
break;
case 2:
WRITE_WORD_BE_A(buf, value & 0xFFFFU);
break;
case 1:
*buf = value & 0xFF;
break;
default:
LOG_F(WARNING, "WRITE_MEM: invalid size %d!", size);
}
}
/* write the specified value of the specified size to memory pointed
to by addr, perform necessary byte swapping so that the destination
data in memory will be in the reversed byte order. */
inline void write_mem_rev(uint8_t* buf, uint32_t value, uint32_t size) {
switch (size) {
case 4:
WRITE_DWORD_LE_A(buf, value);
break;
case 2:
WRITE_WORD_LE_A(buf, value & 0xFFFFU);
break;
case 1:
*buf = value & 0xFF;
break;
default:
LOG_F(WARNING, "WRITE_MEM_REV: invalid size %d!", size);
}
}
#endif /* MEM_ACCESS_H */
#endif /* MEM_READ_WRITE_H */
Submodule thirdparty/capstone deleted from bfb2e45d76.