2011-12-18 15:26:51 -05:00
|
|
|
import inspect
|
|
|
|
import re
|
2012-11-29 15:22:38 -05:00
|
|
|
import builtins
|
2012-09-10 17:45:02 -04:00
|
|
|
from collections import defaultdict
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2012-03-06 12:33:44 -05:00
|
|
|
from migen.fhdl import tracer
|
2013-12-02 19:23:56 -05:00
|
|
|
from migen.util.misc import flat_iteration
|
2012-01-16 12:09:52 -05:00
|
|
|
|
2012-11-23 12:38:03 -05:00
|
|
|
class HUID:
|
|
|
|
__next_uid = 0
|
|
|
|
def __init__(self):
|
|
|
|
self.huid = HUID.__next_uid
|
|
|
|
HUID.__next_uid += 1
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return self.huid
|
|
|
|
|
|
|
|
class Value(HUID):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Base class for operands
|
|
|
|
|
|
|
|
Instances of `Value` or its subclasses can be operands to
|
|
|
|
arithmetic, comparison, bitwise, and logic operators.
|
|
|
|
They can be assigned (:meth:`eq`) or indexed/sliced (using the usual
|
|
|
|
Python indexing and slicing notation).
|
|
|
|
|
|
|
|
Values created from integers have the minimum bit width to necessary to
|
|
|
|
represent the integer.
|
|
|
|
"""
|
2011-12-08 15:15:44 -05:00
|
|
|
def __invert__(self):
|
2011-12-16 15:30:14 -05:00
|
|
|
return _Operator("~", [self])
|
2012-11-29 16:52:57 -05:00
|
|
|
def __neg__(self):
|
|
|
|
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):
|
2012-11-29 15:22:38 -05:00
|
|
|
return _Operator("<<<", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rlshift__(self, other):
|
2012-11-29 15:22:38 -05:00
|
|
|
return _Operator("<<<", [other, self])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rshift__(self, other):
|
2012-11-29 15:22:38 -05:00
|
|
|
return _Operator(">>>", [self, other])
|
2011-12-04 10:44:38 -05:00
|
|
|
def __rrshift__(self, other):
|
2012-11-29 15:22:38 -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):
|
2013-12-03 16:12:40 -05:00
|
|
|
from migen.fhdl.bitcontainer import flen
|
2013-05-22 11:11:09 -04:00
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
if isinstance(key, int):
|
2013-03-17 10:33:38 -04:00
|
|
|
if key < 0:
|
2013-05-22 11:11:09 -04:00
|
|
|
key += flen(self)
|
2011-12-21 16:57:07 -05:00
|
|
|
return _Slice(self, key, key+1)
|
2011-12-04 10:44:38 -05:00
|
|
|
elif isinstance(key, slice):
|
2013-06-27 15:31:00 -04:00
|
|
|
start, stop, step = key.indices(flen(self))
|
|
|
|
if step != 1:
|
2013-12-02 19:23:56 -05:00
|
|
|
return Cat(self[i] for i in range(start, stop, step))
|
2011-12-21 16:57:07 -05:00
|
|
|
return _Slice(self, start, stop)
|
2011-12-04 10:44:38 -05:00
|
|
|
else:
|
|
|
|
raise KeyError
|
2011-12-16 15:30:14 -05:00
|
|
|
|
|
|
|
def eq(self, r):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Assignment
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
r : Value, in
|
|
|
|
Value to be assigned.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
_Assign
|
|
|
|
Assignment statement that can be used in combinatorial or
|
|
|
|
synchronous context.
|
|
|
|
"""
|
2011-12-21 16:57:07 -05:00
|
|
|
return _Assign(self, r)
|
2012-11-06 07:51:51 -05:00
|
|
|
|
|
|
|
def __hash__(self):
|
2012-12-18 08:54:33 -05:00
|
|
|
return HUID.__hash__(self)
|
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):
|
2012-12-18 08:54:33 -05:00
|
|
|
Value.__init__(self)
|
2011-12-04 10:44:38 -05:00
|
|
|
self.op = op
|
2012-11-28 17:18:43 -05:00
|
|
|
self.operands = operands
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2013-08-11 17:53:33 -04:00
|
|
|
def Mux(sel, val1, val0):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Multiplex between two values
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
sel : Value(1), in
|
|
|
|
Selector.
|
|
|
|
val1 : Value(N), in
|
|
|
|
val0 : Value(N), in
|
|
|
|
Input values.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
Value(N), out
|
|
|
|
Output `Value`. If `sel` is asserted, the Mux returns
|
|
|
|
`val1`, else `val0`.
|
|
|
|
"""
|
2013-08-11 17:53:33 -04:00
|
|
|
return _Operator("m", [sel, val1, val0])
|
|
|
|
|
2011-12-21 16:57:07 -05:00
|
|
|
class _Slice(Value):
|
2011-12-04 10:44:38 -05:00
|
|
|
def __init__(self, value, start, stop):
|
2012-12-18 08:54:33 -05:00
|
|
|
Value.__init__(self)
|
2011-12-04 10:44:38 -05:00
|
|
|
self.value = value
|
|
|
|
self.start = start
|
|
|
|
self.stop = stop
|
|
|
|
|
|
|
|
class Cat(Value):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Concatenate values
|
|
|
|
|
|
|
|
Form a compound `Value` from several smaller ones by concatenation.
|
|
|
|
The first argument occupies the lower bits of the result.
|
|
|
|
The return value can be used on either side of an assignment, that
|
|
|
|
is, the concatenated value can be used as an argument on the RHS or
|
|
|
|
as a target on the LHS. If it is used on the LHS, it must solely
|
2013-11-29 16:35:53 -05:00
|
|
|
consist of `Signal` s, slices of `Signal` s, and other concatenations
|
|
|
|
meeting these properties. The bit length of the return value is the sum of
|
2013-11-29 01:34:06 -05:00
|
|
|
the bit lengths of the arguments::
|
|
|
|
|
2013-12-02 19:23:56 -05:00
|
|
|
flen(Cat(args)) == sum(flen(arg) for arg in args)
|
2013-11-29 01:34:06 -05:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2013-12-02 19:23:56 -05:00
|
|
|
*args : Values or iterables of Values, inout
|
2013-11-29 01:34:06 -05:00
|
|
|
`Value` s to be concatenated.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
Cat, inout
|
|
|
|
Resulting `Value` obtained by concatentation.
|
|
|
|
"""
|
2011-12-04 10:44:38 -05:00
|
|
|
def __init__(self, *args):
|
2012-12-18 08:54:33 -05:00
|
|
|
Value.__init__(self)
|
2013-12-02 19:23:56 -05:00
|
|
|
self.l = list(flat_iteration(args))
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2011-12-09 07:11:34 -05:00
|
|
|
class Replicate(Value):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Replicate a value
|
|
|
|
|
|
|
|
An input value is replicated (repeated) several times
|
|
|
|
to be used on the RHS of assignments::
|
|
|
|
|
|
|
|
flen(Replicate(s, n)) == flen(s)*n
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
v : Value, in
|
|
|
|
Input value to be replicated.
|
|
|
|
n : int
|
|
|
|
Number of replications.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
Replicate, out
|
|
|
|
Replicated value.
|
|
|
|
"""
|
2011-12-09 07:11:34 -05:00
|
|
|
def __init__(self, v, n):
|
2012-12-18 08:54:33 -05:00
|
|
|
Value.__init__(self)
|
2012-11-28 17:18:43 -05:00
|
|
|
self.v = v
|
2011-12-09 07:11:34 -05:00
|
|
|
self.n = n
|
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
class Signal(Value):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""A `Value` that can change
|
|
|
|
|
|
|
|
The `Signal` object represents a value that is expected to change
|
|
|
|
in the circuit. It does exactly what Verilog's `wire` and
|
|
|
|
`reg` and VHDL's `signal` do.
|
|
|
|
|
|
|
|
A `Signal` can be indexed to access a subset of its bits. Negative
|
|
|
|
indices (`signal[-1]`) and the extended Python slicing notation
|
|
|
|
(`signal[start:stop:step]`) are supported.
|
|
|
|
The indeces 0 and -1 are the least and most significant bits
|
|
|
|
respectively.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
bits_sign : int or tuple
|
|
|
|
Either an integer `bits` or a tuple `(bits, signed)`
|
|
|
|
specifying the number of bits in this `Signal` and whether it is
|
|
|
|
signed (can represent negative values). `signed` defaults to
|
|
|
|
`False`.
|
|
|
|
name : str or None
|
|
|
|
Name hint for this signal. If `None` (default) the name is
|
|
|
|
inferred from the variable name this `Signal` is assigned to.
|
|
|
|
Name collisions are automatically resolved by prepending
|
|
|
|
names of objects that contain this `Signal` and by
|
|
|
|
appending integer sequences.
|
|
|
|
variable : bool
|
|
|
|
Deprecated.
|
|
|
|
reset : int
|
|
|
|
Reset (synchronous) or default (combinatorial) value.
|
|
|
|
When this `Signal` is assigned to in synchronous context and the
|
|
|
|
corresponding clock domain is reset, the `Signal` assumes the
|
|
|
|
given value. When this `Signal` is unassigned in combinatorial
|
|
|
|
context (due to conditional assignments not being taken),
|
|
|
|
the `Signal` assumes its `reset` value. Defaults to 0.
|
|
|
|
name_override : str or None
|
|
|
|
Do not use the inferred name but the given one.
|
|
|
|
min : int or None
|
|
|
|
max : int or None
|
|
|
|
If `bits_sign` is `None`, the signal bit width and signedness are
|
|
|
|
determined by the integer range given by `min` (inclusive,
|
|
|
|
defaults to 0) and `max` (exclusive, defaults to 2).
|
|
|
|
related : Signal or None
|
|
|
|
"""
|
2013-08-08 05:32:37 -04:00
|
|
|
def __init__(self, bits_sign=None, name=None, variable=False, reset=0, name_override=None, min=None, max=None, related=None):
|
2013-12-03 16:12:40 -05:00
|
|
|
from migen.fhdl.bitcontainer import bits_for
|
2013-05-22 11:11:09 -04:00
|
|
|
|
2012-12-18 08:54:33 -05:00
|
|
|
Value.__init__(self)
|
2012-11-29 15:22:38 -05:00
|
|
|
|
|
|
|
# determine number of bits and signedness
|
|
|
|
if bits_sign is None:
|
|
|
|
if min is None:
|
|
|
|
min = 0
|
|
|
|
if max is None:
|
|
|
|
max = 2
|
|
|
|
max -= 1 # make both bounds inclusive
|
|
|
|
assert(min < max)
|
|
|
|
self.signed = min < 0 or max < 0
|
|
|
|
self.nbits = builtins.max(bits_for(min, self.signed), bits_for(max, self.signed))
|
|
|
|
else:
|
|
|
|
assert(min is None and max is None)
|
|
|
|
if isinstance(bits_sign, tuple):
|
|
|
|
self.nbits, self.signed = bits_sign
|
|
|
|
else:
|
|
|
|
self.nbits, self.signed = bits_sign, False
|
|
|
|
assert(isinstance(self.nbits, int))
|
|
|
|
|
2013-06-30 14:14:20 -04:00
|
|
|
self.variable = variable # deprecated
|
2012-11-28 17:18:43 -05:00
|
|
|
self.reset = reset
|
2012-01-16 12:09:52 -05:00
|
|
|
self.name_override = name_override
|
2013-08-08 13:22:17 -04:00
|
|
|
self.backtrace = tracer.trace_back(name)
|
|
|
|
self.related = related
|
2012-09-11 03:59:37 -04:00
|
|
|
|
2012-01-06 05:20:33 -05:00
|
|
|
def __repr__(self):
|
2012-09-09 13:33:55 -04:00
|
|
|
return "<Signal " + (self.backtrace[-1][0] or "anonymous") + " at " + hex(id(self)) + ">"
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2013-03-18 13:36:50 -04:00
|
|
|
class ClockSignal(Value):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Clock signal for a given clock domain
|
|
|
|
|
|
|
|
`ClockSignal` s for a given clock domain can be retrieved multiple
|
|
|
|
times. They all ultimately refer to the same signal.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cd : str
|
|
|
|
Clock domain to obtain a clock signal for. Defaults to `"sys"`.
|
|
|
|
"""
|
2013-03-18 13:36:50 -04:00
|
|
|
def __init__(self, cd="sys"):
|
|
|
|
Value.__init__(self)
|
|
|
|
self.cd = cd
|
|
|
|
|
|
|
|
class ResetSignal(Value):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Reset signal for a given clock domain
|
|
|
|
|
|
|
|
`ResetSignal` s for a given clock domain can be retrieved multiple
|
|
|
|
times. They all ultimately refer to the same signal.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cd : str
|
|
|
|
Clock domain to obtain a reset signal for. Defaults to `"sys"`.
|
|
|
|
"""
|
2013-03-18 13:36:50 -04:00
|
|
|
def __init__(self, cd="sys"):
|
|
|
|
Value.__init__(self)
|
|
|
|
self.cd = cd
|
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
# statements
|
|
|
|
|
2011-12-21 16:57:07 -05:00
|
|
|
class _Assign:
|
2011-12-04 10:44:38 -05:00
|
|
|
def __init__(self, l, r):
|
|
|
|
self.l = l
|
2012-11-28 17:18:43 -05:00
|
|
|
self.r = r
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2011-12-16 15:30:14 -05:00
|
|
|
class If:
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Conditional execution of statements
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cond : Value(1), in
|
|
|
|
Condition
|
|
|
|
*t : Statements
|
|
|
|
Statements to execute if `cond` is asserted.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
>>> a = Signal()
|
|
|
|
>>> b = Signal()
|
|
|
|
>>> c = Signal()
|
|
|
|
>>> d = Signal()
|
|
|
|
>>> If(a,
|
|
|
|
... b.eq(1)
|
|
|
|
... ).Elif(c,
|
|
|
|
... b.eq(0)
|
|
|
|
... ).Else(
|
|
|
|
... b.eq(d)
|
|
|
|
... )
|
|
|
|
"""
|
2011-12-16 15:30:14 -05:00
|
|
|
def __init__(self, cond, *t):
|
|
|
|
self.cond = cond
|
2012-07-13 11:07:56 -04:00
|
|
|
self.t = list(t)
|
|
|
|
self.f = []
|
2011-12-16 15:30:14 -05:00
|
|
|
|
|
|
|
def Else(self, *f):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Add an `else` conditional block
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
*f : Statements
|
|
|
|
Statements to execute if all previous conditions fail.
|
|
|
|
"""
|
2012-07-13 11:07:56 -04:00
|
|
|
_insert_else(self, list(f))
|
2011-12-16 15:30:14 -05:00
|
|
|
return self
|
|
|
|
|
|
|
|
def Elif(self, cond, *t):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Add an `else if` conditional block
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cond : Value(1), in
|
|
|
|
Condition
|
|
|
|
*t : Statements
|
|
|
|
Statements to execute if previous conditions fail and `cond`
|
|
|
|
is asserted.
|
|
|
|
"""
|
2012-07-13 11:07:56 -04:00
|
|
|
_insert_else(self, [If(cond, *t)])
|
2011-12-16 15:30:14 -05:00
|
|
|
return self
|
|
|
|
|
2011-12-17 14:31:42 -05:00
|
|
|
def _insert_else(obj, clause):
|
|
|
|
o = obj
|
2012-07-13 11:07:56 -04:00
|
|
|
while o.f:
|
|
|
|
assert(len(o.f) == 1)
|
|
|
|
assert(isinstance(o.f[0], If))
|
|
|
|
o = o.f[0]
|
2011-12-17 14:31:42 -05:00
|
|
|
o.f = clause
|
|
|
|
|
2011-12-04 10:44:38 -05:00
|
|
|
class Case:
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Case/Switch statement
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
test : Value, in
|
|
|
|
Selector value used to decide which block to execute
|
|
|
|
cases : dict
|
|
|
|
Dictionary of cases. The keys are numeric constants to compare
|
|
|
|
with `test`. The values are statements to be executed the
|
|
|
|
corresponding key matches `test`. The dictionary may contain a
|
|
|
|
string key `"default"` to mark a fall-through case that is
|
|
|
|
executed if no other key matches.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
>>> a = Signal()
|
|
|
|
>>> b = Signal()
|
|
|
|
>>> Case(a, {
|
|
|
|
... 0: b.eq(1),
|
|
|
|
... 1: b.eq(0),
|
|
|
|
... "default": b.eq(0),
|
|
|
|
... })
|
|
|
|
"""
|
2012-11-28 19:11:15 -05:00
|
|
|
def __init__(self, test, cases):
|
2011-12-04 10:44:38 -05:00
|
|
|
self.test = test
|
2012-11-28 19:11:15 -05:00
|
|
|
self.cases = cases
|
|
|
|
|
|
|
|
def makedefault(self, key=None):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Mark a key as the default case
|
|
|
|
|
|
|
|
Deletes/Substitutes any previously existing default case.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
key : int or None
|
|
|
|
Key to use as default case if no other key matches.
|
|
|
|
By default, the largest key is the default key.
|
|
|
|
"""
|
2012-11-28 19:11:15 -05:00
|
|
|
if key is None:
|
|
|
|
for choice in self.cases.keys():
|
|
|
|
if key is None or choice > key:
|
|
|
|
key = choice
|
|
|
|
self.cases["default"] = self.cases[key]
|
|
|
|
del self.cases[key]
|
|
|
|
return self
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2012-07-09 09:16:38 -04:00
|
|
|
# arrays
|
|
|
|
|
|
|
|
class _ArrayProxy(Value):
|
|
|
|
def __init__(self, choices, key):
|
|
|
|
self.choices = choices
|
|
|
|
self.key = key
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return _ArrayProxy([getattr(choice, attr) for choice in self.choices],
|
|
|
|
self.key)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return _ArrayProxy([choice.__getitem__(key) for choice in self.choices],
|
|
|
|
self.key)
|
|
|
|
|
|
|
|
class Array(list):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Addressable multiplexer
|
|
|
|
|
|
|
|
An array is created from an iterable of values and indexed using the
|
|
|
|
usual Python simple indexing notation (no negative indices or
|
|
|
|
slices). It can be indexed by numeric constants, `Value` s, or
|
|
|
|
`Signal` s.
|
|
|
|
|
|
|
|
The result of indexing the array is a proxy for the entry at the
|
|
|
|
given index that can be used on either RHS or LHS of assignments.
|
|
|
|
|
|
|
|
An array can be indexed multiple times.
|
|
|
|
|
|
|
|
Multidimensional arrays are supported by packing inner arrays into
|
|
|
|
outer arrays.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
values : iterable of ints, Values, Signals
|
|
|
|
Entries of the array. Each entry can be a numeric constant, a
|
|
|
|
`Signal` or a `Record`.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
>>> a = Array(range(10))
|
|
|
|
>>> b = Signal(max=10)
|
|
|
|
>>> c = Signal(max=10)
|
|
|
|
>>> b.eq(a[9 - c])
|
|
|
|
"""
|
2012-07-09 09:16:38 -04:00
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, Value):
|
|
|
|
return _ArrayProxy(self, key)
|
|
|
|
else:
|
2012-12-18 08:54:33 -05:00
|
|
|
return list.__getitem__(self, key)
|
2012-07-09 09:16:38 -04:00
|
|
|
|
2013-03-15 13:17:33 -04:00
|
|
|
class ClockDomain:
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Synchronous domain
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
name : str or None
|
|
|
|
Domain name. If None (the default) the name is inferred from the
|
|
|
|
variable name this `ClockDomain` is assigned to (stripping any
|
|
|
|
`"cd_"` prefix).
|
|
|
|
reset_less : bool
|
|
|
|
The domain does not use a reset signal. Registers within this
|
|
|
|
domain are still all initialized to their reset state once, e.g.
|
|
|
|
through Verilog `"initial"` statements.
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
clk : Signal, inout
|
|
|
|
The clock for this domain. Can be driven or used to drive other
|
|
|
|
signals (preferably in combinatorial context).
|
|
|
|
rst : Signal or None, inout
|
|
|
|
Reset signal for this domain. Can be driven or used to drive.
|
|
|
|
"""
|
2013-04-23 05:53:37 -04:00
|
|
|
def __init__(self, name=None, reset_less=False):
|
2013-03-15 13:17:33 -04:00
|
|
|
self.name = tracer.get_obj_var_name(name)
|
|
|
|
if self.name is None:
|
|
|
|
raise ValueError("Cannot extract clock domain name from code, need to specify.")
|
|
|
|
if len(self.name) > 3 and self.name[:3] == "cd_":
|
|
|
|
self.name = self.name[3:]
|
|
|
|
self.clk = Signal(name_override=self.name + "_clk")
|
2013-04-23 05:53:37 -04:00
|
|
|
if reset_less:
|
|
|
|
self.rst = None
|
|
|
|
else:
|
|
|
|
self.rst = Signal(name_override=self.name + "_rst")
|
2013-03-15 13:17:33 -04:00
|
|
|
|
|
|
|
def rename(self, new_name):
|
2013-11-29 01:34:06 -05:00
|
|
|
"""Rename the clock domain
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
new_name : str
|
|
|
|
New name
|
|
|
|
"""
|
2013-03-15 13:17:33 -04:00
|
|
|
self.name = new_name
|
|
|
|
self.clk.name_override = new_name + "_clk"
|
2013-04-23 05:53:37 -04:00
|
|
|
if self.rst is not None:
|
|
|
|
self.rst.name_override = new_name + "_rst"
|
2013-03-15 13:17:33 -04:00
|
|
|
|
|
|
|
class _ClockDomainList(list):
|
|
|
|
def __getitem__(self, key):
|
|
|
|
if isinstance(key, str):
|
|
|
|
for cd in self:
|
|
|
|
if cd.name == key:
|
|
|
|
return cd
|
|
|
|
raise KeyError(key)
|
|
|
|
else:
|
|
|
|
return list.__getitem__(self, key)
|
|
|
|
|
2013-03-18 13:36:50 -04:00
|
|
|
(SPECIAL_INPUT, SPECIAL_OUTPUT, SPECIAL_INOUT) = range(3)
|
|
|
|
|
2014-01-26 16:19:43 -05:00
|
|
|
class StopSimulation(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-07-25 12:52:54 -04:00
|
|
|
class _Fragment:
|
2013-03-15 13:17:33 -04:00
|
|
|
def __init__(self, comb=None, sync=None, specials=None, clock_domains=None, sim=None):
|
2011-12-11 14:17:29 -05:00
|
|
|
if comb is None: comb = []
|
2012-09-10 17:45:02 -04:00
|
|
|
if sync is None: sync = dict()
|
2013-02-22 11:56:35 -05:00
|
|
|
if specials is None: specials = set()
|
2013-03-15 13:17:33 -04:00
|
|
|
if clock_domains is None: clock_domains = _ClockDomainList()
|
2012-03-06 07:58:22 -05:00
|
|
|
if sim is None: sim = []
|
2012-09-10 17:45:02 -04:00
|
|
|
|
2012-07-13 11:07:56 -04:00
|
|
|
self.comb = comb
|
|
|
|
self.sync = sync
|
2013-07-25 12:52:54 -04:00
|
|
|
self.specials = specials
|
2013-03-15 13:17:33 -04:00
|
|
|
self.clock_domains = _ClockDomainList(clock_domains)
|
2012-03-06 07:58:22 -05:00
|
|
|
self.sim = sim
|
2011-12-04 10:44:38 -05:00
|
|
|
|
2011-12-05 11:43:56 -05:00
|
|
|
def __add__(self, other):
|
2012-09-10 17:45:02 -04:00
|
|
|
newsync = defaultdict(list)
|
|
|
|
for k, v in self.sync.items():
|
|
|
|
newsync[k] = v[:]
|
|
|
|
for k, v in other.sync.items():
|
|
|
|
newsync[k].extend(v)
|
2013-07-25 12:52:54 -04:00
|
|
|
return _Fragment(self.comb + other.comb, newsync,
|
2013-02-22 11:56:35 -05:00
|
|
|
self.specials | other.specials,
|
2013-03-15 13:17:33 -04:00
|
|
|
self.clock_domains + other.clock_domains,
|
2012-03-06 07:58:22 -05:00
|
|
|
self.sim + other.sim)
|
2013-08-11 17:11:30 -04:00
|
|
|
|
|
|
|
def __iadd__(self, other):
|
|
|
|
newsync = defaultdict(list)
|
|
|
|
for k, v in self.sync.items():
|
|
|
|
newsync[k] = v[:]
|
|
|
|
for k, v in other.sync.items():
|
|
|
|
newsync[k].extend(v)
|
|
|
|
self.comb += other.comb
|
|
|
|
self.sync = newsync
|
|
|
|
self.specials |= other.specials
|
|
|
|
self.clock_domains += other.clock_domains
|
|
|
|
self.sim += other.sim
|
|
|
|
return self
|