Files
BloomPatched/src/Targets/TargetDescription/MemorySegment.hpp

85 lines
2.5 KiB
C++
Raw Normal View History

2021-04-04 21:04:12 +01:00
#pragma once
#include <cstdint>
2022-10-09 13:10:17 +01:00
#include <optional>
2021-04-04 21:04:12 +01:00
#include "MemorySegmentSection.hpp"
#include "src/Services/StringService.hpp"
#include "src/Exceptions/Exception.hpp"
2021-04-04 21:04:12 +01:00
namespace Targets::TargetDescription
2021-04-04 21:04:12 +01:00
{
enum class MemorySegmentType: std::uint8_t
{
ALIASED,
2021-04-04 21:04:12 +01:00
REGISTERS,
EEPROM,
FLASH,
FUSES,
IO,
RAM,
2021-04-04 21:04:12 +01:00
LOCKBITS,
OSCCAL,
PRODUCTION_SIGNATURES,
SIGNATURES,
USER_SIGNATURES,
2021-04-04 21:04:12 +01:00
};
struct MemorySegment
{
std::string key;
2021-04-04 21:04:12 +01:00
std::string name;
MemorySegmentType type;
std::uint32_t startAddress;
std::uint32_t size;
std::optional<std::uint16_t> pageSize;
std::map<std::string, MemorySegmentSection, std::less<void>> sectionsByKey;
MemorySegment(
const std::string& key,
const std::string& name,
MemorySegmentType type,
std::uint32_t startAddress,
std::uint32_t size,
const std::optional<std::uint16_t>& pageSize,
const std::map<std::string, MemorySegmentSection, std::less<void>>& sectionsByKey
)
: key(key)
, name(name)
, type(type)
, startAddress(startAddress)
, size(size)
, pageSize(pageSize)
, 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"
);
}
2021-04-04 21:04:12 +01:00
return propertyGroup->get();
}
2021-04-04 21:04:12 +01:00
};
}