Debian Codegraph @

legacy/generic/write_fasm.py

raw symbols SCIP
1 from collections import namedtuple
2
3 """
4 write: set to True to enable writing this parameter to FASM
5
6 numeric: set to True to write this parameter as a bit array (width>1) or
7 single bit (width==1) named after the parameter. Otherwise this
8 parameter will be written as `name.value`
9
10 width: width of numeric parameter (ignored for non-numeric parameters)
11
12 alias: an alternative name for this parameter (parameter name used if alias
13 is None)
14 """
15 ParameterConfig = namedtuple('ParameterConfig', 'write numeric width alias')
16
17 # FIXME use defaults= once Python 3.7 is standard
18 ParameterConfig.__new__.__defaults__ = (False, True, 1, None)
19
20
21 """
22 Write a design as FASM
23
24 ctx: nextpnr context
25 paramCfg: map from (celltype, parametername) -> ParameterConfig describing how to write parameters
26 f: output file
27 """
28 def write_fasm(ctx, paramCfg, f):
29 for nname, net in sorted(ctx.nets, key=lambda x: str(x[1].name)):
30 print("# Net %s" % nname, file=f)
31 for wire, pip in sorted(net.wires, key=lambda x: str(x[1])):
32 if pip.pip != "":
33 print("%s" % pip.pip, file=f)
34 print("", file=f)
35 for cname, cell in sorted(ctx.cells, key=lambda x: str(x[1].name)):
36 print("# Cell %s at %s" % (cname, cell.bel), file=f)
37 for param, val in sorted(cell.params, key=lambda x: str(x)):
38 cfg = paramCfg[(cell.type, param)]
39 if not cfg.write:
40 continue
41 fasm_name = cfg.alias if cfg.alias is not None else param
42 if cfg.numeric:
43 if cfg.width == 1:
44 if int(val) != 0:
45 print("%s.%s" % (cell.bel, fasm_name), file=f)
46 else:
47 # Parameters with width >32 are direct binary, otherwise denary
48 print("%s.%s[%d:0] = %d'b%s" % (cell.bel, fasm_name, cfg.width-1, cfg.width, val), file=f)
49 else:
50 print("%s.%s.%s" % (cell.bel, fasm_name, val), file=f)
51 print("", file=f)