WIP parsing hex line

This commit is contained in:
Sergey Shchelkanov 2023-05-11 11:49:41 +03:00
parent 1b63ed557f
commit 292b90620e
2 changed files with 28 additions and 20 deletions

View File

@ -35,41 +35,43 @@ def parse_hex_line(line: str) -> Record:
line, line[0])) line, line[0]))
return () return ()
reclen = int(line[1:3], base=16) # Record length datalen = int(line[1:3], base=16) # Data field length
addr = int(line[3:7], base=16) # Initial address of data byte addr = int(line[3:7], base=16) # Load offset field
rectype = int(line[7:9], base=16) # Record type rectype = int(line[7:9], base=16) # Record type field
data_bytes: List[str] = [] data_bytes_line = line[9:datalen*2 + 9] # Data field
crc = int(line[datalen*2 + 9:datalen*2 + 11], base=16)
splitted_by_bytes: List[str] = []
for i in range(datalen):
splitted_by_bytes.append(data_bytes_line[i*2:i*2+2])
data_bytes_line = line[9:reclen*2 + 9] data_bytes = list(map(lambda x: int(x, base=16), splitted_by_bytes))
for i in range(reclen):
data_bytes.append(data_bytes_line[i*2:i*2+2])
record = Record(RecordType.UNKNOWN, 0, []) record = Record(RecordType.UNKNOWN, 0, [])
if rectype == 0: # Data Record if rectype == 0: # Data Record
record.type = RecordType.DATA record.type = RecordType.DATA
record.address = addr record.address = addr
record.data = list(map(lambda x: int(x, base=16), data_bytes)) record.data = data_bytes
elif rectype == 1: # End of File Record elif rectype == 1: # End of File Record
record.type = RecordType.EOF record.type = RecordType.EOF
elif rectype == 2: # Extended Segment Address Record elif rectype == 2: # Extended Segment Address Record
record.type = RecordType.SEGADDR record.type = RecordType.SEGADDR
record.address = addr # record.address = addr
record.data = list(map(lambda x: int(x, base=16), data_bytes)) # record.data = list(map(lambda x: int(x, base=16), splitted_by_bytes))
elif rectype == 3: # Start Segment Address Record elif rectype == 3: # Start Segment Address Record
print("Start Segment Address Record") record.type = RecordType.STARTADDR
print("ERROR: unimplemented record type 3 on line %i" % (i+1)) # record.address = addr
is_error = True # record.data = list(map(lambda x: int(x, base=16), splitted_by_bytes))
elif rectype == 4: # Extended Linear Address Record elif rectype == 4: # Extended Linear Address Record
record.type = RecordType.EXTADDR record.type = RecordType.EXTADDR
record.address = addr record.address = data_bytes[1] * pow(256, 2) + data_bytes[0] * pow(256, 3)
record.data = list(map(lambda x: int(x, base=16), data_bytes))
elif rectype == 5: # Start Linear Address Record elif rectype == 5: # Start Linear Address Record
record.type = RecordType.LINEARSTARTADDR record.type = RecordType.LINEARSTARTADDR
record.address = addr address = 0
record.data = list(map(lambda x: int(x, base=16), data_bytes)) data_bytes.reverse()
print("Start Linear Address is 0x%s (line %s)" % for i in range(4):
(data_bytes_line, line)) address += data_bytes[i] * pow(256, i)
record.address = address
else: else:
record_type = RecordType.UNKNOWN record_type = RecordType.UNKNOWN

View File

@ -55,6 +55,12 @@ def read_file(filename: str) -> List[Record]:
for line in lines: for line in lines:
record: Record = parse_line(line, file_extension) record: Record = parse_line(line, file_extension)
print(record) print(record)
if record.type == RecordType.DATA:
pass
elif record.type == RecordType.EXTADDR:
pass
elif record.type == RecordType.LINEARSTARTADDR:
print("Start Linear Address:", record.address)
return segments return segments