#!/usr/bin/env python

import sys

data = open(sys.argv[1], 'rb').read()
template = open(sys.argv[2]).read()
romsize = int(sys.argv[3])

if len(data) > romsize:
    raise RuntimeError, 'Data too large to fit in ROM'

# pad to fit ROM fully
if len(data) < romsize:
    data = data + (romsize - len(data)) * chr(0xff)

vhdl = []

while True:
    line = data[:16]
    data = data[16:]
    if line == '':
        break
    out = []
    for byte in line:
        out.append('X"%02X"' % ord(byte))
    vhdl.append(', '.join(out))

sys.stdout.write(template.replace("%ROMHERE%", ',\n'.join(vhdl)))
