24 lines
1.0 KiB
Python
24 lines
1.0 KiB
Python
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") |