2013-11-29 04:20:29 -05:00
|
|
|
import unittest
|
|
|
|
|
2015-09-12 07:34:07 -04:00
|
|
|
from migen import *
|
2013-11-29 04:20:29 -05:00
|
|
|
from migen.genlib.coding import *
|
|
|
|
|
2015-09-17 05:22:59 -04:00
|
|
|
from migen.test.support import SimCase
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-11-30 08:51:24 -05:00
|
|
|
class EncCase(SimCase, unittest.TestCase):
|
2015-09-17 05:22:59 -04:00
|
|
|
class TestBench(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.submodules.dut = Encoder(8)
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_sizes(self):
|
2015-09-26 06:45:10 -04:00
|
|
|
self.assertEqual(len(self.tb.dut.i), 8)
|
|
|
|
self.assertEqual(len(self.tb.dut.o), 3)
|
|
|
|
self.assertEqual(len(self.tb.dut.n), 1)
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_run_sequence(self):
|
|
|
|
seq = list(range(1<<8))
|
2015-09-17 05:22:59 -04:00
|
|
|
def gen():
|
|
|
|
for _ in range(256):
|
|
|
|
if seq:
|
2015-09-20 03:04:15 -04:00
|
|
|
yield self.tb.dut.i.eq(seq.pop(0))
|
2015-09-17 05:22:59 -04:00
|
|
|
if (yield self.tb.dut.n):
|
|
|
|
self.assertNotIn((yield self.tb.dut.i), [1<<i for i in range(8)])
|
|
|
|
else:
|
|
|
|
self.assertEqual((yield self.tb.dut.i), 1<<(yield self.tb.dut.o))
|
|
|
|
yield
|
|
|
|
self.run_with(gen())
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-11-30 08:51:24 -05:00
|
|
|
class PrioEncCase(SimCase, unittest.TestCase):
|
2015-09-17 05:22:59 -04:00
|
|
|
class TestBench(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.submodules.dut = PriorityEncoder(8)
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_sizes(self):
|
2015-09-26 06:45:10 -04:00
|
|
|
self.assertEqual(len(self.tb.dut.i), 8)
|
|
|
|
self.assertEqual(len(self.tb.dut.o), 3)
|
|
|
|
self.assertEqual(len(self.tb.dut.n), 1)
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_run_sequence(self):
|
|
|
|
seq = list(range(1<<8))
|
2015-09-17 05:22:59 -04:00
|
|
|
def gen():
|
|
|
|
for _ in range(256):
|
|
|
|
if seq:
|
2015-09-20 03:04:15 -04:00
|
|
|
yield self.tb.dut.i.eq(seq.pop(0))
|
2015-09-17 05:22:59 -04:00
|
|
|
i = yield self.tb.dut.i
|
|
|
|
if (yield self.tb.dut.n):
|
|
|
|
self.assertEqual(i, 0)
|
|
|
|
else:
|
|
|
|
o = yield self.tb.dut.o
|
|
|
|
if o > 0:
|
|
|
|
self.assertEqual(i & 1<<(o - 1), 0)
|
|
|
|
self.assertGreaterEqual(i, 1<<o)
|
|
|
|
yield
|
|
|
|
self.run_with(gen())
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-11-30 08:51:24 -05:00
|
|
|
class DecCase(SimCase, unittest.TestCase):
|
2015-09-17 05:22:59 -04:00
|
|
|
class TestBench(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.submodules.dut = Decoder(8)
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_sizes(self):
|
2015-09-26 06:45:10 -04:00
|
|
|
self.assertEqual(len(self.tb.dut.i), 3)
|
|
|
|
self.assertEqual(len(self.tb.dut.o), 8)
|
|
|
|
self.assertEqual(len(self.tb.dut.n), 1)
|
2013-11-29 04:20:29 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_run_sequence(self):
|
|
|
|
seq = list(range(8*2))
|
2015-09-17 05:22:59 -04:00
|
|
|
def gen():
|
|
|
|
for _ in range(256):
|
|
|
|
if seq:
|
|
|
|
i = seq.pop()
|
2015-09-20 03:04:15 -04:00
|
|
|
yield self.tb.dut.i.eq(i//2)
|
|
|
|
yield self.tb.dut.n.eq(i%2)
|
2015-09-17 05:22:59 -04:00
|
|
|
i = yield self.tb.dut.i
|
|
|
|
o = yield self.tb.dut.o
|
|
|
|
if (yield self.tb.dut.n):
|
|
|
|
self.assertEqual(o, 0)
|
|
|
|
else:
|
|
|
|
self.assertEqual(o, 1<<i)
|
|
|
|
self.run_with(gen())
|
2014-03-19 19:47:26 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2014-03-19 19:47:26 -04:00
|
|
|
class SmallPrioEncCase(SimCase, unittest.TestCase):
|
2015-09-17 05:22:59 -04:00
|
|
|
class TestBench(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.submodules.dut = PriorityEncoder(1)
|
2014-03-19 19:47:26 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_sizes(self):
|
2015-09-26 06:45:10 -04:00
|
|
|
self.assertEqual(len(self.tb.dut.i), 1)
|
|
|
|
self.assertEqual(len(self.tb.dut.o), 1)
|
|
|
|
self.assertEqual(len(self.tb.dut.n), 1)
|
2014-03-19 19:47:26 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_run_sequence(self):
|
|
|
|
seq = list(range(1))
|
2015-09-17 05:22:59 -04:00
|
|
|
def gen():
|
|
|
|
for _ in range(5):
|
|
|
|
if seq:
|
2015-09-20 03:04:15 -04:00
|
|
|
yield self.tb.dut.i.eq(seq.pop(0))
|
2015-09-17 05:22:59 -04:00
|
|
|
i = yield self.tb.dut.i
|
|
|
|
if (yield self.tb.dut.n):
|
|
|
|
self.assertEqual(i, 0)
|
|
|
|
else:
|
|
|
|
o = yield self.tb.dut.o
|
|
|
|
if o > 0:
|
|
|
|
self.assertEqual(i & 1<<(o - 1), 0)
|
|
|
|
self.assertGreaterEqual(i, 1<<o)
|
|
|
|
yield
|
|
|
|
self.run_with(gen())
|