Files
eb001/tb/sw/bin_to_hex.py
2024-11-05 11:09:40 +08:00

24 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def bin_to_hex_file(bin_file, hex_file, address_depth=1024, data_width=32):
# 打开二进制文件和输出的Hex文件
with open(bin_file, 'rb') as bf, open(hex_file, 'w') as hf:
# 写入文件头
hf.write("#File_format=Hex\n")
hf.write(f"#Address_depth={address_depth}\n")
hf.write(f"#Data_width={data_width}\n")
# 读取二进制文件内容
data = bf.read()
# 将二进制内容按每4字节分割并转换为十六进制字符串
for i in range(address_depth):
if i * 4 < len(data):
# 提取4字节数据并转为32位的十六进制字符串
value = int.from_bytes(data[i*4:(i+1)*4], byteorder='little')
hf.write(f"{value:08X}\n")
else:
# 如果数据不够填充0
hf.write("00000000\n")
# 运行脚本
bin_to_hex_file("./tb/sw/build/slvTest.bin", "./tb/sw/build/slvTest.mi")
bin_to_hex_file("./tb/sw/build/mstTest.bin", "./tb/sw/build/mstTest.mi")