Files
eb001/tb/sw/bin_to_coe.py

29 lines
1.2 KiB
Python
Raw Normal View History

2024-11-14 17:10:54 +08:00
def bin_to_coe_file(bin_file, coe_file, address_depth=1024, data_width=32, start_address=0x80000000):
# 打开二进制文件和输出的 COE 文件
with open(bin_file, 'rb') as bf, open(coe_file, 'w') as cf:
# 写入文件头
cf.write("memory_initialization_radix=16;\n")
cf.write("memory_initialization_vector=\n")
# 将二进制内容按每4字节分割并转换为十六进制字符串
for i in range(address_depth):
data = bf.read(4)
if data:
# 将4字节数据转为32位的十六进制字符串
value = int.from_bytes(data, byteorder='little')
cf.write(f"{value:08X}")
else:
# 如果数据不够填充0
cf.write("00000000")
# 在每行数据后添加逗号,最后一行用分号结尾
if i < address_depth - 1:
cf.write(",\n")
else:
cf.write(";\n")
# 运行脚本
bin_to_coe_file("./tb/sw/build/slvTest.bin", "./tb/sw/build/slvTest.coe")
bin_to_coe_file("./tb/sw/build/mstTest.bin", "./tb/sw/build/mstTest.coe")