2011-12-04 16:26:32 -05:00
|
|
|
from functools import partial
|
2012-09-11 04:00:03 -04:00
|
|
|
from operator import itemgetter
|
2011-12-04 16:26:32 -05:00
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
from migen.fhdl.structure import *
|
2012-07-13 11:07:56 -04:00
|
|
|
from migen.fhdl.structure import _Operator, _Slice, _Assign
|
2012-01-06 18:39:28 -05:00
|
|
|
from migen.fhdl.tools import *
|
2012-01-20 16:20:32 -05:00
|
|
|
from migen.fhdl.namer import Namespace, build_namespace
|
2011-12-16 10:02:55 -05:00
|
|
|
|
2011-12-08 10:35:32 -05:00
|
|
|
def _printsig(ns, s):
|
2012-11-29 15:22:38 -05:00
|
|
|
if s.signed:
|
2011-12-08 10:35:32 -05:00
|
|
|
n = "signed "
|
|
|
|
else:
|
|
|
|
n = ""
|
2012-07-13 12:32:54 -04:00
|
|
|
if len(s) > 1:
|
|
|
|
n += "[" + str(len(s)-1) + ":0] "
|
2011-12-16 10:02:55 -05:00
|
|
|
n += ns.get_name(s)
|
2011-12-08 10:35:32 -05:00
|
|
|
return n
|
|
|
|
|
2012-11-28 17:18:43 -05:00
|
|
|
def _printintbool(node):
|
|
|
|
if isinstance(node, bool):
|
|
|
|
if node:
|
2012-11-29 16:59:54 -05:00
|
|
|
return "1'd1", False
|
2012-11-28 17:18:43 -05:00
|
|
|
else:
|
2012-11-29 16:59:54 -05:00
|
|
|
return "1'd0", False
|
2012-11-28 17:18:43 -05:00
|
|
|
elif isinstance(node, int):
|
|
|
|
if node >= 0:
|
2012-11-29 16:59:54 -05:00
|
|
|
return str(bits_for(node)) + "'d" + str(node), False
|
2011-12-08 10:35:32 -05:00
|
|
|
else:
|
2012-11-29 16:59:54 -05:00
|
|
|
return "-" + str(bits_for(node)) + "'sd" + str(-node), True
|
2012-11-28 17:18:43 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
def _printexpr(ns, node):
|
|
|
|
if isinstance(node, (int, bool)):
|
|
|
|
return _printintbool(node)
|
2011-12-08 10:35:32 -05:00
|
|
|
elif isinstance(node, Signal):
|
2012-11-29 16:59:54 -05:00
|
|
|
return ns.get_name(node), node.signed
|
2011-12-16 15:30:14 -05:00
|
|
|
elif isinstance(node, _Operator):
|
2011-12-08 10:35:32 -05:00
|
|
|
arity = len(node.operands)
|
2012-11-29 16:59:54 -05:00
|
|
|
r1, s1 = _printexpr(ns, node.operands[0])
|
2011-12-08 10:35:32 -05:00
|
|
|
if arity == 1:
|
2012-11-29 16:59:54 -05:00
|
|
|
if node.op == "-":
|
|
|
|
if s1:
|
|
|
|
r = node.op + r1
|
|
|
|
else:
|
|
|
|
r = "-$signed({1'd0, " + r1 + "})"
|
|
|
|
s = True
|
|
|
|
else:
|
|
|
|
r = node.op + r1
|
|
|
|
s = s1
|
2011-12-08 10:35:32 -05:00
|
|
|
elif arity == 2:
|
2012-11-29 16:59:54 -05:00
|
|
|
r2, s2 = _printexpr(ns, node.operands[1])
|
|
|
|
if node.op in ["+", "-", "*", "&", "^", "|"]:
|
|
|
|
if s2 and not s1:
|
|
|
|
r1 = "$signed({1'd0, " + r1 + "})"
|
|
|
|
if s1 and not s2:
|
|
|
|
r2 = "$signed({1'd0, " + r2 + "})"
|
|
|
|
r = r1 + " " + node.op + " " + r2
|
|
|
|
s = s1 or s2
|
2011-12-08 10:35:32 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
2012-11-29 16:59:54 -05:00
|
|
|
return "(" + r + ")", s
|
2011-12-21 16:57:07 -05:00
|
|
|
elif isinstance(node, _Slice):
|
2012-02-06 12:07:02 -05:00
|
|
|
# Verilog does not like us slicing non-array signals...
|
|
|
|
if isinstance(node.value, Signal) \
|
2012-07-13 12:32:54 -04:00
|
|
|
and len(node.value) == 1 \
|
2012-02-06 12:07:02 -05:00
|
|
|
and node.start == 0 and node.stop == 1:
|
|
|
|
return _printexpr(ns, node.value)
|
|
|
|
|
2011-12-08 10:35:32 -05:00
|
|
|
if node.start + 1 == node.stop:
|
|
|
|
sr = "[" + str(node.start) + "]"
|
|
|
|
else:
|
|
|
|
sr = "[" + str(node.stop-1) + ":" + str(node.start) + "]"
|
2012-11-29 16:59:54 -05:00
|
|
|
r, s = _printexpr(ns, node.value)
|
|
|
|
return r + sr, s
|
2011-12-08 10:35:32 -05:00
|
|
|
elif isinstance(node, Cat):
|
2012-11-29 16:59:54 -05:00
|
|
|
l = [_printexpr(ns, v)[0] for v in reversed(node.l)]
|
|
|
|
return "{" + ", ".join(l) + "}", False
|
2011-12-09 07:11:34 -05:00
|
|
|
elif isinstance(node, Replicate):
|
2012-11-29 17:36:55 -05:00
|
|
|
return "{" + str(node.n) + "{" + _printexpr(ns, node.v)[0] + "}}", False
|
2011-12-08 10:35:32 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
2012-01-07 06:19:06 -05:00
|
|
|
(_AT_BLOCKING, _AT_NONBLOCKING, _AT_SIGNAL) = range(3)
|
|
|
|
|
|
|
|
def _printnode(ns, at, level, node):
|
2012-01-15 11:45:54 -05:00
|
|
|
if node is None:
|
|
|
|
return ""
|
|
|
|
elif isinstance(node, _Assign):
|
2012-01-07 06:19:06 -05:00
|
|
|
if at == _AT_BLOCKING:
|
|
|
|
assignment = " = "
|
|
|
|
elif at == _AT_NONBLOCKING:
|
|
|
|
assignment = " <= "
|
|
|
|
elif is_variable(node.l):
|
2011-12-08 10:35:32 -05:00
|
|
|
assignment = " = "
|
|
|
|
else:
|
|
|
|
assignment = " <= "
|
2012-11-29 16:59:54 -05:00
|
|
|
return "\t"*level + _printexpr(ns, node.l)[0] + assignment + _printexpr(ns, node.r)[0] + ";\n"
|
2013-01-05 08:18:15 -05:00
|
|
|
elif isinstance(node, (list, tuple)):
|
2012-07-13 11:07:56 -04:00
|
|
|
return "".join(list(map(partial(_printnode, ns, at, level), node)))
|
2011-12-08 10:35:32 -05:00
|
|
|
elif isinstance(node, If):
|
2012-11-29 16:59:54 -05:00
|
|
|
r = "\t"*level + "if (" + _printexpr(ns, node.cond)[0] + ") begin\n"
|
2012-01-07 06:19:06 -05:00
|
|
|
r += _printnode(ns, at, level + 1, node.t)
|
2012-07-13 11:07:56 -04:00
|
|
|
if node.f:
|
2011-12-08 10:35:32 -05:00
|
|
|
r += "\t"*level + "end else begin\n"
|
2012-01-07 06:19:06 -05:00
|
|
|
r += _printnode(ns, at, level + 1, node.f)
|
2011-12-08 10:35:32 -05:00
|
|
|
r += "\t"*level + "end\n"
|
|
|
|
return r
|
|
|
|
elif isinstance(node, Case):
|
2012-11-28 19:11:15 -05:00
|
|
|
if node.cases:
|
2012-11-29 16:59:54 -05:00
|
|
|
r = "\t"*level + "case (" + _printexpr(ns, node.test)[0] + ")\n"
|
2012-11-28 19:11:15 -05:00
|
|
|
css = sorted([(k, v) for (k, v) in node.cases.items() if k != "default"], key=itemgetter(0))
|
|
|
|
for choice, statements in css:
|
2012-11-29 16:59:54 -05:00
|
|
|
r += "\t"*(level + 1) + _printexpr(ns, choice)[0] + ": begin\n"
|
2012-11-28 19:11:15 -05:00
|
|
|
r += _printnode(ns, at, level + 2, statements)
|
2012-11-18 10:32:51 -05:00
|
|
|
r += "\t"*(level + 1) + "end\n"
|
2012-11-28 19:11:15 -05:00
|
|
|
if "default" in node.cases:
|
2012-11-18 10:32:51 -05:00
|
|
|
r += "\t"*(level + 1) + "default: begin\n"
|
2012-11-28 19:11:15 -05:00
|
|
|
r += _printnode(ns, at, level + 2, node.cases["default"])
|
2012-11-18 10:32:51 -05:00
|
|
|
r += "\t"*(level + 1) + "end\n"
|
|
|
|
r += "\t"*level + "endcase\n"
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
return ""
|
2011-12-08 10:35:32 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
2012-01-07 06:19:06 -05:00
|
|
|
def _list_comb_wires(f):
|
|
|
|
r = set()
|
|
|
|
groups = group_by_targets(f.comb)
|
|
|
|
for g in groups:
|
|
|
|
if len(g[1]) == 1 and isinstance(g[1][0], _Assign):
|
|
|
|
r |= g[0]
|
|
|
|
return r
|
|
|
|
|
2011-12-21 17:08:50 -05:00
|
|
|
def _printheader(f, ios, name, ns):
|
2013-02-22 11:56:35 -05:00
|
|
|
sigs = list_signals(f) | list_special_ios(f, True, True, True)
|
|
|
|
it_mem_outs = list_special_ios(f, False, True, False)
|
|
|
|
inouts = list_special_ios(f, False, False, True)
|
2013-02-14 18:17:24 -05:00
|
|
|
targets = list_targets(f) | it_mem_outs
|
|
|
|
wires = _list_comb_wires(f) | it_mem_outs
|
2011-12-21 17:08:50 -05:00
|
|
|
r = "module " + name + "(\n"
|
|
|
|
firstp = True
|
2012-11-23 12:38:03 -05:00
|
|
|
for sig in sorted(ios, key=lambda x: x.huid):
|
2011-12-21 17:08:50 -05:00
|
|
|
if not firstp:
|
|
|
|
r += ",\n"
|
|
|
|
firstp = False
|
2012-02-16 12:34:32 -05:00
|
|
|
if sig in inouts:
|
|
|
|
r += "\tinout " + _printsig(ns, sig)
|
|
|
|
elif sig in targets:
|
2012-01-07 06:19:06 -05:00
|
|
|
if sig in wires:
|
|
|
|
r += "\toutput " + _printsig(ns, sig)
|
|
|
|
else:
|
|
|
|
r += "\toutput reg " + _printsig(ns, sig)
|
2011-12-21 17:08:50 -05:00
|
|
|
else:
|
|
|
|
r += "\tinput " + _printsig(ns, sig)
|
|
|
|
r += "\n);\n\n"
|
2012-11-23 12:38:03 -05:00
|
|
|
for sig in sorted(sigs - ios, key=lambda x: x.huid):
|
2012-01-27 10:54:48 -05:00
|
|
|
if sig in wires:
|
2011-12-21 17:08:50 -05:00
|
|
|
r += "wire " + _printsig(ns, sig) + ";\n"
|
|
|
|
else:
|
|
|
|
r += "reg " + _printsig(ns, sig) + ";\n"
|
|
|
|
r += "\n"
|
|
|
|
return r
|
|
|
|
|
2012-04-30 17:38:40 -04:00
|
|
|
def _printcomb(f, ns, display_run):
|
2011-12-21 17:08:50 -05:00
|
|
|
r = ""
|
2012-07-13 11:07:56 -04:00
|
|
|
if f.comb:
|
2011-12-21 17:08:50 -05:00
|
|
|
# Generate a dummy event to get the simulator
|
|
|
|
# to run the combinatorial process once at the beginning.
|
|
|
|
syn_off = "// synthesis translate off\n"
|
|
|
|
syn_on = "// synthesis translate on\n"
|
2012-01-16 12:42:55 -05:00
|
|
|
dummy_s = Signal(name_override="dummy_s")
|
2011-12-21 17:08:50 -05:00
|
|
|
r += syn_off
|
|
|
|
r += "reg " + _printsig(ns, dummy_s) + ";\n"
|
2012-11-28 17:18:43 -05:00
|
|
|
r += "initial " + ns.get_name(dummy_s) + " <= 1'd0;\n"
|
2011-12-21 17:08:50 -05:00
|
|
|
r += syn_on
|
2012-01-07 06:19:06 -05:00
|
|
|
|
|
|
|
groups = group_by_targets(f.comb)
|
|
|
|
|
2012-04-30 17:38:40 -04:00
|
|
|
for n, g in enumerate(groups):
|
2012-01-07 06:19:06 -05:00
|
|
|
if len(g[1]) == 1 and isinstance(g[1][0], _Assign):
|
|
|
|
r += "assign " + _printnode(ns, _AT_BLOCKING, 0, g[1][0])
|
|
|
|
else:
|
2012-01-16 12:42:55 -05:00
|
|
|
dummy_d = Signal(name_override="dummy_d")
|
2012-01-07 06:19:06 -05:00
|
|
|
r += "\n" + syn_off
|
|
|
|
r += "reg " + _printsig(ns, dummy_d) + ";\n"
|
|
|
|
r += syn_on
|
|
|
|
|
|
|
|
r += "always @(*) begin\n"
|
2012-04-30 17:38:40 -04:00
|
|
|
if display_run:
|
|
|
|
r += "\t$display(\"Running comb block #" + str(n) + "\");\n"
|
2012-01-07 06:19:06 -05:00
|
|
|
for t in g[0]:
|
2012-11-29 16:59:54 -05:00
|
|
|
r += "\t" + ns.get_name(t) + " <= " + _printexpr(ns, t.reset)[0] + ";\n"
|
2012-07-13 11:07:56 -04:00
|
|
|
r += _printnode(ns, _AT_NONBLOCKING, 1, g[1])
|
2012-01-07 06:19:06 -05:00
|
|
|
r += syn_off
|
|
|
|
r += "\t" + ns.get_name(dummy_d) + " <= " + ns.get_name(dummy_s) + ";\n"
|
|
|
|
r += syn_on
|
|
|
|
r += "end\n"
|
|
|
|
r += "\n"
|
2011-12-21 17:08:50 -05:00
|
|
|
return r
|
|
|
|
|
2012-09-10 17:45:02 -04:00
|
|
|
def _printsync(f, ns, clock_domains):
|
2011-12-21 17:08:50 -05:00
|
|
|
r = ""
|
2012-09-11 04:00:03 -04:00
|
|
|
for k, v in sorted(f.sync.items(), key=itemgetter(0)):
|
2012-09-10 17:45:02 -04:00
|
|
|
r += "always @(posedge " + ns.get_name(clock_domains[k].clk) + ") begin\n"
|
|
|
|
r += _printnode(ns, _AT_SIGNAL, 1, insert_reset(clock_domains[k].rst, v))
|
2011-12-21 17:08:50 -05:00
|
|
|
r += "end\n\n"
|
|
|
|
return r
|
|
|
|
|
2013-02-23 13:03:16 -05:00
|
|
|
def _call_special_classmethod(overrides, obj, method, *args, **kwargs):
|
|
|
|
cl = obj.__class__
|
|
|
|
if cl in overrides:
|
|
|
|
cl = overrides[cl]
|
|
|
|
if hasattr(cl, method):
|
|
|
|
return getattr(cl, method)(obj, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _lower_specials(overrides, specials):
|
|
|
|
f = Fragment()
|
|
|
|
lowered_specials = set()
|
|
|
|
for special in sorted(specials, key=lambda x: x.huid):
|
|
|
|
impl = _call_special_classmethod(overrides, special, "lower")
|
|
|
|
if impl is not None:
|
|
|
|
f += impl.get_fragment()
|
|
|
|
lowered_specials.add(special)
|
|
|
|
return f, lowered_specials
|
|
|
|
|
|
|
|
def _printspecials(overrides, specials, ns, clock_domains):
|
2011-12-08 10:35:32 -05:00
|
|
|
r = ""
|
2013-02-23 13:03:16 -05:00
|
|
|
for special in sorted(specials, key=lambda x: x.huid):
|
|
|
|
pr = _call_special_classmethod(overrides, special, "emit_verilog", ns, clock_domains)
|
|
|
|
if pr is None:
|
|
|
|
raise NotImplementedError("Special " + str(special) + " failed to implement emit_verilog")
|
|
|
|
r += pr
|
2012-01-27 10:54:48 -05:00
|
|
|
return r
|
|
|
|
|
2012-04-02 06:59:42 -04:00
|
|
|
def _printinit(f, ios, ns):
|
2012-04-01 10:39:11 -04:00
|
|
|
r = ""
|
2012-04-02 06:59:42 -04:00
|
|
|
signals = list_signals(f) \
|
|
|
|
- ios \
|
|
|
|
- list_targets(f) \
|
2013-02-22 11:56:35 -05:00
|
|
|
- list_special_ios(f, False, True, False)
|
2012-04-01 10:39:11 -04:00
|
|
|
if signals:
|
|
|
|
r += "initial begin\n"
|
2012-11-23 12:38:03 -05:00
|
|
|
for s in sorted(signals, key=lambda x: x.huid):
|
2012-11-29 16:59:54 -05:00
|
|
|
r += "\t" + ns.get_name(s) + " <= " + _printexpr(ns, s.reset)[0] + ";\n"
|
2012-04-01 10:39:11 -04:00
|
|
|
r += "end\n\n"
|
|
|
|
return r
|
|
|
|
|
2013-01-23 09:13:06 -05:00
|
|
|
def convert(f, ios=None, name="top",
|
2012-09-10 17:45:02 -04:00
|
|
|
clock_domains=None,
|
2012-01-27 10:54:48 -05:00
|
|
|
return_ns=False,
|
2013-02-23 13:03:16 -05:00
|
|
|
special_overrides=dict(),
|
2012-04-30 17:38:40 -04:00
|
|
|
display_run=False):
|
2013-01-23 09:13:06 -05:00
|
|
|
if ios is None:
|
|
|
|
ios = set()
|
2012-09-10 17:45:02 -04:00
|
|
|
if clock_domains is None:
|
|
|
|
clock_domains = dict()
|
2013-02-22 11:56:35 -05:00
|
|
|
for d in list_clock_domains(f):
|
2012-09-10 17:45:02 -04:00
|
|
|
cd = ClockDomain(d)
|
|
|
|
clock_domains[d] = cd
|
|
|
|
ios.add(cd.clk)
|
|
|
|
ios.add(cd.rst)
|
2012-07-09 09:16:38 -04:00
|
|
|
|
|
|
|
f = lower_arrays(f)
|
2013-02-23 13:03:16 -05:00
|
|
|
fs, lowered_specials = _lower_specials(special_overrides, f.specials)
|
|
|
|
f += fs
|
2012-01-20 16:59:40 -05:00
|
|
|
|
2012-01-27 10:54:48 -05:00
|
|
|
ns = build_namespace(list_signals(f) \
|
2013-02-22 11:56:35 -05:00
|
|
|
| list_special_ios(f, True, True, True) \
|
2012-01-27 10:54:48 -05:00
|
|
|
| ios)
|
2011-12-12 18:24:40 -05:00
|
|
|
|
2011-12-08 10:35:32 -05:00
|
|
|
r = "/* Machine-generated using Migen */\n"
|
2011-12-21 17:08:50 -05:00
|
|
|
r += _printheader(f, ios, name, ns)
|
2012-04-30 17:38:40 -04:00
|
|
|
r += _printcomb(f, ns, display_run)
|
2012-09-10 17:45:02 -04:00
|
|
|
r += _printsync(f, ns, clock_domains)
|
2013-02-23 13:03:16 -05:00
|
|
|
r += _printspecials(special_overrides, f.specials - lowered_specials, ns, clock_domains)
|
2012-04-01 10:39:11 -04:00
|
|
|
r += _printinit(f, ios, ns)
|
2011-12-04 16:26:32 -05:00
|
|
|
r += "endmodule\n"
|
2012-01-27 10:54:48 -05:00
|
|
|
|
2012-01-16 12:09:52 -05:00
|
|
|
if return_ns:
|
|
|
|
return r, ns
|
|
|
|
else:
|
|
|
|
return r
|