return Operator(">=", [self, other])
def __len__(self):
- return self.bits_sign()[0]
+ return self.shape()[0]
def __getitem__(self, key):
n = len(self)
"""
return Assign(self, value)
- def bits_sign(self):
+ def shape(self):
"""Bit length and signedness of a value.
Returns
Examples
--------
- >>> Value.bits_sign(Signal(8))
+ >>> Value.shape(Signal(8))
8, False
- >>> Value.bits_sign(C(0xaa))
+ >>> Value.shape(C(0xaa))
8, False
"""
raise NotImplementedError # :nocov:
Parameters
----------
value : int
- bits_sign : int or tuple or None
+ shape : int or tuple or None
Either an integer `bits` or a tuple `(bits, signed)`
specifying the number of bits in this `Const` and whether it is
- signed (can represent negative values). `bits_sign` defaults
+ signed (can represent negative values). `shape` defaults
to the minimum width and signedness of `value`.
Attributes
nbits : int
signed : bool
"""
- def __init__(self, value, bits_sign=None):
+ def __init__(self, value, shape=None):
self.value = int(value)
- if bits_sign is None:
- bits_sign = self.value.bit_length(), self.value < 0
- if isinstance(bits_sign, int):
- bits_sign = bits_sign, self.value < 0
- self.nbits, self.signed = bits_sign
+ if shape is None:
+ shape = self.value.bit_length(), self.value < 0
+ if isinstance(shape, int):
+ shape = shape, self.value < 0
+ self.nbits, self.signed = shape
if not isinstance(self.nbits, int) or self.nbits < 0:
raise TypeError("Width must be a positive integer")
- def bits_sign(self):
+ def shape(self):
return self.nbits, self.signed
def _rhs_signals(self):
self.operands = [Value.wrap(o) for o in operands]
@staticmethod
- def _bitwise_binary_bits_sign(a, b):
+ def _bitwise_binary_shape(a, b):
if not a[1] and not b[1]:
# both operands unsigned
return max(a[0], b[0]), False
# first signed, second operand unsigned (add sign bit)
return max(a[0], b[0] + 1), True
- def bits_sign(self):
- obs = list(map(lambda x: x.bits_sign(), self.operands))
+ def shape(self):
+ obs = list(map(lambda x: x.shape(), self.operands))
if self.op == "+" or self.op == "-":
if len(obs) == 1:
if self.op == "-" and not obs[0][1]:
return obs[0][0] + 1, True
else:
return obs[0]
- n, s = self._bitwise_binary_bits_sign(*obs)
+ n, s = self._bitwise_binary_shape(*obs)
return n + 1, s
elif self.op == "*":
if not obs[0][1] and not obs[1][1]:
extra = 0
return obs[0][0] + extra, obs[0][1]
elif self.op == "&" or self.op == "^" or self.op == "|":
- return self._bitwise_binary_bits_sign(*obs)
+ return self._bitwise_binary_shape(*obs)
elif (self.op == "<" or self.op == "<=" or self.op == "==" or self.op == "!=" or
self.op == ">" or self.op == ">=" or self.op == "b"):
return 1, False
elif self.op == "~":
return obs[0]
elif self.op == "m":
- return self._bitwise_binary_bits_sign(obs[1], obs[2])
+ return self._bitwise_binary_shape(obs[1], obs[2])
else:
raise TypeError # :nocov:
self.start = start
self.end = end
- def bits_sign(self):
+ def shape(self):
return self.end - self.start, False
def _lhs_signals(self):
self.offset = Value.wrap(offset)
self.width = width
- def bits_sign(self):
+ def shape(self):
return self.width, False
def _lhs_signals(self):
super().__init__()
self.operands = [Value.wrap(v) for v in flatten(args)]
- def bits_sign(self):
+ def shape(self):
return sum(len(op) for op in self.operands), False
def _lhs_signals(self):
self.value = Value.wrap(value)
self.count = count
- def bits_sign(self):
+ def shape(self):
return len(self.value) * self.count, False
def _rhs_signals(self):
Parameters
----------
- bits_sign : int or tuple or None
+ shape : int or tuple or None
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).
- ``bits_sign`` defaults to 1-bit and non-signed.
+ ``shape`` defaults to 1-bit and non-signed.
name : str
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
Defaults to ``False``.
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).
+ If ``shape`` 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).
attrs : dict
Dictionary of synthesis attributes.
attrs : dict
"""
- def __init__(self, bits_sign=None, name=None, reset=0, reset_less=False, min=None, max=None,
+ def __init__(self, shape=None, name=None, reset=0, reset_less=False, min=None, max=None,
attrs=None):
super().__init__()
name = "$signal"
self.name = name
- if bits_sign is None:
+ if shape is None:
if min is None:
min = 0
if max is None:
else:
if not (min is None and max is None):
raise ValueError("Only one of bits/signedness or bounds may be specified")
- if isinstance(bits_sign, int):
- self.nbits, self.signed = bits_sign, False
+ if isinstance(shape, int):
+ self.nbits, self.signed = shape, False
else:
- self.nbits, self.signed = bits_sign
+ self.nbits, self.signed = shape
if not isinstance(self.nbits, int) or self.nbits < 0:
raise TypeError("Width must be a positive integer, not {!r}".format(self.nbits))
other : Value
Object to base this Signal on.
"""
- kw = dict(bits_sign=cls.wrap(other).bits_sign())
+ kw = dict(shape=cls.wrap(other).shape())
if isinstance(other, cls):
kw.update(reset=other.reset, reset_less=other.reset_less, attrs=other.attrs)
kw.update(kwargs)
return cls(**kw)
- def bits_sign(self):
+ def shape(self):
return self.nbits, self.signed
def _lhs_signals(self):
class ConstTestCase(unittest.TestCase):
- def test_bits_sign(self):
- self.assertEqual(Const(0).bits_sign(), (0, False))
- self.assertEqual(Const(1).bits_sign(), (1, False))
- self.assertEqual(Const(10).bits_sign(), (4, False))
- self.assertEqual(Const(-10).bits_sign(), (4, True))
+ def test_shape(self):
+ self.assertEqual(Const(0).shape(), (0, False))
+ self.assertEqual(Const(1).shape(), (1, False))
+ self.assertEqual(Const(10).shape(), (4, False))
+ self.assertEqual(Const(-10).shape(), (4, True))
- self.assertEqual(Const(1, 4).bits_sign(), (4, False))
- self.assertEqual(Const(1, (4, True)).bits_sign(), (4, True))
+ self.assertEqual(Const(1, 4).shape(), (4, False))
+ self.assertEqual(Const(1, (4, True)).shape(), (4, True))
with self.assertRaises(TypeError):
Const(1, -1)
def test_invert(self):
v = ~Const(0, 4)
self.assertEqual(repr(v), "(~ (const 4'd0))")
- self.assertEqual(v.bits_sign(), (4, False))
+ self.assertEqual(v.shape(), (4, False))
def test_neg(self):
v1 = -Const(0, (4, False))
self.assertEqual(repr(v1), "(- (const 4'd0))")
- self.assertEqual(v1.bits_sign(), (5, True))
+ self.assertEqual(v1.shape(), (5, True))
v2 = -Const(0, (4, True))
self.assertEqual(repr(v2), "(- (const 4'sd0))")
- self.assertEqual(v2.bits_sign(), (4, True))
+ self.assertEqual(v2.shape(), (4, True))
def test_add(self):
v1 = Const(0, (4, False)) + Const(0, (6, False))
self.assertEqual(repr(v1), "(+ (const 4'd0) (const 6'd0))")
- self.assertEqual(v1.bits_sign(), (7, False))
+ self.assertEqual(v1.shape(), (7, False))
v2 = Const(0, (4, True)) + Const(0, (6, True))
- self.assertEqual(v2.bits_sign(), (7, True))
+ self.assertEqual(v2.shape(), (7, True))
v3 = Const(0, (4, True)) + Const(0, (4, False))
- self.assertEqual(v3.bits_sign(), (6, True))
+ self.assertEqual(v3.shape(), (6, True))
v4 = Const(0, (4, False)) + Const(0, (4, True))
- self.assertEqual(v4.bits_sign(), (6, True))
+ self.assertEqual(v4.shape(), (6, True))
v5 = 10 + Const(0, 4)
- self.assertEqual(v5.bits_sign(), (5, False))
+ self.assertEqual(v5.shape(), (5, False))
def test_sub(self):
v1 = Const(0, (4, False)) - Const(0, (6, False))
self.assertEqual(repr(v1), "(- (const 4'd0) (const 6'd0))")
- self.assertEqual(v1.bits_sign(), (7, False))
+ self.assertEqual(v1.shape(), (7, False))
v2 = Const(0, (4, True)) - Const(0, (6, True))
- self.assertEqual(v2.bits_sign(), (7, True))
+ self.assertEqual(v2.shape(), (7, True))
v3 = Const(0, (4, True)) - Const(0, (4, False))
- self.assertEqual(v3.bits_sign(), (6, True))
+ self.assertEqual(v3.shape(), (6, True))
v4 = Const(0, (4, False)) - Const(0, (4, True))
- self.assertEqual(v4.bits_sign(), (6, True))
+ self.assertEqual(v4.shape(), (6, True))
v5 = 10 - Const(0, 4)
- self.assertEqual(v5.bits_sign(), (5, False))
+ self.assertEqual(v5.shape(), (5, False))
def test_mul(self):
v1 = Const(0, (4, False)) * Const(0, (6, False))
self.assertEqual(repr(v1), "(* (const 4'd0) (const 6'd0))")
- self.assertEqual(v1.bits_sign(), (10, False))
+ self.assertEqual(v1.shape(), (10, False))
v2 = Const(0, (4, True)) * Const(0, (6, True))
- self.assertEqual(v2.bits_sign(), (9, True))
+ self.assertEqual(v2.shape(), (9, True))
v3 = Const(0, (4, True)) * Const(0, (4, False))
- self.assertEqual(v3.bits_sign(), (8, True))
+ self.assertEqual(v3.shape(), (8, True))
v5 = 10 * Const(0, 4)
- self.assertEqual(v5.bits_sign(), (8, False))
+ self.assertEqual(v5.shape(), (8, False))
def test_and(self):
v1 = Const(0, (4, False)) & Const(0, (6, False))
self.assertEqual(repr(v1), "(& (const 4'd0) (const 6'd0))")
- self.assertEqual(v1.bits_sign(), (6, False))
+ self.assertEqual(v1.shape(), (6, False))
v2 = Const(0, (4, True)) & Const(0, (6, True))
- self.assertEqual(v2.bits_sign(), (6, True))
+ self.assertEqual(v2.shape(), (6, True))
v3 = Const(0, (4, True)) & Const(0, (4, False))
- self.assertEqual(v3.bits_sign(), (5, True))
+ self.assertEqual(v3.shape(), (5, True))
v4 = Const(0, (4, False)) & Const(0, (4, True))
- self.assertEqual(v4.bits_sign(), (5, True))
+ self.assertEqual(v4.shape(), (5, True))
v5 = 10 & Const(0, 4)
- self.assertEqual(v5.bits_sign(), (4, False))
+ self.assertEqual(v5.shape(), (4, False))
def test_or(self):
v1 = Const(0, (4, False)) | Const(0, (6, False))
self.assertEqual(repr(v1), "(| (const 4'd0) (const 6'd0))")
- self.assertEqual(v1.bits_sign(), (6, False))
+ self.assertEqual(v1.shape(), (6, False))
v2 = Const(0, (4, True)) | Const(0, (6, True))
- self.assertEqual(v2.bits_sign(), (6, True))
+ self.assertEqual(v2.shape(), (6, True))
v3 = Const(0, (4, True)) | Const(0, (4, False))
- self.assertEqual(v3.bits_sign(), (5, True))
+ self.assertEqual(v3.shape(), (5, True))
v4 = Const(0, (4, False)) | Const(0, (4, True))
- self.assertEqual(v4.bits_sign(), (5, True))
+ self.assertEqual(v4.shape(), (5, True))
v5 = 10 | Const(0, 4)
- self.assertEqual(v5.bits_sign(), (4, False))
+ self.assertEqual(v5.shape(), (4, False))
def test_xor(self):
v1 = Const(0, (4, False)) ^ Const(0, (6, False))
self.assertEqual(repr(v1), "(^ (const 4'd0) (const 6'd0))")
- self.assertEqual(v1.bits_sign(), (6, False))
+ self.assertEqual(v1.shape(), (6, False))
v2 = Const(0, (4, True)) ^ Const(0, (6, True))
- self.assertEqual(v2.bits_sign(), (6, True))
+ self.assertEqual(v2.shape(), (6, True))
v3 = Const(0, (4, True)) ^ Const(0, (4, False))
- self.assertEqual(v3.bits_sign(), (5, True))
+ self.assertEqual(v3.shape(), (5, True))
v4 = Const(0, (4, False)) ^ Const(0, (4, True))
- self.assertEqual(v4.bits_sign(), (5, True))
+ self.assertEqual(v4.shape(), (5, True))
v5 = 10 ^ Const(0, 4)
- self.assertEqual(v5.bits_sign(), (4, False))
+ self.assertEqual(v5.shape(), (4, False))
def test_lt(self):
v = Const(0, 4) < Const(0, 6)
self.assertEqual(repr(v), "(< (const 4'd0) (const 6'd0))")
- self.assertEqual(v.bits_sign(), (1, False))
+ self.assertEqual(v.shape(), (1, False))
def test_le(self):
v = Const(0, 4) <= Const(0, 6)
self.assertEqual(repr(v), "(<= (const 4'd0) (const 6'd0))")
- self.assertEqual(v.bits_sign(), (1, False))
+ self.assertEqual(v.shape(), (1, False))
def test_gt(self):
v = Const(0, 4) > Const(0, 6)
self.assertEqual(repr(v), "(> (const 4'd0) (const 6'd0))")
- self.assertEqual(v.bits_sign(), (1, False))
+ self.assertEqual(v.shape(), (1, False))
def test_ge(self):
v = Const(0, 4) >= Const(0, 6)
self.assertEqual(repr(v), "(>= (const 4'd0) (const 6'd0))")
- self.assertEqual(v.bits_sign(), (1, False))
+ self.assertEqual(v.shape(), (1, False))
def test_eq(self):
v = Const(0, 4) == Const(0, 6)
self.assertEqual(repr(v), "(== (const 4'd0) (const 6'd0))")
- self.assertEqual(v.bits_sign(), (1, False))
+ self.assertEqual(v.shape(), (1, False))
def test_ne(self):
v = Const(0, 4) != Const(0, 6)
self.assertEqual(repr(v), "(!= (const 4'd0) (const 6'd0))")
- self.assertEqual(v.bits_sign(), (1, False))
+ self.assertEqual(v.shape(), (1, False))
def test_mux(self):
s = Const(0)
v1 = Mux(s, Const(0, (4, False)), Const(0, (6, False)))
self.assertEqual(repr(v1), "(m (const 0'd0) (const 4'd0) (const 6'd0))")
- self.assertEqual(v1.bits_sign(), (6, False))
+ self.assertEqual(v1.shape(), (6, False))
v2 = Mux(s, Const(0, (4, True)), Const(0, (6, True)))
- self.assertEqual(v2.bits_sign(), (6, True))
+ self.assertEqual(v2.shape(), (6, True))
v3 = Mux(s, Const(0, (4, True)), Const(0, (4, False)))
- self.assertEqual(v3.bits_sign(), (5, True))
+ self.assertEqual(v3.shape(), (5, True))
v4 = Mux(s, Const(0, (4, False)), Const(0, (4, True)))
- self.assertEqual(v4.bits_sign(), (5, True))
+ self.assertEqual(v4.shape(), (5, True))
def test_bool(self):
v = Const(0).bool()
self.assertEqual(repr(v), "(b (const 0'd0))")
- self.assertEqual(v.bits_sign(), (1, False))
+ self.assertEqual(v.shape(), (1, False))
def test_hash(self):
with self.assertRaises(TypeError):
class SliceTestCase(unittest.TestCase):
- def test_bits_sign(self):
+ def test_shape(self):
s1 = Const(10)[2]
- self.assertEqual(s1.bits_sign(), (1, False))
+ self.assertEqual(s1.shape(), (1, False))
s2 = Const(-10)[0:2]
- self.assertEqual(s2.bits_sign(), (2, False))
+ self.assertEqual(s2.shape(), (2, False))
def test_repr(self):
s1 = Const(10)[2]
class CatTestCase(unittest.TestCase):
- def test_bits_sign(self):
+ def test_shape(self):
c1 = Cat(Const(10))
- self.assertEqual(c1.bits_sign(), (4, False))
+ self.assertEqual(c1.shape(), (4, False))
c2 = Cat(Const(10), Const(1))
- self.assertEqual(c2.bits_sign(), (5, False))
+ self.assertEqual(c2.shape(), (5, False))
c3 = Cat(Const(10), Const(1), Const(0))
- self.assertEqual(c3.bits_sign(), (5, False))
+ self.assertEqual(c3.shape(), (5, False))
def test_repr(self):
c1 = Cat(Const(10), Const(1))
class ReplTestCase(unittest.TestCase):
- def test_bits_sign(self):
+ def test_shape(self):
r1 = Repl(Const(10), 3)
- self.assertEqual(r1.bits_sign(), (12, False))
+ self.assertEqual(r1.shape(), (12, False))
def test_count_wrong(self):
with self.assertRaises(TypeError):
class SignalTestCase(unittest.TestCase):
- def test_bits_sign(self):
+ def test_shape(self):
s1 = Signal()
- self.assertEqual(s1.bits_sign(), (1, False))
+ self.assertEqual(s1.shape(), (1, False))
s2 = Signal(2)
- self.assertEqual(s2.bits_sign(), (2, False))
+ self.assertEqual(s2.shape(), (2, False))
s3 = Signal((2, False))
- self.assertEqual(s3.bits_sign(), (2, False))
+ self.assertEqual(s3.shape(), (2, False))
s4 = Signal((2, True))
- self.assertEqual(s4.bits_sign(), (2, True))
+ self.assertEqual(s4.shape(), (2, True))
s5 = Signal(max=16)
- self.assertEqual(s5.bits_sign(), (4, False))
+ self.assertEqual(s5.shape(), (4, False))
s6 = Signal(min=4, max=16)
- self.assertEqual(s6.bits_sign(), (4, False))
+ self.assertEqual(s6.shape(), (4, False))
s7 = Signal(min=-4, max=16)
- self.assertEqual(s7.bits_sign(), (5, True))
+ self.assertEqual(s7.shape(), (5, True))
s8 = Signal(min=-20, max=16)
- self.assertEqual(s8.bits_sign(), (6, True))
+ self.assertEqual(s8.shape(), (6, True))
with self.assertRaises(ValueError):
Signal(min=10, max=4)
def test_like(self):
s1 = Signal.like(Signal(4))
- self.assertEqual(s1.bits_sign(), (4, False))
+ self.assertEqual(s1.shape(), (4, False))
s2 = Signal.like(Signal(min=-15))
- self.assertEqual(s2.bits_sign(), (5, True))
+ self.assertEqual(s2.shape(), (5, True))
s3 = Signal.like(Signal(4, reset=0b111, reset_less=True))
self.assertEqual(s3.reset, 0b111)
self.assertEqual(s3.reset_less, True)
s4 = Signal.like(Signal(attrs={"no_retiming": True}))
self.assertEqual(s4.attrs, {"no_retiming": True})
s5 = Signal.like(10)
- self.assertEqual(s5.bits_sign(), (4, False))
+ self.assertEqual(s5.shape(), (4, False))
class ClockSignalTestCase(unittest.TestCase):