Skip to content

Commit e1e1adf

Browse files
committed
lib.cdc: Add docstrings and basic parameter checking to MultiReg and ResetSynchroniser.
1 parent a74cacd commit e1e1adf

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

nmigen/lib/cdc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class MultiReg:
5050
MultiReg is reset by the ``odomain`` reset only.
5151
"""
5252
def __init__(self, i, o, odomain="sync", n=2, reset=0, reset_less=True):
53+
if not isinstance(n, int) or n < 1:
54+
raise TypeError("n must be a positive integer, not '{!r}'".format(n))
5355
self.i = i
5456
self.o = o
5557
self.odomain = odomain
@@ -70,7 +72,27 @@ def elaborate(self, platform):
7072

7173

7274
class ResetSynchronizer:
75+
"""Synchronize the deassertion of a reset to a local clock.
76+
77+
Output `assertion` is asynchronous, so the local clock need not be free-running.
78+
79+
Parameters
80+
----------
81+
arst : Signal(1), out
82+
Asynchronous reset signal, to be synchronized.
83+
domain : str
84+
Name of domain to synchronize reset to.
85+
n : int, >=1
86+
Number of clock edges from input deassertion to output deassertion
87+
88+
Override
89+
--------
90+
Define the ``get_reset_sync`` platform attribute to override the implementation of
91+
ResetSynchronizer, e.g. to instantiate library cells directly.
92+
"""
7393
def __init__(self, arst, domain="sync", n=2):
94+
if not isinstance(n, int) or n < 1:
95+
raise TypeError("n must be a positive integer, not '{!r}'".format(n))
7496
self.arst = arst
7597
self.domain = domain
7698

nmigen/test/test_lib_cdc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77

88

99
class MultiRegTestCase(FHDLTestCase):
10+
def test_paramcheck(self):
11+
i = Signal()
12+
o = Signal()
13+
with self.assertRaises(TypeError):
14+
m = MultiReg(i, o, n=0)
15+
with self.assertRaises(TypeError):
16+
m = MultiReg(i, o, n="x")
17+
with self.assertRaises(ValueError):
18+
m = MultiReg(i, o, n=2, reset="a")
19+
with self.assertRaises(TypeError):
20+
m = MultiReg(i, o, n=2, reset=i)
21+
m = MultiReg(i, o, n=1)
22+
m = MultiReg(i, o, reset=-1)
23+
1024
def test_basic(self):
1125
i = Signal()
1226
o = Signal()
@@ -45,6 +59,14 @@ def process():
4559

4660

4761
class ResetSynchronizerTestCase(FHDLTestCase):
62+
def test_paramcheck(self):
63+
arst = Signal()
64+
with self.assertRaises(TypeError):
65+
r = ResetSynchronizer(arst, n=0)
66+
with self.assertRaises(TypeError):
67+
r = ResetSynchronizer(arst, n="a")
68+
r = ResetSynchronizer(arst)
69+
4870
def test_basic(self):
4971
arst = Signal()
5072
m = Module()

0 commit comments

Comments
 (0)