You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0036-async-testbench-functions.md
+78-44Lines changed: 78 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -25,34 +25,24 @@ As an example, let's consider a simple stream interface with `valid`, `ready` an
25
25
We can then implement `stream_send()` and `stream_recv()` functions like this:
26
26
27
27
```python
28
-
@testbench_helper
29
28
asyncdefstream_recv(sim, stream):
30
-
await sim.set(stream.ready, 1)
31
-
await sim.tick().until(stream.valid)
32
-
33
-
value =await sim.get(stream.data)
34
-
29
+
sim.set(stream.ready, 1)
30
+
value =await sim.tick().until(stream.valid).sample(stream.data)
35
31
await sim.tick()
36
-
await sim.set(stream.ready, 0)
37
-
32
+
sim.set(stream.ready, 0)
38
33
return value
39
34
40
-
@testbench_helper
41
35
asyncdefstream_send(sim, stream, value):
42
-
await sim.set(stream.data, value)
43
-
44
-
await sim.set(stream.valid, 1)
36
+
sim.set(stream.data, value)
37
+
sim.set(stream.valid, 1)
45
38
await sim.tick().until(stream.ready)
46
-
47
39
await sim.tick()
48
-
awaitsim.set(stream.valid, 0)
40
+
sim.set(stream.valid, 0)
49
41
```
50
42
51
-
`await sim.get()` and `await sim.set()` replaces the existing operations `yield signal` and `yield signal.eq()` respectively.
52
-
53
-
`sim.tick()` replaces the existing `Tick()`. It returns a trigger object that either can be awaited directly, or made conditional through `.until()`.
43
+
`sim.get()` and `sim.set()` replaces the existing operations `yield signal` and `yield signal.eq()` respectively.
54
44
55
-
The `testbench_helper` decorator indicates that this function is only designed to be called from testbench processes and will raise an exception if called elsewhere.
45
+
`sim.tick()` replaces the existing `Tick()`. It returns a trigger object that either can be awaited directly, or made conditional through `.until()`. Values of signals can be captured using `.sample()`, which is used to sample the interface members at the active edge of the clock. This approach makes these functions robust in presence of combinational feedback or concurrent use in multiple testbench processes.
56
46
57
47
> **Note**
58
48
> This simplified example does not include any way of specifying the clock domain of the interface and as such is only directly applicable to single domain simulations.
@@ -89,7 +79,7 @@ Since `stream_send()` and `stream_recv()` invokes `sim.get()` and `sim.set()` th
89
79
`Tick()` and `Delay()` are replaced by `sim.tick()` and `sim.delay()` respectively.
90
80
In addition, `sim.changed()` and `sim.edge()` is introduced that allows creating triggers from arbitrary signals.
91
81
92
-
`sim.tick()` return a domain trigger object that can be made conditional through `.until()` or repeated through `.repeat()`.
82
+
`sim.tick()` return a domain trigger object that can be made conditional through `.until()` or repeated through `.repeat()`. Arbitrary expressions may be sampled at the active edge of the domain clock using `.sample()`.
93
83
94
84
`sim.delay()`, `sim.changed()` and `sim.edge()` return a combinable trigger object that can be used to add additional triggers.
95
85
@@ -118,7 +108,7 @@ Combinational adder as a process:
asyncfor clk_hit, rst_hit in sim.posedge(clk).edge(rst, rst_edge):
143
-
awaitsim.set(q, 0if rst_hit elseawait sim.get(d))
133
+
sim.set(q, 0if rst_hit elsed)
144
134
return process
145
135
sim.add_process(dff(rst_edge=0))
146
136
```
@@ -157,32 +147,63 @@ Both methods are updated to accept an async function passed as `process`.
157
147
The async function must accept an argument `sim`, which will be passed a simulator context.
158
148
(Argument name is just convention, will be passed positionally.)
159
149
150
+
The usage model of the two kinds of processes are:
151
+
- Processes are added with `add_process()` for the sole purpose of simulating a part of the netlist with behavioral Python code.
152
+
- Typically such a process will consist of a top-level `async for values in sim.tick().sample(...):` or `async for values in sim.changed(...)`, but this is not a requirement.
153
+
- Such processes may only wait on signals, via `sim.tick()`, `sim.changed()`, and `sim.edge()`. They cannot advance simulation.
154
+
- In these processes, `sim.get()` is not available; values of signals may only be obtained by awaiting on triggers.
155
+
`sim.set(x, y)` may be used to propagate the value of `y` without reading it.
156
+
- The function passed to `add_process()` must be idempotent: applying it multiple times to the same simulation state and with same local variable values must produce the same effect each time.
157
+
Provided that, the outcome of running such a process is deterministic regardless of the order of their execution.
158
+
- Processes are added with `add_testbench()` for any other purpose, including but not limited to: providing a stimulus, performing I/O, displaying state, asserting outcomes, and so on.
159
+
- Such a process may be a simple linear function, use a top-level loop, or have arbitrarily complex structure.
160
+
- Such processes may wait on signals as well as advance simulation time.
161
+
- In these processes, `sim.get(x)` is available and returns the most current value of `x` (after all pending combinatorial propagation finishes).
162
+
- The function passed to `add_testbench()` may have arbitrary side effects.
163
+
These processes are scheduled in an unspecified order that may not be deterministic, and no mechanisms are provided to recover determinism of outcomes.
164
+
- When waiting on signals, e.g. via `sim.tick()`, the requested expressions are sampled before the processes added with `add_process()` and RTL processes perform combinatorial propagation. However, execution continues only after all pending combinatorial propagation finishes.
165
+
166
+
The following concurrency guarantees are provided:
167
+
- Async processes registered with `add_testbench` may be preempted by:
168
+
- Any other process when calling `await ...`.
169
+
- A process registered with `add_process` (or an RTL process) when calling `sim.set()` or `sim.memory_write()`. In this case, control is returned to the same testbench after combinational settling.
170
+
- Async processes registered with `add_process` may be preempted by:
171
+
- Any other process when calling `await ...`.
172
+
- Legacy processes follow the same rules as async processes, with the exception of:
173
+
- A legacy process may not be preempted when calling `yield x:ValueLike` or `yield x:Assign`.
174
+
- Once running, a process continues to execute until it terminates or is preempted.
175
+
160
176
The new optional named argument `background` registers the testbench as a background process when true.
161
177
Processes created through `add_process` are always registered as background processes (except when registering legacy non-async generator functions).
162
178
163
179
The simulator context has the following methods:
164
180
-`get(expr: Value) -> int`
165
181
-`get(expr: ValueCastable) -> any`
166
-
- Returns the value of `expr` when awaited.
182
+
- Returns the value of `expr`.
167
183
When `expr` is a value-castable, and its `shape()` is a `ShapeCastable`, the value will be converted through the shape's `.from_bits()`.
168
184
Otherwise, a plain integer is returned.
185
+
This function is not available in processes created through `add_process`.
169
186
-`set(expr: Value, value: ConstLike)`
170
187
-`set(expr: ValueCastable, value: any)`
171
-
- Set `expr` to `value` when awaited.
188
+
- Set `expr` to `value`.
172
189
When `expr` is a value-castable, and its `shape()` is a `ShapeCastable`, the value will be converted through the shape's `.const()`.
173
190
Otherwise, it must be a const-castable `ValueLike`.
191
+
When used in a process created through `add_testbench`, it may execute RTL processes and processes created through `add_process`.
- Write `value` to `address` in `instance` when awaited. If `mask` is given, only the corresponding bits are written.
196
+
- Write `value` to `address` in `instance`. If `mask` is given, only the corresponding bits are written.
178
197
Like `MemoryInstance`, these two functions are an internal interface that will be usually only used via `lib.Memory`.
198
+
When used in a process created through `add_testbench`, it may execute RTL processes and processes created through `add_process`.
179
199
It comes without a stability guarantee.
180
200
-`tick(domain="sync", *, context=None)`
181
201
- Create a domain trigger object for advancing simulation until the next active edge of the `domain` clock.
182
202
When an elaboratable is passed to `context`, `domain` will be resolved from its perspective.
183
203
- If `domain` is asynchronously reset while this is being awaited, `amaranth.sim.AsyncReset` is raised.
184
204
-`delay(interval: float)`
185
205
- Create a combinable trigger object for advancing simulation by `interval` seconds.
206
+
This function is not available in processes created through `add_process`.
186
207
-`changed(*signals)`
187
208
- Create a combinable trigger object for advancing simulation until any signal in `signals` changes.
188
209
-`edge(signal, value: int)`
@@ -198,29 +219,37 @@ The simulator context has the following methods:
198
219
199
220
A domain trigger object is immutable and has the following methods:
200
221
-`__await__()`
201
-
- Advance simulation. No value is returned.
222
+
- Advance simulation and return the value(s) of the sampled expression(s). Values are returned in the same order as the expressions were added.
223
+
-`__aiter__()`
224
+
- Return an async generator that is equivalent to repeatedly awaiting the trigger object in an infinite loop.
225
+
- The async generator yields value(s) of the sampled expression(s).
226
+
-`sample(*expressions)`
227
+
- Create a new trigger object by copying the current object and appending the expressions to be sampled.
202
228
-`until(condition)`
203
229
- Repeat the trigger until `condition` is true.
204
230
`condition` is an arbitrary Amaranth expression.
205
-
If `condition` is initially true, `await` will return immediately without advancing simulation.
206
231
The return value is an unspecified awaitable with `await` as the only defined operation.
207
-
It is only awaitable once and awaiting it returns no value.
208
-
- Example implementation:
232
+
It is only awaitable once and returns the value(s) of the sampled expression(s) at the last time the trigger was repeated.
233
+
- Example implementation (without error checking):
209
234
```python
210
235
asyncdefuntil(self, condition):
211
-
whilenotawaitself._sim.get(condition):
212
-
awaitself
236
+
whileTrue:
237
+
*values, done =awaitself.sample(condition)
238
+
if done:
239
+
return values
213
240
```
214
241
-`repeat(times: int)`
215
242
- Repeat the trigger `times` times.
216
-
Valid values are `times >=0`.
243
+
Valid values are `times >0`.
217
244
The return value is an unspecified awaitable with`await`as the only defined operation.
218
-
It is only awaitable once andawaiting it returns no value.
219
-
- Example implementation:
245
+
It is only awaitable once and returns the value(s) of the sampled expression(s) at the last time the trigger was repeated.
246
+
- Example implementation (without error checking):
220
247
```python
221
248
asyncdefrepeat(self, times):
222
-
for _ inrange(times):
223
-
awaitself
249
+
values =None
250
+
for _ inrange(times):
251
+
values =awaitself
252
+
return values
224
253
```
225
254
226
255
A combinable trigger objectis immutable and has the following methods:
@@ -232,6 +261,7 @@ A combinable trigger object is immutable and has the following methods:
232
261
In case of multiple triggers occuring at the same time step, it is unspecified which of these will show up in the return value beyond “at least one”.
233
262
-`__aiter__()`
234
263
- Return an async generator that is equivalent to repeatedly awaiting the trigger objectin an infinite loop.
264
+
- The async generator yields value(s) of the trigger(s).
235
265
-`delay(interval: float)`
236
266
-`changed(*signals)`
237
267
-`edge(signal, value)`
@@ -240,10 +270,6 @@ A combinable trigger object is immutable and has the following methods:
240
270
- Create a new trigger object by copying the current objectand appending another trigger.
241
271
- Awaiting the returned trigger object pauses the process until the first of the combined triggers hit, i.e. the triggers are combined using OR semantics.
242
272
243
-
To ensure testbench helper functions are only called from a testbench process, the `amaranth.sim.testbench_helper` decorator is added.
244
-
The function wrapper expects the first positional argument (or second, after `self`or`cls`if decorating a method/classmethod) to be a simulator context, and will raise`TypeError`ifnot.
245
-
If the function is called outside a testbench process, an exception will be raised.
246
-
247
273
`Tick()`, `Delay()`, `Active()`and`Passive()`as well as the ability to pass generator coroutines as`process` are deprecated and removed in a future version.
248
274
249
275
## Drawbacks
@@ -255,7 +281,12 @@ If the function is called outside a testbench process, an exception will be rais
- Do nothing. Keep the existing interface, add `Changed()` alongside `Delay()`and`Tick()`, use `yield from` when calling functions.
284
+
`sim.get()`isnot available in processes created with`add_process()` to simplify the user interface and eliminate the possibility of misusing a helper function by calling it from the wrong type of process.
285
+
- Most helper functions will be implemented using `await sim.tick().sample(...)`, mirroring the structure of the gateware they are driving. These functions may be safely called from either processes added with`add_testbench()`orwith`add_process()` since the semantics of `await sim.tick()`is the same between them.
286
+
- Some helper functions will be using `sim.get(val)`, and will only be callablefrom processes added with`add_testbench()`, raising an error otherwise. In the legacy interface, the semantics of `yield val` changes depending on the type of the process, potentially leading to extremely confusing behavior. This isnot possible in the async interface.
287
+
288
+
Alternatives:
289
+
- Do nothing. Keep the existing interface, add `Changed()` alongside `Delay()`and`Tick()`, expand `Tick()` to add sampling, use `yield from` when calling functions.
259
290
260
291
## Prior art
261
292
[prior-art]: #prior-art
@@ -265,7 +296,10 @@ Other python libraries like [cocotb](https://docs.cocotb.org/en/stable/coroutine
265
296
## Unresolved questions
266
297
[unresolved-questions]: #unresolved-questions
267
298
268
-
None.
299
+
- Is there really a need to ban `sim.delay()`from processes added with`add_process()`?
300
+
- The value of `add_process()`isin ensuring that multiple processes waiting on the same trigger will modify simulation state deterministically no matter which order they run. Multiple processes waiting on a specific point in time using `sim.delay()` does not appear a common case.
301
+
-`sim.delay()`in processes added with`add_process()` may unduly complicate implementation, since timeline advancement then can raise readiness of two kinds of processes instead of one. It is also likely to cause issues withCXXRTL integration.
302
+
-`sim.delay()`in processes added with`add_process()`is useful to implement delay and phase shift blocks. However, these can be implemented in processes added with`add_testbench()`with no loss of functionality, as such blocks do not need delta cycle accurate synchronization with others on the same trigger.
0 commit comments