gmod-lcpu/native/projects/lcpu/src/LuaCpu.cpp

91 lines
1.9 KiB
C++
Raw Normal View History

2023-07-24 06:50:18 -04:00
#include "LuaCpu.hpp"
#include <lucore/Logger.hpp>
#include "LuaDevice.hpp"
2023-07-24 06:50:18 -04:00
2023-07-27 16:55:29 -04:00
namespace lcpu {
LUA_CLASS_FUNCTION(LuaCpu, PoweredOn) {
2023-07-27 16:55:29 -04:00
auto self = LuaCpu::FromLua(LUA, 1);
LUA->PushBool(self->poweredOn);
return 1;
}
2023-07-24 06:50:18 -04:00
LUA_CLASS_FUNCTION(LuaCpu, Cycle) {
2023-07-27 16:55:29 -04:00
auto self = LuaCpu::FromLua(LUA, 1);
if(!self->poweredOn)
return 0;
self->system->Step();
2023-07-25 06:46:52 -04:00
return 0;
2023-07-27 16:55:29 -04:00
}
2023-07-24 06:50:18 -04:00
LUA_CLASS_FUNCTION(LuaCpu, PowerOff) {
2023-07-27 16:55:29 -04:00
auto self = LuaCpu::FromLua(LUA, 1);
if(!self->poweredOn)
return 0;
self->poweredOn = false;
2023-07-25 06:46:52 -04:00
return 0;
2023-07-27 16:55:29 -04:00
}
2023-07-24 06:50:18 -04:00
LUA_CLASS_FUNCTION(LuaCpu, PowerOn) {
2023-07-27 16:55:29 -04:00
auto self = LuaCpu::FromLua(LUA, 1);
if(self->poweredOn)
return 0;
2023-07-24 06:50:18 -04:00
2023-07-27 16:55:29 -04:00
self->poweredOn = true;
self->system->bus->Reset();
2023-07-25 06:46:52 -04:00
return 0;
2023-07-27 16:55:29 -04:00
}
LUA_CLASS_FUNCTION(LuaCpu, Reset) {
2023-07-27 16:55:29 -04:00
auto self = LuaCpu::FromLua(LUA, 1);
self->system->bus->Reset();
return 0;
}
LUA_CLASS_FUNCTION(LuaCpu, AttachDevice) {
2023-07-27 16:55:29 -04:00
auto self = LuaCpu::FromLua(LUA, 1);
auto device = LuaDevice::FromLua(LUA, 2);
// Attach it
LUA->PushBool(self->system->bus->AttachDevice(static_cast<riscv::Bus::Device*>(device)));
return 1;
}
void LuaCpu::RegisterClass(GarrysMod::Lua::ILuaBase* LUA) {
RegisterClassStart(LUA);
RegisterMethod("PoweredOn", PoweredOn);
RegisterMethod("Cycle", Cycle);
RegisterMethod("PowerOff", PowerOff);
RegisterMethod("PowerOn", PowerOn);
RegisterMethod("Reset", Reset);
RegisterMethod("AttachDevice", AttachDevice);
}
LuaCpu::LuaCpu(u32 memorySize) {
poweredOn = true;
system = riscv::System::Create(memorySize);
system->OnPowerOff = [&]() {
poweredOn = false;
};
// lame test code. this WILL be removed, I just want this for a quick test
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);
}
}
2023-07-25 06:46:52 -04:00
2023-07-27 16:55:29 -04:00
LuaCpu::~LuaCpu() {
delete system;
2023-07-24 06:50:18 -04:00
}
2023-07-27 16:55:29 -04:00
} // namespace lcpu