Skip to content

Commit

Permalink
Merge pull request #1844 from nicolasnoble/pcsxlua
Browse files Browse the repository at this point in the history
Adding real time Lua property editor example.
  • Loading branch information
nicolasnoble authored Jan 16, 2025
2 parents 9897447 + bc5f1ff commit f208e1e
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/core/pcsxlua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ void PCSX::LuaFFI::open_pcsx(Lua L) {
registerAllSymbols(L);
L.load(pcsxFFI, "src:core/pcsxffi.lua");
L.getfieldtable("PCSX", LUA_GLOBALSINDEX);
L.push("execSlots");
L.newtable();
L.settable();
L.declareFunc(
"getSaveStateProtoSchema",
[](lua_State* L_) -> int {
Expand Down
3 changes: 0 additions & 3 deletions src/core/psxhw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,7 @@ void PCSX::HW::write8(uint32_t add, uint32_t rawvalue) {
try {
L.pcall();
} catch (...) {
g_system->pause();
}
} else {
g_system->pause();
}
while (top != L.gettop()) L.pop();
}
Expand Down
12 changes: 12 additions & 0 deletions src/mips/psyqo/examples/pcsxlua/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TARGET = pcsxlua
TYPE = ps-exe

SRCS = \
pcsxlua.cpp \

ifeq ($(TEST),true)
CPPFLAGS = -Werror
endif
CXXFLAGS = -std=c++20

include ../../psyqo.mk
98 changes: 98 additions & 0 deletions src/mips/psyqo/examples/pcsxlua/pcsxlua.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
MIT License
Copyright (c) 2025 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "psyqo/application.hh"
#include "psyqo/font.hh"
#include "psyqo/gpu.hh"
#include "psyqo/scene.hh"

namespace {

// This is a demo application that shows how to use Lua to inspect
// or modify some state inside an application. In this case, we're
// going to modify the background color of the scene in real time.
// The script called `pcsxlua.lua` will be responsible for that,
// and needs to be loaded by the emulator before running this.
// This can be achieved by using the `-dofile pcsxlua.lua` command
// line argument when starting the emulator.
class PCSXLua final : public psyqo::Application {
void prepare() override;
void createScene() override;

public:
psyqo::Font<> m_systemFont;
};

class PCSXLuaScene final : public psyqo::Scene {
void start(StartReason reason) override;
void frame() override;
// This variable will be registered to PCSX in order
// to be modified in real time from the Lua script.
psyqo::Color m_bg = {{.r = 0, .g = 64, .b = 91}};
};

PCSXLua pcsxLua;
PCSXLuaScene pcsxLuaScene;

// This will be calling into the Lua script from execSlot 255,
// where it will be able to modify the background color of the scene.
void pcsxRegisterVariable(void* address, const char* name) {
register void* a0 asm("a0") = address;
register const char* a1 asm("a1") = name;
__asm__ volatile("" : : "r"(a0), "r"(a1));
*((volatile uint8_t* const)0x1f802081) = 255;
}

} // namespace

void PCSXLua::prepare() {
psyqo::GPU::Configuration config;
config.set(psyqo::GPU::Resolution::W320)
.set(psyqo::GPU::VideoMode::AUTO)
.set(psyqo::GPU::ColorMode::C15BITS)
.set(psyqo::GPU::Interlace::PROGRESSIVE);
gpu().initialize(config);
}

void PCSXLua::createScene() {
m_systemFont.uploadSystemFont(gpu());
pushScene(&pcsxLuaScene);
}

void PCSXLuaScene::start(StartReason reason) {
if (reason == StartReason::Create) {
pcsxRegisterVariable(&m_bg, "pcsxLuaScene.m_bg");
}
}

void PCSXLuaScene::frame() {
pcsxLua.gpu().clear(m_bg);

psyqo::Color c = {{.r = 255, .g = 255, .b = uint8_t(255)}};
pcsxLua.m_systemFont.print(pcsxLua.gpu(), "Hello World!", {{.x = 16, .y = 32}}, c);
}

int main() { return pcsxLua.run(); }
50 changes: 50 additions & 0 deletions src/mips/psyqo/examples/pcsxlua/pcsxlua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--[[
MIT License
Copyright (c) 2025 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]

-- This is the companion Lua script for the PCSX-Redux Lua editor demo.
-- See pcsxlua.cpp for the C++ side of the demo.

local addresses = {}
PCSX.execSlots[255] = function()
local mem = PCSX.getMemPtr()
local regs = PCSX.getRegisters().GPR.n
local name = ffi.string(mem + bit.band(regs.a1, 0x7fffff))
addresses[name] = regs.a0
end

function DrawImguiFrame()
imgui.safe.Begin('Lua editor demo', true, function()
local mem = PCSX.getMemoryAsFile()
local addr = addresses['pcsxLuaScene.m_bg']
if type(addr) == 'number' then
local color = { r = mem:readU8At(addr + 0) / 255, g = mem:readU8At(addr + 1) / 255, b = mem:readU8At(addr + 2) / 255 }
local modified, n = imgui.extra.ColorEdit3('bgColor', color)
if modified then
mem:writeU8At(n.r * 255, addr + 0)
mem:writeU8At(n.g * 255, addr + 1)
mem:writeU8At(n.b * 255, addr + 2)
end
end
end)
end

0 comments on commit f208e1e

Please sign in to comment.