2011-12-04 10:44:38 -05:00
|
|
|
import math
|
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def bits_for(n):
|
2011-12-09 07:11:34 -05:00
|
|
|
if isinstance(n, Constant):
|
|
|
|
return n.bv.width
|
2011-12-04 10:44:38 -05:00
|
|
|
else:
|
2011-12-09 07:11:34 -05:00
|
|
|
if n == 0:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return int(math.ceil(math.log(n+1, 2)))
|
2011-12-04 10:44:38 -05:00
|
|
|
|
|
|
|
class BV:
|
|
|
|
def __init__(self, width=1, signed=False):
|
|
|
|
self.width = width
|
|
|
|
self.signed = signed
|
|
|
|
|
2011-12-09 07:11:34 -05:00
|
|
|
def __repr__(self):
|
2011-12-04 10:44:38 -05:00
|
|
|
r = str(self.width) + "'"
|
|
|
|
if self.signed:
|
|
|
|
r += "s"
|
|
|
|
r += "d"
|
|
|
|
return r
|
|
|
|
|
|
|
|
class Value:
|
2011-12-08 15:15:44 -05:00
|
|
|
def __invert__(self):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("~", [self])
|
2011-12-08 15:15:44 -05:00
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
def __add__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("+", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __radd__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("+", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __sub__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("-", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rsub__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("-", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __mul__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("*", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rmul__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("*", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __lshift__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("<<", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rlshift__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("<<", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rshift__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator(">>", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rrshift__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator(">>", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __and__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("&", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rand__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("&", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __xor__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("^", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rxor__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("^", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __or__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("|", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __ror__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("|", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
|
|
|
|
def __lt__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("<", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __le__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("<=", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __eq__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("==", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __ne__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("!=", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __gt__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator(">", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __ge__(self, other):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator(">=", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, int):
|
|
|
|
return Slice(self, key, key+1)
|
|
|
|
elif isinstance(key, slice):
|
|
|
|
start = key.start or 0
|
|
|
|
stop = key.stop or self.bv.width
|
2011-12-07 16:21:30 -05:00
|
|
|
if stop > self.bv.width:
|
|
|
|
stop = self.bv.width
|
2011-12-04 10:44:38 -05:00
|
|
|
if key.step != None:
|
|
|
|
raise KeyError
|
|
|
|
return Slice(self, start, stop)
|
|
|
|
else:
|
|
|
|
raise KeyError
|
2011-12-16 15:30:14 -05:00
|
|
|
|
|
|
|
def eq(self, r):
|
|
|
|
return Assign(self, r)
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2011-12-16 15:30:14 -05:00
|
|
|
class _Operator(Value):
|
2011-12-04 10:44:38 -05:00
|
|
|
def __init__(self, op, operands):
|
|
|
|
self.op = op
|
|
|
|
self.operands = list(map(_cst, operands))
|
|
|
|
|
|
|
|
class Slice(Value):
|
|
|
|
def __init__(self, value, start, stop):
|
|
|
|
self.value = value
|
|
|
|
self.start = start
|
|
|
|
self.stop = stop
|
|
|
|
|
|
|
|
class Cat(Value):
|
|
|
|
def __init__(self, *args):
|
|
|
|
self.l = list(map(_cst, args))
|
|
|
|
|
2011-12-09 07:11:34 -05:00
|
|
|
class Replicate(Value):
|
|
|
|
def __init__(self, v, n):
|
|
|
|
self.v = v
|
|
|
|
self.n = n
|
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
class Constant(Value):
|
|
|
|
def __init__(self, n, bv=None):
|
2011-12-16 10:02:55 -05:00
|
|
|
self.bv = bv or BV(bits_for(n))
|
2011-12-04 10:44:38 -05:00
|
|
|
self.n = n
|
2011-12-09 07:11:34 -05:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self.bv) + str(self.n)
|
2011-12-04 10:44:38 -05:00
|
|
|
|
|
|
|
def _cst(x):
|
|
|
|
if isinstance(x, int):
|
|
|
|
return Constant(x)
|
|
|
|
else:
|
|
|
|
return x
|
|
|
|
|
|
|
|
class Signal(Value):
|
|
|
|
def __init__(self, bv=BV(), name="anonymous", variable=False, reset=0):
|
|
|
|
self.bv = bv
|
|
|
|
self.variable = variable
|
|
|
|
self.name = name
|
|
|
|
self.reset = Constant(reset, bv)
|
|
|
|
|
2011-12-04 16:26:32 -05:00
|
|
|
def __hash__(self):
|
|
|
|
return id(self)
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def declare_signal(parent, name, bv=BV(), variable=False, reset=0):
|
2011-12-08 15:28:20 -05:00
|
|
|
# try to find a meaningful prefix
|
|
|
|
if parent.__module__ == "__main__":
|
|
|
|
prefix = parent.__class__.__name__
|
|
|
|
else:
|
|
|
|
modules = parent.__module__.split('.')
|
|
|
|
prefix = modules[len(modules)-1]
|
|
|
|
setattr(parent, name, Signal(bv, prefix + "_" + name, variable, reset))
|
2011-12-04 10:44:38 -05:00
|
|
|
|
|
|
|
# statements
|
|
|
|
|
|
|
|
class Assign:
|
|
|
|
def __init__(self, l, r):
|
|
|
|
self.l = l
|
|
|
|
self.r = _cst(r)
|
|
|
|
|
|
|
|
class StatementList:
|
2011-12-11 14:17:29 -05:00
|
|
|
def __init__(self, l=None):
|
|
|
|
if l is None: l = []
|
2011-12-04 10:44:38 -05:00
|
|
|
self.l = l
|
|
|
|
|
2011-12-16 15:30:14 -05:00
|
|
|
class If:
|
|
|
|
def __init__(self, cond, *t):
|
|
|
|
self.cond = cond
|
|
|
|
self.t = StatementList(t)
|
|
|
|
self.f = StatementList()
|
|
|
|
|
|
|
|
def Else(self, *f):
|
|
|
|
self.f = StatementList(f)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def Elif(self, cond, *t):
|
|
|
|
self.f = StatementList([If(cond, *t)])
|
|
|
|
return self
|
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
def _sl(x):
|
|
|
|
if isinstance(x, list):
|
|
|
|
return StatementList(x)
|
|
|
|
else:
|
|
|
|
return x
|
|
|
|
|
2011-12-16 15:30:14 -05:00
|
|
|
class Default:
|
|
|
|
pass
|
2011-12-04 10:44:38 -05:00
|
|
|
|
|
|
|
class Case:
|
2011-12-16 15:30:14 -05:00
|
|
|
def __init__(self, test, *cases):
|
2011-12-04 10:44:38 -05:00
|
|
|
self.test = test
|
2011-12-16 15:30:14 -05:00
|
|
|
self.cases = [(c[0], StatementList(c[1:])) for c in cases if not isinstance(c[0], Default)]
|
|
|
|
self.default = None
|
|
|
|
for c in cases:
|
|
|
|
if isinstance(c[0], Default):
|
|
|
|
if self.default is not None:
|
|
|
|
raise ValueError
|
|
|
|
self.default = StatementList(c[1:])
|
|
|
|
if self.default is None:
|
|
|
|
self.default = StatementList()
|
2011-12-04 10:44:38 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
|
2011-12-08 10:35:32 -05:00
|
|
|
class Instance:
|
|
|
|
def __init__(self, of, outs=[], ins=[], parameters=[], clkport="", rstport="", name=""):
|
|
|
|
self.of = of
|
|
|
|
if name:
|
|
|
|
self.name = name
|
|
|
|
else:
|
|
|
|
self.name = of
|
2011-12-08 12:56:14 -05:00
|
|
|
def process_io(x):
|
|
|
|
if isinstance(x[1], Signal):
|
|
|
|
return x # override
|
|
|
|
elif isinstance(x[1], BV):
|
|
|
|
return (x[0], Signal(x[1], self.name + "_" + x[0]))
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
self.outs = dict(map(process_io, outs))
|
|
|
|
self.ins = dict(map(process_io, ins))
|
2011-12-08 10:35:32 -05:00
|
|
|
self.parameters = parameters
|
|
|
|
self.clkport = clkport
|
|
|
|
self.rstport = rstport
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return id(self)
|
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
class Fragment:
|
2011-12-11 14:17:29 -05:00
|
|
|
def __init__(self, comb=None, sync=None, instances=None, pads=set()):
|
|
|
|
if comb is None: comb = []
|
|
|
|
if sync is None: sync = []
|
|
|
|
if instances is None: instances = []
|
2011-12-04 10:44:38 -05:00
|
|
|
self.comb = _sl(comb)
|
|
|
|
self.sync = _sl(sync)
|
2011-12-08 10:35:32 -05:00
|
|
|
self.instances = instances
|
2011-12-10 14:25:24 -05:00
|
|
|
self.pads = pads
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2011-12-05 11:43:56 -05:00
|
|
|
def __add__(self, other):
|
2011-12-10 14:25:24 -05:00
|
|
|
return Fragment(self.comb.l + other.comb.l,
|
|
|
|
self.sync.l + other.sync.l,
|
|
|
|
self.instances + other.instances,
|
|
|
|
self.pads | other.pads)
|