Skip to content

Commit 11ea342

Browse files
committed
Merge branch 'main' of github.com:grumpycoders/pcsx-redux
2 parents e4a2537 + f208e1e commit 11ea342

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

src/core/pcsxlua.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ void PCSX::LuaFFI::open_pcsx(Lua L) {
178178
registerAllSymbols(L);
179179
L.load(pcsxFFI, "src:core/pcsxffi.lua");
180180
L.getfieldtable("PCSX", LUA_GLOBALSINDEX);
181+
L.push("execSlots");
182+
L.newtable();
183+
L.settable();
181184
L.declareFunc(
182185
"getSaveStateProtoSchema",
183186
[](lua_State* L_) -> int {

src/core/psxhw.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,7 @@ void PCSX::HW::write8(uint32_t add, uint32_t rawvalue) {
437437
try {
438438
L.pcall();
439439
} catch (...) {
440-
g_system->pause();
441440
}
442-
} else {
443-
g_system->pause();
444441
}
445442
while (top != L.gettop()) L.pop();
446443
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
TARGET = pcsxlua
2+
TYPE = ps-exe
3+
4+
SRCS = \
5+
pcsxlua.cpp \
6+
7+
ifeq ($(TEST),true)
8+
CPPFLAGS = -Werror
9+
endif
10+
CXXFLAGS = -std=c++20
11+
12+
include ../../psyqo.mk
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2025 PCSX-Redux authors
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
#include "psyqo/application.hh"
28+
#include "psyqo/font.hh"
29+
#include "psyqo/gpu.hh"
30+
#include "psyqo/scene.hh"
31+
32+
namespace {
33+
34+
// This is a demo application that shows how to use Lua to inspect
35+
// or modify some state inside an application. In this case, we're
36+
// going to modify the background color of the scene in real time.
37+
// The script called `pcsxlua.lua` will be responsible for that,
38+
// and needs to be loaded by the emulator before running this.
39+
// This can be achieved by using the `-dofile pcsxlua.lua` command
40+
// line argument when starting the emulator.
41+
class PCSXLua final : public psyqo::Application {
42+
void prepare() override;
43+
void createScene() override;
44+
45+
public:
46+
psyqo::Font<> m_systemFont;
47+
};
48+
49+
class PCSXLuaScene final : public psyqo::Scene {
50+
void start(StartReason reason) override;
51+
void frame() override;
52+
// This variable will be registered to PCSX in order
53+
// to be modified in real time from the Lua script.
54+
psyqo::Color m_bg = {{.r = 0, .g = 64, .b = 91}};
55+
};
56+
57+
PCSXLua pcsxLua;
58+
PCSXLuaScene pcsxLuaScene;
59+
60+
// This will be calling into the Lua script from execSlot 255,
61+
// where it will be able to modify the background color of the scene.
62+
void pcsxRegisterVariable(void* address, const char* name) {
63+
register void* a0 asm("a0") = address;
64+
register const char* a1 asm("a1") = name;
65+
__asm__ volatile("" : : "r"(a0), "r"(a1));
66+
*((volatile uint8_t* const)0x1f802081) = 255;
67+
}
68+
69+
} // namespace
70+
71+
void PCSXLua::prepare() {
72+
psyqo::GPU::Configuration config;
73+
config.set(psyqo::GPU::Resolution::W320)
74+
.set(psyqo::GPU::VideoMode::AUTO)
75+
.set(psyqo::GPU::ColorMode::C15BITS)
76+
.set(psyqo::GPU::Interlace::PROGRESSIVE);
77+
gpu().initialize(config);
78+
}
79+
80+
void PCSXLua::createScene() {
81+
m_systemFont.uploadSystemFont(gpu());
82+
pushScene(&pcsxLuaScene);
83+
}
84+
85+
void PCSXLuaScene::start(StartReason reason) {
86+
if (reason == StartReason::Create) {
87+
pcsxRegisterVariable(&m_bg, "pcsxLuaScene.m_bg");
88+
}
89+
}
90+
91+
void PCSXLuaScene::frame() {
92+
pcsxLua.gpu().clear(m_bg);
93+
94+
psyqo::Color c = {{.r = 255, .g = 255, .b = uint8_t(255)}};
95+
pcsxLua.m_systemFont.print(pcsxLua.gpu(), "Hello World!", {{.x = 16, .y = 32}}, c);
96+
}
97+
98+
int main() { return pcsxLua.run(); }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--[[
2+
MIT License
3+
4+
Copyright (c) 2025 PCSX-Redux authors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
--]]
24+
25+
-- This is the companion Lua script for the PCSX-Redux Lua editor demo.
26+
-- See pcsxlua.cpp for the C++ side of the demo.
27+
28+
local addresses = {}
29+
PCSX.execSlots[255] = function()
30+
local mem = PCSX.getMemPtr()
31+
local regs = PCSX.getRegisters().GPR.n
32+
local name = ffi.string(mem + bit.band(regs.a1, 0x7fffff))
33+
addresses[name] = regs.a0
34+
end
35+
36+
function DrawImguiFrame()
37+
imgui.safe.Begin('Lua editor demo', true, function()
38+
local mem = PCSX.getMemoryAsFile()
39+
local addr = addresses['pcsxLuaScene.m_bg']
40+
if type(addr) == 'number' then
41+
local color = { r = mem:readU8At(addr + 0) / 255, g = mem:readU8At(addr + 1) / 255, b = mem:readU8At(addr + 2) / 255 }
42+
local modified, n = imgui.extra.ColorEdit3('bgColor', color)
43+
if modified then
44+
mem:writeU8At(n.r * 255, addr + 0)
45+
mem:writeU8At(n.g * 255, addr + 1)
46+
mem:writeU8At(n.b * 255, addr + 2)
47+
end
48+
end
49+
end)
50+
end

0 commit comments

Comments
 (0)