Skip to content

Commit 37d72c3

Browse files
committed
docs/memory: add guide-level documentation.
1 parent bda11ee commit 37d72c3

File tree

2 files changed

+236
-27
lines changed

2 files changed

+236
-27
lines changed

amaranth_soc/memory.py

+4-22
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ def width(self):
195195
"""
196196
return self._width
197197

198+
def __repr__(self):
199+
return f"ResourceInfo(path={self.path}, start={self.start:#x}, end={self.end:#x}, " \
200+
f"width={self.width})"
201+
198202

199203
class MemoryMap:
200204
"""Memory map.
@@ -206,13 +210,6 @@ class MemoryMap:
206210
(range allocations for bus bridges), and can be queried later to determine the address of
207211
any given resource from a specific vantage point in the design.
208212
209-
.. note::
210-
211-
To simplify address assignment, each :class:`MemoryMap` has an implicit next address,
212-
starting at 0. If a resource or a window is added without specifying an address explicitly,
213-
the implicit next address is used. In any case, the implicit next address is set to the
214-
address immediately following the newly added resource or window.
215-
216213
Arguments
217214
---------
218215
addr_width : :class:`int`
@@ -391,8 +388,6 @@ def add_resource(self, resource, *, name, size, addr=None, alignment=None):
391388
------
392389
:exc:`ValueError`
393390
If the :class:`MemoryMap` is frozen.
394-
:exc:`TypeError`
395-
If the resource is not a :class:`wiring.Component`.
396391
:exc:`ValueError`
397392
If the requested address and size, after alignment, would overlap with any resources or
398393
windows that have already been added, or would be out of bounds.
@@ -466,19 +461,6 @@ def add_window(self, window, *, addr=None, sparse=None):
466461
addresses; the memory map reflects this address translation when resources are looked up
467462
through the window.
468463
469-
.. note::
470-
471-
If a narrow bus is bridged to a wide bus, the bridge can perform *sparse* or *dense*
472-
address translation.
473-
474-
In the sparse case, each transaction on the wide bus results in one transaction on the
475-
narrow bus; high data bits on the wide bus are ignored, and any contiguous resource on
476-
the narrow bus becomes discontiguous on the wide bus.
477-
478-
In the dense case, each transaction on the wide bus results in several transactions on
479-
the narrow bus, and any contiguous resource on the narrow bus stays contiguous on the
480-
wide bus.
481-
482464
Arguments
483465
---------
484466
window : :class:`MemoryMap`

docs/memory.rst

+232-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,241 @@
11
Memory maps
2-
===========
3-
4-
.. warning::
5-
6-
This manual is a work in progress and is seriously incomplete!
2+
###########
73

84
.. py:module:: amaranth_soc.memory
95
106
The :mod:`amaranth_soc.memory` module provides primitives for organizing the address space of a bus interface.
117

8+
.. testsetup::
9+
10+
from amaranth import *
11+
12+
from amaramtn_soc import csr
13+
from amaranth_soc.memory import *
14+
15+
.. _memory-introduction:
16+
17+
Introduction
18+
============
19+
20+
The purpose of :class:`MemoryMap` is to provide a hierarchical description of the address space of a System-on-Chip, from its bus interconnect to the registers of its peripherals. It is composed of :ref:`resources <memory-resources>` (representing registers, memories, etc) and :ref:`windows <memory-windows>` (representing bus bridges), and may be :ref:`queried <memory-accessing-windows>` afterwards in order to enumerate its contents, or determine the address of a resource.
21+
22+
.. _memory-resources:
23+
24+
Resources
25+
=========
26+
27+
A *resource* is a :class:`~amaranth.lib.wiring.Component` previously added to a :class:`MemoryMap`. Each resource occupies an unique range of addresses within the memory map, and represents a device that is a target for bus transactions.
28+
29+
Adding resources
30+
++++++++++++++++
31+
32+
Resources are added with :meth:`MemoryMap.add_resource`, which returns a ``(start, end)`` tuple describing their address range:
33+
34+
.. testcode::
35+
36+
memory_map = MemoryMap(addr_width=3, data_width=8)
37+
38+
reg_ctrl = csr.Register(csr.Field(csr.action.RW, 32), "rw")
39+
reg_data = csr.Register(csr.Field(csr.action.RW, 32), "rw")
40+
41+
.. doctest::
42+
43+
>>> memory_map.add_resource(reg_ctrl, size=4, addr=0x0, name=("ctrl",))
44+
(0, 4)
45+
>>> memory_map.add_resource(reg_data, size=4, addr=0x4, name=("data",))
46+
(4, 8)
47+
48+
.. note::
49+
50+
The ``addr`` parameter of :meth:`MemoryMap.add_resource` and :meth:`MemoryMap.add_window` is optional.
51+
52+
To simplify address assignment, each :class:`MemoryMap` has an *implicit next address*, starting at 0. If a resource or a window is added without an explicit address, the implicit next address is used. In any case, the implicit next address is set to the address immediately following the newly added resource or window.
53+
54+
Accessing resources
55+
+++++++++++++++++++
56+
57+
Memory map resources can be iterated with :meth:`MemoryMap.resources`:
58+
59+
.. doctest::
60+
61+
>>> for resource, name, (start, end) in memory_map.resources():
62+
... print(f"name={name}, start={start:#x}, end={end:#x}, resource={resource}")
63+
name=('ctrl',), start=0x0, end=0x4, resource=<...>
64+
name=('data',), start=0x4, end=0x8, resource=<...>
65+
66+
A memory map can be queried with :meth:`MemoryMap.find_resource` to get the name and address range of a given resource:
67+
68+
.. doctest::
69+
70+
>>> memory_map.find_resource(reg_ctrl)
71+
ResourceInfo(path=('ctrl',), start=0x0, end=0x4, width=8)
72+
73+
The resource located at a given address can be retrieved with :meth:`MemoryMap.decode_address`:
74+
75+
.. doctest::
76+
77+
>>> memory_map.decode_address(0x4) is reg_data
78+
True
79+
80+
Alignment
81+
=========
82+
83+
The value of :attr:`MemoryMap.alignment` constrains the layout of a memory map. If unspecified, it defaults to 0.
84+
85+
Each resource or window added to a memory map is placed at an address that is a multiple of ``2 ** alignment``, and its size is rounded up to a multiple of ``2 ** alignment``.
86+
87+
For example, the resources of this memory map are 64-bit aligned:
88+
89+
.. testcode::
90+
91+
memory_map = MemoryMap(addr_width=8, data_width=8, alignment=3)
92+
93+
reg_foo = csr.Register(csr.Field(csr.action.RW, 32), "rw")
94+
reg_bar = csr.Register(csr.Field(csr.action.RW, 32), "rw")
95+
reg_baz = csr.Register(csr.Field(csr.action.RW, 32), "rw")
96+
97+
.. doctest::
98+
99+
>>> memory_map.add_resource(reg_foo, size=4, name=("foo",))
100+
(0, 8)
101+
>>> memory_map.add_resource(reg_bar, size=4, name=("bar",), addr=0x9)
102+
ValueError
103+
104+
:meth:`MemoryMap.add_resource` takes an optional ``alignment`` parameter. If a value greater than :attr:`MemoryMap.alignment` is given, it becomes the alignment of this resource:
105+
106+
.. doctest::
107+
108+
>>> memory_map.add_resource(reg_bar, size=4, name=("bar",), alignment=4)
109+
(16, 32)
110+
111+
:meth:`MemoryMap.align_to` can be used to align the implicit next address. Its alignment is modified if a value greater than :attr:`MemoryMap.alignment` is given.
112+
113+
.. doctest::
114+
115+
>>> memory_map.align_to(6)
116+
64
117+
>>> memory_map.add_resource(reg_baz, size=4, name=("baz",))
118+
(64, 72)
119+
120+
.. note:: :meth:`MemoryMap.align_to` has no effect on the size of the next resource or window.
121+
122+
.. _memory-windows:
123+
124+
Windows
125+
=======
126+
127+
A *window* is a :class:`MemoryMap` nested inside another memory map. Each window occupies an unique range of addresses within the memory map, and represents a bridge to a subordinate bus.
128+
129+
Adding windows
130+
++++++++++++++
131+
132+
Windows are added with :meth:`MemoryMap.add_window`, which returns a ``(start, end, ratio)`` tuple describing their address range:
133+
134+
.. testcode::
135+
136+
reg_ctrl = csr.Register(csr.Field(csr.action.RW, 32), "rw")
137+
reg_rx_data = csr.Register(csr.Field(csr.action.RW, 32), "rw")
138+
reg_tx_data = csr.Register(csr.Field(csr.action.RW, 32), "rw")
139+
140+
memory_map = MemoryMap(addr_width=14, data_width=32)
141+
rx_window = MemoryMap(addr_width=12, data_width=32, name="rx")
142+
tx_window = MemoryMap(addr_width=12, data_width=32, name="tx")
143+
144+
.. doctest::
145+
146+
>>> memory_map.add_resource(reg_ctrl, size=1, name=("ctrl",))
147+
(0, 1)
148+
149+
>>> rx_window.add_resource(reg_rx_data, size=1, name=("data",))
150+
(0, 1)
151+
>>> memory_map.add_window(rx_window)
152+
(4096, 8192, 1)
153+
154+
The third value returned by :meth:`MemoryMap.add_window` represents the number of addresses that are accessed in the bus described by ``rx_window`` for one transaction in the bus described by ``memory_map``. It is 1 in this case, as both busses have the same width.
155+
156+
.. doctest::
157+
158+
>>> tx_window.add_resource(reg_tx_data, size=1, name=("data",))
159+
(0, 1)
160+
>>> memory_map.add_window(tx_window)
161+
(8192, 12288, 1)
162+
163+
.. _memory-accessing-windows:
164+
165+
Accessing windows
166+
-----------------
167+
168+
Memory map windows can be iterated with :meth:`MemoryMap.windows`:
169+
170+
.. doctest::
171+
172+
>>> for window, (start, end, ratio) in memory_map.windows():
173+
... print(f"{window}, start={start:#x}, end={end:#x}, ratio={ratio}")
174+
MemoryMap(name='rx'), start=0x1000, end=0x2000, ratio=1
175+
MemoryMap(name='tx'), start=0x2000, end=0x3000, ratio=1
176+
177+
Windows can also be iterated with :meth:`MemoryMap.window_patterns`, which encodes their address ranges as bit patterns compatible with the :ref:`match operator <lang-matchop>` and the :ref:`Case block <lang-switch>`:
178+
179+
.. doctest::
180+
181+
>>> for window, (pattern, ratio) in memory_map.window_patterns():
182+
... print(f"{window}, pattern={pattern}, ratio={ratio}")
183+
MemoryMap(name='rx'), pattern='01------------', ratio=1
184+
MemoryMap(name='tx'), pattern='10------------', ratio=1
185+
186+
Memory map resources can be recursively iterated with :meth:`MemoryMap.all_resources`, which yields instances of :class:`ResourceInfo`:
187+
188+
.. doctest::
189+
190+
>>> for res_info in memory_map.all_resources():
191+
... print(res_info)
192+
ResourceInfo(path=(('ctrl',),), start=0x0, end=0x1, width=32)
193+
ResourceInfo(path=('rx', ('data',)), start=0x1000, end=0x1001, width=32)
194+
ResourceInfo(path=('tx', ('data',)), start=0x2000, end=0x2001, width=32)
195+
196+
Address translation
197+
+++++++++++++++++++
198+
199+
When a memory map resource is accessed through a window, address translation may happen in three different modes.
200+
201+
Transparent mode
202+
----------------
203+
204+
In *transparent mode*, each transaction on the primary bus results in one transaction on the subordinate bus without loss of data. This mode is selected when :meth:`MemoryMap.add_window` is given ``sparse=None``, which will fail if the window and the memory map have a different data widths.
205+
206+
.. note::
207+
208+
In practice, transparent mode is identical to other modes; it can only be used with equal data widths, which results in the same behavior regardless of the translation mode. However, it allows :meth:`MemoryMap.add_window` to fail if the data widths are different.
209+
210+
Sparse mode
211+
-----------
212+
213+
In *sparse mode*, each transaction on the wide primary bus results in one transaction on the narrow subordinate bus. High data bits on the primary bus are ignored, and any contiguous resource on the subordinate bus becomes discontiguous on the primary bus. This mode is selected when :meth:`MemoryMap.add_window` is given ``sparse=True``.
214+
215+
Dense mode
216+
----------
217+
218+
In *dense mode*, each transaction on the wide primary bus results in several transactions on the narrow subordinate bus, and any contiguous resource on the subordinate bus stays contiguous on the primary bus. This mode is selected when :meth:`MemoryMap.add_window` is given ``sparse=False``.
219+
220+
Freezing
221+
========
222+
223+
The visible state of a memory map can become immutable by calling :meth:`MemoryMap.freeze`:
224+
225+
.. testcode::
226+
227+
memory_map = MemoryMap(addr_width=3, data_width=8)
228+
229+
reg_ctrl = csr.Register(csr.Field(csr.action.RW, 32), "rw")
230+
231+
.. doctest::
232+
233+
>>> memory_map.freeze()
234+
>>> memory_map.add_resource(reg_ctrl, size=4, addr=0x0, name=("ctrl",))
235+
ValueError
236+
237+
It is recommended to freeze a memory map before passing it to external logic, as a preventive measure against TOCTTOU bugs.
238+
12239
.. autoclass:: MemoryMap()
13240
:no-members:
14241

0 commit comments

Comments
 (0)