Skip to content

Commit a24b649

Browse files
committed
fix return value for unconnected signals in capture
Key Changes: - Updated ClockedSignal.capture to return the default value for unconnected signals. - Added unit test to verify capture behavior with default values. - Mocked ReadOnlyManager.wait in tests to support capture verification without a simulator.
1 parent 0696550 commit a24b649

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,10 @@ bus = MyBus.from_pattern(dut, pattern="/u_axi_[0-9]_%/")
199199

200200
While `from_entity` is strict, `from_pattern` allows for flexibility when the RTL structure is non-standard:
201201

202-
- **Keyword Overrides:** Pass a signal handle as a keyword argument to skip the pattern search for that specific attribute.
203-
- **Indexing:** Use the `idx` argument to index into all signals discovered via the pattern (e.g., `dut.u_axi_tdata[1]`).
202+
- **Keyword Overrides:** Pass a signal handle as a keyword argument to skip the pattern search for that specific
203+
attribute.
204+
- **Indexing:** Use the `idx` argument to index into all signals discovered via the pattern (e.g.,
205+
`dut.u_axi_tdata[1]`).
204206

205207
```python
206208
# Connect to index 1 and manually override 'rdy'
@@ -228,8 +230,8 @@ edge and the defined input skew before returning the value.
228230

229231
The `capture()` method accepts two optional arguments:
230232

231-
- **`resolver`**: A resolution function (e.g., `cocotb.types.Logic.resolve`) to resolve 4-state logic values ('X', 'Z') to
232-
a simpler 2-state representation.
233+
- **`resolver`**: A resolution function (e.g., `cocotb.types.Logic.resolve`) to resolve 4-state logic values ('X', 'Z')
234+
to a simpler 2-state representation.
233235
- **`default`**: The value returned if the signal is an **optional signal** that is unconnected.
234236

235237
```python

cocotbext/interface/interface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ async def capture(self, resolver=None, default: Any = None):
112112
else:
113113
logger.warning(f"{self._handle} is not resolvable")
114114
return value
115+
return default
115116

116117
def __getattr__(self, name):
117118
"""Forward other cocotb handle methods (like ._name, etc)"""

tests/test_interface.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
@pytest.fixture(autouse=True)
3434
def mock_cocotb_scheduler(monkeypatch):
35-
"""Auto-patch cocotb.start_soon to avoid scheduler errors in tests.
35+
"""Auto-patch cocotb.start_soon and ReadOnlyManager to avoid scheduler errors in tests.
3636
3737
Properly closes coroutines to avoid RuntimeWarning about unawaited coroutines.
3838
"""
@@ -42,7 +42,11 @@ def mock_start_soon(coro):
4242
if inspect.iscoroutine(coro):
4343
coro.close()
4444

45+
async def mock_wait():
46+
pass
47+
4548
monkeypatch.setattr("cocotb.start_soon", mock_start_soon)
49+
monkeypatch.setattr("cocotbext.interface.interface.ReadOnlyManager.wait", mock_wait)
4650

4751

4852
# ============================================================================
@@ -577,6 +581,24 @@ async def inner():
577581

578582
asyncio.run(inner())
579583

584+
def test_clocked_signal_capture_default(self, mock_hierarchy):
585+
"""Test capturing with a default value for unconnected signals."""
586+
587+
async def inner():
588+
clk = mock_hierarchy._children["clk"]
589+
# Signal with no handle
590+
cs = ClockedSignal(None, clk, RisingEdge, is_input=True)
591+
592+
# Capture should return the default value
593+
val = await cs.capture(default=123)
594+
assert val == 123
595+
596+
# Capture without default should return None
597+
val_none = await cs.capture()
598+
assert val_none is None
599+
600+
asyncio.run(inner())
601+
580602

581603
# ============================================================================
582604
# Tests: Modport + Clocking Integration

0 commit comments

Comments
 (0)