Skip to content

Commit 1d2b9c3

Browse files
wanda-phiwhitequark
authored andcommitted
back.rtlil: set read port init to all-x.
This is an unfortunate necessity needed to fix memory inference regressions introduced when we switched to using v2 cells. A better approach, compatible with RFC 54, will need to be figured out for Amaranth 0.6. Fixes #1011.
1 parent 4b49284 commit 1d2b9c3

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

amaranth/back/rtlil.py

+31-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@
2020
})
2121

2222

23+
# Special hack to emit 'x consts for memory read port init.
24+
class Undef:
25+
def __init__(self, width):
26+
self.width = width
27+
28+
2329
def _signed(value):
2430
if isinstance(value, str):
2531
return False
2632
elif isinstance(value, int):
2733
return value < 0
2834
elif isinstance(value, _ast.Const):
2935
return value.shape().signed
36+
elif isinstance(value, Undef):
37+
return False
3038
else:
3139
assert False, f"Invalid constant {value!r}"
3240

@@ -45,6 +53,8 @@ def _const(value):
4553
elif isinstance(value, _ast.Const):
4654
value_twos_compl = value.value & ((1 << len(value)) - 1)
4755
return "{}'{:0{}b}".format(len(value), value_twos_compl, len(value))
56+
elif isinstance(value, Undef):
57+
return f"{value.width}'" + "x" * value.width
4858
else:
4959
assert False, f"Invalid constant {value!r}"
5060

@@ -1070,9 +1080,27 @@ def emit_read_port(self, cell_idx, cell):
10701080
"WIDTH": cell.width,
10711081
"TRANSPARENCY_MASK": _ast.Const(transparency_mask, memory_info.num_write_ports),
10721082
"COLLISION_X_MASK": _ast.Const(0, memory_info.num_write_ports),
1073-
"ARST_VALUE": _ast.Const(0, cell.width),
1074-
"SRST_VALUE": _ast.Const(0, cell.width),
1075-
"INIT_VALUE": _ast.Const(0, cell.width),
1083+
# Horrible hack alert: Yosys has two different Verilog code patterns it can emit for
1084+
# transparent synchronous read ports — the old, limitted one and the generic new one.
1085+
# The old one essentially consists of a combinational read port with a register added
1086+
# on the *address* input. It has several limitations:
1087+
#
1088+
# - can only express read ports transparent wrt *all* write ports
1089+
# - cannot support initial values
1090+
# - cannot support reset
1091+
# - cannot support clock enable
1092+
#
1093+
# The new pattern can express any supported read port, but is not widely recognized
1094+
# by other toolchains, leading to memory inference failures. Thus, Yosys will use
1095+
# the old pattern whenever possible.
1096+
#
1097+
# In order to enable Yosys to use the old pattern and avoid memory inference regressions
1098+
# with non-Yosys synthesis, we need to emit undefined initial value here. This is in
1099+
# direct conflict with RFC 54, and will have to be revisited before 0.6, possibly
1100+
# requiring a large-scale design change in Amaranth memory support.
1101+
"ARST_VALUE": Undef(cell.width),
1102+
"SRST_VALUE": Undef(cell.width),
1103+
"INIT_VALUE": Undef(cell.width),
10761104
"CE_OVER_SRST": False,
10771105
}
10781106
if isinstance(cell, _nir.AsyncReadPort):

tests/test_back_rtlil.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1643,9 +1643,9 @@ def test_async_read(self):
16431643
parameter \WIDTH 8
16441644
parameter \TRANSPARENCY_MASK 1'0
16451645
parameter \COLLISION_X_MASK 1'0
1646-
parameter \ARST_VALUE 8'00000000
1647-
parameter \SRST_VALUE 8'00000000
1648-
parameter \INIT_VALUE 8'00000000
1646+
parameter \ARST_VALUE 8'xxxxxxxx
1647+
parameter \SRST_VALUE 8'xxxxxxxx
1648+
parameter \INIT_VALUE 8'xxxxxxxx
16491649
parameter \CE_OVER_SRST 0
16501650
parameter \CLK_ENABLE 0
16511651
parameter \CLK_POLARITY 1
@@ -1725,9 +1725,9 @@ def test_sync_read(self):
17251725
parameter \WIDTH 8
17261726
parameter \TRANSPARENCY_MASK 1'1
17271727
parameter \COLLISION_X_MASK 1'0
1728-
parameter \ARST_VALUE 8'00000000
1729-
parameter \SRST_VALUE 8'00000000
1730-
parameter \INIT_VALUE 8'00000000
1728+
parameter \ARST_VALUE 8'xxxxxxxx
1729+
parameter \SRST_VALUE 8'xxxxxxxx
1730+
parameter \INIT_VALUE 8'xxxxxxxx
17311731
parameter \CE_OVER_SRST 0
17321732
parameter \CLK_ENABLE 1
17331733
parameter \CLK_POLARITY 1

0 commit comments

Comments
 (0)