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 <thread>
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <algorithm>
#include "src/Services/PathService.hpp" #include "src/Services/PathService.hpp"
#include "src/Services/StringService.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 * We can't just forward the memory type to readMemory(), because some memory types (such as
* EEPROM_ATOMIC) can only be used for writing. * EEPROM_ATOMIC) can only be used for writing.
*/ */
auto alignedBuffer = this->readMemory( const auto readMemType = type == Avr8MemoryType::EEPROM_ATOMIC ? Avr8MemoryType::EEPROM : type;
type == Avr8MemoryType::EEPROM_ATOMIC ? Avr8MemoryType::EEPROM : type, auto alignedBuffer = (alignedStartAddress < startAddress)
alignedStartAddress, ? this->readMemory(readMemType, alignedStartAddress, startAddress - alignedStartAddress)
alignedBytes : TargetMemoryBuffer{};
);
assert(alignedBuffer.size() >= buffer.size());
const auto offset = alignedBuffer.begin() + (startAddress - alignedStartAddress); alignedBuffer.resize(alignedBytes);
std::copy(buffer.begin(), buffer.end(), offset);
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); return this->writeMemory(type, alignedStartAddress, alignedBuffer);
} }

View File

@@ -5,6 +5,7 @@
#include <chrono> #include <chrono>
#include <limits> #include <limits>
#include <cassert> #include <cassert>
#include <algorithm>
#include "Registers/CpuRegisterNumbers.hpp" #include "Registers/CpuRegisterNumbers.hpp"
#include "DebugModule/Registers/RegisterAddresses.hpp" #include "DebugModule/Registers/RegisterAddresses.hpp"
@@ -411,19 +412,31 @@ namespace DebugToolDrivers::Protocols::RiscVDebugSpec
addressSpaceDescriptor, addressSpaceDescriptor,
memorySegmentDescriptor, memorySegmentDescriptor,
alignedStartAddress, alignedStartAddress,
(startAddress - alignedStartAddress) (startAddress - alignedStartAddress),
{}
) )
: TargetMemoryBuffer{}; : 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( const auto dataBack = this->readMemory(
addressSpaceDescriptor, addressSpaceDescriptor,
memorySegmentDescriptor, memorySegmentDescriptor,
startAddress + bytes, 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( return this->writeMemory(
addressSpaceDescriptor, addressSpaceDescriptor,