decodeRecord static method
Decode the hex record to be combined with other portions of the software
Implementation
static IntelHex decodeRecord(String data){
List<String> value = data.replaceAll('\r\n', '').replaceAll('\n','').split(':');
IntelHex ihr = IntelHex();
for(int i = 1; i < value.length;i++){
List<int> bin = unhexlify(value[i]);
int length = bin.length;
int recordLength = bin[0];
if(length != (5 + recordLength)){
throw Exception('Record at line $i has invalid length: $length , $recordLength');
}
int address = bin[1]*256 + bin[2];
int recordType = bin[3];
if(recordType < 0 || recordType > 5){
throw Exception('Invalid RecordType at line $i recordType: $recordType');
}
int crc = bin.fold(0, (p, c) => p + c);
crc &= 0x0FF;
if(crc != 0){
throw Exception('Record at line $i has invalid checksum: $crc');
}
if(recordType == 0){
address += ihr.offset;
for(int j = 4; j < 4+recordLength;j++){//i in range_g(4, 4+record_length){
if(ihr.buffer.containsKey(address)){
throw Exception('Address overlapped at line $i');
}
ihr.buffer[address] = bin[j];
address += 1;
}
}
else if(recordType == 1){
if(recordLength != 0){
throw Exception('End of record error at line $i');
}
}
else if(recordType == 2){
//Extended 8086 Segment Record
if(recordLength != 2 || address != 0){
throw Exception('Invalid Extended Segment Address Record at line $i');
}
ihr.offset = (bin[4]*256 + bin[5]) * 16;
}
else if(recordType == 3){
//Start Segment Address Record
if(recordLength != 4 || address != 0){
throw Exception('Invalid Start Segment Address Record at line $i');
}
if(ihr.startAddress != null){
throw Exception('Start Address Record appears twice at line $i');
}
ihr.startAddress = {
'CS': bin[4]*256 + bin[5],
'IP': bin[6]*256 + bin[7],
};
}
else if(recordType == 4){
//Extended Linear Address Record
if(recordLength != 2 || address != 0){
throw Exception('Invalid Extended Linear Address Record at line $i');
}
ihr.offset = (bin[4]*256 + bin[5]) * 65536;
}
else if(recordType == 5){
//Start Linear Address Record
if(recordLength != 4 || address != 0){
throw Exception('Invalid Start Linear Address Record at line $i');
}
if(ihr.startAddress != null){
throw Exception('Start Address Record appears twice at line $i');
}
ihr.startAddress = {
'EIP': (bin[4]*16777216 + bin[5]*65536 + bin[6]*256 + bin[7]),
};
}
}
return ihr;
}