Refactored page alignment code in EDBG and RISC-V debug translator driver

This commit is contained in:
Nav
2024-11-16 19:54:40 +00:00
parent 8f61c5a839
commit a7ee6cbae2
2 changed files with 42 additions and 13 deletions

View File

@@ -3,6 +3,7 @@
#include <thread>
#include <cassert>
#include <cmath>
#include <algorithm>
#include "src/Services/PathService.hpp"
#include "src/Services/StringService.hpp"
@@ -1505,15 +1506,30 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
* We can't just forward the memory type to readMemory(), because some memory types (such as
* EEPROM_ATOMIC) can only be used for writing.
*/
auto alignedBuffer = this->readMemory(
type == Avr8MemoryType::EEPROM_ATOMIC ? Avr8MemoryType::EEPROM : type,
alignedStartAddress,
alignedBytes
);
assert(alignedBuffer.size() >= buffer.size());
const auto readMemType = type == Avr8MemoryType::EEPROM_ATOMIC ? Avr8MemoryType::EEPROM : type;
auto alignedBuffer = (alignedStartAddress < startAddress)
? this->readMemory(readMemType, alignedStartAddress, startAddress - alignedStartAddress)
: TargetMemoryBuffer{};
const auto offset = alignedBuffer.begin() + (startAddress - alignedStartAddress);
std::copy(buffer.begin(), buffer.end(), offset);
alignedBuffer.resize(alignedBytes);
std::copy(
buffer.begin(),
buffer.end(),
alignedBuffer.begin() + (startAddress - alignedStartAddress)
);
const auto dataBack = this->readMemory(
readMemType,
startAddress + bytes,
alignedBytes - bytes - (startAddress - alignedStartAddress),
{}
);
std::copy(
dataBack.begin(),
dataBack.end(),
alignedBuffer.begin() + (startAddress - alignedStartAddress) + bytes
);
return this->writeMemory(type, alignedStartAddress, alignedBuffer);
}

View File

@@ -5,6 +5,7 @@
#include <chrono>
#include <limits>
#include <cassert>
#include <algorithm>
#include "Registers/CpuRegisterNumbers.hpp"
#include "DebugModule/Registers/RegisterAddresses.hpp"
@@ -411,19 +412,31 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec
addressSpaceDescriptor,
memorySegmentDescriptor,
alignedStartAddress,
(startAddress - alignedStartAddress)
(startAddress - alignedStartAddress),
{}
)
: TargetMemoryBuffer{};
alignedBuffer.reserve(alignedBytes);
// Read the offset bytes required to align the buffer size
alignedBuffer.resize(alignedBytes);
std::copy(
buffer.begin(),
buffer.end(),
alignedBuffer.begin() + (startAddress - alignedStartAddress)
);
const auto dataBack = this->readMemory(
addressSpaceDescriptor,
memorySegmentDescriptor,
startAddress + bytes,
alignedBytes - bytes - (startAddress - alignedStartAddress)
alignedBytes - bytes - (startAddress - alignedStartAddress),
{}
);
std::copy(
dataBack.begin(),
dataBack.end(),
alignedBuffer.begin() + (startAddress - alignedStartAddress) + bytes
);
alignedBuffer.insert(alignedBuffer.end(), dataBack.begin(), dataBack.end());
return this->writeMemory(
addressSpaceDescriptor,