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
value = await sim.tick().sample(stream.data).until(stream.valid)
167
+
sim.set(stream.ready, 0)
174
168
return value
175
169
176
-
@testbench_helper
177
170
async def stream_send(sim, stream, value):
178
-
await sim.set(stream.data, value)
179
-
180
-
await sim.set(stream.valid, 1)
171
+
sim.set(stream.data, value)
172
+
sim.set(stream.valid, 1)
181
173
await sim.tick().until(stream.ready)
182
-
183
-
await sim.tick()
184
-
await sim.set(stream.valid, 0)
174
+
sim.set(stream.valid, 0)
185
175
</code></pre>
186
-
<p><code>await sim.get()</code> and <code>await sim.set()</code> replaces the existing operations <code>yield signal</code> and <code>yield signal.eq()</code> respectively.</p>
187
-
<p><code>sim.tick()</code> replaces the existing <code>Tick()</code>. It returns a trigger object that either can be awaited directly, or made conditional through <code>.until()</code>.</p>
188
-
<p>The <code>testbench_helper</code> decorator indicates that this function is only designed to be called from testbench processes and will raise an exception if called elsewhere.</p>
176
+
<p><code>sim.get()</code> and <code>sim.set()</code> replaces the existing operations <code>yield signal</code> and <code>yield signal.eq()</code> respectively.</p>
177
+
<p><code>sim.tick()</code> replaces the existing <code>Tick()</code>. It returns a trigger object that either can be awaited directly, or made conditional through <code>.until()</code>. Values of signals can be captured using <code>.sample()</code>, 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.</p>
189
178
<blockquote>
190
179
<p><strong>Note</strong>
191
180
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.
<p>Since <code>stream_send()</code> and <code>stream_recv()</code> invokes <code>sim.get()</code> and <code>sim.set()</code> that in turn will invoke the appropriate value conversions for a value castable (here <code>data.View</code>), it is general enough to work for streams with arbitrary shapes.</p>
215
204
<p><code>Tick()</code> and <code>Delay()</code> are replaced by <code>sim.tick()</code> and <code>sim.delay()</code> respectively.
216
205
In addition, <code>sim.changed()</code> and <code>sim.edge()</code> is introduced that allows creating triggers from arbitrary signals.</p>
217
-
<p><code>sim.tick()</code> return a domain trigger object that can be made conditional through <code>.until()</code> or repeated through <code>.repeat()</code>.</p>
206
+
<p><code>sim.tick()</code> return a domain trigger object that can be made conditional through <code>.until()</code> or repeated through <code>.repeat()</code>. Arbitrary expressions may be sampled at the active edge of the domain clock using <code>.sample()</code>.</p>
218
207
<p><code>sim.delay()</code>, <code>sim.changed()</code> and <code>sim.edge()</code> return a combinable trigger object that can be used to add additional triggers.</p>
219
208
<p><code>Active()</code> and <code>Passive()</code> are replaced by an <code>background=False</code> keyword argument to <code>.add_testbench()</code>.
220
209
Processes created through <code>.add_process()</code> are always created as background processes.
<p>Both methods are updated to accept an async function passed as <code>process</code>.
268
257
The async function must accept an argument <code>sim</code>, which will be passed a simulator context.
269
258
(Argument name is just convention, will be passed positionally.)</p>
259
+
<p>The usage model of the two kinds of processes are:</p>
260
+
<ul>
261
+
<li>Processes are added with <code>add_process()</code> for the sole purpose of simulating a part of the netlist with behavioral Python code.
262
+
<ul>
263
+
<li>Typically such a process will consist of a top-level <code>async for values in sim.tick().sample(...):</code> or <code>async for values in sim.changed(...)</code>, but this is not a requirement.</li>
264
+
<li>Such processes may only wait on signals, via <code>sim.tick()</code>, <code>sim.changed()</code>, and <code>sim.edge()</code>. They cannot advance simulation time via <code>sim.delay()</code>.</li>
265
+
<li>In these processes, <code>sim.get()</code> is not available; values of signals may only be obtained by awaiting on triggers.
266
+
<code>sim.set(x, y)</code> may be used to propagate the value of <code>y</code> without reading it.</li>
267
+
<li>The function passed to <code>add_process()</code> 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.
268
+
Provided that, the outcome of running such a process is deterministic regardless of the order of their execution.</li>
269
+
</ul>
270
+
</li>
271
+
<li>Processes are added with <code>add_testbench()</code> for any other purpose, including but not limited to: providing a stimulus, performing I/O, displaying state, asserting outcomes, and so on.
272
+
<ul>
273
+
<li>Such a process may be a simple linear function, use a top-level loop, or have arbitrarily complex structure.</li>
274
+
<li>Such processes may wait on signals as well as advance simulation time.</li>
275
+
<li>In these processes, <code>sim.get(x)</code> is available and returns the most current value of <code>x</code> (after all pending combinatorial propagation finishes).</li>
276
+
<li>The function passed to <code>add_testbench()</code> may have arbitrary side effects.
277
+
These processes are scheduled in an unspecified order that may not be deterministic, and no mechanisms are provided to recover determinism of outcomes.</li>
278
+
<li>When waiting on signals, e.g. via <code>sim.tick()</code>, the requested expressions are sampled before the processes added with <code>add_process()</code> and RTL processes perform combinatorial propagation. However, execution continues only after all pending combinatorial propagation finishes.</li>
279
+
</ul>
280
+
</li>
281
+
</ul>
282
+
<p>The following concurrency guarantees are provided:</p>
283
+
<ul>
284
+
<li>Async processes registered with <code>add_testbench</code> may be preempted by:
285
+
<ul>
286
+
<li>Any other process when calling <code>await ...</code>.</li>
287
+
<li>A process registered with <code>add_process</code> (or an RTL process) when calling <code>sim.set()</code> or <code>sim.memory_write()</code>. In this case, control is returned to the same testbench after combinational settling.</li>
288
+
</ul>
289
+
</li>
290
+
<li>Async processes registered with <code>add_process</code> may be preempted by:
291
+
<ul>
292
+
<li>Any other process when calling <code>await ...</code>.</li>
293
+
</ul>
294
+
</li>
295
+
<li>Legacy processes follow the same rules as async processes, with the exception of:
296
+
<ul>
297
+
<li>A legacy process may not be preempted when calling <code>yield x:ValueLike</code> or <code>yield x:Assign</code>.</li>
298
+
</ul>
299
+
</li>
300
+
<li>Once running, a process continues to execute until it terminates or is preempted.</li>
301
+
</ul>
270
302
<p>The new optional named argument <code>background</code> registers the testbench as a background process when true.
271
303
Processes created through <code>add_process</code> are always registered as background processes (except when registering legacy non-async generator functions).</p>
272
304
<p>The simulator context has the following methods:</p>
<li>Returns the value of <code>expr</code> when awaited.
309
+
<li>Returns the value of <code>expr</code>.
278
310
When <code>expr</code> is a value-castable, and its <code>shape()</code> is a <code>ShapeCastable</code>, the value will be converted through the shape's <code>.from_bits()</code>.
279
-
Otherwise, a plain integer is returned.</li>
311
+
Otherwise, a plain integer is returned.
312
+
This function is not available in processes created through <code>add_process</code>.</li>
<li>Set <code>expr</code> to <code>value</code> when awaited.
318
+
<li>Set <code>expr</code> to <code>value</code>.
286
319
When <code>expr</code> is a value-castable, and its <code>shape()</code> is a <code>ShapeCastable</code>, the value will be converted through the shape's <code>.const()</code>.
287
-
Otherwise, it must be a const-castable <code>ValueLike</code>.</li>
320
+
Otherwise, it must be a const-castable <code>ValueLike</code>.
321
+
When used in a process created through <code>add_testbench</code>, it may execute RTL processes and processes created through <code>add_process</code>.</li>
<li>Write <code>value</code> to <code>address</code> in <code>instance</code> when awaited. If <code>mask</code> is given, only the corresponding bits are written.
332
+
<li>Write <code>value</code> to <code>address</code> in <code>instance</code>. If <code>mask</code> is given, only the corresponding bits are written.
298
333
Like <code>MemoryInstance</code>, these two functions are an internal interface that will be usually only used via <code>lib.Memory</code>.
334
+
When used in a process created through <code>add_testbench</code>, it may execute RTL processes and processes created through <code>add_process</code>.
<p>To ensure testbench helper functions are only called from a testbench process, the <code>amaranth.sim.testbench_helper</code> decorator is added.
407
-
The function wrapper expects the first positional argument (or second, after <code>self</code> or <code>cls</code> if decorating a method/classmethod) to be a simulator context, and will raise <code>TypeError</code> if not.
408
-
If the function is called outside a testbench process, an exception will be raised.</p>
409
458
<p><code>Tick()</code>, <code>Delay()</code>, <code>Active()</code> and <code>Passive()</code> as well as the ability to pass generator coroutines as <code>process</code> are deprecated and removed in a future version.</p>
<li>Increase in API surface area and complexity.</li>
413
462
<li>Churn.</li>
414
463
</ul>
415
464
<h2id="rationale-and-alternatives"><aclass="header" href="#rationale-and-alternatives">Rationale and alternatives</a></h2>
465
+
<p><code>sim.get()</code> is not available in processes created with <code>add_process()</code> to simplify the user interface and eliminate the possibility of misusing a helper function by calling it from the wrong type of process.</p>
466
+
<ul>
467
+
<li>Most helper functions will be implemented using <code>await sim.tick().sample(...)</code>, mirroring the structure of the gateware they are driving. These functions may be safely called from either processes added with <code>add_testbench()</code> or with <code>add_process()</code> since the semantics of <code>await sim.tick()</code> is the same between them.</li>
468
+
<li>Some helper functions will be using <code>sim.get(val)</code>, and will only be callable from processes added with <code>add_testbench()</code>, raising an error otherwise. In the legacy interface, the semantics of <code>yield val</code> changes depending on the type of the process, potentially leading to extremely confusing behavior. This is not possible in the async interface.</li>
469
+
</ul>
470
+
<p>Alternatives:</p>
416
471
<ul>
417
-
<li>Do nothing. Keep the existing interface, add <code>Changed()</code> alongside <code>Delay()</code> and <code>Tick()</code>, use <code>yield from</code> when calling functions.</li>
472
+
<li>Do nothing. Keep the existing interface, add <code>Changed()</code> alongside <code>Delay()</code> and <code>Tick()</code>, expand <code>Tick()</code> to add sampling, use <code>yield from</code> when calling functions.</li>
<p>Other python libraries like <ahref="https://docs.cocotb.org/en/stable/coroutines.html">cocotb</a> that originally used generator based coroutines have also moved to <code>async</code>/<code>await</code> style coroutines.</p>
<li>Is there really a need to ban <code>sim.delay()</code> from processes added with <code>add_process()</code>?
479
+
<ul>
480
+
<li>The value of <code>add_process()</code> is in 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 <code>sim.delay()</code> does not appear a common case.</li>
481
+
<li><code>sim.delay()</code> in processes added with <code>add_process()</code> 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 with CXXRTL integration.</li>
482
+
<li><code>sim.delay()</code> in processes added with <code>add_process()</code> is useful to implement delay and phase shift blocks. However, these can be implemented in processes added with <code>add_testbench()</code> with no loss of functionality, as such blocks do not need delta cycle accurate synchronization with others on the same trigger.</li>
0 commit comments