2020-02-04 20:10:09 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#############################################################################
|
2020-03-26 12:18:59 -04:00
|
|
|
# hex2hex <inputfile> <outputfile> <memory-width> <offset>
|
2020-02-04 20:10:09 -05:00
|
|
|
#
|
2020-02-06 10:05:55 -05:00
|
|
|
# Script uses objcopy to dump a Verilog comptatible hex file
|
2020-02-04 20:10:09 -05:00
|
|
|
#
|
|
|
|
# The script assumes the hex file has max 16 byte entries per line
|
|
|
|
#
|
|
|
|
# Script also supports creating EMF transactions with one address per data
|
|
|
|
#
|
2020-03-26 12:18:59 -04:00
|
|
|
# Offset to remove from all hex addresses
|
|
|
|
#
|
2020-02-04 20:10:09 -05:00
|
|
|
#############################################################################
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
2020-02-04 20:36:00 -05:00
|
|
|
hexin = sys.argv[1]
|
|
|
|
hexout = sys.argv[2]
|
|
|
|
width = int(int(sys.argv[3])/8) #needed for both
|
2020-03-26 12:18:59 -04:00
|
|
|
offset = int(sys.argv[4],16)
|
2020-02-04 20:36:00 -05:00
|
|
|
emf = 0 # emf
|
2020-02-04 20:10:09 -05:00
|
|
|
|
2020-02-06 10:05:55 -05:00
|
|
|
###################################################
|
|
|
|
# Read file into list and strip off trail junk
|
2020-02-04 20:36:00 -05:00
|
|
|
list = [line.rstrip(' \n') for line in open(hexin)]
|
2020-02-04 20:10:09 -05:00
|
|
|
|
2020-02-06 10:05:55 -05:00
|
|
|
###################################################
|
|
|
|
# Loop through byte list and print to output file
|
2020-02-04 20:36:00 -05:00
|
|
|
fileout=open(hexout,"w")
|
2020-02-04 20:10:09 -05:00
|
|
|
for line in list:
|
|
|
|
if (~line.find('@')): #detects new memory section
|
2020-03-26 12:18:59 -04:00
|
|
|
address = int((int(line.replace('@','',1),16)-offset)/8)
|
2020-02-04 20:10:09 -05:00
|
|
|
if(emf):
|
|
|
|
print ("Not Implemented")
|
2020-02-04 20:36:00 -05:00
|
|
|
else:
|
|
|
|
fileout.write("@"+format(address, 'x')+"\n")
|
|
|
|
else: #write out data byte stream
|
2020-02-04 20:10:09 -05:00
|
|
|
bytes=line.split(' ')
|
|
|
|
length=len(bytes)
|
|
|
|
for i in range(0,(16-length)): # zero extend buffer for simplicity
|
|
|
|
bytes.append("00")
|
|
|
|
for i in range(0,16,width):
|
|
|
|
sublist=bytes[i:i+width]
|
|
|
|
sublist.reverse()
|
|
|
|
word=""
|
|
|
|
word=word.join(sublist)
|
|
|
|
if(emf):
|
|
|
|
print ("Not Implemented")
|
|
|
|
else :
|
2020-02-04 20:36:00 -05:00
|
|
|
fileout.write(word+"\n")
|
|
|
|
fileout.close()
|