您尚未登录。

楼主 #1 2020-08-23 21:38:34

laotui
会员
注册时间: 2020-03-07
已发帖子: 11
积分: 15

分享一个 bin 转 coe 的python脚本

最近想在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)

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn