2019-10-26 00:36:54 -06:00
|
|
|
# nmigen: UnusedElaboratable=no
|
|
|
|
|
2019-10-13 12:53:38 -06:00
|
|
|
from .utils import *
|
2019-06-04 02:18:50 -06:00
|
|
|
from ..hdl import *
|
2018-12-26 05:58:30 -07:00
|
|
|
from ..back.pysim import *
|
|
|
|
from ..lib.cdc import *
|
|
|
|
|
|
|
|
|
2019-09-23 08:17:44 -06:00
|
|
|
class FFSynchronizerTestCase(FHDLTestCase):
|
2019-09-23 13:38:21 -06:00
|
|
|
def test_stages_wrong(self):
|
|
|
|
with self.assertRaises(TypeError,
|
2019-10-11 05:47:42 -06:00
|
|
|
msg="Synchronization stage count must be a positive integer, not 0"):
|
2019-09-23 13:38:21 -06:00
|
|
|
FFSynchronizer(Signal(), Signal(), stages=0)
|
|
|
|
with self.assertRaises(ValueError,
|
|
|
|
msg="Synchronization stage count may not safely be less than 2"):
|
|
|
|
FFSynchronizer(Signal(), Signal(), stages=1)
|
|
|
|
|
2018-12-26 05:58:30 -07:00
|
|
|
def test_basic(self):
|
|
|
|
i = Signal()
|
|
|
|
o = Signal()
|
2019-09-23 08:17:44 -06:00
|
|
|
frag = FFSynchronizer(i, o)
|
2018-12-26 05:58:30 -07:00
|
|
|
with Simulator(frag) as sim:
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield i.eq(1)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
sim.add_process(process)
|
2018-12-29 08:02:44 -07:00
|
|
|
sim.run()
|
2018-12-26 05:58:30 -07:00
|
|
|
|
2019-01-26 11:07:59 -07:00
|
|
|
def test_reset_value(self):
|
2018-12-26 05:58:30 -07:00
|
|
|
i = Signal(reset=1)
|
|
|
|
o = Signal()
|
2019-09-23 08:17:44 -06:00
|
|
|
frag = FFSynchronizer(i, o, reset=1)
|
2018-12-26 05:58:30 -07:00
|
|
|
with Simulator(frag) as sim:
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield i.eq(0)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 1)
|
|
|
|
yield Tick()
|
|
|
|
self.assertEqual((yield o), 0)
|
|
|
|
sim.add_process(process)
|
2018-12-29 08:02:44 -07:00
|
|
|
sim.run()
|
2019-01-26 11:07:59 -07:00
|
|
|
|
|
|
|
|
|
|
|
class ResetSynchronizerTestCase(FHDLTestCase):
|
2019-09-23 13:38:21 -06:00
|
|
|
def test_stages_wrong(self):
|
|
|
|
with self.assertRaises(TypeError,
|
2019-10-11 05:47:42 -06:00
|
|
|
msg="Synchronization stage count must be a positive integer, not 0"):
|
2019-09-23 13:38:21 -06:00
|
|
|
ResetSynchronizer(Signal(), stages=0)
|
|
|
|
with self.assertRaises(ValueError,
|
|
|
|
msg="Synchronization stage count may not safely be less than 2"):
|
|
|
|
ResetSynchronizer(Signal(), stages=1)
|
|
|
|
|
2019-01-26 11:07:59 -07:00
|
|
|
def test_basic(self):
|
|
|
|
arst = Signal()
|
|
|
|
m = Module()
|
|
|
|
m.domains += ClockDomain("sync")
|
|
|
|
m.submodules += ResetSynchronizer(arst)
|
|
|
|
s = Signal(reset=1)
|
|
|
|
m.d.sync += s.eq(0)
|
|
|
|
|
|
|
|
with Simulator(m, vcd_file=open("test.vcd", "w")) as sim:
|
|
|
|
sim.add_clock(1e-6)
|
|
|
|
def process():
|
|
|
|
# initial reset
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
|
|
|
|
yield arst.eq(1)
|
|
|
|
yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield arst.eq(0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 1)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
self.assertEqual((yield s), 0)
|
|
|
|
yield Tick(); yield Delay(1e-8)
|
|
|
|
sim.add_process(process)
|
|
|
|
sim.run()
|