最近想在Xilinx的fpga上实现picorv32,初始化rom需要转一下格式,感觉python确实挺方便的,边搜语法边写大概一个多小时吧。
import sys
import os
import binascii
import struct
path = sys.argv[2]
if os.path.exists(path):
os.remove(path)
else:
print('no such file')
filepath= sys.argv[1]
f = open(path, "w")
fb = open(filepath, "rb")
databuffer = ["0","0","0","0","0","0","0","0"]
print >> f,"MEMORY_INITIALIZATION_RADIX=16;"
print >> f,"MEMORY_INITIALIZATION_VECTOR="
size = os.path.getsize(filepath)
for i in range(size/4):
for j in range(4):
datab = fb.read(1)
datah = str(binascii.b2a_hex(datab))[0]
databuffer[(3-j)*2] = datah
datah = str(binascii.b2a_hex(datab))[1]
databuffer[(3-j)*2+1] = datah
for n in range(8):
f.write(databuffer[n])
if i < (size/4)-1:
print >> f,","
else :
print >> f,";"
fb.close()
f.close()
转换结果FPGA测试可以正常运行
使用方法 :python ***.py ***.bin ***.coe
python版本2.7
最近编辑记录 laotui (2020-08-23 21:40:06)
离线