lcpu: Move LuaCpu to LuaObject

This commit is contained in:
Lily Tsuru 2023-07-27 16:36:08 -04:00
parent deadeb5c51
commit b5595cc996
3 changed files with 36 additions and 47 deletions

View File

@ -65,7 +65,7 @@ LUA_FUNCTION(LCPUNative_CreateDevice) {
} }
void GlobalsBind(GarrysMod::Lua::ILuaBase* LUA) { void GlobalsBind(GarrysMod::Lua::ILuaBase* LUA) {
LuaCpu::Bind(LUA); LuaCpu::RegisterClass(LUA);
LuaDevice::RegisterClass(LUA); LuaDevice::RegisterClass(LUA);
TestLuaObject::RegisterClass(LUA); TestLuaObject::RegisterClass(LUA);

View File

@ -3,7 +3,6 @@
#include <lucore/Logger.hpp> #include <lucore/Logger.hpp>
#include "LuaDevice.hpp" #include "LuaDevice.hpp"
#include "LuaHelpers.hpp"
// this is temporary from the thing // 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) { LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PoweredOn) {
auto self = LUA_CLASS_GET(LuaCpu)(1); auto self = LuaCpu::FromLua(LUA, 1);
LUA->PushBool(self->poweredOn); LUA->PushBool(self->poweredOn);
return 1; return 1;
} }
LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Cycle) { LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Cycle) {
auto self = LUA_CLASS_GET(LuaCpu)(1); auto self = LuaCpu::FromLua(LUA, 1);
if(!self->poweredOn) if(!self->poweredOn)
return 0; return 0;
self->system->Step(); self->system->Step();
@ -48,7 +45,7 @@ LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Cycle) {
} }
LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOff) { LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOff) {
auto self = LUA_CLASS_GET(LuaCpu)(1); auto self = LuaCpu::FromLua(LUA, 1);
if(!self->poweredOn) if(!self->poweredOn)
return 0; return 0;
@ -58,7 +55,7 @@ LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOff) {
} }
LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOn) { LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOn) {
auto self = LUA_CLASS_GET(LuaCpu)(1); auto self = LuaCpu::FromLua(LUA, 1);
if(self->poweredOn) if(self->poweredOn)
return 0; return 0;
@ -68,49 +65,29 @@ LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, PowerOn) {
} }
LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Reset) { LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, Reset) {
auto self = LUA_CLASS_GET(LuaCpu)(1); auto self = LuaCpu::FromLua(LUA, 1);
self->system->bus->Reset(); self->system->bus->Reset();
return 0; return 0;
} }
LUA_MEMBER_FUNCTION_IMPLEMENT(LuaCpu, AttachDevice) { 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); auto device = LuaDevice::FromLua(LUA, 2);
// Attach it // Attach it
LUA->PushBool(self->system->bus->AttachDevice(static_cast<riscv::Bus::Device*>(device))); LUA->PushBool(self->system->bus->AttachDevice(static_cast<riscv::Bus::Device*>(device)));
return 1; return 1;
} }
void LuaCpu::Bind(GarrysMod::Lua::ILuaBase* LUA) { void LuaCpu::RegisterClass(GarrysMod::Lua::ILuaBase* LUA) {
// clang-format off RegisterClassStart(LUA);
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::Create(GarrysMod::Lua::ILuaBase* LUA, u32 memorySize) { RegisterMethod("PoweredOn", PoweredOn);
auto cpuWrapper = new LuaCpu(memorySize); RegisterMethod("Cycle", Cycle);
RegisterMethod("PowerOff", PowerOff);
// lame test code. this WILL be removed, I just want this for a quick test RegisterMethod("PowerOn", PowerOn);
cpuWrapper->system->bus->AttachDevice(new SimpleUartDevice); RegisterMethod("Reset", Reset);
auto fp = std::fopen("/home/lily/test-gmod.bin", "rb"); RegisterMethod("AttachDevice", AttachDevice);
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);
} }
LuaCpu::LuaCpu(u32 memorySize) { LuaCpu::LuaCpu(u32 memorySize) {
@ -120,6 +97,18 @@ LuaCpu::LuaCpu(u32 memorySize) {
poweredOn = false; poweredOn = false;
system->bus->Reset(); 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() { LuaCpu::~LuaCpu() {

View File

@ -1,20 +1,20 @@
#pragma once #pragma once
#include <riscv/System.hpp> #include <riscv/System.hpp>
#include "LuaHelpers.hpp" #include "LuaObject.hpp"
/// Bindings of [riscv::System] to Lua. /// Bindings of [riscv::System] to Lua.
struct LuaCpu { struct LuaCpu : public lcpu::lua::LuaObject<LuaCpu> {
/// Lua binding stuff /// Lua binding stuff
static void Bind(GarrysMod::Lua::ILuaBase* LUA); constexpr static const char* Name() { return "LuaCpu"; }
static void Create(GarrysMod::Lua::ILuaBase* LUA, u32 memorySize); static void RegisterClass(GarrysMod::Lua::ILuaBase* LUA);
private:
protected:
friend struct lcpu::lua::LuaObject<LuaCpu>;
LuaCpu(u32 memorySize); LuaCpu(u32 memorySize);
~LuaCpu(); ~LuaCpu();
private:
LUA_MEMBER_FUNCTION(PoweredOn); // Check if the CPU is powered on 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(Cycle); // do a single cycle (called internally by LCPU entity)
LUA_MEMBER_FUNCTION(PowerOff); // power off and reset the LCPU LUA_MEMBER_FUNCTION(PowerOff); // power off and reset the LCPU