Skip to content

Commit b4e3235

Browse files
authored
RFC #54: Initial and reset values on memory read ports.
2 parents bdea337 + 6aa6d3c commit b4e3235

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

text/0054-read-port-init.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
- Start Date: 2024-03-18
2+
- RFC PR: [amaranth-lang/rfcs#54](https://github.com/amaranth-lang/rfcs/pull/54)
3+
- Amaranth Issue: [amaranth-lang/amaranth#1212](https://github.com/amaranth-lang/amaranth/issues/1212)
4+
5+
# Initial and reset values on memory read ports
6+
7+
## Summary
8+
[summary]: #summary
9+
10+
Synchronous memory read ports get initial values, just like `Signal`s.
11+
12+
## Motivation
13+
[motivation]: #motivation
14+
15+
Currently, the initial state of synchronous memory read port's data output is undefined in synthesis, making it one of the very few places in Amaranth where an undefined value can be obtained, and an unexpected one at that. This also cannot be caught with pysim, as it initializes all read ports to 0. This is easily fixable on almost all FPGA targets, as almost all FPGAs have well-defined initial values.
16+
17+
Further, the read ports on almost all FPGA targets also support a reset signal, which is currently not exposed in any way in Amaranth.
18+
19+
The hardware capabilities for yosys-supported targets are as follows:
20+
21+
- Xilinx BRAM: arbitrary initial and reset values, reset can be sync or async (except for very old FPGAs that have sync reset only)
22+
- Lattice, Anlogic, Gowin BRAM; Xilinx URAM, Nexus LRAM: always-zero initial and reset value, reset can be sync or async
23+
- Efinix, iCE40, Gatemate BRAM; iCE40 SPRAM: undefined initial value, no reset
24+
- LUT RAM on any target: full support (uses a normal FF to create a sync read port)
25+
26+
Additionally, on any platform where requested initial value or reset is not natively supported by hardware, yosys will insert a FF and a mux to make it work regardless.
27+
28+
This RFC thus proposes to:
29+
30+
- close the expressiveness hole, making use of the hardware features where supported, using emulation otherwise
31+
- get rid of the undefined behavior
32+
33+
34+
## Guide-level explanation
35+
[guide-level-explanation]: #guide-level-explanation
36+
37+
Synchronous memory read ports behave in most respects like `Signal`s driven from a synchronous clock domain. As such, they have an initial value that can be set via `init=` on the constructor:
38+
39+
```py
40+
mem = Memory(shape=unsigned(8), depth=8, init=[])
41+
rp = mem.read_port(domain="sync", init=13)
42+
```
43+
44+
The read port's `data` signal will hold the initial value at startup and whenever a domain reset occurs. Additionally, as for `Signal`, `reset_less=True` can be specified to make the port not react to the domain reset.
45+
46+
## Reference-level explanation
47+
[reference-level-explanation]: #reference-level-explanation
48+
49+
`lib.memory.Memory.read_port` and `lib.memory.ReadPort` get two new keyword-only arguments: `init=None` and `reset_less=False`. For synchronous read ports, they effectively have the same behavior as they have on `Signal` when applied to `port.data`. They become introspectable attributes on the port. If the port has `comb` domain, they cannot be changed from their default values and are meaningless.
50+
51+
`hdl.MemoryInstance.read_port` likewise gets two new keyword-only arguments: `init=0` and `reset_less=False`. `init` must be an integer.
52+
53+
## Drawbacks
54+
[drawbacks]: #drawbacks
55+
56+
This is not natively supported by *all* FPGAs. While it can be reasonably cheaply emulated, in the author's experience, any amount of emulation circuitry inserted automatically by the toolchain to ensure well-defined behavior results solely in complaints.
57+
58+
## Rationale and alternatives
59+
[rationale-and-alternatives]: #rationale-and-alternatives
60+
61+
An alternative is to put `init` and `reset_less` on the signature and on the `port.data` signal instead of on the read port. However, `reset_less` is currently not supported by `lib.wiring`, and `is_compliant` will reject any signal with `reset_less=True`. This could be changed by a simple amendment to RFC 2.
62+
63+
## Prior art
64+
[prior-art]: #prior-art
65+
66+
None, or rather obvious enough.
67+
68+
## Unresolved questions
69+
[unresolved-questions]: #unresolved-questions
70+
71+
None.
72+
73+
## Future possibilities
74+
[future-possibilities]: #future-possibilities
75+
76+
A way to explicitly request undefined initial value could be added in the future, once undefined values are a well-defined concept in Amaranth.

0 commit comments

Comments
 (0)