From 75d5124265f01613858dcc98af059ad4f91703d4 Mon Sep 17 00:00:00 2001 From: Nav Date: Sat, 9 Mar 2024 17:16:29 +0000 Subject: [PATCH] Moved `TargetRegisterDescriptor` struct to separate file --- .../AvrGdb/CommandPackets/ReadRegister.cpp | 3 +- .../AvrGdb/CommandPackets/ReadRegisters.cpp | 1 + .../AvrGdb/CommandPackets/WriteRegister.cpp | 1 + .../AvrGdb/CommandPackets/WriteRegister.hpp | 1 - src/DebugServer/Gdb/TargetDescriptor.hpp | 1 + src/DebugToolDrivers/DebugTool.hpp | 2 +- .../Protocols/EDBG/AVR/EdbgAvr8Interface.hpp | 1 + .../Microchip/AVR/AVR8/Avr8DebugInterface.hpp | 1 + src/Insight/InsightSignals.hpp | 2 +- .../Tasks/ReadTargetRegisters.hpp | 1 + .../RegisterHistoryWidget.hpp | 1 + .../TargetRegisterInspectorWindow.hpp | 2 +- .../TargetRegistersPane/RegisterGroupItem.hpp | 2 +- .../TargetRegistersPane/RegisterItem.hpp | 2 +- .../TargetWidgets/TargetPackageWidget.hpp | 1 - src/Services/TargetControllerService.hpp | 1 + .../Commands/ReadTargetRegisters.hpp | 2 +- .../TargetControllerComponent.hpp | 2 +- src/Targets/CMakeLists.txt | 2 +- src/Targets/Microchip/AVR/AVR8/Avr8.cpp | 1 - src/Targets/Microchip/AVR/AVR8/Avr8.hpp | 2 +- .../TargetDescriptionFile.hpp | 2 +- src/Targets/RiscV/RiscVRegisterDescriptor.hpp | 2 +- src/Targets/Target.hpp | 1 + src/Targets/TargetDescriptor.hpp | 2 +- src/Targets/TargetRegister.hpp | 113 +--------------- ...ister.cpp => TargetRegisterDescriptor.cpp} | 2 +- src/Targets/TargetRegisterDescriptor.hpp | 125 ++++++++++++++++++ 28 files changed, 149 insertions(+), 130 deletions(-) rename src/Targets/{TargetRegister.cpp => TargetRegisterDescriptor.cpp} (91%) create mode 100644 src/Targets/TargetRegisterDescriptor.hpp diff --git a/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegister.cpp b/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegister.cpp index fdd16616..a624d5c2 100644 --- a/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegister.cpp +++ b/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegister.cpp @@ -3,7 +3,7 @@ #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" #include "src/DebugServer/Gdb/AvrGdb/TargetDescriptor.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Services/StringService.hpp" #include "src/Logger/Logger.hpp" @@ -14,7 +14,6 @@ namespace DebugServer::Gdb::AvrGdb::CommandPackets { using Services::TargetControllerService; - using Targets::TargetRegister; using Targets::TargetRegisterDescriptors; using ResponsePackets::ResponsePacket; diff --git a/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegisters.cpp b/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegisters.cpp index 5479c2fe..c1b2c761 100644 --- a/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegisters.cpp +++ b/src/DebugServer/Gdb/AvrGdb/CommandPackets/ReadRegisters.cpp @@ -3,6 +3,7 @@ #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" #include "src/DebugServer/Gdb/AvrGdb/TargetDescriptor.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" #include "src/Targets/TargetMemory.hpp" diff --git a/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.cpp b/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.cpp index afa4a8bf..fe7f1f01 100644 --- a/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.cpp +++ b/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.cpp @@ -4,6 +4,7 @@ #include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" #include "src/DebugServer/Gdb/AvrGdb/TargetDescriptor.hpp" +#include "src/Targets/TargetRegister.hpp" #include "src/Logger/Logger.hpp" #include "src/Exceptions/Exception.hpp" diff --git a/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.hpp b/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.hpp index 9ef7a489..51dda24f 100644 --- a/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.hpp +++ b/src/DebugServer/Gdb/AvrGdb/CommandPackets/WriteRegister.hpp @@ -4,7 +4,6 @@ #include "src/DebugServer/Gdb/RegisterDescriptor.hpp" -#include "src/Targets/TargetRegister.hpp" #include "src/Targets/TargetMemory.hpp" namespace DebugServer::Gdb::AvrGdb::CommandPackets diff --git a/src/DebugServer/Gdb/TargetDescriptor.hpp b/src/DebugServer/Gdb/TargetDescriptor.hpp index fa8ddfe7..3d927682 100644 --- a/src/DebugServer/Gdb/TargetDescriptor.hpp +++ b/src/DebugServer/Gdb/TargetDescriptor.hpp @@ -8,6 +8,7 @@ #include "src/Helpers/BiMap.hpp" #include "src/Targets/TargetDescriptor.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" #include "src/Targets/TargetMemory.hpp" diff --git a/src/DebugToolDrivers/DebugTool.hpp b/src/DebugToolDrivers/DebugTool.hpp index cead69ee..988a94d5 100644 --- a/src/DebugToolDrivers/DebugTool.hpp +++ b/src/DebugToolDrivers/DebugTool.hpp @@ -11,7 +11,7 @@ #include "TargetInterfaces/RiscV/RiscVDebugInterface.hpp" #include "TargetInterfaces/RiscV/RiscVProgramInterface.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" /** * A debug tool can be any device that provides access to the connected target. Debug tools are usually connected diff --git a/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.hpp b/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.hpp index 422466ea..62294b11 100644 --- a/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.hpp +++ b/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.hpp @@ -13,6 +13,7 @@ #include "src/Targets/TargetPhysicalInterface.hpp" #include "src/Targets/TargetMemory.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" #include "src/Targets/Microchip/AVR/AVR8/Family.hpp" #include "src/Targets/Microchip/AVR/AVR8/TargetParameters.hpp" diff --git a/src/DebugToolDrivers/TargetInterfaces/Microchip/AVR/AVR8/Avr8DebugInterface.hpp b/src/DebugToolDrivers/TargetInterfaces/Microchip/AVR/AVR8/Avr8DebugInterface.hpp index 65c73209..b11a0385 100644 --- a/src/DebugToolDrivers/TargetInterfaces/Microchip/AVR/AVR8/Avr8DebugInterface.hpp +++ b/src/DebugToolDrivers/TargetInterfaces/Microchip/AVR/AVR8/Avr8DebugInterface.hpp @@ -12,6 +12,7 @@ #include "src/Targets/Microchip/AVR/AVR8/TargetParameters.hpp" #include "src/Targets/TargetState.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" #include "src/Targets/TargetMemory.hpp" diff --git a/src/Insight/InsightSignals.hpp b/src/Insight/InsightSignals.hpp index a97b68f1..749800ba 100644 --- a/src/Insight/InsightSignals.hpp +++ b/src/Insight/InsightSignals.hpp @@ -6,7 +6,7 @@ #include "src/Targets/TargetState.hpp" #include "src/Targets/TargetDescriptor.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "InsightWorker/Tasks/InsightWorkerTask.hpp" diff --git a/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp b/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp index c1bdb132..aef9142b 100644 --- a/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp +++ b/src/Insight/InsightWorker/Tasks/ReadTargetRegisters.hpp @@ -1,6 +1,7 @@ #pragma once #include "InsightWorkerTask.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" class ReadTargetRegisters: public InsightWorkerTask diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/RegisterHistoryWidget/RegisterHistoryWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/RegisterHistoryWidget/RegisterHistoryWidget.hpp index 8e561724..a8e031d0 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/RegisterHistoryWidget/RegisterHistoryWidget.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/RegisterHistoryWidget/RegisterHistoryWidget.hpp @@ -8,6 +8,7 @@ #include #include +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" #include "src/Targets/TargetMemory.hpp" #include "src/Targets/TargetState.hpp" diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/TargetRegisterInspectorWindow.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/TargetRegisterInspectorWindow.hpp index 44fc8de7..967a1b47 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/TargetRegisterInspectorWindow.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegisterInspector/TargetRegisterInspectorWindow.hpp @@ -10,7 +10,7 @@ #include #include -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetState.hpp" #include "src/Insight/UserInterfaces/InsightWindow/Widgets/Label.hpp" diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterGroupItem.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterGroupItem.hpp index e49a7f6f..b3f2e520 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterGroupItem.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterGroupItem.hpp @@ -9,7 +9,7 @@ #include "src/Insight/UserInterfaces/InsightWindow/Widgets/ListView/ListItem.hpp" #include "RegisterItem.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" namespace Widgets { diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterItem.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterItem.hpp index 74d58c98..54fa762c 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterItem.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/RegisterItem.hpp @@ -6,7 +6,7 @@ #include "src/Insight/UserInterfaces/InsightWindow/Widgets/ListView/ListItem.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" namespace Widgets { diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.hpp index 3ff86137..defcda0f 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetWidgets/TargetPackageWidget.hpp @@ -9,7 +9,6 @@ #include "TargetPinWidget.hpp" #include "src/Targets/TargetVariant.hpp" #include "src/Targets/TargetState.hpp" -#include "src/Targets/TargetRegister.hpp" namespace Widgets::InsightTargetWidgets { diff --git a/src/Services/TargetControllerService.hpp b/src/Services/TargetControllerService.hpp index 123e65ff..a150c787 100644 --- a/src/Services/TargetControllerService.hpp +++ b/src/Services/TargetControllerService.hpp @@ -9,6 +9,7 @@ #include "src/TargetController/AtomicSession.hpp" #include "src/Targets/TargetState.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetRegister.hpp" #include "src/Targets/TargetMemory.hpp" #include "src/Targets/TargetBreakpoint.hpp" diff --git a/src/TargetController/Commands/ReadTargetRegisters.hpp b/src/TargetController/Commands/ReadTargetRegisters.hpp index 25ca5224..a8df6793 100644 --- a/src/TargetController/Commands/ReadTargetRegisters.hpp +++ b/src/TargetController/Commands/ReadTargetRegisters.hpp @@ -3,7 +3,7 @@ #include "Command.hpp" #include "src/TargetController/Responses/TargetRegistersRead.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" namespace TargetController::Commands { diff --git a/src/TargetController/TargetControllerComponent.hpp b/src/TargetController/TargetControllerComponent.hpp index b2b36855..a5286923 100644 --- a/src/TargetController/TargetControllerComponent.hpp +++ b/src/TargetController/TargetControllerComponent.hpp @@ -61,7 +61,7 @@ #include "src/Targets/BriefTargetDescriptor.hpp" #include "src/Targets/Target.hpp" #include "src/Targets/Targets.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetMemory.hpp" #include "src/Targets/TargetMemoryCache.hpp" diff --git a/src/Targets/CMakeLists.txt b/src/Targets/CMakeLists.txt index 4d8cea1a..a818e82d 100755 --- a/src/Targets/CMakeLists.txt +++ b/src/Targets/CMakeLists.txt @@ -2,7 +2,7 @@ target_sources( Bloom PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/TargetDescription/TargetDescriptionFile.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/TargetRegister.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/TargetRegisterDescriptor.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TargetMemoryCache.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TargetPhysicalInterface.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Microchip/AVR/AVR8/Avr8.cpp diff --git a/src/Targets/Microchip/AVR/AVR8/Avr8.cpp b/src/Targets/Microchip/AVR/AVR8/Avr8.cpp index 71d6f902..686ab38b 100644 --- a/src/Targets/Microchip/AVR/AVR8/Avr8.cpp +++ b/src/Targets/Microchip/AVR/AVR8/Avr8.cpp @@ -12,7 +12,6 @@ #include "src/Exceptions/InvalidConfig.hpp" #include "Exceptions/DebugWirePhysicalInterfaceError.hpp" -#include "src/Targets/TargetRegister.hpp" namespace Targets::Microchip::Avr::Avr8Bit { diff --git a/src/Targets/Microchip/AVR/AVR8/Avr8.hpp b/src/Targets/Microchip/AVR/AVR8/Avr8.hpp index 3fec75a2..b8b5cb31 100644 --- a/src/Targets/Microchip/AVR/AVR8/Avr8.hpp +++ b/src/Targets/Microchip/AVR/AVR8/Avr8.hpp @@ -19,7 +19,7 @@ #include "src/Targets/Microchip/AVR/Fuse.hpp" #include "src/Targets/TargetPhysicalInterface.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/TargetBreakpoint.hpp" #include "TargetDescription/TargetDescriptionFile.hpp" diff --git a/src/Targets/Microchip/AVR/AVR8/TargetDescription/TargetDescriptionFile.hpp b/src/Targets/Microchip/AVR/AVR8/TargetDescription/TargetDescriptionFile.hpp index 4a03977a..5a371504 100644 --- a/src/Targets/Microchip/AVR/AVR8/TargetDescription/TargetDescriptionFile.hpp +++ b/src/Targets/Microchip/AVR/AVR8/TargetDescription/TargetDescriptionFile.hpp @@ -6,7 +6,7 @@ #include "src/Targets/TargetDescription/TargetDescriptionFile.hpp" #include "src/Targets/TargetVariant.hpp" -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "src/Targets/Microchip/AVR/TargetSignature.hpp" #include "src/Targets/Microchip/AVR/IspParameters.hpp" diff --git a/src/Targets/RiscV/RiscVRegisterDescriptor.hpp b/src/Targets/RiscV/RiscVRegisterDescriptor.hpp index 23f2fcec..e5f61044 100644 --- a/src/Targets/RiscV/RiscVRegisterDescriptor.hpp +++ b/src/Targets/RiscV/RiscVRegisterDescriptor.hpp @@ -2,7 +2,7 @@ #include -#include "src/Targets/TargetRegister.hpp" +#include "src/Targets/TargetRegisterDescriptor.hpp" #include "RiscVGeneric.hpp" diff --git a/src/Targets/Target.hpp b/src/Targets/Target.hpp index 290c4a38..1dd8d1f7 100644 --- a/src/Targets/Target.hpp +++ b/src/Targets/Target.hpp @@ -11,6 +11,7 @@ #include "TargetDescriptor.hpp" #include "TargetState.hpp" +#include "TargetRegisterDescriptor.hpp" #include "TargetRegister.hpp" #include "TargetMemory.hpp" #include "TargetBreakpoint.hpp" diff --git a/src/Targets/TargetDescriptor.hpp b/src/Targets/TargetDescriptor.hpp index e8700258..a6c43b1d 100644 --- a/src/Targets/TargetDescriptor.hpp +++ b/src/Targets/TargetDescriptor.hpp @@ -9,7 +9,7 @@ #include "TargetFamily.hpp" #include "TargetMemory.hpp" -#include "TargetRegister.hpp" +#include "TargetRegisterDescriptor.hpp" #include "TargetVariant.hpp" #include "TargetBreakpoint.hpp" diff --git a/src/Targets/TargetRegister.hpp b/src/Targets/TargetRegister.hpp index eb45a059..0d9d7028 100644 --- a/src/Targets/TargetRegister.hpp +++ b/src/Targets/TargetRegister.hpp @@ -1,105 +1,14 @@ #pragma once #include -#include -#include -#include #include #include -#include -#include +#include "TargetRegisterDescriptor.hpp" #include "TargetMemory.hpp" namespace Targets { - using TargetRegisterDescriptorId = std::uint32_t; - using TargetRegisterDescriptorIds = std::set; - - enum class TargetRegisterType: std::uint8_t - { - GENERAL_PURPOSE_REGISTER, - PROGRAM_COUNTER, - STACK_POINTER, - STATUS_REGISTER, - PORT_REGISTER, - OTHER, - }; - - struct TargetRegisterAccess - { - bool readable = false; - bool writable = false; - - TargetRegisterAccess( - bool readable, - bool writable - ) - : readable(readable) - , writable(writable) - {} - }; - - struct TargetRegisterDescriptor - { - public: - TargetRegisterDescriptorId id; - TargetRegisterType type; - std::optional startAddress; - TargetMemorySize size; - TargetMemoryType memoryType; - - std::optional name; - std::optional groupName; - std::optional description; - - TargetRegisterAccess access; - - TargetRegisterDescriptor( - TargetRegisterType type, - std::optional startAddress, - TargetMemorySize size, - TargetMemoryType memoryType, - std::optional name, - std::optional groupName, - std::optional description, - TargetRegisterAccess access - ) - : id(++(TargetRegisterDescriptor::lastRegisterDescriptorId)) - , type(type) - , startAddress(startAddress) - , size(size) - , memoryType(memoryType) - , name(name) - , groupName(groupName) - , description(description) - , access(access) - {}; - - bool operator == (const TargetRegisterDescriptor& other) const { - return this->getHash() == other.getHash(); - } - - bool operator < (const TargetRegisterDescriptor& other) const { - if (this->type == other.type) { - return this->startAddress.value_or(0) < other.startAddress.value_or(0); - } - - /* - * If the registers are of different type, there is no meaningful way to sort them, so we just use - * the unique hash. - */ - return this->getHash() < other.getHash(); - } - - private: - mutable std::optional cachedHash; - static inline std::atomic lastRegisterDescriptorId = 0; - std::size_t getHash() const; - - friend std::hash; - }; - struct TargetRegister { TargetRegisterDescriptorId descriptorId; @@ -120,24 +29,4 @@ namespace Targets }; using TargetRegisters = std::vector; - using TargetRegisterDescriptors = std::set; - using TargetRegisterDescriptorMapping = std::map; -} - -namespace std -{ - /** - * Hashing function for TargetRegisterDescriptor type. - * - * This is required in order to use TargetRegisterDescriptor as a key in an std::unordered_map (see the BiMap - * class) - */ - template<> - class hash - { - public: - std::size_t operator()(const Targets::TargetRegisterDescriptor& descriptor) const { - return descriptor.getHash(); - } - }; } diff --git a/src/Targets/TargetRegister.cpp b/src/Targets/TargetRegisterDescriptor.cpp similarity index 91% rename from src/Targets/TargetRegister.cpp rename to src/Targets/TargetRegisterDescriptor.cpp index 15107714..ee3ddb47 100644 --- a/src/Targets/TargetRegister.cpp +++ b/src/Targets/TargetRegisterDescriptor.cpp @@ -1,4 +1,4 @@ -#include "TargetRegister.hpp" +#include "TargetRegisterDescriptor.hpp" namespace Targets { diff --git a/src/Targets/TargetRegisterDescriptor.hpp b/src/Targets/TargetRegisterDescriptor.hpp new file mode 100644 index 00000000..c656c72b --- /dev/null +++ b/src/Targets/TargetRegisterDescriptor.hpp @@ -0,0 +1,125 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include "TargetMemory.hpp" + +namespace Targets +{ + using TargetRegisterDescriptorId = std::uint32_t; + using TargetRegisterDescriptorIds = std::set; + + enum class TargetRegisterType: std::uint8_t + { + GENERAL_PURPOSE_REGISTER, + PROGRAM_COUNTER, + STACK_POINTER, + STATUS_REGISTER, + PORT_REGISTER, + OTHER, + }; + + struct TargetRegisterAccess + { + bool readable = false; + bool writable = false; + + TargetRegisterAccess( + bool readable, + bool writable + ) + : readable(readable) + , writable(writable) + {} + }; + + struct TargetRegisterDescriptor + { + public: + TargetRegisterDescriptorId id; + TargetRegisterType type; + std::optional startAddress; + TargetMemorySize size; + TargetMemoryType memoryType; + + std::optional name; + std::optional groupName; + std::optional description; + + TargetRegisterAccess access; + + TargetRegisterDescriptor( + TargetRegisterType type, + std::optional startAddress, + TargetMemorySize size, + TargetMemoryType memoryType, + std::optional name, + std::optional groupName, + std::optional description, + TargetRegisterAccess access + ) + : id(++(TargetRegisterDescriptor::lastRegisterDescriptorId)) + , type(type) + , startAddress(startAddress) + , size(size) + , memoryType(memoryType) + , name(name) + , groupName(groupName) + , description(description) + , access(access) + {}; + + bool operator == (const TargetRegisterDescriptor& other) const { + return this->getHash() == other.getHash(); + } + + bool operator < (const TargetRegisterDescriptor& other) const { + if (this->type == other.type) { + return this->startAddress.has_value() && other.startAddress.has_value() + ? this->startAddress < other.startAddress + : this->name < other.name + ; + } + + /* + * If the registers are of different type, there is no meaningful way to sort them, so we just use + * the unique hash. + */ + return this->getHash() < other.getHash(); + } + + private: + mutable std::optional cachedHash; + static inline std::atomic lastRegisterDescriptorId = 0; + std::size_t getHash() const; + + friend std::hash; + }; + + using TargetRegisterDescriptors = std::set; + using TargetRegisterDescriptorMapping = std::map; +} + +namespace std +{ + /** + * Hashing function for TargetRegisterDescriptor type. + * + * This is required in order to use TargetRegisterDescriptor as a key in an std::unordered_map (see the BiMap + * class) + */ + template<> + class hash + { + public: + std::size_t operator()(const Targets::TargetRegisterDescriptor& descriptor) const { + return descriptor.getHash(); + } + }; +}