Skip to content

Commit dd0a663

Browse files
committed
Enable doctest for memory.py.
1 parent 76f52c1 commit dd0a663

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

pyrtl/memory.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,31 @@ def __init__(self, bitwidth: int, addrwidth: int, name: str = '',
162162
---------------------------
163163
Simultaneous Read and Write
164164
---------------------------
165+
..
166+
# For ``doctest``.
167+
>>> import pyrtl
168+
>>> pyrtl.reset_working_block()
169+
165170
In PyRTL simulations, if the same address is read and written in the same cycle,
166171
the read will return the `last` value stored in the MemBlock, not the newly
167172
written value. Example::
168173
169-
mem = pyrtl.MemBlock(addrwidth=1, bitwidth=1)
170-
mem[0] <<= 1
171-
read_data = pyrtl.Output(name="read_data", bitwidth=1)
172-
read_data <<= mem[0]
174+
>>> mem = pyrtl.MemBlock(addrwidth=1, bitwidth=1)
175+
>>> mem[0] <<= 1
176+
>>> read_data = pyrtl.Output(name="read_data", bitwidth=1)
177+
>>> read_data <<= mem[0]
173178
174-
# In the first cycle, read_data will be the default MemBlock data value (0),
175-
# not the newly written value (1).
176-
sim = pyrtl.Simulation()
177-
sim.step()
178-
print("Cycle 0 read_data", sim.inspect("read_data"))
179+
>>> # In the first cycle, read_data will be the default MemBlock data value
180+
>>> # (0), not the newly written value (1).
181+
>>> sim = pyrtl.Simulation()
182+
>>> sim.step()
183+
>>> sim.inspect("read_data")
184+
0
179185
180186
# In the second cycle, read_data will be the newly written value (1).
181-
sim.step()
182-
print("Cycle 1 read_data", sim.inspect("read_data"))
187+
>>> sim.step()
188+
>>> sim.inspect("read_data")
189+
1
183190
184191
-----------------------------
185192
Mapping MemBlocks to Hardware

tests/test_memblock.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
import doctest
12
from random import randint
23
import unittest
34

45
import pyrtl
56

67

7-
# -------------------------------------------------------------------
8+
class TestDocTests(unittest.TestCase):
9+
"""Test documentation examples."""
10+
def test_doctests(self):
11+
failures, tests = doctest.testmod(m=pyrtl.memory)
12+
self.assertGreater(tests, 0)
13+
self.assertEqual(failures, 0)
14+
15+
816
class RTLMemBlockDesignBase(unittest.TestCase):
917
def setUp(self):
1018
pyrtl.reset_working_block()

0 commit comments

Comments
 (0)