2011-12-04 16:26:32 -05:00
|
|
|
from functools import partial
|
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
from migen.fhdl.structure import *
|
2011-12-21 16:57:07 -05:00
|
|
|
from migen.fhdl.structure import _Operator, _Slice, _Assign, _StatementList
|
2011-12-16 10:02:55 -05:00
|
|
|
from migen.fhdl.convtools import *
|
|
|
|
|
2011-12-08 10:35:32 -05:00
|
|
|
def _printsig(ns, s):
|
|
|
|
if s.bv.signed:
|
|
|
|
n = "signed "
|
|
|
|
else:
|
|
|
|
n = ""
|
|
|
|
if s.bv.width > 1:
|
|
|
|
n += "[" + str(s.bv.width-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
|
|
|
|
|
|
|
|
def _printexpr(ns, node):
|
|
|
|
if isinstance(node, Constant):
|
|
|
|
if node.n >= 0:
|
|
|
|
return str(node.bv) + str(node.n)
|
|
|
|
else:
|
|
|
|
return "-" + str(node.bv) + str(-self.n)
|
|
|
|
elif isinstance(node, Signal):
|
2011-12-16 10:02:55 -05:00
|
|
|
return ns.get_name(node)
|
2011-12-16 15:30:14 -05:00
|
|
|
elif isinstance(node, _Operator):
|
2011-12-08 10:35:32 -05:00
|
|
|
arity = len(node.operands)
|
|
|
|
if arity == 1:
|
2011-12-08 15:15:24 -05:00
|
|
|
r = node.op + _printexpr(ns, node.operands[0])
|
2011-12-08 10:35:32 -05:00
|
|
|
elif arity == 2:
|
|
|
|
r = _printexpr(ns, node.operands[0]) + " " + node.op + " " + _printexpr(ns, node.operands[1])
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
return "(" + r + ")"
|
2011-12-21 16:57:07 -05:00
|
|
|
elif isinstance(node, _Slice):
|
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) + "]"
|
|
|
|
return _printexpr(ns, node.value) + sr
|
|
|
|
elif isinstance(node, Cat):
|
|
|
|
l = list(map(partial(_printexpr, ns), node.l))
|
|
|
|
l.reverse()
|
|
|
|
return "{" + ", ".join(l) + "}"
|
2011-12-09 07:11:34 -05:00
|
|
|
elif isinstance(node, Replicate):
|
|
|
|
return "{" + str(node.n) + "{" + _printexpr(ns, node.v) + "}}"
|
2011-12-08 10:35:32 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
2011-12-21 17:00:36 -05:00
|
|
|
def _printnode(ns, is_sync, level, node):
|
2011-12-21 16:57:07 -05:00
|
|
|
if isinstance(node, _Assign):
|
2011-12-21 17:00:36 -05:00
|
|
|
if is_sync and is_variable(node.l):
|
2011-12-08 10:35:32 -05:00
|
|
|
assignment = " = "
|
|
|
|
else:
|
|
|
|
assignment = " <= "
|
|
|
|
return "\t"*level + _printexpr(ns, node.l) + assignment + _printexpr(ns, node.r) + ";\n"
|
2011-12-21 16:57:07 -05:00
|
|
|
elif isinstance(node, _StatementList):
|
2011-12-21 17:00:36 -05:00
|
|
|
return "".join(list(map(partial(_printnode, ns, is_sync, level), node.l)))
|
2011-12-08 10:35:32 -05:00
|
|
|
elif isinstance(node, If):
|
|
|
|
r = "\t"*level + "if (" + _printexpr(ns, node.cond) + ") begin\n"
|
2011-12-21 17:00:36 -05:00
|
|
|
r += _printnode(ns, is_sync, level + 1, node.t)
|
2011-12-08 10:35:32 -05:00
|
|
|
if node.f.l:
|
|
|
|
r += "\t"*level + "end else begin\n"
|
2011-12-21 17:00:36 -05:00
|
|
|
r += _printnode(ns, is_sync, level + 1, node.f)
|
2011-12-08 10:35:32 -05:00
|
|
|
r += "\t"*level + "end\n"
|
|
|
|
return r
|
|
|
|
elif isinstance(node, Case):
|
|
|
|
r = "\t"*level + "case (" + _printexpr(ns, node.test) + ")\n"
|
|
|
|
for case in node.cases:
|
|
|
|
r += "\t"*(level + 1) + _printexpr(ns, case[0]) + ": begin\n"
|
2011-12-21 17:00:36 -05:00
|
|
|
r += _printnode(ns, is_sync, level + 2, case[1])
|
2011-12-08 10:35:32 -05:00
|
|
|
r += "\t"*(level + 1) + "end\n"
|
2011-12-08 17:04:20 -05:00
|
|
|
if node.default.l:
|
|
|
|
r += "\t"*(level + 1) + "default: begin\n"
|
2011-12-21 17:00:36 -05:00
|
|
|
r += _printnode(ns, is_sync, level + 2, node.default)
|
2011-12-08 17:04:20 -05:00
|
|
|
r += "\t"*(level + 1) + "end\n"
|
2011-12-08 10:35:32 -05:00
|
|
|
r += "\t"*level + "endcase\n"
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
2011-12-21 17:08:50 -05:00
|
|
|
def _printheader(f, ios, name, ns):
|
|
|
|
sigs = list_signals(f)
|
|
|
|
targets = list_targets(f)
|
|
|
|
instouts = list_inst_outs(f)
|
|
|
|
r = "module " + name + "(\n"
|
|
|
|
firstp = True
|
|
|
|
for sig in ios:
|
|
|
|
if not firstp:
|
|
|
|
r += ",\n"
|
|
|
|
firstp = False
|
|
|
|
if sig in targets:
|
|
|
|
r += "\toutput reg " + _printsig(ns, sig)
|
|
|
|
elif sig in instouts:
|
|
|
|
r += "\toutput " + _printsig(ns, sig)
|
|
|
|
else:
|
|
|
|
r += "\tinput " + _printsig(ns, sig)
|
|
|
|
r += "\n);\n\n"
|
|
|
|
for sig in sigs - ios:
|
|
|
|
if sig in instouts:
|
|
|
|
r += "wire " + _printsig(ns, sig) + ";\n"
|
|
|
|
else:
|
|
|
|
r += "reg " + _printsig(ns, sig) + ";\n"
|
|
|
|
r += "\n"
|
|
|
|
return r
|
|
|
|
|
|
|
|
def _printcomb(f, ns):
|
|
|
|
r = ""
|
|
|
|
if f.comb.l:
|
|
|
|
# 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"
|
|
|
|
dummy_s = Signal(name="dummy_s")
|
|
|
|
dummy_d = Signal(name="dummy_d")
|
|
|
|
r += syn_off
|
|
|
|
r += "reg " + _printsig(ns, dummy_s) + ";\n"
|
|
|
|
r += "reg " + _printsig(ns, dummy_d) + ";\n"
|
|
|
|
r += "initial " + ns.get_name(dummy_s) + " <= 1'b0;\n"
|
|
|
|
r += syn_on + "\n"
|
|
|
|
|
|
|
|
r += "always @(*) begin\n"
|
2011-12-21 18:04:53 -05:00
|
|
|
to_reset = list_targets(f.comb)
|
|
|
|
# do not reset signals with obvious unconditional assignments
|
|
|
|
for s in f.comb.l:
|
|
|
|
if isinstance(s, _Assign) and isinstance(s.l, Signal):
|
|
|
|
try:
|
|
|
|
to_reset.remove(s.l)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
for t in to_reset:
|
|
|
|
r += "\t" + ns.get_name(t) + " <= " + str(t.reset) + ";\n"
|
2011-12-21 17:08:50 -05:00
|
|
|
r += _printnode(ns, False, 1, f.comb)
|
|
|
|
r += syn_off
|
|
|
|
r += "\t" + ns.get_name(dummy_d) + " <= " + ns.get_name(dummy_s) + ";\n"
|
|
|
|
r += syn_on
|
|
|
|
r += "end\n\n"
|
|
|
|
return r
|
|
|
|
|
|
|
|
def _printsync(f, ns, clk_signal, rst_signal):
|
|
|
|
r = ""
|
|
|
|
if f.sync.l:
|
|
|
|
r += "always @(posedge " + ns.get_name(clk_signal) + ") begin\n"
|
|
|
|
r += _printnode(ns, True, 1, insert_reset(rst_signal, f.sync))
|
|
|
|
r += "end\n\n"
|
|
|
|
return r
|
|
|
|
|
2011-12-08 10:35:32 -05:00
|
|
|
def _printinstances(ns, i, clk, rst):
|
|
|
|
r = ""
|
|
|
|
for x in i:
|
|
|
|
r += x.of + " "
|
|
|
|
if x.parameters:
|
|
|
|
r += "#(\n"
|
|
|
|
firstp = True
|
|
|
|
for p in x.parameters:
|
|
|
|
if not firstp:
|
|
|
|
r += ",\n"
|
|
|
|
firstp = False
|
|
|
|
r += "\t." + p[0] + "("
|
2011-12-17 08:59:27 -05:00
|
|
|
if isinstance(p[1], int) or isinstance(p[1], float) or isinstance(p[1], Constant):
|
2011-12-08 10:35:32 -05:00
|
|
|
r += str(p[1])
|
2011-12-11 14:17:51 -05:00
|
|
|
elif isinstance(p[1], str):
|
2011-12-08 10:35:32 -05:00
|
|
|
r += "\"" + p[1] + "\""
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
r += ")"
|
|
|
|
r += "\n) "
|
2011-12-16 10:02:55 -05:00
|
|
|
r += ns.get_name(x)
|
2011-12-11 14:17:51 -05:00
|
|
|
if x.parameters: r += " "
|
|
|
|
r += "(\n"
|
2011-12-08 10:35:32 -05:00
|
|
|
ports = list(x.ins.items()) + list(x.outs.items())
|
|
|
|
if x.clkport:
|
|
|
|
ports.append((x.clkport, clk))
|
|
|
|
if x.rstport:
|
|
|
|
ports.append((x.rstport, rst))
|
|
|
|
firstp = True
|
|
|
|
for p in ports:
|
|
|
|
if not firstp:
|
|
|
|
r += ",\n"
|
|
|
|
firstp = False
|
2011-12-16 10:02:55 -05:00
|
|
|
r += "\t." + p[0] + "(" + ns.get_name(p[1]) + ")"
|
2011-12-08 10:35:32 -05:00
|
|
|
if not firstp:
|
|
|
|
r += "\n"
|
|
|
|
r += ");\n\n"
|
|
|
|
return r
|
2012-01-05 13:27:33 -05:00
|
|
|
|
|
|
|
def convert(f, ios=set(), name="top", clk_signal=None, rst_signal=None, ns=None):
|
2011-12-16 16:25:05 -05:00
|
|
|
if clk_signal is None:
|
|
|
|
clk_signal = Signal(name="sys_clk")
|
|
|
|
ios.add(clk_signal)
|
|
|
|
if rst_signal is None:
|
|
|
|
rst_signal = Signal(name="sys_rst")
|
|
|
|
ios.add(rst_signal)
|
|
|
|
if ns is None:
|
|
|
|
ns = Namespace()
|
2011-12-12 18:24:40 -05:00
|
|
|
|
2011-12-10 14:25:24 -05:00
|
|
|
ios |= f.pads
|
2011-12-05 13:25:32 -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)
|
|
|
|
r += _printcomb(f, ns)
|
|
|
|
r += _printsync(f, ns, clk_signal, rst_signal)
|
2011-12-16 16:25:05 -05:00
|
|
|
r += _printinstances(ns, f.instances, clk_signal, rst_signal)
|
2011-12-04 16:26:32 -05:00
|
|
|
|
|
|
|
r += "endmodule\n"
|
|
|
|
|
2011-12-16 16:25:05 -05:00
|
|
|
return r
|