From b5595cc996493f02683babd7dc8e2182d2b40c97 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Thu, 27 Jul 2023 16:36:08 -0400 Subject: [PATCH] lcpu: Move LuaCpu to LuaObject --- native/projects/lcpu/src/LcpuGlobals.cpp | 2 +- native/projects/lcpu/src/LuaCpu.cpp | 65 ++++++++++-------------- native/projects/lcpu/src/LuaCpu.hpp | 16 +++--- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/native/projects/lcpu/src/LcpuGlobals.cpp b/native/projects/lcpu/src/LcpuGlobals.cpp index 321845c..0b95e69 100644 --- a/native/projects/lcpu/src/LcpuGlobals.cpp +++ b/native/projects/lcpu/src/LcpuGlobals.cpp @@ -65,7 +65,7 @@ LUA_FUNCTION(LCPUNative_CreateDevice) { } void GlobalsBind(GarrysMod::Lua::ILuaBase* LUA) { - LuaCpu::Bind(LUA); + LuaCpu::RegisterClass(LUA); LuaDevice::RegisterClass(LUA); TestLuaObject::RegisterClass(LUA); diff --git a/native/projects/lcpu/src/LuaCpu.cpp b/native/projects/lcpu/src/LuaCpu.cpp index 255be64..b70dfab 100644 --- a/native/projects/lcpu/src/LuaCpu.cpp +++ b/native/projects/lcpu/src/LuaCpu.cpp @@ -3,7 +3,6 @@ #include #include "LuaDevice.hpp" -#include "LuaHelpers.hpp" // this is temporary from the thing @@ -31,16 +30,14 @@ struct SimpleUartDevice : public riscv::Bus::MmioDevice { } }; -LUA_CLASS_BIND_VARIABLES_IMPLEMENT(LuaCpu); - LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PoweredOn) { - auto self = LUA_CLASS_GET(LuaCpu)(1); + auto self = LuaCpu::FromLua(LUA, 1); LUA->PushBool(self->poweredOn); return 1; } LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Cycle) { - auto self = LUA_CLASS_GET(LuaCpu)(1); + auto self = LuaCpu::FromLua(LUA, 1); if(!self->poweredOn) return 0; self->system->Step(); @@ -48,7 +45,7 @@ LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Cycle) { } LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOff) { - auto self = LUA_CLASS_GET(LuaCpu)(1); + auto self = LuaCpu::FromLua(LUA, 1); if(!self->poweredOn) return 0; @@ -58,7 +55,7 @@ LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOff) { } LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOn) { - auto self = LUA_CLASS_GET(LuaCpu)(1); + auto self = LuaCpu::FromLua(LUA, 1); if(self->poweredOn) return 0; @@ -68,49 +65,29 @@ LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOn) { } LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Reset) { - auto self = LUA_CLASS_GET(LuaCpu)(1); + auto self = LuaCpu::FromLua(LUA, 1); self->system->bus->Reset(); return 0; } LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, AttachDevice) { - auto self = LUA_CLASS_GET(LuaCpu)(1); + auto self = LuaCpu::FromLua(LUA, 1); auto device = LuaDevice::FromLua(LUA, 2); - + // Attach it LUA->PushBool(self->system->bus->AttachDevice(static_cast(device))); return 1; } -void LuaCpu::Bind(GarrysMod::Lua::ILuaBase* LUA) { - // clang-format off - LUA_CLASS_BIND_BEGIN(LuaCpu); - LUA_SET_C_FUNCTION(PoweredOn); - LUA_SET_C_FUNCTION(Cycle); - LUA_SET_C_FUNCTION(PowerOff); - LUA_SET_C_FUNCTION(PowerOn); - LUA_SET_C_FUNCTION(Reset); - LUA_SET_C_FUNCTION(AttachDevice); - LUA_CLASS_BIND_END(); - // clang-format on -} +void LuaCpu::RegisterClass(GarrysMod::Lua::ILuaBase* LUA) { + RegisterClassStart(LUA); -void LuaCpu::Create(GarrysMod::Lua::ILuaBase* LUA, u32 memorySize) { - auto cpuWrapper = new LuaCpu(memorySize); - - // lame test code. this WILL be removed, I just want this for a quick test - cpuWrapper->system->bus->AttachDevice(new SimpleUartDevice); - auto fp = std::fopen("/home/lily/test-gmod.bin", "rb"); - if(fp) { - std::fseek(fp, 0, SEEK_END); - auto len = std::ftell(fp); - std::fseek(fp, 0, SEEK_SET); - - std::fread(cpuWrapper->system->ram->Raw(), 1, len, fp); - std::fclose(fp); - } - - LUA->PushUserType(cpuWrapper, __lua_typeid); + RegisterMethod("PoweredOn", PoweredOn); + RegisterMethod("Cycle", Cycle); + RegisterMethod("PowerOff", PowerOff); + RegisterMethod("PowerOn", PowerOn); + RegisterMethod("Reset", Reset); + RegisterMethod("AttachDevice", AttachDevice); } LuaCpu::LuaCpu(u32 memorySize) { @@ -120,6 +97,18 @@ LuaCpu::LuaCpu(u32 memorySize) { poweredOn = false; system->bus->Reset(); }; + + // lame test code. this WILL be removed, I just want this for a quick test + system->bus->AttachDevice(new SimpleUartDevice); + auto fp = std::fopen("/home/lily/test-gmod.bin", "rb"); + if(fp) { + std::fseek(fp, 0, SEEK_END); + auto len = std::ftell(fp); + std::fseek(fp, 0, SEEK_SET); + + std::fread(system->ram->Raw(), 1, len, fp); + std::fclose(fp); + } } LuaCpu::~LuaCpu() { diff --git a/native/projects/lcpu/src/LuaCpu.hpp b/native/projects/lcpu/src/LuaCpu.hpp index 10729b7..e39835a 100644 --- a/native/projects/lcpu/src/LuaCpu.hpp +++ b/native/projects/lcpu/src/LuaCpu.hpp @@ -1,20 +1,20 @@ -#pragma once +#pragma once #include -#include "LuaHelpers.hpp" +#include "LuaObject.hpp" /// Bindings of [riscv::System] to Lua. -struct LuaCpu { +struct LuaCpu : public lcpu::lua::LuaObject { /// Lua binding stuff - static void Bind(GarrysMod::Lua::ILuaBase* LUA); - static void Create(GarrysMod::Lua::ILuaBase* LUA, u32 memorySize); - - private: + constexpr static const char* Name() { return "LuaCpu"; } + static void RegisterClass(GarrysMod::Lua::ILuaBase* LUA); + protected: + friend struct lcpu::lua::LuaObject; LuaCpu(u32 memorySize); ~LuaCpu(); - + private: LUA_MEMBER_FUNCTION(PoweredOn); // Check if the CPU is powered on LUA_MEMBER_FUNCTION(Cycle); // do a single cycle (called internally by LCPU entity) LUA_MEMBER_FUNCTION(PowerOff); // power off and reset the LCPU