From 9d7ef71d3b3cecfa111b0ec18c96dba18a68769f Mon Sep 17 00:00:00 2001 From: modeco80 Date: Tue, 18 Jul 2023 02:05:39 -0400 Subject: [PATCH] RamDevice is public start skeleton gmod module --- .gitmodules | 4 + native/CMakeLists.txt | 2 +- .../projects/gmsv_lcpu_native/CMakeLists.txt | 3 - native/projects/gmsv_lcpu_native/NOTES.md | 4 - native/projects/lcpu/CMakeLists.txt | 45 ++++++++++ .../abi.ver => lcpu/gmod_abi.ver} | 0 native/projects/lcpu/src/main.cpp | 9 ++ native/projects/lcpu/third_party/gmod_headers | 1 + native/projects/lucore/src/Logger.cpp | 7 +- native/projects/riscv/CMakeLists.txt | 2 +- .../riscv/include/riscv/Devices/RamDevice.hpp | 41 +++++++++ .../projects/riscv/src/Devices/RamDevice.cpp | 53 ++++++++++++ native/projects/riscv/src/MemoryDevice.cpp | 84 ------------------- 13 files changed, 159 insertions(+), 96 deletions(-) create mode 100644 .gitmodules delete mode 100644 native/projects/gmsv_lcpu_native/CMakeLists.txt delete mode 100644 native/projects/gmsv_lcpu_native/NOTES.md create mode 100644 native/projects/lcpu/CMakeLists.txt rename native/projects/{gmsv_lcpu_native/abi.ver => lcpu/gmod_abi.ver} (100%) create mode 100644 native/projects/lcpu/src/main.cpp create mode 160000 native/projects/lcpu/third_party/gmod_headers create mode 100644 native/projects/riscv/include/riscv/Devices/RamDevice.hpp create mode 100644 native/projects/riscv/src/Devices/RamDevice.cpp delete mode 100644 native/projects/riscv/src/MemoryDevice.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5f832f3 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "native/projects/lcpu/third_party/gmod_headers"] + path = native/projects/lcpu/third_party/gmod_headers + url = https://github.com/Facepunch/gmod-module-base + branch = development diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 03c39f3..69dac9a 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -10,4 +10,4 @@ add_subdirectory(projects/lucore_test) add_subdirectory(projects/riscv) # Garry's Mod bindings -#add_subdirectory(projects/gmsv_lcpu) +add_subdirectory(projects/lcpu) diff --git a/native/projects/gmsv_lcpu_native/CMakeLists.txt b/native/projects/gmsv_lcpu_native/CMakeLists.txt deleted file mode 100644 index 31135de..0000000 --- a/native/projects/gmsv_lcpu_native/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ - - -add_library() diff --git a/native/projects/gmsv_lcpu_native/NOTES.md b/native/projects/gmsv_lcpu_native/NOTES.md deleted file mode 100644 index 4b436a9..0000000 --- a/native/projects/gmsv_lcpu_native/NOTES.md +++ /dev/null @@ -1,4 +0,0 @@ -# Notes - -- gmsv_lcpu doesn't use the upstream cmake buildsystem for gmod lua headers (instead crafting our own) - - this is because, in facepunchs infinite wisdom, they decided to unconditionally build the examples. diff --git a/native/projects/lcpu/CMakeLists.txt b/native/projects/lcpu/CMakeLists.txt new file mode 100644 index 0000000..bd509ce --- /dev/null +++ b/native/projects/lcpu/CMakeLists.txt @@ -0,0 +1,45 @@ + + +add_library(gmod_headers INTERFACE) +target_include_directories(gmod_headers INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/third_party/gmod_headers/include) + +# Originally from facepunch cmake build system, modified to be slightly less painful +function(set_gmod_suffix_prefix library) + set_target_properties(${library} PROPERTIES PREFIX "gmsv_") + if(APPLE) + if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") + set_target_properties(${library} PROPERTIES SUFFIX "_osx.dll") + else() + set_target_properties(${library} PROPERTIES SUFFIX "_osx64.dll") + endif() + elseif(UNIX) + if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") + set_target_properties(${library} PROPERTIES SUFFIX "_linux.dll") + else() + set_target_properties(${library} PROPERTIES SUFFIX "_linux64.dll") + endif() + elseif(WIN32) + if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "4") + set_target_properties(${library} PROPERTIES SUFFIX "_win32.dll") + else() + set_target_properties(${library} PROPERTIES SUFFIX "_win64.dll") + endif() + endif() +endfunction() + + +add_library(lcpu_native SHARED + src/main.cpp +) + +if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + target_link_options(lcpu_native PRIVATE "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/gmod_abi.ver") +endif() + +target_link_libraries(lcpu_native + gmod_headers + lucore::lucore + riscv::riscv +) + +set_gmod_suffix_prefix(lcpu_native) diff --git a/native/projects/gmsv_lcpu_native/abi.ver b/native/projects/lcpu/gmod_abi.ver similarity index 100% rename from native/projects/gmsv_lcpu_native/abi.ver rename to native/projects/lcpu/gmod_abi.ver diff --git a/native/projects/lcpu/src/main.cpp b/native/projects/lcpu/src/main.cpp new file mode 100644 index 0000000..28a660c --- /dev/null +++ b/native/projects/lcpu/src/main.cpp @@ -0,0 +1,9 @@ +#include + +GMOD_MODULE_OPEN() { + return 0; +} + +GMOD_MODULE_CLOSE() { + return 0; +} diff --git a/native/projects/lcpu/third_party/gmod_headers b/native/projects/lcpu/third_party/gmod_headers new file mode 160000 index 0000000..ad1795c --- /dev/null +++ b/native/projects/lcpu/third_party/gmod_headers @@ -0,0 +1 @@ +Subproject commit ad1795cf84ee715a0afc75db0cfed093a42f3cd9 diff --git a/native/projects/lucore/src/Logger.cpp b/native/projects/lucore/src/Logger.cpp index 86d2637..fee6d29 100644 --- a/native/projects/lucore/src/Logger.cpp +++ b/native/projects/lucore/src/Logger.cpp @@ -60,9 +60,10 @@ namespace lucore { private: std::FILE* file; }; - std::format_to(FileOutIterator(data.severity < Logger::MessageSeverity::Error ? stdout : stderr), "[Lucore/{}] [{}] {}\n", - Logger::SeverityToString(data.severity), data.time, - std::vformat(data.format, data.args)); + std::format_to( + FileOutIterator(data.severity < Logger::MessageSeverity::Error ? stdout : stderr), + "[Lucore/{}] [{}] {}\n", Logger::SeverityToString(data.severity), data.time, + std::vformat(data.format, data.args)); } void LoggerAttachStdout() { diff --git a/native/projects/riscv/CMakeLists.txt b/native/projects/riscv/CMakeLists.txt index 47ced39..12d2b19 100644 --- a/native/projects/riscv/CMakeLists.txt +++ b/native/projects/riscv/CMakeLists.txt @@ -7,7 +7,7 @@ project(riscv_emu add_library(riscv src/Bus.cpp - src/MemoryDevice.cpp + src/Devices/RamDevice.cpp ) target_compile_features(riscv PUBLIC cxx_std_20) diff --git a/native/projects/riscv/include/riscv/Devices/RamDevice.hpp b/native/projects/riscv/include/riscv/Devices/RamDevice.hpp new file mode 100644 index 0000000..6ba096c --- /dev/null +++ b/native/projects/riscv/include/riscv/Devices/RamDevice.hpp @@ -0,0 +1,41 @@ + +#include + +#include "riscv/Types.hpp" + +namespace riscv::devices { + + struct RamDevice : public Bus::Device { + RamDevice(AddressT size); + virtual ~RamDevice(); + + AddressT Size() const override; + + // Implementation of Device interface + + void Attached(Bus* bus, AddressT base) override; + + u8 PeekByte(AddressT address) override; + u16 PeekShort(AddressT address) override; + u32 PeekWord(AddressT address) override; + + void PokeByte(AddressT address, u8 value) override; + void PokeShort(AddressT address, u16 value) override; + void PokeWord(AddressT address, u32 value) override; + + private: + /// helper used for implementing stuff + template + constexpr usize AddressToIndex(AddressT address) { + return ((address - baseAddress) % memorySize) / sizeof(T); + } + + // remember what we were attached to via "signal" + Bus* attachedBus {}; + AddressT baseAddress {}; + + u8* memory {}; + usize memorySize {}; + }; + +} // namespace riscv::devices diff --git a/native/projects/riscv/src/Devices/RamDevice.cpp b/native/projects/riscv/src/Devices/RamDevice.cpp new file mode 100644 index 0000000..a749be2 --- /dev/null +++ b/native/projects/riscv/src/Devices/RamDevice.cpp @@ -0,0 +1,53 @@ +#include + +#include "riscv/Types.hpp" + +namespace riscv::devices { + + RamDevice::RamDevice(AddressT size) : Bus::Device(), memorySize(size) { + memory = new u8[size]; + LUCORE_CHECK(memory, "Could not allocate buffer for memory device with size 0x{:08x}.", + size); + } + + RamDevice::~RamDevice() { + if(memory) + delete[] memory; + } + + AddressT RamDevice::Size() const { + return memorySize; + } + + // Implementation of Device interface + + void RamDevice::Attached(Bus* bus, AddressT base) { + attachedBus = bus; + baseAddress = base; + } + + u8 RamDevice::PeekByte(AddressT address) { + return memory[AddressToIndex(address)]; + } + + u16 RamDevice::PeekShort(AddressT address) { + return std::bit_cast(memory)[AddressToIndex(address)]; + } + + u32 RamDevice::PeekWord(AddressT address) { + return std::bit_cast(memory)[AddressToIndex(address)]; + } + + void RamDevice::PokeByte(AddressT address, u8 value) { + memory[AddressToIndex(address)] = value; + } + + void RamDevice::PokeShort(AddressT address, u16 value) { + std::bit_cast(memory)[AddressToIndex(address)] = value; + } + + void RamDevice::PokeWord(AddressT address, u32 value) { + std::bit_cast(memory)[AddressToIndex(address)] = value; + } + +} // namespace riscv::devices diff --git a/native/projects/riscv/src/MemoryDevice.cpp b/native/projects/riscv/src/MemoryDevice.cpp deleted file mode 100644 index 8fefcf5..0000000 --- a/native/projects/riscv/src/MemoryDevice.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include - -namespace riscv { - - namespace { - - template - struct BasicMemoryDevice : public Bus::Device { - BasicMemoryDevice(usize size) : Bus::Device(), memorySize(size) { - memory = new u8[size]; - LUCORE_CHECK(memory, "Could not allocate buffer for memory device."); - } - - virtual ~BasicMemoryDevice() { - if(memory) - delete[] memory; - } - - AddressT Size() const override { - return memorySize; - } - - // Implementation of Device interface - - void Attached(Bus* bus, AddressT base) override { - attachedBus = bus; - baseAddress = base; - } - - u8 PeekByte(AddressT address) override { - return memory[AddressToIndex(address)]; - } - - u16 PeekShort(AddressT address) override { - return std::bit_cast(memory)[AddressToIndex(address)]; - } - - u32 PeekWord(AddressT address) override { - return std::bit_cast(memory)[AddressToIndex(address)]; - } - - void PokeByte(AddressT address, u8 value) override { - if constexpr(!Rom) { - memory[AddressToIndex(address)] = value; - } - } - - void PokeShort(AddressT address, u16 value) override { - if constexpr(!Rom) { - std::bit_cast(memory)[AddressToIndex(address)] = value; - } - } - - void PokeWord(AddressT address, u32 value) override { - if constexpr(!Rom) { - std::bit_cast(memory)[AddressToIndex(address)] = value; - } - } - - private: - /// helper used for implementing stuff - template - constexpr usize AddressToIndex(AddressT address) { - return ((address - baseAddress) % memorySize) / sizeof(T); - } - - // remember what we were attached to via "signal" - Bus* attachedBus {}; - AddressT baseAddress {}; - - u8* memory {}; - usize memorySize {}; - }; - - using RamDevice = BasicMemoryDevice; - using RomDevice = BasicMemoryDevice; - - } // namespace - - Bus::Device* NewRam(usize size) { - return new RamDevice(size); - } - -} // namespace riscv