Updated TDF address space, memory segment and memory segment section extraction to align with new TDF format

This commit is contained in:
Nav
2024-02-13 20:22:18 +00:00
parent 46d75b3f4b
commit f5677b6235
5 changed files with 316 additions and 139 deletions

View File

@@ -1,18 +1,58 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <string>
#include <optional>
#include <map>
#include "MemorySegment.hpp" #include "MemorySegment.hpp"
#include "src/Targets/TargetMemory.hpp"
namespace Targets::TargetDescription namespace Targets::TargetDescription
{ {
struct AddressSpace struct AddressSpace
{ {
std::string id; std::string key;
std::string name;
std::uint32_t startAddress; std::uint32_t startAddress;
std::uint32_t size; std::uint32_t size;
bool littleEndian = true; std::optional<TargetMemoryEndianness> endianness;
std::map<MemorySegmentType, std::map<std::string, MemorySegment>> memorySegmentsByTypeAndName; std::map<std::string, MemorySegment, std::less<void>> memorySegmentsByKey;
AddressSpace(
const std::string& key,
std::uint32_t startAddress,
std::uint32_t size,
const std::optional<TargetMemoryEndianness>& endianness,
const std::map<std::string, MemorySegment, std::less<void>>& memorySegmentsByKey
)
: key(key)
, startAddress(startAddress)
, size(size)
, endianness(endianness)
, memorySegmentsByKey(memorySegmentsByKey)
{}
std::optional<std::reference_wrapper<const MemorySegment>> tryGetMemorySegment(std::string_view key) const {
const auto segmentIt = this->memorySegmentsByKey.find(key);
if (segmentIt == this->memorySegmentsByKey.end()) {
return std::nullopt;
}
return std::cref(segmentIt->second);
}
const MemorySegment& getMemorySegment(std::string_view key) const {
const auto segment = this->tryGetMemorySegment(key);
if (!segment.has_value()) {
throw Exceptions::Exception(
"Failed to get memory segment \"" + std::string(key)
+ "\" from address space in TDF - segment not found"
);
}
return segment->get();
}
}; };
} }

View File

@@ -2,9 +2,11 @@
#include <cstdint> #include <cstdint>
#include <optional> #include <optional>
#include <QDomElement>
#include "src/Helpers/BiMap.hpp" #include "MemorySegmentSection.hpp"
#include "src/Services/StringService.hpp"
#include "src/Exceptions/Exception.hpp"
namespace Targets::TargetDescription namespace Targets::TargetDescription
{ {
@@ -19,34 +21,64 @@ namespace Targets::TargetDescription
RAM, RAM,
LOCKBITS, LOCKBITS,
OSCCAL, OSCCAL,
PRODUCTION_SIGNATURES,
SIGNATURES, SIGNATURES,
USER_SIGNATURES, USER_SIGNATURES,
}; };
struct MemorySegment struct MemorySegment
{ {
std::string key;
std::string name; std::string name;
MemorySegmentType type; MemorySegmentType type;
std::uint32_t startAddress; std::uint32_t startAddress;
std::uint32_t size; std::uint32_t size;
std::optional<std::uint16_t> pageSize; std::optional<std::uint16_t> pageSize;
std::map<std::string, MemorySegmentSection, std::less<void>> sectionsByKey;
/** MemorySegment(
* Mapping of all known memory segment types by their name. Any memory segments belonging to a type const std::string& key,
* not defined in here should be ignored. const std::string& name,
*/ MemorySegmentType type,
static const inline BiMap<std::string, MemorySegmentType> typesMappedByName = { std::uint32_t startAddress,
{"aliased", MemorySegmentType::ALIASED}, std::uint32_t size,
{"regs", MemorySegmentType::REGISTERS}, const std::optional<std::uint16_t>& pageSize,
{"eeprom", MemorySegmentType::EEPROM}, const std::map<std::string, MemorySegmentSection, std::less<void>>& sectionsByKey
{"flash", MemorySegmentType::FLASH}, )
{"fuses", MemorySegmentType::FUSES}, : key(key)
{"io", MemorySegmentType::IO}, , name(name)
{"ram", MemorySegmentType::RAM}, , type(type)
{"lockbits", MemorySegmentType::LOCKBITS}, , startAddress(startAddress)
{"osccal", MemorySegmentType::OSCCAL}, , size(size)
{"signatures", MemorySegmentType::SIGNATURES}, , pageSize(pageSize)
{"user_signatures", MemorySegmentType::USER_SIGNATURES}, , sectionsByKey(sectionsByKey)
}; {}
std::optional<std::reference_wrapper<const MemorySegmentSection>> tryGetSection(
std::string_view keyStr
) const {
const auto keys = Services::StringService::split(keyStr, '.');
const auto firstSubgroupIt = this->sectionsByKey.find(*keys.begin());
return firstSubgroupIt != this->sectionsByKey.end()
? keys.size() > 1
? firstSubgroupIt->second.tryGetSubSection(keys | std::ranges::views::drop(1))
: std::optional(std::cref(firstSubgroupIt->second))
: std::nullopt;
}
std::optional<std::reference_wrapper<const MemorySegmentSection>> getSection(
std::string_view keyStr
) const {
const auto propertyGroup = this->tryGetSection(keyStr);
if (!propertyGroup.has_value()) {
throw Exceptions::Exception(
"Failed to get memory segment section \"" + std::string(keyStr)
+ "\" from memory segment in TDF - section not found"
);
}
return propertyGroup->get();
}
}; };
} }

View File

@@ -0,0 +1,79 @@
#pragma once
#include <cstdint>
#include <string>
#include <map>
#include <optional>
#include <concepts>
#include <functional>
#include <ranges>
#include "src/Services/StringService.hpp"
#include "src/Exceptions/Exception.hpp"
namespace Targets::TargetDescription
{
struct MemorySegmentSection
{
std::string key;
std::string name;
std::uint32_t startAddress;
std::uint32_t size;
std::map<std::string, MemorySegmentSection, std::less<void>> subSectionsByKey;
MemorySegmentSection(
const std::string& key,
const std::string& name,
std::uint32_t startAddress,
std::uint32_t size,
const std::map<std::string, MemorySegmentSection, std::less<void>>& subSectionsByKey
)
: key(key)
, name(name)
, startAddress(startAddress)
, size(size)
, subSectionsByKey(subSectionsByKey)
{}
template <typename KeysType>
requires
std::ranges::sized_range<KeysType>
std::optional<std::reference_wrapper<const MemorySegmentSection>> tryGetSubSection(KeysType keys) const {
const auto firstSubSectionIt = this->subSectionsByKey.find(*(keys.begin()));
if (firstSubSectionIt == this->subSectionsByKey.end()) {
return std::nullopt;
}
auto subSection = std::optional(std::cref(firstSubSectionIt->second));
for (const auto key : keys | std::ranges::views::drop(1)) {
subSection = subSection->get().tryGetSubSection(key);
if (!subSection.has_value()) {
break;
}
}
return subSection;
}
std::optional<std::reference_wrapper<const MemorySegmentSection>> tryGetSubSection(
std::string_view keyStr
) const {
return this->tryGetSubSection(Services::StringService::split(keyStr, '.'));
}
std::optional<std::reference_wrapper<const MemorySegmentSection>> getSubSection(
std::string_view keyStr
) const {
const auto propertyGroup = this->tryGetSubSection(keyStr);
if (!propertyGroup.has_value()) {
throw Exceptions::Exception(
"Failed to get memory segment sub-section \"" + std::string(keyStr)
+ "\" from memory segment in TDF - sub-section not found"
);
}
return propertyGroup->get();
}
};
}

View File

@@ -3,11 +3,16 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonArray> #include <QJsonArray>
#include "Exceptions/TargetDescriptionParsingFailureException.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Services/PathService.hpp" #include "src/Services/PathService.hpp"
#include "src/Services/StringService.hpp" #include "src/Services/StringService.hpp"
#include "src/Targets/TargetMemory.hpp"
#include "src/Helpers/BiMap.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp"
#include "Exceptions/TargetDescriptionParsingFailureException.hpp"
namespace Targets::TargetDescription namespace Targets::TargetDescription
{ {
using namespace Exceptions; using namespace Exceptions;
@@ -70,6 +75,27 @@ namespace Targets::TargetDescription
return propertyGroup->get(); return propertyGroup->get();
} }
std::optional<std::reference_wrapper<const AddressSpace>> TargetDescriptionFile::tryGetAddressSpace(
std::string_view key
) const {
const auto addressSpaceIt = this->addressSpacesByKey.find(key);
return addressSpaceIt != this->addressSpacesByKey.end()
? std::optional(std::cref(addressSpaceIt->second))
: std::nullopt;
}
const AddressSpace & TargetDescriptionFile::getAddressSpace(std::string_view key) const {
const auto addressSpace = this->tryGetAddressSpace(key);
if (!addressSpace.has_value()) {
throw Exception(
"Failed to get address space \"" + std::string(key) + "\" from TDF - address space not found"
);
}
return addressSpace->get();
}
void TargetDescriptionFile::init(const std::string& xmlFilePath) { void TargetDescriptionFile::init(const std::string& xmlFilePath) {
auto file = QFile(QString::fromStdString(xmlFilePath)); auto file = QFile(QString::fromStdString(xmlFilePath));
if (!file.exists()) { if (!file.exists()) {
@@ -115,7 +141,17 @@ namespace Targets::TargetDescription
); );
} }
this->loadAddressSpaces(document); for (
auto element = deviceElement.firstChildElement("address-spaces").firstChildElement("address-space");
!element.isNull();
element = element.nextSiblingElement("address-space")
) {
auto addressSpace = TargetDescriptionFile::addressSpaceFromXml(element);
this->addressSpacesByKey.insert(
std::pair(addressSpace.key, std::move(addressSpace))
);
}
this->loadModules(document); this->loadModules(document);
this->loadPeripheralModules(document); this->loadPeripheralModules(document);
this->loadVariants(document); this->loadVariants(document);
@@ -176,113 +212,115 @@ namespace Targets::TargetDescription
); );
} }
AddressSpace TargetDescriptionFile::addressSpaceFromXml(const QDomElement& xmlElement) { AddressSpace TargetDescriptionFile::addressSpaceFromXml(const QDomElement &xmlElement) {
if ( static const auto endiannessByName = BiMap<std::string, TargetMemoryEndianness>({
!xmlElement.hasAttribute("id") {"big", TargetMemoryEndianness::BIG},
|| !xmlElement.hasAttribute("name") {"little", TargetMemoryEndianness::LITTLE},
|| !xmlElement.hasAttribute("size") });
|| !xmlElement.hasAttribute("start")
) { const auto endiannessName = TargetDescriptionFile::tryGetAttribute(xmlElement, "endianness");
throw Exception("Address space element missing id/name/size/start attributes.");
auto endianness = std::optional<TargetMemoryEndianness>();
if (endiannessName.has_value()) {
endianness = endiannessByName.valueAt(*endiannessName);
if (!endianness.has_value()) {
throw Exception(
"Failed to extract address space from TDF - invalid endianness name \"" + *endiannessName + "\""
);
}
} }
auto addressSpace = AddressSpace(); auto output = AddressSpace(
addressSpace.name = xmlElement.attribute("name").toStdString(); TargetDescriptionFile::getAttribute(xmlElement, "key"),
addressSpace.id = xmlElement.attribute("id").toStdString(); StringService::toUint32(TargetDescriptionFile::getAttribute(xmlElement, "start")),
StringService::toUint32(TargetDescriptionFile::getAttribute(xmlElement, "size")),
bool conversionStatus; endianness,
addressSpace.startAddress = xmlElement.attribute("start").toUInt(&conversionStatus, 16); {}
if (!conversionStatus) {
throw Exception("Failed to convert start address hex value to integer.");
}
addressSpace.size = xmlElement.attribute("size").toUInt(&conversionStatus, 16);
if (!conversionStatus) {
throw Exception("Failed to convert size hex value to integer.");
}
if (xmlElement.hasAttribute("endianness")) {
addressSpace.littleEndian = (xmlElement.attribute("endianness").toStdString() == "little");
}
// Create memory segment objects and add them to the mapping.
auto segmentNodes = xmlElement.elementsByTagName("memory-segment");
auto& memorySegments = addressSpace.memorySegmentsByTypeAndName;
for (int segmentIndex = 0; segmentIndex < segmentNodes.count(); segmentIndex++) {
try {
auto segment = TargetDescriptionFile::memorySegmentFromXml(
segmentNodes.item(segmentIndex).toElement()
); );
if (!memorySegments.contains(segment.type)) { for (
memorySegments.insert( auto element = xmlElement.firstChildElement("memory-segment");
std::pair< !element.isNull();
MemorySegmentType, element = element.nextSiblingElement("memory-segment")
decltype(addressSpace.memorySegmentsByTypeAndName)::mapped_type ) {
>(segment.type, {})); auto section = TargetDescriptionFile::memorySegmentFromXml(element);
output.memorySegmentsByKey.insert(std::pair(section.key, std::move(section)));
} }
memorySegments.find(segment.type)->second.insert(std::pair(segment.name, segment)); return output;
} catch (const Exception& exception) {
Logger::debug("Failed to extract memory segment from target description element - "
+ exception.getMessage());
}
}
return addressSpace;
} }
MemorySegment TargetDescriptionFile::memorySegmentFromXml(const QDomElement& xmlElement) { MemorySegment TargetDescriptionFile::memorySegmentFromXml(const QDomElement& xmlElement) {
if ( static const auto typesByName = BiMap<std::string, MemorySegmentType>({
!xmlElement.hasAttribute("type") {"aliased", MemorySegmentType::ALIASED},
|| !xmlElement.hasAttribute("name") {"regs", MemorySegmentType::REGISTERS},
|| !xmlElement.hasAttribute("size") {"eeprom", MemorySegmentType::EEPROM},
|| !xmlElement.hasAttribute("start") {"flash", MemorySegmentType::FLASH},
) { {"fuses", MemorySegmentType::FUSES},
throw Exception("Missing type/name/size/start attributes"); {"io", MemorySegmentType::IO},
} {"ram", MemorySegmentType::RAM},
{"lockbits", MemorySegmentType::LOCKBITS},
{"osccal", MemorySegmentType::OSCCAL},
{"production_signatures", MemorySegmentType::PRODUCTION_SIGNATURES},
{"signatures", MemorySegmentType::SIGNATURES},
{"user_signatures", MemorySegmentType::USER_SIGNATURES},
});
auto segment = MemorySegment(); const auto typeName = TargetDescriptionFile::getAttribute(xmlElement, "type");
auto typeName = xmlElement.attribute("type").toStdString();
auto type = MemorySegment::typesMappedByName.valueAt(typeName);
const auto type = typesByName.valueAt(typeName);
if (!type.has_value()) { if (!type.has_value()) {
throw Exception("Unknown type: \"" + typeName + "\""); throw Exception(
"Failed to extract memory segment from TDF - invalid memory segment type name \"" + typeName + "\""
);
} }
segment.type = type.value(); const auto pageSize = TargetDescriptionFile::tryGetAttribute(xmlElement, "page-size");
segment.name = xmlElement.attribute("name").toLower().toStdString();
bool conversionStatus = false; auto output = MemorySegment(
segment.startAddress = xmlElement.attribute("start").toUInt(&conversionStatus, 16); TargetDescriptionFile::getAttribute(xmlElement, "key"),
TargetDescriptionFile::getAttribute(xmlElement, "name"),
*type,
StringService::toUint32(TargetDescriptionFile::getAttribute(xmlElement, "start")),
StringService::toUint32(TargetDescriptionFile::getAttribute(xmlElement, "size")),
pageSize.has_value()
? std::optional(StringService::toUint16(*pageSize))
: std::nullopt,
{}
);
if (!conversionStatus) { for (
// Failed to convert startAddress hex value as string to uint16_t auto element = xmlElement.firstChildElement("section");
throw Exception("Invalid start address"); !element.isNull();
element = element.nextSiblingElement("section")
) {
auto section = TargetDescriptionFile::memorySegmentSectionFromXml(element);
output.sectionsByKey.insert(std::pair(section.key, std::move(section)));
} }
segment.size = xmlElement.attribute("size").toUInt(&conversionStatus, 16); return output;
if (!conversionStatus) {
// Failed to convert size hex value as string to uint16_t
throw Exception("Invalid size");
} }
if (xmlElement.hasAttribute("pagesize")) { MemorySegmentSection TargetDescriptionFile::memorySegmentSectionFromXml(const QDomElement& xmlElement) {
// The page size can be in single byte hexadecimal form ("0x01"), or it can be in plain integer form! auto output = MemorySegmentSection(
auto pageSize = xmlElement.attribute("pagesize"); TargetDescriptionFile::getAttribute(xmlElement, "key"),
segment.pageSize = pageSize.toUInt(&conversionStatus, pageSize.contains("0x") ? 16 : 10); TargetDescriptionFile::getAttribute(xmlElement, "name"),
StringService::toUint32(TargetDescriptionFile::getAttribute(xmlElement, "start")),
StringService::toUint32(TargetDescriptionFile::getAttribute(xmlElement, "size")),
{}
);
if (!conversionStatus) { for (
// Failed to convert size hex value as string to uint16_t auto element = xmlElement.firstChildElement("section");
throw Exception("Invalid size"); !element.isNull();
} element = element.nextSiblingElement("section")
) {
auto section = TargetDescriptionFile::memorySegmentSectionFromXml(element);
output.subSectionsByKey.insert(std::pair(section.key, std::move(section)));
} }
return segment; return output;
} }
RegisterGroup TargetDescriptionFile::registerGroupFromXml(const QDomElement& xmlElement) { RegisterGroup TargetDescriptionFile::registerGroupFromXml(const QDomElement& xmlElement) {
@@ -415,28 +453,6 @@ namespace Targets::TargetDescription
return attributeIt->second; return attributeIt->second;
} }
void TargetDescriptionFile::loadAddressSpaces(const QDomDocument& document) {
const auto deviceElement = document.elementsByTagName("device").item(0).toElement();
auto addressSpaceNodes = deviceElement.elementsByTagName("address-spaces").item(0).toElement()
.elementsByTagName("address-space");
for (int addressSpaceIndex = 0; addressSpaceIndex < addressSpaceNodes.count(); addressSpaceIndex++) {
try {
auto addressSpace = TargetDescriptionFile::addressSpaceFromXml(
addressSpaceNodes.item(addressSpaceIndex).toElement()
);
this->addressSpacesMappedById.insert(std::pair(addressSpace.id, addressSpace));
} catch (const Exception& exception) {
Logger::debug(
"Failed to extract address space from target description element - "
+ exception.getMessage()
);
}
}
}
void TargetDescriptionFile::loadModules(const QDomDocument& document) { void TargetDescriptionFile::loadModules(const QDomDocument& document) {
const auto deviceElement = document.elementsByTagName("device").item(0).toElement(); const auto deviceElement = document.elementsByTagName("device").item(0).toElement();

View File

@@ -12,6 +12,7 @@
#include "AddressSpace.hpp" #include "AddressSpace.hpp"
#include "MemorySegment.hpp" #include "MemorySegment.hpp"
#include "PropertyGroup.hpp" #include "PropertyGroup.hpp"
#include "MemorySegmentSection.hpp"
#include "RegisterGroup.hpp" #include "RegisterGroup.hpp"
#include "Module.hpp" #include "Module.hpp"
#include "Variant.hpp" #include "Variant.hpp"
@@ -92,9 +93,15 @@ namespace Targets::TargetDescription
std::string_view keyStr std::string_view keyStr
) const; ) const;
[[nodiscard]] const PropertyGroup& getPropertyGroup(std::string_view keyStr) const; [[nodiscard]] const PropertyGroup& getPropertyGroup(std::string_view keyStr) const;
[[nodiscard]] std::optional<std::reference_wrapper<const AddressSpace>> tryGetAddressSpace(
std::string_view key
) const;
[[nodiscard]] const AddressSpace& getAddressSpace(std::string_view key) const;
protected: protected:
std::map<std::string, std::string> deviceAttributesByName; std::map<std::string, std::string> deviceAttributesByName;
std::map<std::string, AddressSpace> addressSpacesMappedById; std::map<std::string, AddressSpace, std::less<void>> addressSpacesByKey;
std::map<std::string, PropertyGroup, std::less<void>> propertyGroupsMappedByKey; std::map<std::string, PropertyGroup, std::less<void>> propertyGroupsMappedByKey;
std::map<std::string, Module> modulesMappedByName; std::map<std::string, Module> modulesMappedByName;
std::map<std::string, Module> peripheralModulesMappedByName; std::map<std::string, Module> peripheralModulesMappedByName;
@@ -137,6 +144,14 @@ namespace Targets::TargetDescription
*/ */
static MemorySegment memorySegmentFromXml(const QDomElement& xmlElement); static MemorySegment memorySegmentFromXml(const QDomElement& xmlElement);
/**
* Constructs a MemorySegmentSection from an XML element.
*
* @param xmlElement
* @return
*/
static MemorySegmentSection memorySegmentSectionFromXml(const QDomElement& xmlElement);
/** /**
* Constructs a RegisterGroup object from an XML element. * Constructs a RegisterGroup object from an XML element.
* *
@@ -169,11 +184,6 @@ namespace Targets::TargetDescription
*/ */
const std::string& deviceAttribute(const std::string& attributeName) const; const std::string& deviceAttribute(const std::string& attributeName) const;
/**
* Extracts all address spaces and loads them into this->addressSpacesMappedById.
*/
void loadAddressSpaces(const QDomDocument& document);
/** /**
* Extracts all modules and loads them into this->modulesMappedByName. * Extracts all modules and loads them into this->modulesMappedByName.
*/ */