From 48c4c1ed0561e643a591ed7fca21c69954abd8d2 Mon Sep 17 00:00:00 2001 From: David Shah Date: Wed, 17 Apr 2019 11:00:23 +0100 Subject: generic/examples: Add FASM writer Python script Signed-off-by: David Shah --- generic/examples/write_fasm.py | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 generic/examples/write_fasm.py (limited to 'generic/examples/write_fasm.py') diff --git a/generic/examples/write_fasm.py b/generic/examples/write_fasm.py new file mode 100644 index 00000000..fb55c41d --- /dev/null +++ b/generic/examples/write_fasm.py @@ -0,0 +1,52 @@ +from collections import namedtuple + +""" + write: set to True to enable writing this parameter to FASM + + numeric: set to True to write this parameter as a bit array (width>1) or + single bit (width==1) named after the parameter. Otherwise this + parameter will be written as `name.value` + + width: width of numeric parameter (ignored for non-numeric parameters) + + alias: an alternative name for this parameter (parameter name used if alias + is None) +""" +ParameterConfig = namedtuple('ParameterConfig', 'write numeric width alias') + +# FIXME use defaults= once Python 3.7 is standard +ParameterConfig.__new__.__defaults__ = (False, True, 1, None) + + +""" +Write a design as FASM + + ctx: nextpnr context + paramCfg: ParameterConfig describing how to write parameters + f: output file +""" +def write_fasm(ctx, paramCfg, f): + for nname, net in sorted(ctx.nets, key=lambda x: str(x[1].name)): + print("# Net %s" % nname, file=f) + for wire, pip in sorted(net.wires, key=lambda x: str(x[1])): + if pip.pip != "": + print("%s" % pip.pip, file=f) + print("", file=f) + for cname, cell in sorted(ctx.cells, key=lambda x: str(x[1].name)): + print("# Cell %s at %s" % (cname, cell.bel), file=f) + for param, val in sorted(cell.params, key=lambda x: str(x)): + cfg = paramCfg[(cell.type, param)] + if not cfg.write: + continue + fasm_name = cfg.alias if cfg.alias is not None else param + if cfg.numeric: + if cfg.width == 1: + if int(val) != 0: + print("%s.%s" % (cell.bel, fasm_name), file=f) + else: + # Parameters with width >32 are direct binary, otherwise denary + binval = val if cfg.width > 32 else "{:0{}b}".format(int(val), cfg.width) + print("%s.%s[%d:0] = %d'b%s" % (cell.bel, fasm_name, cfg.width-1, cfg.width, binval), file=f) + else: + print("%s.%s.%s" % (cell.bel, fasm_name, val), file=f) + print("", file=f) \ No newline at end of file -- cgit v1.2.3 From 9fa13b5adcb4bfb193645fee0091c5c51c88c17b Mon Sep 17 00:00:00 2001 From: David Shah Date: Wed, 17 Apr 2019 11:12:58 +0100 Subject: pybindings: make errors in Python scripts stop nextpnr execution Signed-off-by: David Shah --- generic/examples/write_fasm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'generic/examples/write_fasm.py') diff --git a/generic/examples/write_fasm.py b/generic/examples/write_fasm.py index fb55c41d..1f279b63 100644 --- a/generic/examples/write_fasm.py +++ b/generic/examples/write_fasm.py @@ -22,7 +22,7 @@ ParameterConfig.__new__.__defaults__ = (False, True, 1, None) Write a design as FASM ctx: nextpnr context - paramCfg: ParameterConfig describing how to write parameters + paramCfg: map from (celltype, parametername) -> ParameterConfig describing how to write parameters f: output file """ def write_fasm(ctx, paramCfg, f): -- cgit v1.2.3