SummerCart64/sw/riscv/tools/bin2sv.py

35 lines
908 B
Python
Raw Normal View History

2021-08-12 21:07:47 +02:00
#!/usr/bin/env python3
import struct
import sys
sv_template = None
sv_code = None
2021-09-01 17:21:41 +02:00
template_name = sys.argv[1] or 'template.sv'
code_name = sys.argv[2] or 'result.sv'
2021-08-12 21:07:47 +02:00
try:
sv_template = open(template_name, mode='r')
sv_code = open(code_name, mode='w')
var_name = sv_template.readline().strip()
rom_formatted = ''
index = 0
2021-09-01 17:21:41 +02:00
for line in iter(lambda: sys.stdin.buffer.read(4), ''):
2021-08-12 21:07:47 +02:00
if (not line):
break
value = format(struct.unpack('<I', line)[0], '08x')
2021-08-15 21:49:02 +02:00
rom_formatted += f'\n {index}: {var_name} = 32\'h{value};'
2021-08-12 21:07:47 +02:00
index += 1
sv_code.write(sv_template.read().format(rom_formatted=rom_formatted))
except Exception as e:
print(f'Unable to convert the code: {e}', file=sys.stderr)
sys.exit(-1)
finally:
if (sv_template): sv_template.close()
if (sv_code): sv_code.close()