Skip to content

Instantly share code, notes, and snippets.

@misodengaku
Created March 9, 2021 14:45
Show Gist options
  • Save misodengaku/5f0b253eac042ce454e4cb10a69c5dd7 to your computer and use it in GitHub Desktop.
Save misodengaku/5f0b253eac042ce454e4cb10a69c5dd7 to your computer and use it in GitHub Desktop.
Xilinx COE file generator
import binascii
import hashlib
import argparse
parser = argparse.ArgumentParser(description='Xilinx COE file generator', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('input', metavar='input_file', type=str, help='input file path')
parser.add_argument('output', metavar='output_file', type=str, help='output file path')
parser.add_argument('--width', type=int, help='Memory width (bits)', default=32, choices=[32, 64, 128, 256, 512, 1024, 2048, 4096])
args = parser.parse_args()
f = open(args.input, 'rb').read()
of = open(args.output, 'wb')
of.write(b'memory_initialization_radix=16;\nmemory_initialization_vector=')
buf = []
dp = 0
for i,b in enumerate(f):
# print(binascii.hexlify(bytes([b])))
if (i == 0) or (i % (args.width / 8) > 0):
buf.append(b)
if i == (len(f) - 1):
buf.reverse()
of.write(binascii.hexlify(bytes(buf)))
print("output size: %d[byte]" % (i + 1))
of.write(b';\n')
dp += 1
break
else:
buf.reverse()
of.write(binascii.hexlify(bytes(buf)))
dp += 1
if i == (len(f) - 1):
print("output size: %d[byte]" % (i + 1))
of.write(b';\n')
break
else:
of.write(b',\n')
buf = []
buf.append(b)
print('Memory width: %d[bits]' % args.width)
print('Memory depth: %d' % dp)
print('Memory size: %d[bits] (%d[bytes])' % (dp * args.width, dp * args.width / 8))
m = hashlib.sha256()
m.update(f)
print("sha256: %s" % binascii.hexlify(m.digest()).decode('ascii'))
of.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment