prng = Random(7345)
for i in range(35):
print("{0:0{1}b} CE={2} bin={3}".format((yield dut.q),
- flen(dut.q), (yield dut.ce), (yield dut.q_binary)))
+ len(dut.q), (yield dut.ce), (yield dut.q_binary)))
yield dut.ce.eq(prng.getrandbits(1))
yield
(gpif.ctl, "in"), (gpif.adr, "out"),
(gpif.slwr, "out"), (gpif.sloe, "out"),
(gpif.slrd, "out"), (gpif.pktend, "out")]:
- if flen(i) > 1:
+ if len(i) > 1:
q = "(*)"
else:
q = ""
from migen.fhdl import structure as f
-__all__ = ["log2_int", "bits_for", "flen"]
+__all__ = ["log2_int", "bits_for", "value_bits_sign"]
def log2_int(n, need_pow2=True):
def value_bits_sign(v):
+ """Bit length and signedness of a value.
+
+ Parameters
+ ----------
+ v : Value
+
+ Returns
+ -------
+ int, bool
+ Number of bits required to store `v` or available in `v`, followed by
+ whether `v` has a sign bit (included in the bit count).
+
+ Examples
+ --------
+ >>> value_bits_sign(f.Signal(8))
+ 8, False
+ >>> value_bits_sign(C(0xaa))
+ 8, False
+ """
if isinstance(v, (f.Constant, f.Signal)):
return v.nbits, v.signed
elif isinstance(v, (f.ClockSignal, f.ResetSignal)):
else:
raise TypeError("Can not calculate bit length of {} {}".format(
type(v), v))
-
-
-def flen(v):
- """Bit length of an expression
-
- Parameters
- ----------
- v : int, bool or Value
-
- Returns
- -------
- int
- Number of bits required to store `v` or available in `v`
-
- Examples
- --------
- >>> flen(f.Signal(8))
- 8
- >>> flen(0xaa)
- 8
- """
- return value_bits_sign(v)[0]
-
def __ge__(self, other):
return _Operator(">=", [self, other])
+ def __len__(self):
+ from migen.fhdl.bitcontainer import value_bits_sign
+ return value_bits_sign(self)[0]
def __getitem__(self, key):
- from migen.fhdl.bitcontainer import flen
-
- n = flen(self)
+ n = len(self)
if isinstance(key, int):
if key >= n:
raise IndexError
meeting these properties. The bit length of the return value is the sum of
the bit lengths of the arguments::
- flen(Cat(args)) == sum(flen(arg) for arg in args)
+ len(Cat(args)) == sum(len(arg) for arg in args)
Parameters
----------
An input value is replicated (repeated) several times
to be used on the RHS of assignments::
- flen(Replicate(s, n)) == flen(s)*n
+ len(Replicate(s, n)) == len(s)*n
Parameters
----------
other : _Value
Object to base this Signal on.
- See `migen.fhdl.bitcontainer.value_bits_sign`() for details.
+ See `migen.fhdl.bitcontainer.value_bits_sign` for details.
"""
from migen.fhdl.bitcontainer import value_bits_sign
return cls(bits_sign=value_bits_sign(other), **kwargs)
from migen.fhdl.structure import *
from migen.fhdl.structure import _Operator, _Slice, _Assign, _Fragment
from migen.fhdl.tools import *
-from migen.fhdl.bitcontainer import bits_for, flen
+from migen.fhdl.bitcontainer import bits_for
from migen.fhdl.namer import build_namespace
from migen.fhdl.conv_output import ConvOutput
n = "signed "
else:
n = ""
- if flen(s) > 1:
- n += "[" + str(flen(s)-1) + ":0] "
+ if len(s) > 1:
+ n += "[" + str(len(s)-1) + ":0] "
n += ns.get_name(s)
return n
elif isinstance(node, _Slice):
# Verilog does not like us slicing non-array signals...
if isinstance(node.value, Signal) \
- and flen(node.value) == 1 \
+ and len(node.value) == 1 \
and node.start == 0 and node.stop == 1:
return _printexpr(ns, node.value)
from migen.fhdl.structure import *
from migen.fhdl.module import Module
from migen.fhdl.specials import Memory
-from migen.fhdl.bitcontainer import flen
from migen.genlib.cdc import NoRetiming, MultiReg, GrayCounter
from migen.genlib.record import layout_len, Record
def _inc(signal, modulo):
- if modulo == 2**flen(signal):
+ if modulo == 2**len(signal):
return signal.eq(signal + 1)
else:
return If(signal == (modulo - 1),
if shift is None:
return output.eq(signal)
if n is None:
- n = 2**flen(shift)
- w = flen(signal)
+ n = 2**len(shift)
+ w = len(signal)
if reverse:
r = reversed(range(n))
else:
if shift is None:
return output.eq(signal)
if n is None:
- n = 2**flen(shift)
- w = flen(output)
+ n = 2**len(shift)
+ w = len(output)
cases = {}
for i in range(n):
if reverse:
from migen.fhdl.structure import (_Value, _Statement,
_Operator, _Slice, _ArrayProxy,
_Assign, _Fragment)
-from migen.fhdl.bitcontainer import flen
from migen.fhdl.tools import list_signals, list_targets, insert_resets
from migen.fhdl.simplify import MemoryToArray
from migen.fhdl.specials import _MemoryLocation
shift = 0
r = 0
for element in node.l:
- nbits = flen(element)
+ nbits = len(element)
# make value always positive
r |= (self.eval(element, postcommit) & (2**nbits-1)) << shift
shift += nbits
self.modifications[node] = value
elif isinstance(node, Cat):
for element in node.l:
- nbits = flen(element)
+ nbits = len(element)
self.assign(element, value & (2**nbits-1))
value >>= nbits
elif isinstance(node, _Slice):
from itertools import count
-from migen.fhdl.bitcontainer import flen
from migen.fhdl.namer import build_namespace
code = next(codes)
self.codes[signal] = code
self.fo.write("$var wire {len} {code} {name} $end\n"
- .format(name=name, code=code, len=flen(signal)))
+ .format(name=name, code=code, len=len(signal)))
self.fo.write("$dumpvars\n")
for signal in signals:
value = signal.reset.value
raise
def _write_value(self, signal, value):
- l = flen(signal)
+ l = len(signal)
if value < 0:
value += 2**l
if l > 1:
self.submodules.dut = Encoder(8)
def test_sizes(self):
- self.assertEqual(flen(self.tb.dut.i), 8)
- self.assertEqual(flen(self.tb.dut.o), 3)
- self.assertEqual(flen(self.tb.dut.n), 1)
+ self.assertEqual(len(self.tb.dut.i), 8)
+ self.assertEqual(len(self.tb.dut.o), 3)
+ self.assertEqual(len(self.tb.dut.n), 1)
def test_run_sequence(self):
seq = list(range(1<<8))
self.submodules.dut = PriorityEncoder(8)
def test_sizes(self):
- self.assertEqual(flen(self.tb.dut.i), 8)
- self.assertEqual(flen(self.tb.dut.o), 3)
- self.assertEqual(flen(self.tb.dut.n), 1)
+ self.assertEqual(len(self.tb.dut.i), 8)
+ self.assertEqual(len(self.tb.dut.o), 3)
+ self.assertEqual(len(self.tb.dut.n), 1)
def test_run_sequence(self):
seq = list(range(1<<8))
self.submodules.dut = Decoder(8)
def test_sizes(self):
- self.assertEqual(flen(self.tb.dut.i), 3)
- self.assertEqual(flen(self.tb.dut.o), 8)
- self.assertEqual(flen(self.tb.dut.n), 1)
+ self.assertEqual(len(self.tb.dut.i), 3)
+ self.assertEqual(len(self.tb.dut.o), 8)
+ self.assertEqual(len(self.tb.dut.n), 1)
def test_run_sequence(self):
seq = list(range(8*2))
self.submodules.dut = PriorityEncoder(1)
def test_sizes(self):
- self.assertEqual(flen(self.tb.dut.i), 1)
- self.assertEqual(flen(self.tb.dut.o), 1)
- self.assertEqual(flen(self.tb.dut.n), 1)
+ self.assertEqual(len(self.tb.dut.i), 1)
+ self.assertEqual(len(self.tb.dut.o), 1)
+ self.assertEqual(len(self.tb.dut.n), 1)
def test_run_sequence(self):
seq = list(range(1))
]
def test_sizes(self):
- self.assertEqual(flen(self.tb.dut.din_bits), 64)
- self.assertEqual(flen(self.tb.dut.dout_bits), 64)
+ self.assertEqual(len(self.tb.dut.din_bits), 64)
+ self.assertEqual(len(self.tb.dut.dout_bits), 64)
def test_run_sequence(self):
seq = list(range(20))
self.j = C(-127)
self.s = Signal((13, True))
- def test_flen(self):
- self.assertEqual(flen(self.s), 13)
- self.assertEqual(flen(self.i), 8)
- self.assertEqual(flen(self.j), 8)
-
- def test_flen_type(self):
- self.assertRaises(TypeError, flen, [])
+ def test_len(self):
+ self.assertEqual(len(self.s), 13)
+ self.assertEqual(len(self.i), 8)
+ self.assertEqual(len(self.j), 8)
self.assertEqual(len(self.tb.dut.i), 8)
self.assertEqual(len(self.tb.dut.o), 8)
for i in range(8):
- self.assertEqual(flen(self.tb.dut.i[i]), 4)
- self.assertEqual(flen(self.tb.dut.o[i]), 4)
+ self.assertEqual(len(self.tb.dut.i[i]), 4)
+ self.assertEqual(len(self.tb.dut.o[i]), 4)
def test_sort(self):
def gen():
for repeat in range(20):
for i in self.tb.dut.i:
- yield i.eq(randrange(1<<flen(i)))
+ yield i.eq(randrange(1<<len(i)))
yield
self.assertEqual(sorted((yield self.tb.dut.i)),
(yield self.tb.dut.o))