mirror of
https://github.com/enjoy-digital/litex.git
synced 2025-01-04 09:52:26 -05:00
genlib/coding: gracefully handle flen(i) < 2
This commit is contained in:
parent
0836f2814a
commit
0023b742e4
2 changed files with 28 additions and 3 deletions
|
@ -26,7 +26,7 @@ class Encoder(Module):
|
|||
"""
|
||||
def __init__(self, width):
|
||||
self.i = Signal(width) # one-hot
|
||||
self.o = Signal(max=width) # binary
|
||||
self.o = Signal(max=max(2, width)) # binary
|
||||
self.n = Signal() # invalid: none or multiple
|
||||
act = dict((1<<j, self.o.eq(j)) for j in range(width))
|
||||
act["default"] = self.n.eq(1)
|
||||
|
@ -54,7 +54,7 @@ class PriorityEncoder(Module):
|
|||
"""
|
||||
def __init__(self, width):
|
||||
self.i = Signal(width) # one-hot, lsb has priority
|
||||
self.o = Signal(max=width) # binary
|
||||
self.o = Signal(max=max(2, width)) # binary
|
||||
self.n = Signal() # none
|
||||
for j in range(width)[::-1]: # last has priority
|
||||
self.comb += If(self.i[j], self.o.eq(j))
|
||||
|
@ -82,7 +82,7 @@ class Decoder(Module):
|
|||
"""
|
||||
|
||||
def __init__(self, width):
|
||||
self.i = Signal(max=width) # binary
|
||||
self.i = Signal(max=max(2, width)) # binary
|
||||
self.n = Signal() # none/invalid
|
||||
self.o = Signal(width) # one-hot
|
||||
act = dict((j, self.o.eq(1<<j)) for j in range(width))
|
||||
|
|
|
@ -75,3 +75,28 @@ class DecCase(SimCase, unittest.TestCase):
|
|||
else:
|
||||
self.assertEqual(o, 1<<i)
|
||||
self.run_with(cb, 256)
|
||||
|
||||
class SmallPrioEncCase(SimCase, unittest.TestCase):
|
||||
class TestBench(SimBench):
|
||||
def __init__(self):
|
||||
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)
|
||||
|
||||
def test_run_sequence(self):
|
||||
seq = list(range(1))
|
||||
def cb(tb, tbp):
|
||||
if seq:
|
||||
tbp.dut.i = seq.pop(0)
|
||||
i = tbp.dut.i
|
||||
if tbp.dut.n:
|
||||
self.assertEqual(i, 0)
|
||||
else:
|
||||
o = tbp.dut.o
|
||||
if o > 0:
|
||||
self.assertEqual(i & 1<<(o - 1), 0)
|
||||
self.assertGreaterEqual(i, 1<<o)
|
||||
self.run_with(cb, 5)
|
||||
|
|
Loading…
Reference in a new issue