Skip to content

Commit 127fe1f

Browse files
wanda-phiwhitequark
authored andcommitted
hdl._xfrm: Get rid of _insert_resets, move the logic downstream.
1 parent ea56137 commit 127fe1f

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

amaranth/hdl/_ir.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,22 @@ def emit_top_ports(self, fragment: _ir.Fragment):
11951195

11961196
def emit_drivers(self):
11971197
for driver in self.drivers.values():
1198+
if (driver.domain is not None and
1199+
driver.domain.rst is not None and
1200+
not driver.domain.async_reset and
1201+
not driver.signal.reset_less):
1202+
cell = _nir.Matches(driver.module_idx,
1203+
value=self.emit_signal(driver.domain.rst),
1204+
patterns=("1",),
1205+
src_loc=driver.domain.rst.src_loc)
1206+
cond, = self.netlist.add_value_cell(1, cell)
1207+
cell = _nir.PriorityMatch(driver.module_idx, en=_nir.Net.from_const(1),
1208+
inputs=_nir.Value(cond),
1209+
src_loc=driver.domain.rst.src_loc)
1210+
cond, = self.netlist.add_value_cell(1, cell)
1211+
init = _nir.Value.from_const(driver.signal.init, driver.signal.width)
1212+
driver.assignments.append(_nir.Assignment(cond=cond, start=0,
1213+
value=init, src_loc=driver.signal.src_loc))
11981214
value = driver.emit_value(self)
11991215
if driver.domain is not None:
12001216
clk, = self.emit_signal(driver.domain.clk)

amaranth/hdl/_xfrm.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -526,22 +526,9 @@ def on_ResetSignal(self, value):
526526
.format(value, value.domain))
527527
return domain.rst
528528

529-
def _insert_resets(self, fragment):
530-
for domain_name, signals in fragment.drivers.items():
531-
if domain_name == "comb":
532-
continue
533-
domain = fragment.domains[domain_name]
534-
if domain.rst is None:
535-
continue
536-
stmts = [signal.eq(Const(signal.init, signal.width))
537-
for signal in signals if not signal.reset_less]
538-
fragment.add_statements(domain_name, Switch(domain.rst, {1: stmts}))
539-
540529
def on_fragment(self, fragment):
541530
self.domains = fragment.domains
542-
new_fragment = super().on_fragment(fragment)
543-
self._insert_resets(new_fragment)
544-
return new_fragment
531+
return super().on_fragment(fragment)
545532

546533

547534
class _ControlInserter(FragmentTransformer):

amaranth/sim/_pyrtl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,18 @@ def __call__(self, fragment):
473473

474474
_StatementCompiler(self.state, emitter)(domain_stmts)
475475

476+
if domain.rst is not None:
477+
rhs = _RHSValueCompiler(self.state, emitter, mode="curr")
478+
rst = rhs(domain.rst)
479+
rst = f"(1 & {rst})"
480+
emitter.append(f"if {rst}:")
481+
with emitter.indent():
482+
emitter.append("pass")
483+
for signal in domain_signals:
484+
if not signal.reset_less:
485+
signal_index = self.state.get_signal(signal)
486+
emitter.append(f"next_{signal_index} = {signal.init}")
487+
476488
if isinstance(fragment, MemoryInstance):
477489
memory_index = self.state.memories[fragment._identity]
478490
rhs = _RHSValueCompiler(self.state, emitter, mode="curr")

tests/test_hdl_ir.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,9 +3093,8 @@ def test_sync(self):
30933093
ClockSignal("b"), ResetSignal("b"),
30943094
ClockSignal("c"),
30953095
])
3096-
# TODO: two inefficiencies in NIR emitter:
3097-
# - _ignore_resets inserts useless redundant Switch for async reset
3098-
# - matches and priority_match duplicated between clock domains — add cache?
3096+
# TODO: inefficiency in NIR emitter:
3097+
# matches and priority_match duplicated between clock domains — add cache?
30993098
self.assertRepr(nl, """
31003099
(
31013100
(module 0 None ('top')
@@ -3106,11 +3105,11 @@ def test_sync(self):
31063105
(input 'b_clk' 0.13)
31073106
(input 'b_rst' 0.14)
31083107
(input 'c_clk' 0.15)
3109-
(output 'o1' 12.0:8)
3110-
(output 'o2' 14.0:8)
3111-
(output 'o3' 16.0:8)
3112-
(output 'o4' 18.0:8)
3113-
(output 'o5' 20.0:8)
3108+
(output 'o1' 8.0:8)
3109+
(output 'o2' 12.0:8)
3110+
(output 'o3' 14.0:8)
3111+
(output 'o4' 16.0:8)
3112+
(output 'o5' 18.0:8)
31143113
)
31153114
(cell 0 0 (top
31163115
(input 'i1' 2:10)
@@ -3120,32 +3119,30 @@ def test_sync(self):
31203119
(input 'b_clk' 13:14)
31213120
(input 'b_rst' 14:15)
31223121
(input 'c_clk' 15:16)
3123-
(output 'o1' 12.0:8)
3124-
(output 'o2' 14.0:8)
3125-
(output 'o3' 16.0:8)
3126-
(output 'o4' 18.0:8)
3127-
(output 'o5' 20.0:8)
3122+
(output 'o1' 8.0:8)
3123+
(output 'o2' 12.0:8)
3124+
(output 'o3' 14.0:8)
3125+
(output 'o4' 16.0:8)
3126+
(output 'o5' 18.0:8)
31283127
))
31293128
(cell 1 0 (matches 0.10 1))
31303129
(cell 2 0 (priority_match 1 1.0))
3131-
(cell 3 0 (matches 0.12 1))
3130+
(cell 3 0 (matches 0.10 1))
31323131
(cell 4 0 (priority_match 1 3.0))
31333132
(cell 5 0 (matches 0.10 1))
31343133
(cell 6 0 (priority_match 1 5.0))
3135-
(cell 7 0 (matches 0.14 1))
3136-
(cell 8 0 (priority_match 1 7.0))
3137-
(cell 9 0 (matches 0.10 1))
3134+
(cell 7 0 (assignment_list 8.0:8 (2.0 0:8 0.2:10)))
3135+
(cell 8 0 (flipflop 7.0:8 0 pos 0.11 0))
3136+
(cell 9 0 (matches 0.12 1))
31383137
(cell 10 0 (priority_match 1 9.0))
3139-
(cell 11 0 (assignment_list 12.0:8 (2.0 0:8 0.2:10)))
3140-
(cell 12 0 (flipflop 11.0:8 0 pos 0.11 0))
3141-
(cell 13 0 (assignment_list 14.0:8 (2.0 0:8 0.2:10) (4.0 0:8 8'd123)))
3142-
(cell 14 0 (flipflop 13.0:8 123 pos 0.11 0))
3143-
(cell 15 0 (assignment_list 16.0:8 (6.0 0:8 0.2:10)))
3144-
(cell 16 0 (flipflop 15.0:8 45 pos 0.13 0))
3145-
(cell 17 0 (assignment_list 18.0:8 (6.0 0:8 0.2:10) (8.0 0:8 8'd67)))
3146-
(cell 18 0 (flipflop 17.0:8 67 pos 0.13 0.14))
3147-
(cell 19 0 (assignment_list 20.0:8 (10.0 0:8 0.2:10)))
3148-
(cell 20 0 (flipflop 19.0:8 89 neg 0.15 0))
3138+
(cell 11 0 (assignment_list 12.0:8 (2.0 0:8 0.2:10) (10.0 0:8 8'd123)))
3139+
(cell 12 0 (flipflop 11.0:8 123 pos 0.11 0))
3140+
(cell 13 0 (assignment_list 14.0:8 (4.0 0:8 0.2:10)))
3141+
(cell 14 0 (flipflop 13.0:8 45 pos 0.13 0))
3142+
(cell 15 0 (assignment_list 16.0:8 (4.0 0:8 0.2:10)))
3143+
(cell 16 0 (flipflop 15.0:8 67 pos 0.13 0.14))
3144+
(cell 17 0 (assignment_list 18.0:8 (6.0 0:8 0.2:10)))
3145+
(cell 18 0 (flipflop 17.0:8 89 neg 0.15 0))
31493146
)
31503147
""")
31513148

0 commit comments

Comments
 (0)