Skip to content

Commit f9cbf4c

Browse files
committed
trying a different way of specifying conditional defaults
1 parent e00bb40 commit f9cbf4c

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

pyrtl/conditional.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ def currently_under_condition():
6767
# instances (hopefully the only and unchanging instances) of the following two types.
6868

6969
class _ConditionalAssignment(object):
70+
def __init__(self):
71+
self.defaults = {}
72+
73+
def __call__(self, defaults):
74+
self.defaults = defaults
75+
return self
76+
7077
""" Context providing funcitionality of "conditional_assignment". """
7178
def __enter__(self):
7279
global _depth
@@ -75,7 +82,7 @@ def __enter__(self):
7582

7683
def __exit__(self, *exc_info):
7784
try:
78-
_finalize()
85+
_finalize(self.defaults)
7986
finally:
8087
# even if the above finalization throws an error we need to
8188
# reset the state to prevent errors from bleeding over
@@ -181,7 +188,7 @@ def _pred_sets_are_in_conflict(pred_set_a, pred_set_b):
181188
return True
182189

183190

184-
def _finalize():
191+
def _finalize(defaults):
185192
"""Build the required muxes and call back to WireVector to finalize the wirevector build."""
186193
from .memory import MemBlock
187194
from pyrtl.corecircuits import select
@@ -203,13 +210,13 @@ def _finalize():
203210
# handle wirevector and register assignments
204211
else:
205212
if isinstance(lhs, Register):
206-
if hasattr(lhs, 'condition_default'):
207-
result = lhs.condition_default
213+
if lhs in defaults:
214+
result = defaults[lhs]
208215
else:
209216
result = lhs # default for registers is "self"
210217
elif isinstance(lhs, WireVector):
211-
if hasattr(lhs, 'condition_default'):
212-
result = lhs.condition_default
218+
if lhs in defaults:
219+
result = defaults[lhs]
213220
else:
214221
result = 0 # default for wire is "0"
215222
else:

tests/test_conditional.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ def test_two_signals_under_default_condition(self):
121121
r2.next |= 3
122122
self.check_trace(' i 01230123\nr1 01222344\nr2 00013334\n')
123123

124+
def test_default_value_for_wires(self):
125+
i = pyrtl.Register(bitwidth=2, name='i')
126+
i.next <<= i + 1
127+
r1 = pyrtl.Register(bitwidth=3, name='r1')
128+
r2 = pyrtl.Register(bitwidth=3, name='r2')
129+
r3 = pyrtl.Register(bitwidth=3, name='r3')
130+
o = pyrtl.Output(bitwidth=3, name='o')
131+
with pyrtl.conditional_assignment(defaults={r1: r1 + 2, r2: 6, o: 3}):
132+
with i < 2:
133+
r1.next |= r1 + 1
134+
# r2 will be updated to 6
135+
# r3 remains the same as previous cycle
136+
o |= i
137+
with i < 3:
138+
# r1 will be updated to r1 + 2
139+
r2.next |= r2 + 1
140+
# r3 remains the same as previous cycle
141+
# o will be updated to 3
142+
with pyrtl.otherwise:
143+
# r1 will be updated to r1 + 2
144+
# r2 will be updated to 6
145+
r3.next |= 2
146+
o |= 7
147+
self.check_trace(
148+
' i 01230123\n'
149+
' o 01370137\n'
150+
'r1 01246702\n'
151+
'r2 06676667\n'
152+
'r3 00002222\n'
153+
)
154+
124155
def test_error_on_unconditioned_update_in_under_conditional(self):
125156
i = pyrtl.Register(bitwidth=2, name='i')
126157
with pyrtl.conditional_assignment:

0 commit comments

Comments
 (0)