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

97 lines
2.5 KiB
C++
Raw Normal View History

#include "LuaDevice.hpp"
2023-07-27 16:30:59 -04:00
void LuaDevice::RegisterClass(GarrysMod::Lua::ILuaBase* LUA) {
RegisterClassStart(LUA);
RegisterGetter("Base", [](GarrysMod::Lua::ILuaBase* LUA) {
auto self = LuaDevice::FromLua(LUA, 1);
LUA->PushNumber(static_cast<double>(self->base));
});
RegisterGetter("Size", [](GarrysMod::Lua::ILuaBase* LUA) {
auto self = LuaDevice::FromLua(LUA, 1);
LUA->PushNumber(static_cast<double>(self->size));
});
}
2023-07-25 06:46:52 -04:00
bool LuaDevice::Clocked() const {
// no real good rationale for checking here,
// since function calling does bail out properly
2023-07-25 06:46:52 -04:00
return true;
}
void LuaDevice::Clock() {
// clang-format off
2023-07-27 16:30:59 -04:00
lua->ReferencePush(GetTableReference());
lua->GetField(-1,"Clock");
if(lua->GetType(-1) == GarrysMod::Lua::Type::Function) {
lua->Push(-2); // 'self' argument
lua->Call(1, 0);
} else {
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop the Clock function off the stack
}
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop the reference
// clang-format off
2023-07-25 06:46:52 -04:00
}
riscv::Address LuaDevice::Base() const {
return base;
}
riscv::Address LuaDevice::Size() const {
return base;
}
u32 LuaDevice::Peek(riscv::Address address) {
// clang-format off
2023-07-27 16:30:59 -04:00
lua->ReferencePush(GetTableReference());
lua->GetField(-1,"Peek");
if(lua->GetType(-1) == GarrysMod::Lua::Type::Function) {
lua->Push(-2); // 'self' argument
lua->PushNumber(static_cast<double>(address));
lua->Call(2, 1);
auto result = static_cast<u32>(lua->GetNumber(-1));
lua->Pop(2); // pop result and the table off
return result;
} else {
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop whatever Peek is off the stack
}
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop the table reference
// clang-format on
return 0xffffffff;
2023-07-25 06:46:52 -04:00
}
void LuaDevice::Poke(riscv::Address address, u32 value) {
// clang-format off
2023-07-27 16:30:59 -04:00
lua->ReferencePush(GetTableReference());
lua->GetField(-1,"Poke");
if(lua->GetType(-1) == GarrysMod::Lua::Type::Function) {
lua->Push(-2); // 'self' argument
lua->PushNumber(static_cast<double>(address));
lua->PushNumber(static_cast<double>(value));
lua->Call(3, 0);
} else {
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop whatever Peek is
}
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop the table reference
// clang-format on
2023-07-25 06:46:52 -04:00
}
void LuaDevice::Reset() {
// clang-format off
2023-07-27 16:30:59 -04:00
lua->ReferencePush(GetTableReference());
lua->GetField(-1,"Reset");
if(lua->GetType(-1) == GarrysMod::Lua::Type::Function) {
lua->Push(-2); // 'self' argument
lua->Call(1, 0);
} else {
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop whatever reset is
}
2023-07-27 16:30:59 -04:00
lua->Pop(); // pop the reference
// clang-format on
}
LuaDevice::LuaDevice(riscv::Address base, riscv::Address size) : base(base), size(size) {
2023-07-25 06:46:52 -04:00
}