segmentToI16FileContents function
Converts the segment seg
to an Intel Hex file record block with a max size of 1 MB.
Uses startCode
to start a record and writes lineLength
bytes per line.
lineLength
must be between 1 and 255.
Implementation
String segmentToI16FileContents(
MemorySegment seg, String startCode, int lineLength) {
_validateLineLength(lineLength);
const i16max = 65535;
if (seg.endAddress > i16max * 16) {
throw IHexRangeError(
"Address range [${seg.address},${seg.endAddress}] can not be represented as I16HEX (max. Range: [0,1048560])");
}
var rv = "";
var lastBlockAddress = 0;
for (int i = 0; i < seg.length; i = i + lineLength) {
final dataStartAddress = seg.address + i;
final blockStartAddress = dataStartAddress & 0xF0000;
if (blockStartAddress != lastBlockAddress) {
rv += createExtendedSegmentAddressRecord(blockStartAddress,
startCode: startCode);
}
lastBlockAddress = blockStartAddress;
rv += createDataRecord(dataStartAddress & 0xFFFF,
seg.slice(i, min(i + lineLength, seg.length)),
startCode: startCode);
}
return rv;
}