Integrate Capstone disassembler

This commit is contained in:
wheremyfoodat 2024-04-29 00:28:46 +03:00
parent 2eaaccd96b
commit eab1a12b07
5 changed files with 101 additions and 10 deletions

View file

@ -1,10 +1,13 @@
#ifdef PANDA3DS_ENABLE_LUA
#include <array>
#include "capstone.hpp"
#include "emulator.hpp"
#include "lua_manager.hpp"
#ifndef __ANDROID__
extern "C" {
#include "luv.h"
#include "luv.h"
}
#endif
@ -203,6 +206,27 @@ static int getButtonThunk(lua_State* L) {
return 1;
}
static int disassembleARMThunk(lua_State* L) {
static Common::CapstoneDisassembler disassembler(CS_ARCH_ARM, CS_MODE_ARM);
const u32 pc = u32(lua_tonumber(L, 1));
const u32 instruction = u32(lua_tonumber(L, 2));
std::string disassembly;
// Convert instruction to byte array to pass to Capstone
std::array<u8, 4> bytes = {
instruction & 0xff,
(instruction >> 8) & 0xff,
(instruction >> 16) & 0xff,
(instruction >> 24) & 0xff,
};
disassembler.disassemble(disassembly, pc, std::span(bytes));
lua_pushstring(L, disassembly.c_str());
return 1;
}
// clang-format off
static constexpr luaL_Reg functions[] = {
{ "__read8", read8Thunk },
@ -214,13 +238,14 @@ static constexpr luaL_Reg functions[] = {
{ "__write32", write32Thunk },
{ "__write64", write64Thunk },
{ "__getAppID", getAppIDThunk },
{ "__pause", pauseThunk},
{ "__resume", resumeThunk},
{ "__reset", resetThunk},
{ "__loadROM", loadROMThunk},
{ "__getButtons", getButtonsThunk},
{ "__getCirclepad", getCirclepadThunk},
{ "__getButton", getButtonThunk},
{ "__pause", pauseThunk },
{ "__resume", resumeThunk },
{ "__reset", resetThunk },
{ "__loadROM", loadROMThunk },
{ "__getButtons", getButtonsThunk },
{ "__getCirclepad", getCirclepadThunk },
{ "__getButton", getButtonThunk },
{ "__disassembleARM", disassembleARMThunk },
{ nullptr, nullptr },
};
// clang-format on
@ -254,6 +279,8 @@ void LuaManager::initializeThunks() {
getButton = function(button) return GLOBALS.__getButton(button) end,
getCirclepad = function() return GLOBALS.__getCirclepad() end,
disassembleARM = function(pc, instruction) return GLOBALS.__disassembleARM(pc, instruction) end,
Frame = __Frame,
ButtonA = __ButtonA,
ButtonB = __ButtonB,