gen/fhdl/instance: First cleanup pass.

This commit is contained in:
Florent Kermarrec 2023-11-03 11:46:51 +01:00
parent dee64b346f
commit 079a0a7b75
1 changed files with 35 additions and 18 deletions

View File

@ -12,44 +12,61 @@ from migen.fhdl.specials import *
def _instance_generate_verilog(instance, ns, add_data_file): def _instance_generate_verilog(instance, ns, add_data_file):
r = instance.of + " " r = instance.of + " "
# Instance Parameters.
# --------------------
parameters = list(filter(lambda i: isinstance(i, Instance.Parameter), instance.items)) parameters = list(filter(lambda i: isinstance(i, Instance.Parameter), instance.items))
if parameters: if parameters:
r += "#(\n" r += "#(\n"
firstp = True first = True
for p in parameters: for p in parameters:
if not firstp: if not first:
r += ",\n" r += ",\n"
firstp = False first = False
r += "\t." + p.name + "(" r += "\t." + p.name + "("
# Constant.
if isinstance(p.value, Constant): if isinstance(p.value, Constant):
r += verilog_printexpr(ns, p.value)[0] r += verilog_printexpr(ns, p.value)[0]
# Float.
elif isinstance(p.value, float): elif isinstance(p.value, float):
r += str(p.value) r += str(p.value)
# Preformatted.
elif isinstance(p.value, Instance.PreformattedParam): elif isinstance(p.value, Instance.PreformattedParam):
r += p.value r += p.value
# String.
elif isinstance(p.value, str): elif isinstance(p.value, str):
r += "\"" + p.value + "\"" r += "\"" + p.value + "\""
else: else:
raise TypeError raise TypeError
r += ")" r += ")"
r += "\n) " r += "\n) "
# Instance IOs.
# -------------
r += ns.get_name(instance) r += ns.get_name(instance)
if parameters: r += " " if parameters:
r += " "
r += "(\n" r += "(\n"
firstp = True first = True
for p in instance.items: for io in instance.items:
if isinstance(p, Instance._IO): if isinstance(io, Instance._IO):
name_inst = p.name name_inst = io.name
name_design = verilog_printexpr(ns, p.expr)[0] name_design = verilog_printexpr(ns, io.expr)[0]
if not firstp: if not first:
r += ",\n" r += ",\n"
firstp = False first = False
r += "\t." + name_inst + "(" + name_design + ")" r += "\t." + name_inst + "(" + name_design + ")"
if not firstp: if not first:
r += "\n" r += "\n"
# Instance Synthesis Directive.
# -----------------------------
if instance.synthesis_directive is not None: if instance.synthesis_directive is not None:
synthesis_directive = "/* synthesis {} */".format(instance.synthesis_directive) synthesis_directive = f"/* synthesis {instance.synthesis_directive} */"
r += ")" + synthesis_directive + ";\n\n" r += ")" + synthesis_directive + ";\n"
else: else:
r += ");\n\n" r += ");\n"
r += "\n"
return r return r