aboutsummaryrefslogtreecommitdiffstats
path: root/generic/examples/write_fasm.py
blob: 057e779cad0db786ad34d4b021492102f605d121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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: map from (celltype, parametername) -> 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 is not None:
				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
					print("%s.%s[%d:0] = %d'b%s" % (cell.bel, fasm_name, cfg.width-1, cfg.width, val), file=f)
			else:
				print("%s.%s.%s" % (cell.bel, fasm_name, val), file=f)
		print("", file=f)