mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-16 02:49:48 +12:00
Hook memory RW to Lua
This commit is contained in:
parent
b908f3efc1
commit
74026d2faa
3 changed files with 52 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "helpers.hpp"
|
#include "helpers.hpp"
|
||||||
|
#include "memory.hpp"
|
||||||
|
|
||||||
#ifdef PANDA3DS_ENABLE_LUA
|
#ifdef PANDA3DS_ENABLE_LUA
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -15,8 +16,15 @@ class LuaManager {
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
// For Lua we must have some global pointers to our emulator objects to use them in script code via thunks. See the thunks in lua.cpp as an
|
||||||
|
// example
|
||||||
|
static Memory* g_memory;
|
||||||
|
|
||||||
|
LuaManager(Memory& mem) { g_memory = &mem; }
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
void initialize();
|
void initialize();
|
||||||
|
void initializeThunks();
|
||||||
void loadFile(const char* path);
|
void loadFile(const char* path);
|
||||||
void reset();
|
void reset();
|
||||||
};
|
};
|
||||||
|
@ -24,6 +32,8 @@ class LuaManager {
|
||||||
#elif // Lua not enabled, Lua manager does nothing
|
#elif // Lua not enabled, Lua manager does nothing
|
||||||
class LuaManager {
|
class LuaManager {
|
||||||
public:
|
public:
|
||||||
|
LuaManager(Memory& mem) {}
|
||||||
|
|
||||||
void close() {}
|
void close() {}
|
||||||
void initialize() {}
|
void initialize() {}
|
||||||
void loadFile(const char* path) {}
|
void loadFile(const char* path) {}
|
||||||
|
|
|
@ -13,7 +13,7 @@ __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1;
|
||||||
|
|
||||||
Emulator::Emulator()
|
Emulator::Emulator()
|
||||||
: config(std::filesystem::current_path() / "config.toml"), kernel(cpu, memory, gpu, config), cpu(memory, kernel), gpu(memory, config),
|
: config(std::filesystem::current_path() / "config.toml"), kernel(cpu, memory, gpu, config), cpu(memory, kernel), gpu(memory, config),
|
||||||
memory(cpu.getTicksRef(), config), cheats(memory, kernel.getServiceManager().getHID()), running(false), programRunning(false)
|
memory(cpu.getTicksRef(), config), cheats(memory, kernel.getServiceManager().getHID()), lua(memory), running(false), programRunning(false)
|
||||||
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
|
||||||
, httpServer(this)
|
, httpServer(this)
|
||||||
#endif
|
#endif
|
||||||
|
|
39
src/lua.cpp
39
src/lua.cpp
|
@ -11,6 +11,8 @@ void LuaManager::initialize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
|
initializeThunks();
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,4 +36,41 @@ void LuaManager::loadFile(const char* path) {
|
||||||
void LuaManager::reset() {
|
void LuaManager::reset() {
|
||||||
// Reset scripts
|
// Reset scripts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize C++ thunks for Lua code to call here
|
||||||
|
// All code beyond this point is terrible and full of global state, don't judge
|
||||||
|
|
||||||
|
Memory* LuaManager::g_memory = nullptr;
|
||||||
|
|
||||||
|
#define MAKE_MEMORY_FUNCTIONS(size) \
|
||||||
|
static int read##size##Thunk(lua_State* L) { \
|
||||||
|
const u32 vaddr = (u32)lua_tonumber(L, 1); \
|
||||||
|
lua_pushnumber(L, LuaManager::g_memory->read##size(vaddr)); \
|
||||||
|
return 1; \
|
||||||
|
} \
|
||||||
|
static int write##size##Thunk(lua_State* L) { \
|
||||||
|
const u32 vaddr = (u32)lua_tonumber(L, 1); \
|
||||||
|
const u##size value = (u##size)lua_tonumber(L, 2); \
|
||||||
|
LuaManager::g_memory->write##size(vaddr, value); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
MAKE_MEMORY_FUNCTIONS(8)
|
||||||
|
MAKE_MEMORY_FUNCTIONS(16)
|
||||||
|
MAKE_MEMORY_FUNCTIONS(32)
|
||||||
|
MAKE_MEMORY_FUNCTIONS(64)
|
||||||
|
#undef MAKE_MEMORY_FUNCTIONS
|
||||||
|
|
||||||
|
void LuaManager::initializeThunks() {
|
||||||
|
lua_register(L, "read8", read8Thunk);
|
||||||
|
lua_register(L, "read16", read16Thunk);
|
||||||
|
lua_register(L, "read32", read32Thunk);
|
||||||
|
lua_register(L, "read64", read64Thunk);
|
||||||
|
lua_register(L, "write8", write8Thunk);
|
||||||
|
lua_register(L, "write16", write16Thunk);
|
||||||
|
lua_register(L, "write32", write32Thunk);
|
||||||
|
lua_register(L, "write64", write64Thunk);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Add table
Reference in a new issue