|
| 1 | +- Start Date: 2024-06-28 |
| 2 | +- RFC PR: [amaranth-lang/rfcs#70](https://github.com/amaranth-lang/rfcs/pull/70) |
| 3 | +- Amaranth SoC Issue: [amaranth-lang/amaranth-soc#93](https://github.com/amaranth-lang/amaranth-soc/issues/93) |
| 4 | + |
| 5 | +# Unify the naming of `MemoryMap` resources and windows |
| 6 | + |
| 7 | +## Summary |
| 8 | +[summary]: #summary |
| 9 | + |
| 10 | +- Use a `MemoryMap.Name` class to represent resource and window names. |
| 11 | +- Assign window names in `MemoryMap.add_window()` instead of `MemoryMap.__init__()`. |
| 12 | + |
| 13 | +## Motivation |
| 14 | +[motivation]: #motivation |
| 15 | + |
| 16 | +In a `MemoryMap`, resources (e.g. registers of a peripheral) and windows (nested memory maps) are added with similar methods: `MemoryMap.add_resource()` and `.add_window()`. |
| 17 | + |
| 18 | +However, their names are handled differently: |
| 19 | +- a resource name is a tuple of strings, assigned as a parameter to `MemoryMap.add_resource()`. |
| 20 | +- a window name is a string, assigned at the creation of the window itself (as a parameter to `MemoryMap.__init__()`). |
| 21 | + |
| 22 | +These differences add needless complexity to the API: |
| 23 | +- the name of a window is only relevant from the context of the memory map to which it is added. |
| 24 | +- window names may also benefit from being split into tuples, in order to let consumers of the memory map (such as a BSP generator) decide their format. |
| 25 | + |
| 26 | +Additionally, support for integers in resource and window names is needed to represent indices. A BSP generator may choose to format them in a specific way (e.g. `"foo[1]"`). |
| 27 | + |
| 28 | +## Guide-level explanation |
| 29 | +[guide-level-explanation]: #guide-level-explanation |
| 30 | + |
| 31 | +Resource and window names are instances of `MemoryMap.Name`, a subclass of `tuple` which can only contain non-empty strings and non-negative integers. |
| 32 | + |
| 33 | +Some examples: |
| 34 | + |
| 35 | +```python3 |
| 36 | +>>> MemoryMap.Name(("rx", "status")) |
| 37 | +Name('rx', 'status') |
| 38 | +>>> MemoryMap.Name(("uart", 0)) |
| 39 | +Name('uart', 0) |
| 40 | +>>> MemoryMap.Name(MemoryMap.Name(("uart", 0)) |
| 41 | +Name('uart', 0) |
| 42 | +>>> MemoryMap.Name("foo") |
| 43 | +Name('foo',) |
| 44 | +``` |
| 45 | + |
| 46 | +### Assigning resource names |
| 47 | + |
| 48 | +The name of a resource is given by the `name` parameter of `MemoryMap.add_resource()`. For the sake of brevity, users can pass a tuple which is implicitly cast to a `MemoryMap.Name`. |
| 49 | + |
| 50 | +This example shows how names are assigned to the registers of an UART peripheral: |
| 51 | + |
| 52 | +```python3 |
| 53 | +from amaranth import * |
| 54 | +from amaranth.lib import wiring |
| 55 | +from amaranth.lib.wiring import In, Out |
| 56 | + |
| 57 | +from amaranth_soc import csr |
| 58 | +from amaranth_soc.memory import MemoryMap |
| 59 | + |
| 60 | + |
| 61 | +class UART(wiring.Component): |
| 62 | + class RxConfig(csr.Register, access="rw"): |
| 63 | + enable: csr.Field(csr.action.RW, 1) |
| 64 | + |
| 65 | + class RxStatus(csr.Register, access="rw"): |
| 66 | + ready: csr.Field(csr.action.R, 1) |
| 67 | + error: csr.Field(csr.action.RW1C, 1) |
| 68 | + |
| 69 | + class RxData(csr.Register, access="r"): |
| 70 | + def __init__(self): |
| 71 | + super().__init__(csr.Field(csr.action.R, 8)) |
| 72 | + |
| 73 | + csr_bus: In(csr.Signature(addr_width=10, data_width=32)) |
| 74 | + |
| 75 | + def __init__(self): |
| 76 | + super().__init__() |
| 77 | + |
| 78 | + memory_map = MemoryMap(addr_width=10, data_width=32) |
| 79 | + memory_map.add_resource(self.RxConfig(), size=1, name=("rx", "config")) |
| 80 | + memory_map.add_resource(self.RxStatus(), size=1, name=MemoryMap.Name(("rx", "status"))) |
| 81 | + memory_map.add_resource(self.RxData(), size=1, name=("rx", "data")) |
| 82 | + memory_map.freeze() |
| 83 | + |
| 84 | + self.csr_bus.memory_map = memory_map |
| 85 | + |
| 86 | + def elaborate(self, platform): |
| 87 | + m = Module() |
| 88 | + # ... |
| 89 | + return m |
| 90 | +``` |
| 91 | + |
| 92 | +### Assigning window names |
| 93 | + |
| 94 | +Similarly, the name of a window is given by the `name` parameter of `MemoryMap.add_window()`. Unlike resource names, window names are optional. |
| 95 | + |
| 96 | +This example shows how names are assigned to two UART peripherals, as their memory maps are added as windows to a bus decoder memory map: |
| 97 | + |
| 98 | +```python3 |
| 99 | +class Decoder(wiring.Component): |
| 100 | + csr_bus: In(csr.Signature(addr_width=20, data_width=32)) |
| 101 | + |
| 102 | + def __init__(self): |
| 103 | + super().__init__() |
| 104 | + self._subs = dict() |
| 105 | + self.csr_bus.memory_map = MemoryMap(addr_width=20, data_width=32) |
| 106 | + |
| 107 | + def add(self, sub_bus, *, name=None, addr=None): |
| 108 | + self._subs[sub_bus.memory_map] = sub_bus |
| 109 | + return self.csr_bus.memory_map.add_window(sub_bus.memory_map, name=name, addr=addr) |
| 110 | + |
| 111 | + def elaborate(self, platform): |
| 112 | + m = Module() |
| 113 | + |
| 114 | + with m.Switch(self.csr_bus.addr): |
| 115 | + for window, name, (pattern, ratio) in self.csr_bus.memory_map.window_patterns(): |
| 116 | + sub_bus = self._subs[window] |
| 117 | + with m.Case(pattern): |
| 118 | + pass # ... drive the subordinate bus interface |
| 119 | + |
| 120 | + return m |
| 121 | + |
| 122 | + |
| 123 | +uart_0 = UART() |
| 124 | +uart_1 = UART() |
| 125 | + |
| 126 | +decoder = Decoder() |
| 127 | +decoder.add(uart_0.csr_bus, name=("uart", 0)) |
| 128 | +decoder.add(uart_1.csr_bus, name=MemoryMap.Name(("uart", 1))) |
| 129 | +``` |
| 130 | + |
| 131 | +In a `MemoryMap` hierarchy, each resource is identified by a path. The path of a resource is a tuple ending with its name, preceded by the name of each window that contains it: |
| 132 | + |
| 133 | +```python3 |
| 134 | +>>> for res_info in decoder.csr_bus.memory_map.all_resources(): |
| 135 | +... print(res_info.path) |
| 136 | +(Name('uart', 0), Name('rx', 'config')) |
| 137 | +(Name('uart', 0), Name('rx', 'status')) |
| 138 | +(Name('uart', 0), Name('rx', 'data')) |
| 139 | +(Name('uart', 1), Name('rx', 'config')) |
| 140 | +(Name('uart', 1), Name('rx', 'status')) |
| 141 | +(Name('uart', 1), Name('rx', 'data')) |
| 142 | +``` |
| 143 | + |
| 144 | +## Reference-level explanation |
| 145 | +[reference-level-explanation]: #reference-level-explanation |
| 146 | + |
| 147 | +The following changes to the `amaranth_soc.memory` module are made: |
| 148 | + |
| 149 | +- Add a `MemoryMap.Name(name, /)` class. It is a subclass of `tuple`, where: |
| 150 | + |
| 151 | + * `name` must either be a string or a tuple of strings and non-negative integers. If `name` is a string, it is implicitly converted to `(name,)`. |
| 152 | + |
| 153 | +- The following changes to `MemoryMap` are made: |
| 154 | + |
| 155 | + * The optional `name` parameter of `MemoryMap()` is removed. |
| 156 | + * The `MemoryMap.name` property is removed. |
| 157 | + * The `name` parameter of `MemoryMap.add_resource()` must be a `MemoryMap.Name`. |
| 158 | + * An optional `name` parameter is added to `MemoryMap.add_window()`, which must be a `MemoryMap.Name`. |
| 159 | + * The yield values of `MemoryMap.windows()` are changed to `window, name, (start, end, ratio)`. |
| 160 | + * The yield values of `MemoryMap.window_patterns()` are changed to `window, name, (pattern, ratio)`. |
| 161 | + |
| 162 | +- The following changes to `ResourceInfo` are made: |
| 163 | + |
| 164 | + * The `path` parameter of `ResourceInfo()` must be a tuple of `MemoryMap.Name` objects. |
| 165 | + |
| 166 | +As a consequence of this proposal, the following changes are made to other modules: |
| 167 | + |
| 168 | +- `amaranth_soc.csr.bus` and `amaranth_soc.wishbone.bus`: |
| 169 | + * The optional `name` parameter of `Decoder()` is moved to `Decoder.add()`. |
| 170 | + |
| 171 | +- `amaranth_soc.csr.reg`: |
| 172 | + * The optional `name` parameter of `Builder()` is removed. |
| 173 | + |
| 174 | +- `amaranth_soc.csr.wishbone`: |
| 175 | + * The optional `name` parameter of `WishboneCSRBridge()` is assigned to `csr_bus.memory_map` (instead of `wb_bus.memory_map`). |
| 176 | + |
| 177 | +## Drawbacks |
| 178 | +[drawbacks]: #drawbacks |
| 179 | + |
| 180 | +- This will break codebases that make use of window names. |
| 181 | + |
| 182 | +## Rationale and alternatives |
| 183 | +[rationale-and-alternatives]: #rationale-and-alternatives |
| 184 | + |
| 185 | +- Providing a `MemoryMap.Name` class for resource and window names facilitates their validation and documentation. |
| 186 | + * ~~Alternative #1: do not add a class, and use standard tuples instead. Names will have to be validated by other means.~~ |
| 187 | + * ~~Alternative #2: use `MemoryMap.Name` for resource names only. Window names remain limited to strings.~~ |
| 188 | + |
| 189 | +## Prior art |
| 190 | +[prior-art]: #prior-art |
| 191 | + |
| 192 | +- Resource names became tuples of strings as a consequence of [RFC 16](https://amaranth-lang.org/rfcs/0016-soc-csr-regs.html). However, array indices defined with `csr.Builder.Index()` were [cast to strings](https://github.com/amaranth-lang/amaranth-soc/issues/69) when `.as_memory_map()` was called. |
| 193 | + |
| 194 | +## Resolved questions |
| 195 | +[unresolved-questions]: #unresolved-questions |
| 196 | + |
| 197 | +- Should we require the presence of at least one string in `MemoryMap.Name` ? |
| 198 | + * Empty names are forbidden and transparent windows (i.e. without names) must use `None` instead. Further validation is deferred to consumers of the memory map (e.g. a BSP generator). |
| 199 | +- Should we specify the order between strings and integers ? In `csr.Builder`, array indices precede cluster and register names (e.g. `('bar', 0, 'foo')` could be formatted as `"bar.foo[0]"`). |
| 200 | + * No, this decision is left out to consumers of the memory map. They may interpret a name differently depending on what it is assigned to. |
| 201 | + |
| 202 | +## Future possibilities |
| 203 | +[future-possibilities]: #future-possibilities |
| 204 | + |
| 205 | +None. |
0 commit comments