Skip to content

Commit 7b12ec3

Browse files
committed
lib.stream: add payload_init= argument to Signature().
Fixes #1404.
1 parent eefca83 commit 7b12ec3

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

amaranth/lib/stream.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class Signature(wiring.Signature):
1919
Parameters
2020
----------
2121
payload_shape : :class:`~.hdl.ShapeLike`
22-
Shape of the payload.
22+
Shape of the payload member.
23+
payload_init : :ref:`constant-castable <lang-constcasting>` object
24+
Initial value of the payload member.
2325
always_valid : :class:`bool`
2426
Whether the stream has a payload available each cycle.
2527
always_ready : :class:`bool`
@@ -35,14 +37,15 @@ class Signature(wiring.Signature):
3537
ready : :py:`In(1)`
3638
Whether a payload is accepted. If the stream is :py:`always_ready`, :py:`Const(1)`.
3739
"""
38-
def __init__(self, payload_shape: ShapeLike, *, always_valid=False, always_ready=False):
40+
def __init__(self, payload_shape: ShapeLike, *, payload_init=None,
41+
always_valid=False, always_ready=False):
3942
Shape.cast(payload_shape)
4043
self._payload_shape = payload_shape
4144
self._always_valid = bool(always_valid)
4245
self._always_ready = bool(always_ready)
4346

4447
super().__init__({
45-
"payload": Out(payload_shape),
48+
"payload": Out(payload_shape, init=payload_init),
4649
"valid": Out(1),
4750
"ready": In(1)
4851
})
@@ -119,4 +122,4 @@ def p(self):
119122

120123
def __repr__(self):
121124
return (f"stream.Interface(payload={self.payload!r}, valid={self.valid!r}, "
122-
f"ready={self.ready!r})")
125+
f"ready={self.ready!r})")

tests/test_lib_stream.py

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ def test_eq(self):
108108
self.assertNotEqual(sig_av_ar, sig_nav_nar)
109109
self.assertNotEqual(sig_av_ar, sig_av_ar2)
110110

111+
def test_payload_init(self):
112+
sig = stream.Signature(2, payload_init=0b10)
113+
intf = sig.create()
114+
self.assertEqual(intf.payload.init, 0b10)
115+
111116
def test_interface_create_bad(self):
112117
with self.assertRaisesRegex(TypeError,
113118
r"^Signature of stream\.Interface must be a stream\.Signature, not "

0 commit comments

Comments
 (0)