|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (C) 2021 PCSX-Redux authors * |
| 3 | + * * |
| 4 | + * This program is free software; you can redistribute it and/or modify * |
| 5 | + * it under the terms of the GNU General Public License as published by * |
| 6 | + * the Free Software Foundation; either version 2 of the License, or * |
| 7 | + * (at your option) any later version. * |
| 8 | + * * |
| 9 | + * This program is distributed in the hope that it will be useful, * |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 | + * GNU General Public License for more details. * |
| 13 | + * * |
| 14 | + * You should have received a copy of the GNU General Public License * |
| 15 | + * along with this program; if not, write to the * |
| 16 | + * Free Software Foundation, Inc., * |
| 17 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
| 18 | + ***************************************************************************/ |
| 19 | + |
| 20 | +#include "core/pcsxlua.h" |
| 21 | + |
| 22 | +#include "core/debug.h" |
| 23 | +#include "core/psxemulator.h" |
| 24 | +#include "core/psxmem.h" |
| 25 | +#include "core/r3000a.h" |
| 26 | +#include "lua/luawrapper.h" |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +struct LuaBreakpoint { |
| 31 | + PCSX::Debug::BreakpointUserListType wrapper; |
| 32 | +}; |
| 33 | + |
| 34 | +void setBreakpoint() {} |
| 35 | +void* getMemPtr() { return PCSX::g_emulator->m_psxMem->g_psxM; } |
| 36 | +void* getRomPtr() { return PCSX::g_emulator->m_psxMem->g_psxR; } |
| 37 | +void* getScratchPtr() { return PCSX::g_emulator->m_psxMem->g_psxH; } |
| 38 | +void* getRegisters() { return &PCSX::g_emulator->m_psxCpu->m_psxRegs; } |
| 39 | +LuaBreakpoint* addBreakpoint(uint32_t address, PCSX::Debug::BreakpointType type, unsigned width, const char* cause, |
| 40 | + bool (*invoker)()) { |
| 41 | + LuaBreakpoint* ret = new LuaBreakpoint(); |
| 42 | + auto* bp = |
| 43 | + PCSX::g_emulator->m_debug->addBreakpoint(address, type, width, std::string("Lua Breakpoint ") + cause, |
| 44 | + [invoker](const PCSX::Debug::Breakpoint* self) { return invoker(); }); |
| 45 | + |
| 46 | + ret->wrapper.push_back(bp); |
| 47 | + return ret; |
| 48 | +} |
| 49 | +void enableBreakpoint(LuaBreakpoint* wrapper) { |
| 50 | + if (wrapper->wrapper.size() == 0) return; |
| 51 | + wrapper->wrapper.begin()->enable(); |
| 52 | +} |
| 53 | +void disableBreakpoint(LuaBreakpoint* wrapper) { |
| 54 | + if (wrapper->wrapper.size() == 0) return; |
| 55 | + wrapper->wrapper.begin()->disable(); |
| 56 | +} |
| 57 | +bool breakpointEnabled(LuaBreakpoint* wrapper) { |
| 58 | + if (wrapper->wrapper.size() == 0) return false; |
| 59 | + return wrapper->wrapper.begin()->enabled(); |
| 60 | +} |
| 61 | +void removeBreakpoint(LuaBreakpoint* wrapper) { |
| 62 | + wrapper->wrapper.destroyAll(); |
| 63 | + delete wrapper; |
| 64 | +} |
| 65 | +void pauseEmulator() { PCSX::g_system->pause(); } |
| 66 | +void resumeEmulator() { PCSX::g_system->resume(); } |
| 67 | +void softResetEmulator() { PCSX::g_system->softReset(); } |
| 68 | +void hardResetEmulator() { PCSX::g_system->hardReset(); } |
| 69 | +void luaMessage(const char* msg, bool error) { PCSX::g_system->luaMessage(msg, error); } |
| 70 | +void luaLog(const char* msg) { PCSX::g_system->log(PCSX::LogClass::LUA, msg); } |
| 71 | +void jumpToPC(uint32_t pc) { PCSX::g_system->m_eventBus->signal(PCSX::Events::GUI::JumpToPC{pc}); } |
| 72 | +void jumpToMemory(uint32_t address, unsigned width) { |
| 73 | + PCSX::g_system->m_eventBus->signal(PCSX::Events::GUI::JumpToMemory{address, width}); |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace |
| 77 | + |
| 78 | +template <typename T, size_t S> |
| 79 | +static void registerSymbol(PCSX::Lua* L, const char (&name)[S], const T ptr) { |
| 80 | + L->push<S>(name); |
| 81 | + L->push((void*)ptr); |
| 82 | + L->settable(); |
| 83 | +} |
| 84 | + |
| 85 | +#define REGISTER(L, s) registerSymbol(L, #s, s) |
| 86 | + |
| 87 | +static void registerAllSymbols(PCSX::Lua* L) { |
| 88 | + L->push("_CLIBS"); |
| 89 | + L->gettable(LUA_REGISTRYINDEX); |
| 90 | + if (L->isnil()) { |
| 91 | + L->pop(); |
| 92 | + L->newtable(); |
| 93 | + L->push("_CLIBS"); |
| 94 | + L->copy(-2); |
| 95 | + L->settable(LUA_REGISTRYINDEX); |
| 96 | + } |
| 97 | + L->push("PCSX"); |
| 98 | + L->newtable(); |
| 99 | + REGISTER(L, getMemPtr); |
| 100 | + REGISTER(L, getRomPtr); |
| 101 | + REGISTER(L, getScratchPtr); |
| 102 | + REGISTER(L, getRegisters); |
| 103 | + REGISTER(L, addBreakpoint); |
| 104 | + REGISTER(L, enableBreakpoint); |
| 105 | + REGISTER(L, disableBreakpoint); |
| 106 | + REGISTER(L, breakpointEnabled); |
| 107 | + REGISTER(L, removeBreakpoint); |
| 108 | + REGISTER(L, pauseEmulator); |
| 109 | + REGISTER(L, resumeEmulator); |
| 110 | + REGISTER(L, softResetEmulator); |
| 111 | + REGISTER(L, hardResetEmulator); |
| 112 | + REGISTER(L, luaMessage); |
| 113 | + REGISTER(L, luaLog); |
| 114 | + REGISTER(L, jumpToPC); |
| 115 | + REGISTER(L, jumpToMemory); |
| 116 | + L->settable(); |
| 117 | + L->pop(); |
| 118 | +} |
| 119 | + |
| 120 | +void PCSX::LuaFFI::open_pcsx(Lua* L) { |
| 121 | + static int lualoader = 1; |
| 122 | + static const char* pcsxFFI = ( |
| 123 | +#include "core/pcsxffi.lua" |
| 124 | + ); |
| 125 | + registerAllSymbols(L); |
| 126 | + L->load(pcsxFFI, "internal:core/pcsxffi.lua"); |
| 127 | +} |
0 commit comments