Skip to content

Commit

Permalink
build_rom.py: add new feature of exporting a C header file
Browse files Browse the repository at this point in the history
  • Loading branch information
ldoolitt committed Dec 27, 2023
1 parent a14c846 commit 06e769d
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions build-tools/build_rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def decode_array(a):
def verilog_rom(a, suffix="", prefix=""):
min_rom_size = 2048
max_rom_size = 16384

print("%d/%d ROM entries used" % (len(a), max_rom_size)) # Upper boundary for maximum ROM size, fixed
if len(a) > max_rom_size:
raise RuntimeError("ROM_size input exceeds MAX_ROM_SIZE")
Expand Down Expand Up @@ -118,6 +117,28 @@ def verilog_rom(a, suffix="", prefix=""):
return '\n'.join(outputMessage)


# New as of 2023-12-27. Not as well tested as other routines here.
def c_rom(a, suffix="", prefix=""):
min_rom_size = 2048
max_rom_size = 16384
print("%d/%d ROM entries used" % (len(a), max_rom_size)) # Upper boundary for maximum ROM size, fixed
if len(a) > max_rom_size:
raise RuntimeError("ROM_size input exceeds MAX_ROM_SIZE")
return ""
max_addr_size = opt_bus_width(len(a), min_rom_size, max_rom_size) - 1
d_list = ["0x%4.4x" % d for d in a]
d_list2 = [", ".join(d_list[ix*8:ix*8+8]) for ix in range((len(a)+7)//8)]
config_data = " " + ",\n ".join(d_list2)
outputMessage = ("// 16k x 16 ROM machine generated by python c_rom()",
"static uint16_t config_romx[] = {",
config_data,
"};",
"#define CONFIG_ROM_SIZE (%s)" % (2**max_addr_size),
""
)
return '\n'.join(outputMessage)


def opt_bus_width(entries, min_rom_size, max_rom_size):
'''
In order to limit blockRAM resource utilization, the port size has to be a function of ROM size.
Expand Down Expand Up @@ -186,6 +207,13 @@ def desc_limit_check(dev_desc):
help='Destination config_rom filename',
type=str,
default='')
parser.add_argument(
'-c',
'--c_file',
dest='c_file',
help='Destination config_rom filename',
type=str,
default='')
parser.add_argument(
'-j',
'--json',
Expand Down Expand Up @@ -235,9 +263,12 @@ def desc_limit_check(dev_desc):
r = decode_array(a)
for rr in r:
print(rr)
elif args.verilog_file == '':
print(len(a))
print(a)
else:
elif args.verilog_file != '':
with open(args.verilog_file, 'w') as f:
f.write(verilog_rom(a, suffix=args.mod_suffix, prefix=args.mod_prefix))
elif args.c_file != '':
with open(args.c_file, 'w') as f:
f.write(c_rom(a, suffix=args.mod_suffix, prefix=args.mod_prefix))
else:
print(len(a))
print(a)

0 comments on commit 06e769d

Please sign in to comment.