Created
April 14, 2022 02:32
-
-
Save holmesconan/32fc436ec63bb4f9928d0d7d1016c0b1 to your computer and use it in GitHub Desktop.
Intel HEX format display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
def parse_line(line: str) -> dict: | |
start_index = line.index(':') | |
record_line = line[start_index:] | |
record = { | |
'byte_count': int(record_line[1:3], 16), | |
'address': record_line[3:7], | |
'type': record_line[7:9], | |
'data': record_line[9:-2], | |
'checksum': record_line[-2:] | |
} | |
assert len(record['data']) == record['byte_count'] * 2 | |
return record | |
def print_record(env, record): | |
if 'start_address' in env: | |
print(env['start_address'], end='') | |
print(record['address'] + ': ', end='') | |
data = record['data'] | |
print(' '.join("%s%s%s%s" % x for x in zip( | |
data[::4], data[1::4], data[2::4], data[3::4] | |
)), end='') | |
print(' ', end='') | |
for x in zip(data[::2], data[1::2]): | |
c = chr(int("%s%s" % x, 16)) | |
if c.isprintable(): | |
print(c, end='') | |
else: | |
print('.', end='') | |
print('') | |
def main(args): | |
env = {} | |
with open(args.file, 'r') as f: | |
for line in f: | |
record = parse_line(line.strip()) | |
if record['type'] == '00': # Data | |
print_record(env, record) | |
elif record['type'] == '01': # End Of File | |
break | |
elif record['type'] == '02': # Extended Segment Address | |
pass | |
elif record['type'] == '03': # Start Segment Address | |
pass | |
elif record['type'] == '04': # Extended Linear Address | |
assert record['byte_count'] == 2 | |
env['start_address'] = record['data'] | |
elif record['type'] == '05': # Start Linear Address | |
pass | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('file') | |
main(parser.parse_args()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment