Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux-toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
- name: Build OpenBIOS with it
run: make -C src/mips/openbios -j 6 all
- name: Build psyqo examples with it
run: for d in src/mips/psyqo/examples/* ; do make -C $d -j 6 all TEST=true ; done
run: for d in src/mips/psyqo/examples/* src/mips/psyqo-paths/examples/* src/mips/psyqo-lua/examples/* ; do make -C $d -j 6 all TEST=true ; done
2 changes: 1 addition & 1 deletion src/mips/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ DEPS += $(patsubst %.s, %.dep,$(filter %.s,$(SRCS)))

dep: $(DEPS)

clean: $(EXTRA_CLEAN)
clean::
rm -f $(OBJS) $(BINDIR)*.a $(BINDIR)Overlay.* $(BINDIR)*.elf $(BINDIR)*.ps-exe $(BINDIR)*.map $(DEPS)

ifneq ($(MAKECMDGOALS), clean)
Expand Down
1 change: 1 addition & 0 deletions src/mips/psyqo-lua/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SRCS = \
src/lua.cpp \

CPPFLAGS += -I$(PSYQOLUADIR)../../../third_party/psxlua/src
CPPFLAGS += -DLUA_TARGET_PSX

EXTRA_DEPS += $(PSYQOLUADIR)Makefile

Expand Down
22 changes: 22 additions & 0 deletions src/mips/psyqo-lua/examples/hello/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ function factorial(n)
end
end

function printfactorial(n)
local f = factorial(n)
print('Factorial of ' .. tostring(n) .. ' is ' .. tostring(f))
return f
end

-- Calculate some values
results = {
factorial = factorial(5),
Expand Down Expand Up @@ -159,6 +165,8 @@ void LuaExample::createScene() {
pushScene(&luaExampleScene);
}

using namespace psyqo::fixed_point_literals;

void LuaExampleScene::frame() {
auto& gpu = luaExample.gpu();
auto& font = luaExample.m_font;
Expand Down Expand Up @@ -189,6 +197,20 @@ void LuaExampleScene::frame() {
font.printf(gpu, {{.x = 16, .y = 144}}, textColor, "Calling factorial(7) from C++: %d", factorial7);
}
luaExample.L.pop();

// Example of using FixedPoint
luaExample.L.getGlobal("printfactorial");
luaExample.L.push(6.0_fp);
if (luaExample.L.pcall(1, 1) != 0) {
// Error occurred
font.print(gpu, "Error calling printfactorial(6):", {{.x = 16, .y = 160}}, textColor);
font.print(gpu, luaExample.L.toString(-1), {{.x = 16, .y = 176}}, textColor);
} else {
auto factorial6 = luaExample.L.toFixedPoint(-1);
font.print(gpu, "Calling printfactorial(6.0_fp):", {{.x = 16, .y = 176}}, textColor);
font.printf(gpu, {{.x = 16, .y = 192}}, textColor, "Factorial(6) is %.2f", factorial6);
}
luaExample.L.pop();
} else {
// Display error message
font.print(gpu, "Lua script execution failed:", {{.x = 16, .y = 64}}, textColor);
Expand Down
14 changes: 10 additions & 4 deletions src/mips/psyqo-lua/lua.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ SOFTWARE.
extern "C" {
#include "lauxlib.h"
#include "lua.h"
#include "lualib.h"
}

#include "EASTL/string_view.h"
Expand Down Expand Up @@ -78,7 +79,8 @@ struct Lua {
lua_pushlstring(L, s, S - 1);
}
void push(eastl::string_view s) { lua_pushlstring(L, s.data(), s.size()); }
void push(const char* fmt, ...);
void vpushf(const char* fmt, va_list ap) { lua_pushvfstring(L, fmt, ap); }
void pushf(const char* fmt, ...);
void push(lua_CFunction f, int closure = 0) { lua_pushcclosure(L, f, closure); }
void push(lua_CPPFunction f);
void push(void* p) { lua_pushlightuserdata(L, p); }
Expand All @@ -97,7 +99,10 @@ struct Lua {
const char* toString(int idx, size_t* len = nullptr) { return lua_tolstring(L, idx, len); }
size_t rawLen(int idx) { return lua_rawlen(L, idx); }
lua_CFunction toCFunction(int idx) { return lua_tocfunction(L, idx); }
void* toUserdata(int idx) { return lua_touserdata(L, idx); }
template <typename T = void>
T* toUserdata(int idx) {
return reinterpret_cast<T*>(lua_touserdata(L, idx));
}
lua_State* toThread(int idx) { return lua_tothread(L, idx); }
const void* toPointer(int idx) { return lua_topointer(L, idx); }

Expand Down Expand Up @@ -141,8 +146,8 @@ struct Lua {
int setMetatable(int objindex) { return lua_setmetatable(L, objindex); }

// Function calling
void call(int nargs, int nresults) { lua_call(L, nargs, nresults); }
int pcall(int nargs, int nresults, int errfunc = 0) { return lua_pcall(L, nargs, nresults, errfunc); }
void call(int nargs, int nresults = LUA_MULTRET) { lua_call(L, nargs, nresults); }
int pcall(int nargs, int nresults = LUA_MULTRET);

// Loading and executing
int loadBuffer(const char* buff, size_t sz, const char* chunkname = nullptr) {
Expand Down Expand Up @@ -197,6 +202,7 @@ struct Lua {
private:
lua_State* L;
void setupFixedPointMetatable();
static int traceback(lua_State* L);
};

} // namespace psyqo
7 changes: 3 additions & 4 deletions src/mips/psyqo-lua/psyqo-lua.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ PSYQOLUADIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
LIBRARIES += $(PSYQOLUADIR)libpsyqo-lua.a $(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a

CPPFLAGS += -I$(PSYQOLUADIR)../../../third_party/psxlua/src
CPPFLAGS += -DLUA_TARGET_PSX

LDFLAGS += \
-Wl,--defsym,luaI_sprintf=sprintf_for_Lua \
-Wl,--defsym,luaI_realloc=psyqo_realloc \
-Wl,--defsym,luaI_free=psyqo_free \

EXTRA_CLEAN += clean-psyqo-lua

include $(PSYQOLUADIR)../psyqo/psyqo.mk

$(PSYQOLUADIR)libpsyqo-lua.a:
Expand All @@ -20,9 +19,9 @@ $(PSYQOLUADIR)libpsyqo-lua.a:
$(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a:
$(MAKE) -C $(PSYQOLUADIR)../../../third_party/psxlua/ psx

clean-psyqo-lua:
clean::
$(MAKE) -C $(PSYQOLUADIR) clean
$(MAKE) -C $(PSYQOLUADIR)../../../third_party/psxlua/ clean

.PHONY: clean-psyqo-lua $(PSYQOLUADIR)libpsyqo-lua.a $(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a
.PHONY: clean $(PSYQOLUADIR)libpsyqo-lua.a $(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a
endif
99 changes: 64 additions & 35 deletions src/mips/psyqo-lua/src/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ SOFTWARE.

#include "psyqo-lua/lua.hh"

#include <EASTL/string.h>

#include "psyqo/kernel.hh"
#include "psyqo/xprintf.h"

Expand Down Expand Up @@ -111,6 +113,7 @@ psyqo::FixedPoint<> psyqo::Lua::optFixedPoint(int idx, FixedPoint<> def) {
psyqo::Lua::Lua() : L(luaL_newstate()) {
static_assert(sizeof(Lua) == sizeof(lua_State*));
Kernel::assert(L, "Couldn't create Lua VM");
luaL_openlibs(L);
lua_atpanic(L, [](lua_State* L) -> int {
const char* errorMsg = lua_tolstring(L, 1, nullptr);
Kernel::abort(errorMsg);
Expand Down Expand Up @@ -153,7 +156,7 @@ int psyqo::Lua::error(const char* fmt, ...) {
return lua_error(L);
}

void psyqo::Lua::push(const char* fmt, ...) {
void psyqo::Lua::pushf(const char* fmt, ...) {
va_list argp;
va_start(argp, fmt);
lua_pushvfstring(L, fmt, argp);
Expand Down Expand Up @@ -238,10 +241,10 @@ void psyqo::Lua::setupFixedPointMetatable() {
unsigned fraction = raw & 0xfff;

if (fraction == 0) {
L.push("%d", integer);
L.pushf("%d", integer);
} else {
unsigned decimal = (fraction * 1000) >> 12;
L.push("%d.%03u", integer, decimal);
L.pushf("%d.%03u", integer, decimal);
}

return 1;
Expand All @@ -253,68 +256,80 @@ void psyqo::Lua::setupFixedPointMetatable() {
return function(metatable)
FixedPoint = metatable

-- Create a new FixedPoint from raw value
local newFromRaw = function(raw_value)
return setmetatable({_raw = raw_value}, FixedPoint)
end

FixedPoint.newFromRaw = newFromRaw
local lshift = bit32.lshift
local rshift = bit32.rshift
local err = function(op)
error('Cannot ' .. op .. ' FixedPoint to this type')
end

-- Simple operations can be done directly in Lua
function FixedPoint.__add(a, b)
local raw_a = a._raw
if type(b) == "number" then
-- FixedPoint + number (treated as integer)
return FixedPoint.new(raw_a + bit.lshift(b, 12))
elseif type(b) == "table" and b._raw then
if type(b) == 'number' then
-- FixedPoint + number
return newFromRaw(raw_a + lshift(b, 12))
elseif type(b) == 'table' and b._raw then
-- FixedPoint + FixedPoint
return FixedPoint.new(raw_a + b._raw)
return newFromRaw(raw_a + b._raw)
else
error("Cannot add FixedPoint to this type")
err('add')
end
end

function FixedPoint.__sub(a, b)
local raw_a = a._raw
if type(b) == "number" then
-- FixedPoint - number (treated as integer)
return FixedPoint.new(raw_a - bit.lshift(b, 12))
elseif type(b) == "table" and b._raw then
if type(b) == 'number' then
-- FixedPoint - number
return newFromRaw(raw_a - lshift(b, 12))
elseif type(b) == 'table' and b._raw then
-- FixedPoint - FixedPoint
return FixedPoint.new(raw_a - b._raw)
return newFromRaw(raw_a - b._raw)
else
error("Cannot subtract this type from FixedPoint")
err('subtract')
end
end

function FixedPoint.__unm(a)
-- Unary minus
return FixedPoint.new(-a._raw)
return newFromRaw(-a._raw)
end

function FixedPoint.__eq(a, b)
if type(b) == "table" and b._raw then
if type(b) == 'table' and b._raw then
return a._raw == b._raw
elseif type(b) == "number" then
elseif type(b) == 'number' then
-- Compare with an integer number (shifted)
return a._raw == bit.lshift(b, 12)
return a._raw == lshift(b, 12)
else
return false
end
end

function FixedPoint.__lt(a, b)
if type(b) == "table" and b._raw then
if type(b) == 'table' and b._raw then
return a._raw < b._raw
elseif type(b) == "number" then
elseif type(b) == 'number' then
-- Compare with an integer number (shifted)
return a._raw < bit.lshift(b, 12)
return a._raw < lshift(b, 12)
else
error("Cannot compare FixedPoint with this type")
err('compare')
end
end

function FixedPoint.__le(a, b)
if type(b) == "table" and b._raw then
if type(b) == 'table' and b._raw then
return a._raw <= b._raw
elseif type(b) == "number" then
elseif type(b) == 'number' then
-- Compare with an integer number (shifted)
return a._raw <= bit.lshift(b, 12)
return a._raw <= lshift(b, 12)
else
error("Cannot compare FixedPoint with this type")
err('compare')
end
end

Expand All @@ -325,24 +340,19 @@ void psyqo::Lua::setupFixedPointMetatable() {

-- Method to convert to a simple number (for simple calculations)
function FixedPoint:toNumber()
return (self._raw + 2048) / 4096
end

-- Create a new FixedPoint from raw value
function FixedPoint.newFromRaw(raw_value)
return setmetatable({_raw = raw_value}, FixedPoint)
return rshift((self._raw + 2048), 12)
end

-- Create a new FixedPoint
function FixedPoint.new(integer, fraction)
if fraction == nil then fraction = 0 end
return setmetatable({_raw = bit.lshift(integer, 12) + fraction}, FixedPoint)
return setmetatable({_raw = lshift(integer, 12) + fraction}, FixedPoint)
end
end
)lua";

// Load the Lua script
if (loadBuffer(fixedPointScript, sizeof(fixedPointScript) - 1) != 0) {
if (loadBuffer(fixedPointScript, sizeof(fixedPointScript) - 1, "buffer:fixedPointScript") != 0) {
const char* errorMsg = toString(2);
psyqo::Kernel::abort(errorMsg);
}
Expand All @@ -362,3 +372,22 @@ void psyqo::Lua::setupFixedPointMetatable() {

pop();
}

int psyqo::Lua::pcall(int nargs, int nresults) {
int n = getTop();
int errorfunc = n - nargs;
lua_pushcfunction(L, traceback);
insert(errorfunc);
int r = lua_pcall(L, nargs, nresults, errorfunc);
remove(errorfunc);
return r;
}

int psyqo::Lua::traceback(lua_State* L) {
int n = lua_gettop(L);
const char* msgPtr = n >= 1 ? lua_tostring(L, 1) : nullptr;
eastl::string msg = msgPtr ? msgPtr : "no message";
lua_settop(L, 0);
luaL_traceback(L, L, msg.c_str(), 1);
return 1;
}
6 changes: 2 additions & 4 deletions src/mips/psyqo-paths/psyqo-paths.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ PSYQOPATHSDIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

LIBRARIES += $(PSYQOPATHSDIR)libpsyqo-paths.a

EXTRA_CLEAN += clean-psyqo-paths

include $(PSYQOPATHSDIR)../psyqo/psyqo.mk

$(PSYQOPATHSDIR)libpsyqo-paths.a:
$(MAKE) -C $(PSYQOPATHSDIR) BUILD=$(BUILD)

clean-psyqo-paths:
clean::
$(MAKE) -C $(PSYQOPATHSDIR) clean

.PHONY: clean-psyqo-paths $(PSYQOPATHSDIR)libpsyqo-paths.a
.PHONY: clean $(PSYQOPATHSDIR)libpsyqo-paths.a
endif
6 changes: 2 additions & 4 deletions src/mips/psyqo/psyqo.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ LIBRARIES += $(PSYQODIR)libpsyqo.a
CPPFLAGS += -I$(PSYQODIR)../../../third_party/EASTL/include -I$(PSYQODIR)../../../third_party/EABase/include/Common
CXXFLAGS += -std=c++20

EXTRA_CLEAN += clean-psyqo

include $(PSYQODIR)../common.mk

$(PSYQODIR)libpsyqo.a:
$(MAKE) -C $(PSYQODIR) BUILD=$(BUILD) CPPFLAGS_$(BUILD)=$(CPPFLAGS_$(BUILD)) LDFLAGS_$(BUILD)=$(LDFLAGS_$(BUILD))

clean-psyqo:
clean::
$(MAKE) -C $(PSYQODIR) clean

.PHONY: clean-psyqo $(PSYQODIR)libpsyqo.a
.PHONY: clean $(PSYQODIR)libpsyqo.a
endif
2 changes: 1 addition & 1 deletion third_party/psxlua
Submodule psxlua updated 1 files
+1 −1 src/luaconf.h