diff --git a/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.cpp b/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.cpp index 4de03d92..70aa88fe 100644 --- a/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.cpp +++ b/src/DebugToolDrivers/Microchip/Protocols/EDBG/AVR/EdbgAvr8Interface.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #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); } diff --git a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp index 55028e8e..2621c724 100644 --- a/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp +++ b/src/DebugToolDrivers/Protocols/RiscVDebugSpec/DebugTranslator.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #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,