Once fork...join_any or fork...join_none lets the parent thread resume while some spawned threads are still running, disable fork is how you clean them up: it kills every active thread that was forked by the currently executing process.
7 min read | Intermediate Level
What You'll Learn
- What
disable forkdoes, and exactly which processes it terminates - Why
fork...join_anycommonly needsdisable forkcalled afterward - What happens to a thread that had already fully completed before
disable forkruns - The scope of
disable fork— it isn't limited to just the most recent fork statement
Why Disable a Fork?
Here's what happens at the start of the example below:
- The main thread executes the
initialblock and reaches afork...join_anyblock - It launches three threads in parallel, and waits for any one of them to finish
- Thread1 finishes first, since it has the least delay
- The main thread resumes
Thread2 and Thread3 are still running even after the main thread has come out of the fork...join_any block:
module tb_top;
initial begin
// Fork off 3 sub-threads in parallel and the currently executing main thread
// will finish when any of the 3 sub-threads have finished.
fork
// Thread1 : Will finish first at time 40ns
#40 $display ("[%0t ns] Show #40 $display statement", $time);
// Thread2 : Will finish at time 70ns
begin
#20 $display ("[%0t ns] Show #20 $display statement", $time);
#50 $display ("[%0t ns] Show #50 $display statement", $time);
end
// Thread3 : Will finish at time 60ns
#60 $display ("[%0t ns] TIMEOUT", $time);
join_any
// Display as soon as the fork is done
$display ("[%0tns] Fork join is done", $time);
end
endmodule
Output
ncsim> run [20 ns] Show #20 $display statement [40 ns] Show #40 $display statement [40ns] Fork join is done [60 ns] TIMEOUT [70 ns] Show #50 $display statement ncsim: *W,RNQUIE: Simulation is complete. ncsim> exit
Even after "Fork join is done" prints at 40ns, Thread3's TIMEOUT message still shows up at 60ns, and Thread2's second display still shows up at 70ns — both threads kept running in the background with no way to stop them, since nothing in this version tells them to.
Quick Check
Q: In the output above, why does "Fork join is done" print at exactly 40ns, and not later?
Show Answer
Because this is fork...join_any, not plain join. The main thread resumes as soon as the first of the three threads finishes — Thread1, at 40ns — without waiting for Thread2 (70ns) or Thread3 (60ns) at all.
What Happens When a Fork Is Disabled?
Taking the same example, disable fork is added right after the fork...join_any finishes:
module tb_top;
initial begin
// Fork off 3 sub-threads in parallel and the currently executing main thread
// will finish when any of the 3 sub-threads have finished.
fork
// Thread1 : Will finish first at time 40ns
#40 $display ("[%0t ns] Show #40 $display statement", $time);
// Thread2 : Will finish at time 70ns
begin
#20 $display ("[%0t ns] Show #20 $display statement", $time);
#50 $display ("[%0t ns] Show #50 $display statement", $time);
end
// Thread3 : Will finish at time 60ns
#60 $display ("[%0t ns] TIMEOUT", $time);
join_any
// Display as soon as the fork is done
$display ("[%0tns] Fork join is done, let's disable fork", $time);
disable fork;
end
endmodule
Output
ncsim> run [20 ns] Show #20 $display statement [40 ns] Show #40 $display statement [40ns] Fork join is done, let's disable fork ncsim: *W,RNQUIE: Simulation is complete. ncsim> exit
Thread2 and Thread3 are killed by disable fork at 40ns — neither the TIMEOUT message nor Thread2's second display ever appears. Thread1, on the other hand, is unaffected: it had already fully completed before disable fork ran, so there was nothing left of it to terminate. Notice Thread2's first display, at 20ns, still shows up too — that statement had already executed before disable fork was called; only the parts of a thread still pending when disable fork runs get cut off.
disable fork terminates every active process that was forked by the currently executing process, directly or indirectly — not just the threads from the single most recent fork statement. If the current process had launched several separate fork...join_any or fork...join_none blocks earlier and left threads running from each of them, a single disable fork call kills all of them at once, not just the ones from whichever fork happens to be textually closest.
Where Is disable fork Used?
disable fork is the natural cleanup step after a fork...join_any timeout pattern — once the race between an event and a timeout has resolved one way or the other, the "losing" thread is still running in the background, and disable fork is how you make sure it actually stops instead of lingering for the rest of the test.
Common Beginner Mistakes
Mistake #1: Forgetting disable fork after join_any or join_none
Threads left running after a fork...join_any or fork...join_none don't stop on their own. Why? Without an explicit disable fork (or wait fork, covered next), those threads can keep executing in the background for the rest of the simulation, which is rarely the intended behavior.
Mistake #2: Assuming disable fork only affects the most recent fork block
As the note above explains, disable fork kills every active descendant process of the current process, not just whichever fork statement appears right before it. Why? If earlier code in the same process left other background threads running from a completely different fork block, those get terminated too — which can be surprising if you only meant to clean up the most recent one.
Mistake #3: Expecting disable fork to undo work a thread already finished
As Thread1 shows in the example above, a thread that already completed before disable fork runs is simply unaffected — its effects (like a display statement, or a signal it already drove) already happened and stay that way. Why? disable fork can only stop work that's still pending; it has no way to reverse something a thread already finished doing.
Practice Exercise
Challenge: Clean Up After a Timeout
Take the wait(done)-versus-#100 timeout pattern from the previous article and add disable fork right after the join_any, so that whichever branch didn't finish first gets terminated instead of lingering.
Hint
Place disable fork; as the very next statement after join_any, outside the fork block itself.
Solution
module tb;
bit done;
initial begin
fork
begin
wait (done);
$display ("[%0t] Event occurred", $time);
end
begin
#100;
$display ("[%0t] Timed out", $time);
end
join_any
disable fork;
end
endmodule
If done goes high before 100ns, the first branch finishes, join_any resumes, and disable fork then terminates the still-waiting #100 branch before it ever gets a chance to print "Timed out." If the timeout wins instead, disable fork terminates the branch still stuck on wait (done).
Summary
In this tutorial, you learned:
disable forkterminates every active process forked by the currently executing process- A thread that has already fully completed before
disable forkruns is unaffected — only pending work gets cut off - Statements a thread already executed before
disable forkruns stay executed; only what's still pending is stopped disable fork's scope covers every active descendant process of the current process, not just the most recentforkstatement- It's the standard cleanup step after a
fork...join_anytimeout pattern, terminating whichever branch didn't win the race
Continuing the Threads & Processes Chapter
Continue with SystemVerilog wait fork for explicitly waiting on background threads instead of terminating them.
Ordinary constraints are called hard constraints, because it's mandatory for the solver to always satisfy them — if it can't, randomization fails outright. A constraint declared soft gives the solver some flexibility instead: it doesn't have to be satisfied if some other, higher-priority constraint conflicts with it — either a hard constraint, or a soft constraint the solver has decided takes precedence. Soft constraints are commonly used to specify default values and distributions for random variables.
12 min read | Intermediate Level
What You'll Learn
- The difference between a hard constraint (must always be satisfied) and a soft one (a default that yields to conflicts)
- Why
softapplies to individual expressions, not to an entire constraint block, even when both appear together - What happens when a soft constraint genuinely conflicts with something else — a hard constraint, or an inline one
- Why the exact same conflicting inline constraint produces a completely different outcome depending on whether the original was soft or hard
- How
disable softcleanly replaces an inherited soft default, removing any ambiguity about which of two competing soft constraints actually takes effect
Example
Here, a soft constraint tells the solver to produce values between 4 and 12 for data — but only the first expression is actually soft:
class ABC;
rand bit [3:0] data;
// This constraint is defined as "soft"
constraint c_data { soft data >= 4;
data <= 12; }
endclass
module tb;
ABC abc;
initial begin
abc = new;
for (int i = 0; i < 5; i++) begin
abc.randomize();
$display ("abc = 0x%0h", abc.data);
end
end
endmodule
As expected, the randomized values fall within 4 and 12:
ncsim> run abc = 0x4 abc = 0x8 abc = 0x4 abc = 0x7 abc = 0x7 ncsim: *W,RNQUIE: Simulation is complete.
soft applies only to the specific expression it directly precedes — here, data >= 4. data <= 12 has no soft keyword of its own, so it remains a hard constraint, even though it's sitting in the same block. Mixing soft and hard expressions within one constraint block is completely legal, and each expression's own priority is entirely independent of the others around it.
Now let's see how a contradicting inline constraint is handled:
module tb;
ABC abc;
initial begin
abc = new;
for (int i = 0; i < 5; i++) begin
abc.randomize() with { data == 2; };
$display ("abc = 0x%0h", abc.data);
end
end
endmodule
The contradicting inline constraint successfully sets data to 2 — contradicting because the original constraint's soft part asked for data >= 4, but the inline constraint demands 2, which is outside that range:
ncsim> run abc = 0x2 abc = 0x2 abc = 0x2 abc = 0x2 abc = 0x2 ncsim: *W,RNQUIE: Simulation is complete.
Since the inline constraint (data == 2) outranks the soft one, the soft data >= 4 rule simply yields — no failure, no error, just a quietly overridden default. The hard part, data <= 12, is untouched by any of this, since 2 never conflicted with it in the first place.
Quick Check
Q: Would randomize() with { data == 15; }; succeed against this same class?
Show Answer
No. 15 conflicts with the hard part of the constraint, data <= 12 — and unlike the soft part, a hard constraint never yields. Randomization would fail here, exactly as if the entire constraint were hard.
How Is It Different From Hard Constraints?
Now the same class, with soft removed entirely — both expressions are hard — and the same conflicting inline constraint:
class ABC;
rand bit [3:0] data;
constraint c_data { data >= 4;
data <= 12; }
endclass
module tb;
ABC abc;
initial begin
abc = new;
for (int i = 0; i < 1; i++) begin
abc.randomize() with { data == 2; };
$display ("abc = 0x%0h", abc.data);
end
end
endmodule
This time the solver fails outright, because of the contradicting constraint, and data is left at 0:
ncsim> run
abc.randomize() with { data == 2; };
|
ncsim: *W,SVRNDF (./testbench.sv,14|18): The randomize method call failed. The unique id of the failed randomize call is 4.
Observed simulation time : 0 FS + 0
ncsim: *W,RNDOCS: These constraints contribute to the set of conflicting constraints:
constraint c_data { data >= 4; (./testbench.sv,4)
abc.randomize() with { data == 2; }; (./testbench.sv,14)
ncsim: *W,RNDOCS: These variables contribute to the set of conflicting constraints:
rand variables:
data [./testbench.sv, 2]
abc = 0x0
ncsim: *W,RNQUIE: Simulation is complete.
Same class, same conflicting inline value — but a completely different outcome, purely because data >= 4 is no longer negotiable. This is the entire practical value of soft: the exact same rule either yields gracefully or blocks the entire solve, depending only on whether it was declared soft.
Where Is This Used?
Soft constraints are the standard way to give a variable a reasonable default value or distribution — "most transactions should be reads," for instance — while still letting individual tests freely override that default with an inline constraint or a more specific hard constraint in a subclass, without needing to explicitly disable anything first.
Replacing an Inherited Soft Constraint With disable soft
A subclass can inherit a soft constraint from its parent and simply add a conflicting hard constraint of its own — as already shown, the soft one yields automatically, no extra syntax required. But what if the subclass wants to replace that soft default with a different soft default, rather than a hard rule? Adding a second soft constraint on the same variable, without removing the first, leaves two soft constraints competing on the same variable at once — which one actually takes effect then depends on the solver's own priority rules, rather than being something the subclass author explicitly controls.
disable soft removes that ambiguity. Placed inside a constraint block, it strips away any soft constraint currently active on a specific variable, so whatever comes next — soft or hard — is guaranteed to be the only one governing that variable, rather than one of two competing defaults:
class ABC;
rand bit [3:0] data;
constraint c_data { soft data >= 4;
data <= 12; }
endclass
class XYZ extends ABC;
constraint c_new {
disable soft data; // Remove the inherited soft default entirely
soft data inside {0, 1, 2}; // This is now the only soft rule on 'data'
}
endclass
XYZ inherits ABC's constraints, including the soft data >= 4 default and the hard data <= 12 rule. disable soft data; removes just the inherited soft default, leaving the hard data <= 12 constraint from the parent fully intact. The new soft rule, data inside {0, 1, 2}, is now the only soft constraint in play for data — there's no second soft default left to compete with it.
Quick Check
Q: After disable soft data;, does XYZ still respect the hard data <= 12 rule inherited from ABC?
Show Answer
Yes. disable soft only removes soft constraints on the named variable — it has no effect on hard constraints. data <= 12 stays fully enforced in XYZ, exactly as it was in ABC.
Common Beginner Mistakes
Mistake #1: Assuming soft applies to an entire constraint block
As the first example's note explains, soft only affects the specific expression it directly precedes. Why? A block can freely mix soft and hard expressions — assuming the whole block became soft the moment the keyword appeared anywhere in it would misread which parts are actually negotiable.
Mistake #2: Assuming a soft constraint can cause randomize() to fail
On its own, it can't — a soft constraint always yields rather than blocking a solution. Why? Its entire purpose is to describe a preference that's safe to override; only hard constraints (including inline ones) are capable of causing an outright solver failure.
Mistake #3: Forgetting a hard expression can sit right next to a soft one
As the first example shows, data <= 12 stays fully enforced even while data >= 4 is soft and gets overridden. Why? Each expression's priority is independent — a soft default doesn't weaken any of the hard rules declared alongside it.
Mistake #4: Assuming inline and soft constraints have equal priority
They don't — an inline constraint behaves like a hard one, and overrides a conflicting soft constraint rather than the other way around. Why? This is exactly why the inline data == 2 example succeeds against the soft rule but still has to respect the hard data <= 12 rule sitting right next to it.
Mistake #5: Adding a second soft constraint without disabling the first
Leaving both the inherited soft default and a new soft constraint active on the same variable means the solver decides between them, rather than the subclass author. Why? disable soft removes that ambiguity outright — if the intent is specifically to replace a soft default with a new one, disabling the old one first guarantees only the new rule is in play.
Practice Exercise
Challenge: Soft vs a Separate Hard Block
Given constraint c_default { soft len == 8; } and a second, separate block constraint c_override { len == 3; } in the same class, what value will len randomize to?
Hint
The conflict here works the same way as the inline example in this article — it just comes from a second constraint block instead of a with clause.
Solution
len will always be 3. c_override is a hard constraint, and it directly conflicts with the soft c_default — exactly like the inline data == 2 example, the soft constraint yields without causing any solver failure, regardless of whether the conflicting hard rule comes from an inline clause or a separate named block.
Summary
In this tutorial, you learned:
- A hard constraint must always be satisfied, or randomization fails; a soft constraint is a default that yields when something higher-priority conflicts with it
softapplies only to the specific expression it precedes, not to an entire constraint block, so soft and hard expressions can freely coexist in one block- A soft constraint never causes
randomize()to fail on its own — it simply gets overridden - An inline constraint behaves like a hard one, overriding a conflicting soft constraint while still having to respect any hard constraints that remain in play
disable softremoves an inherited soft constraint on a specific variable, guaranteeing a new soft (or hard) rule replaces it cleanly, rather than competing with it under the solver's own priority rules
Immediate assertions are executed based on simulation event semantics and are required to be specified in a procedural block. They're treated the same way as the expression in an if statement during simulation.
The immediate assertion passes if the expression holds true at the time the statement executes, and fails if the expression evaluates to false (X, Z, or 0). These assertions are intended for use in simulation and aren't suitable for formal verification. They can be used in both RTL design and testbench code to flag errors during simulation.
10 min read | Intermediate Level
What You'll Learn
- What makes an assertion "immediate" rather than concurrent, and where it can be used
- The three ways to write an immediate assertion: bare, with pass/fail blocks, and with a name label
- How an immediate assertion inside a design catches a protocol violation the instant it happens
- Why wrapping a
randomize()call in an assertion turns an easily-missed warning into an unmissable failure
Syntax
// Simple assert statement
assert();
// Assert statement with statements to be executed for pass/fail conditions
assert() begin
// If condition is true, execute these statements
end else begin
// If condition is false, execute these statements
end
// Optionally give name for the assertion
[assert_name] : assert();
Immediate Assertion in Design
Here's an example where the design has an immediate assertion checking that a push request to the FIFO doesn't come at a time when the FIFO is already full. If the expression within the assert statement evaluates to true, the first begin end block executes; if false, the else part executes. This is much like the if construct, with the difference that it isn't required for the user to place a display statement to flag the error — the assertion mechanism does that automatically:
module my_des (my_if _if);
always @ (posedge _if.clk) begin
if (_if.push) begin
// Immediate assertion and ensures that
// fifo is not full when push is 1
a_push: assert (!_if.full) begin
$display("[PASS] push when fifo not full");
end else begin
$display("[FAIL] push when fifo full !");
end
end
if (_if.pop) begin
// Immediate assertion to ensure that fifo is not
// empty when pop is 1
a_pop: assert (!_if.empty) begin
$display ("[PASS] pop when fifo not empty");
end else begin
$display ("[FAIL] pop when fifo empty !");
end
end
end
endmodule
Without immediate assertions like this, achieving the same check with concurrent assertions would mean re-expressing the "only when push is asserted" condition from scratch — the immediate assertion instead reuses the surrounding if (_if.push) that already got you to exactly the right point in the procedural flow, at no extra cost.
interface my_if(input bit clk);
logic pop;
logic push;
logic empty;
logic full;
endinterface
module tb;
bit clk;
always #10 clk <= ~clk;
my_if _if (clk);
my_des u0 (.*);
initial begin
for (int i = 0; i < 5; i++) begin
_if.push <= $random;
_if.pop <= $random;
_if.empty <= $random;
_if.full <= $random;
$strobe("[%0t] push=%0b full=%0b pop=%0b empty=%0b",
$time, _if.push, _if.full, _if.pop, _if.empty);
@(posedge clk);
end
#10 $finish;
end
endmodule
Notice how the time and line of assertion failure is displayed as E:
ncsim> run [0] push=0 full=1 pop=1 empty=1 ncsim: *E,ASRTST (./design.sv,13): (time 10 NS) Assertion tb.u0.a_pop has failed [FAIL] pop when fifo empty ! [10] push=1 full=0 pop=1 empty=1 [PASS] push when fifo not full ncsim: *E,ASRTST (./design.sv,13): (time 30 NS) Assertion tb.u0.a_pop has failed [FAIL] pop when fifo empty ! [30] push=1 full=1 pop=1 empty=0 ncsim: *E,ASRTST (./design.sv,5): (time 50 NS) Assertion tb.u0.a_push has failed [FAIL] push when fifo full ! [PASS] pop when fifo not empty [50] push=1 full=0 pop=0 empty=1 [PASS] push when fifo not full [70] push=1 full=1 pop=0 empty=1 ncsim: *E,ASRTST (./design.sv,5): (time 90 NS) Assertion tb.u0.a_push has failed [FAIL] push when fifo full ! Simulation complete via $finish(1) at time 100 NS + 0 ./testbench.sv:25 #10 $finish; ncsim> exit
At the very first posedge (10ns), the values driven at 0ns are what get checked: pop was 1 and empty was 1, so a_pop fails immediately — push was 0 at that same moment, so a_push's if (push) never even evaluates that cycle.
Quick Check
Q: At 50ns, a_push fails while a_pop passes in the same clock cycle. Why can these two outcomes differ within one edge?
Show Answer
They're two completely independent checks against different signal pairs — push against full, and pop against empty. Nothing links their outcomes together; the random stimulus at that specific moment simply happened to violate one condition while satisfying the other.
Immediate Assertion in Testbench
Assume a class called Packet is created and randomized, but this example has a constraint error, and randomization will fail. Without an assertion, the failure is only displayed as a warning message — if the user isn't careful, the test may display incorrect behavior and may even appear to pass:
class Packet;
rand bit [7:0] addr;
constraint c_addr { addr > 5; addr < 3; }
endclass
module tb;
initial begin
Packet m_pkt = new();
m_pkt.randomize();
end
endmodule
Output
ncsim> run
m_pkt.randomize();
|
ncsim: *W,SVRNDF (./testbench.sv,11|18): The randomize method call failed. The unique id of the failed randomize call is 0.
Observed simulation time : 0 FS + 0
ncsim: *W,RNDOCS: These constraints contribute to the set of conflicting constraints:
constraint c_addr { addr > 5; addr < 3; } (./testbench.sv,4)
ncsim: *W,RNDOCS: These variables contribute to the set of conflicting constraints:
rand variables:
addr [./testbench.sv, 2]
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
Notice that c_addr itself asks for an impossible value — addr can never be both greater than 5 and less than 3 at once — so this call fails every single time, yet the only sign of it is a warning easy to scroll straight past.
Instead, an immediate assertion can be placed directly on the randomization method call, ensuring the return value is always 1, indicating success. If the assertion fails, it forces the user to actually look at the failure, reducing debug effort:
class Packet;
rand bit [7:0] addr;
constraint c_addr { addr > 5; addr < 3; }
endclass
module tb;
initial begin
Packet m_pkt = new();
assert(m_pkt.randomize());
end
endmodule
The simulator assigns a generated name for the assertion if the user hasn't specified one:
ncsim> run
assert(m_pkt.randomize());
|
ncsim: *W,SVRNDF (./testbench.sv,11|25): The randomize method call failed. The unique id of the failed randomize call is 0.
Observed simulation time : 0 FS + 0
ncsim: *W,RNDOCS: These constraints contribute to the set of conflicting constraints:
constraint c_addr { addr > 5; addr < 3; } (./testbench.sv,4)
ncsim: *W,RNDOCS: These variables contribute to the set of conflicting constraints:
rand variables:
addr [./testbench.sv, 2]
ncsim: *E,ASRTST (./testbench.sv,11): (time 0 FS) Assertion tb.unmblk1.__assert_1 has failed
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
The exact same underlying failure now produces a hard ASRTST assertion failure alongside the warning, instead of just the easily-missed warning on its own. In a similar way, assert can be used with any expression that evaluates to true or false within a procedural block.
Quick Check
Q: If the constraint were fixed to something satisfiable, like addr > 5; addr < 10;, would assert(m_pkt.randomize()); ever fail?
Show Answer
No. With a satisfiable constraint, randomize() succeeds and returns 1 every time, which the assertion accepts — the assertion only exists to catch a failure, not to introduce one on its own.
By default, a failed immediate assertion doesn't stop simulation on its own — both examples above continue running normally after each reported failure. This is because an assertion with no explicit severity system task defaults to $error severity, which reports but doesn't halt. Terminating on failure requires explicitly using $fatal in the assertion's else clause instead.
Where Is This Used?
Wrapping every randomize() call in an assertion (often through a team-defined shorthand macro) is considered mandatory practice in most production testbenches. An unnoticed randomization failure can silently leave a variable at its default or previous value instead of the intended randomized stimulus — potentially masking a real bug or producing a false pass with no obvious sign anything went wrong.
Common Beginner Mistakes
Mistake #1: Assuming immediate assertions work for formal verification
Immediate assertions are simulation-only. Why? They follow simulation event semantics tied to procedural execution order, which formal tools have no equivalent concept of — formal verification needs concurrent assertions instead.
Mistake #2: Trying to use an immediate assertion outside a procedural block
An immediate assertion has to be inside an always, initial, or similar procedural block. Why? It's evaluated exactly like an if statement at the point it's reached, so it needs the same procedural context an if statement would.
Mistake #3: Forgetting to wrap a randomize() call in an assertion
As the Packet example shows, an unwrapped randomize() failure only produces a warning that's easy to overlook. Why? A test can silently continue running with unintended data after a failed randomization, producing confusing or outright wrong results with no obvious explanation.
Mistake #4: Assuming a failed assertion always stops simulation
As the note above explains, the default severity only reports the failure — it doesn't halt anything. Why? If you specifically need simulation to stop the moment a check fails, that requires an explicit $fatal call in the assertion's else clause, not just the bare assertion itself.
Practice Exercise
Challenge: Check Mutual Exclusion
Write an immediate assertion inside an always @(posedge clk) block that checks wr_en and rd_en are never both high at the same time. Name the assertion a_mutex, and print "[FAIL] wr_en and rd_en both high!" on failure.
Hint
The condition you want to hold true is that at most one of the two signals is high — not both. Express that as a single boolean expression inside the assert(...).
Solution
always @ (posedge clk) begin
a_mutex: assert (!(wr_en && rd_en)) begin
// Passed - nothing to do
end else begin
$display ("[FAIL] wr_en and rd_en both high!");
end
end
!(wr_en && rd_en) is true whenever at most one of the two signals is high, and false only when both are high at once — exactly the mutual exclusion condition being checked.
Summary
In this tutorial, you learned:
- An immediate assertion is evaluated like an
ifstatement, at the exact point it's reached in a procedural block - Immediate assertions are simulation-only, and can be used in both design and testbench code
- Placing an immediate assertion inside existing control flow (like
if (push)) reuses that condition instead of re-expressing it separately, unlike a concurrent assertion - Wrapping a
randomize()call in an assertion turns an easily-missed warning into an unmissable failure - A failed assertion doesn't stop simulation by default — that requires explicitly using
$fatalseverity instead
Recap: What Is a Skew?
Part 1 covered how a clocking block lets you group signals under a clock and apply timing rules to them. Those timing rules are called skews. This article goes deep on how input and output skews work, what #1step and #0 actually mean in simulation, and how to read the waveforms that result.
Here's the key intuition before looking at any code:
- Input skew — how far before the clock edge the signal is sampled (read)
- Output skew — how far after the clock edge the signal is driven (written)
Think of it like a daily 9:00 am team meeting. An input skew of 2ns is like taking notes on what's happening at 8:58 am — just before the meeting. An output skew of 5ns is like sending out your decisions 5 minutes after the meeting starts. The clock edge is the meeting; the skew controls when you observe or act.
10 min read | Intermediate Level
What You'll Learn
- How to specify a skew explicitly per signal, and what bare numbers versus explicit time units mean
- Exactly what
#1stepmeans, and why it's the safest input skew - Exactly what output
#0(or no skew at all) means, and why it isn't literally "zero delay" - How different output skews change when a signal is actually driven relative to the clock edge
- How different input skews change what value gets captured when a signal is changing rapidly
Skew Syntax
Skews can be specified as a default for all signals in the block, or per-signal to override the default. They can also be given in explicit time units (like #2ns, #1ps) or as bare numbers that follow the active timescale:
clocking cb @(posedge clk);
input #1ps gnt; // sample 1 ps before posedge clk
output #2 req; // drive 2 time-units after posedge clk (follows timescale)
input #1 output #3 sig; // inout: sample 1 unit before, drive 3 units after
endclocking
For the bare #2 on req: if the active timescale is 1ns/1ps, then #2 means 2ns. If it were 1ps/1ps, it would mean 2ps.
Always check your active timescale when using bare numbers for skew — the same #2 can mean very different things depending on what's in effect for that file.
The Special Case: #1step
#1step is the most commonly used input skew, and it's worth understanding precisely. Instead of specifying a time offset, it tells the simulator to sample the signal in the Preponed region of the current time step. This region runs before any active logic evaluates at that time — which means you're guaranteed to read the value the signal had at the end of the previous time step, before any new changes take effect.
In practical terms: #1step gives you the last stable, settled value of the signal just before the clock edge fires. It's the safest choice for inputs because it completely sidesteps race conditions.
clocking cb @(posedge clk);
input #1step gnt; // sample gnt in the Preponed region — always safe
endclocking
The Special Case: Output with #0 or No Skew
Outputs declared with #0 or with no skew at all are driven in the Re-NBA region of the same time step as the clock edge. The Re-NBA region runs after all non-blocking assignments in the design have settled. This ensures your testbench output doesn't interfere with what the design is computing on the same edge. It isn't simply "zero nanoseconds" — it's a deliberate scheduling position that keeps testbench and design updates separated.
A Complete Example
Let's build a working example step by step. The design is a simple request-grant module: it asserts gnt as soon as it sees req high.
module des (input logic req, clk, output logic gnt);
always @(posedge clk)
gnt <= req ? 1 : 0;
endmodule
The interface bundles the signals and the clocking block together. Here, gnt gets an input skew of 1ns (sampled 1ns before the positive edge), and req gets an output skew of 5ns (driven 5ns after the positive edge):
interface req_gnt_if (input logic clk);
logic gnt;
logic req;
clocking cb @(posedge clk);
input #1ns gnt; // sample gnt 1ns before posedge
output #5ns req; // drive req 5ns after posedge
endclocking
endinterface
The testbench drives req through the clocking block and then reads back gnt:
module tb;
logic clk;
initial clk = 0;
always #10 clk = ~clk; // 20ns period, posedge at 10, 30, 50 ...
logic req_val = 0; // testbench copy of req: an output clockvar cannot be read back
req_gnt_if vif (.clk(clk));
des dut (.clk(clk), .req(vif.req), .gnt(vif.gnt));
initial begin
vif.cb.req <= req_val;
// Toggle req several times at random intervals
repeat (5) begin
repeat ($urandom_range(1,3)) @(vif.cb);
req_val = ~req_val;
vif.cb.req <= req_val;
end
#40 $finish;
end
endmodule
From a waveform viewer, you'd see that every time the testbench assigns vif.cb.req, the actual change on the wire appears 5ns after the positive clock edge — not at the edge itself. This is the output skew at work.
Quick Check
Q: In the example above, if the testbench had written vif.req <= ~vif.req instead of going through vif.cb.req, when would the actual change appear on the wire?
Show Answer
Immediately, at whatever simulation time that statement executes — with no relationship to the clock edge at all. Bypassing the clocking block means the 5ns output skew is never applied.
Output Skew in Detail
To make output skew very visible, here's an interface with three clocking blocks that all share the same clock but each have a different output skew:
interface _if (input logic clk);
logic req;
clocking cb_0 @(posedge clk);
output #0ns req; // driven in Re-NBA region (same edge)
endclocking
clocking cb_1 @(posedge clk);
output #2ns req; // driven 2ns after posedge
endclocking
clocking cb_2 @(posedge clk);
output #5ns req; // driven 5ns after posedge
endclocking
endinterface
cb_0, cb_1, and cb_2 are all declared on the exact same @(posedge clk) event, side by side — SystemVerilog allows multiple clocking blocks to reference the same clock event, each maintaining its own independent skew, which is exactly what this example (and the next one) relies on to demonstrate different skews side by side.
The testbench iterates through three rounds, using a different clocking block each time to assert req:
module tb;
logic clk;
initial clk = 0;
always #10 clk = ~clk;
_if vif (.clk(clk));
initial begin
for (int i = 0; i < 3; i++) begin
repeat (2) @(vif.cb_0); // wait 2 posedges
case (i)
0: vif.cb_0.req <= 1; // driven at Re-NBA of posedge
1: vif.cb_1.req <= 1; // driven 2ns after posedge
2: vif.cb_2.req <= 1; // driven 5ns after posedge
endcase
repeat (2) @(vif.cb_0);
vif.req <= 0; // direct reset between rounds (no clocking block skew intended here)
end
#20 $finish;
end
endmodule
The line vif.req <= 0; bypasses the clocking block intentionally — it's used here only to reset the signal between test rounds, not as part of the timed stimulus. In a real testbench, always drive stimulus through the clocking block; direct assignments skip the skew entirely and should only be used for housekeeping tasks like initialization or reset.
In a waveform viewer, all three rounds show req going high at different offsets from the clock edge: immediately (Re-NBA region), 2ns later, and 5ns later. The clock edge itself is the same in all three cases — only the output skew differs.
Input Skew in Detail
Input skew controls when the clocking block takes a snapshot of a signal. To demonstrate this clearly, we use a design that drives a random 4-bit value every 1ns — so the signal is changing quickly, and different sampling times will capture different values:
module des (output logic [3:0] gnt);
always #1 gnt <= $random;
endmodule
The interface has four clocking blocks, each sampling gnt at a different point relative to the positive clock edge:
interface _if (input logic clk);
logic [3:0] gnt;
clocking cb_0 @(posedge clk);
input #0 gnt; // sample at posedge, in Observed region
endclocking
clocking cb_1 @(posedge clk);
input #1step gnt; // sample in Preponed region (just before posedge)
endclocking
clocking cb_2 @(posedge clk);
input #1ns gnt; // sample 1ns before posedge
endclocking
clocking cb_3 @(posedge clk);
input #2ns gnt; // sample 2ns before posedge
endclocking
endinterface
The testbench launches four parallel threads. Each thread waits for its own clocking block's edge and then prints the sampled value:
module tb;
logic clk;
initial clk = 0;
always #5 clk = ~clk; // 10ns period
_if vif (.clk(clk));
des dut (.gnt(vif.gnt));
initial begin
fork
begin @(vif.cb_0); $display("cb_0 (#0 ): gnt = 0x%0h", vif.cb_0.gnt); end
begin @(vif.cb_1); $display("cb_1 (#1step): gnt = 0x%0h", vif.cb_1.gnt); end
begin @(vif.cb_2); $display("cb_2 (#1ns ): gnt = 0x%0h", vif.cb_2.gnt); end
begin @(vif.cb_3); $display("cb_3 (#2ns ): gnt = 0x%0h", vif.cb_3.gnt); end
join
#10 $finish;
end
endmodule
Example output (exact values depend on the simulation seed):
cb_3 (#2ns ): gnt = 0x9
cb_2 (#1ns ): gnt = 0x3
cb_1 (#1step): gnt = 0x3
cb_0 (#0 ): gnt = 0xd
Why are the values different? The design is changing gnt every 1ns. The testbench's clock starts at 0 and first toggles high 5ns later, so the first positive edge actually lands at 5ns, not a round number like 10ns. At that edge:
- cb_3 samples at 3ns — it sees whatever value was present 2ns before the edge
- cb_2 samples at 4ns — it sees the value from 1ns before the edge
- cb_1 samples in the Preponed region at 5ns — it captures the last settled value before any active logic fires at the edge. This is why it gets the same result as cb_2 here: both saw the same stable value set at 4ns, with nothing changing gnt again before 5ns
- cb_0 samples at the edge itself, in the Observed region — it may catch a value already updated by active events at 5ns, which is why it differs from the others
This illustrates exactly why #1step is preferred for testbench inputs: it's designed to give you the last clean, settled value rather than a potentially mid-update snapshot.
Quick Check
Q: Of the four clocking blocks in this example, which one risks sampling a value the design just updated on the very same clock edge you're trying to synchronize with?
Show Answer
cb_0 (input #0). Sampling in the Observed region happens after active events at that time step have already run, so it can catch a brand-new value from that same edge instead of the stable value from before it — exactly the race condition #1step is designed to avoid.
Choosing the Right Skew: Practical Guidelines
- For inputs, always prefer
#1stepunless you have a specific reason to sample at a precise time offset. It gives the cleanest, race-free read. - For outputs, use a small positive time value (like
#1nsor#2ns) that still satisfies the setup time of the design but clearly places the drive event after the clock edge. Avoid#0unless you understand the Re-NBA scheduling implications. - Match your skew to your clock period. Driving an output
#8nsafter the edge of a 10ns clock means it arrives very close to the next edge — this could violate setup time on the design side. - Use bare numbers with caution. Always confirm the active
timescalein the file so you know what a bare#2actually means.
Where Does Skew Choice Matter in Practice?
Getting output skew right matters most when the design has real setup/hold requirements — a DDR-style interface that samples on both clock edges, or a protocol with a tight setup window, needs its output skew tuned so stimulus arrives comfortably before the design's own sampling point, not right on top of it.
Common Beginner Mistakes
Mistake #1: Driving signals directly instead of through the clocking block
Writing vif.req <= 1 in your testbench bypasses the output skew entirely — the signal is driven at whatever time that line executes, with no guaranteed relationship to the clock edge. Why? Always use vif.cb.req <= 1 for stimulus, or the clocking block's timing guarantees simply don't apply.
Mistake #2: Confusing the direction convention
Remember: input in the clocking block means the testbench reads it (the design drives it). Why? Swapping this causes your testbench to try to drive a signal it declared as input, which results in a compile error or X values.
Mistake #3: Assuming #0 output means "no delay"
It means the Re-NBA region — a specific simulation scheduling point after the design's non-blocking assignments have settled. Why? This makes it safer than a raw #0 delay outside a clocking block, but it's still not literally zero time — it's a protected ordering point, not a timing guarantee of "instantly."
Mistake #4: Using a bare number without checking timescale
output #5 req could mean 5ns or 5ps depending on the file's timescale directive. Why? Use explicit units like #5ns whenever precision matters, so the skew doesn't silently change meaning if the timescale is ever edited.
Practice Exercise
Challenge: Work Out the Sample Time
A clocking block declares input #3ns gnt, and the clock period is 20ns with a posedge at 100ns. At what simulation time is gnt sampled?
Hint
Input skew is always measured as an offset before the clocking event — subtract the skew value from the clock edge time.
Solution
97ns. Input skew is a delay before the clocking event, so 100ns − 3ns = 97ns.
Quick Check
Q: Two clocking blocks are both on posedge clk. One has input #1step gnt, the other has input #0 gnt. The design updates gnt on that same positive edge. Which one is safer to use for reading gnt, and why?
Show Answer
#1step is safer. It reads in the Preponed region, before any active events fire at that edge, so it can't see a partially-updated or brand-new value. #0 reads in the Observed region, after active events, which may already reflect the design's new value from that very same edge.
Summary
- Input skew is the time before the clock edge at which a signal is sampled — a larger input skew samples further in the past relative to the edge
- Output skew is the time after the clock edge at which a signal is driven — a larger output skew changes the signal later in the clock cycle
#1stepis the safest input skew — it samples in the Preponed region, capturing the last stable value before the edge- Output
#0is driven in the Re-NBA region — not "zero delay," but a protected scheduling zone after the design's non-blocking assignments - Multiple clocking blocks can share the same clock event, each with its own independent skew — useful for observing the same signal at several different timing references at once
- Always drive testbench signals through the clocking block handle to ensure skew is applied correctly
Continuing the Interfaces & Modports Chapter
Continue with SystemVerilog Modport.
Plain fork...join waits until every one of its spawned threads finishes — which means the simulation hangs if even one of those threads runs forever and never completes. fork...join_any is the variation that avoids this: the main thread resumes as soon as any one of the forked threads finishes, not all of them.
Fork off five threads with join_any, and the main thread resumes as soon as any single one of those five finishes — the remaining four keep running in the background after that.
7 min read | Intermediate Level
What You'll Learn
- Why plain
fork...joincan hang a simulation, and howjoin_anyavoids that - How
join_anydecides exactly when the parent thread resumes - How a nested
fork...join_anybehaves, and what "finishing" means for the nested block itself - What happens to the remaining threads that are still running once the parent resumes
Syntax
fork
// Thread 1
// Thread 2
// ...
// Thread N
join_any
A Basic fork...join_any Example
module tb;
initial begin
$display ("[%0t] Main Thread: Fork join going to start", $time);
fork
print (20, "Thread1_0");
print (30, "Thread1_1");
print (10, "Thread2");
join_any
$display ("[%0t] Main Thread: Fork join has finished", $time);
end
// Note that this task needs to be automatic
task automatic print (int _time, string t_name);
#(_time) $display ("[%0t] %s", $time, t_name);
endtask
endmodule
Output
ncsim> run [0] Main Thread: Fork join going to start [10] Thread2 [10] Main Thread: Fork join has finished [20] Thread1_0 [30] Thread1_1 ncsim: *W,RNQUIE: Simulation is complete.
All three threads are forked at 0ns. Thread2 finishes first, at 10ns, and since that's the very first thread to complete, the main thread resumes right there at 10ns — without waiting for Thread1_0 (20ns) or Thread1_1 (30ns) at all. Both of those keep running independently in the background and print later, on their own schedule.
Quick Check
Q: If Thread2's delay above were changed from #10 to #25, at what time would the main thread resume?
Show Answer
20ns. join_any resumes the parent as soon as the fastest thread finishes — with the delays now 20, 30, and 25, the fastest is Thread1_0 at 20ns.
Nested fork...join_any
module tb;
initial begin
$display ("[%0t] Main Thread: Fork join going to start", $time);
fork
fork
print (20, "Thread1_0");
print (30, "Thread1_1");
join_any
print (10, "Thread2");
join_any
$display ("[%0t] Main Thread: Fork join has finished", $time);
end
// Note that this task has to be automatic
task automatic print (int _time, string t_name);
#(_time) $display ("[%0t] %s", $time, t_name);
endtask
endmodule
Output
ncsim> run [0] Main Thread: Fork join going to start [10] Thread2 [10] Main Thread: Fork join has finished [20] Thread1_0 [30] Thread1_1 ncsim: *W,RNQUIE: Simulation is complete. ncsim> exit
The inner fork...join_any is itself treated as a single unit by the outer fork — it's considered "done" as soon as any one of its own two threads finishes. Thread1_0 finishes first among the inner pair, at 20ns, so the inner block is done at 20ns. The outer join_any then resumes as soon as the faster of its two threads finishes: the inner block (done at 20ns) or Thread2 directly (done at 10ns) — and 10ns wins, so the main thread resumes there, exactly as in the non-nested example. Thread1_1, still running inside the inner block, keeps going in the background and prints at 30ns regardless.
Quick Check
Q: If Thread2's delay in the nested example above were changed from #10 to #25, at what time would the main thread resume?
Show Answer
20ns. The inner block is still done at 20ns (Thread1_0 finishes first among the inner pair), and the outer join_any now compares that 20ns against Thread2's new 25ns — 20ns is faster, so the main thread resumes there instead.
Where Is fork...join_any Used?
fork...join_any is the standard way to implement a timeout in a testbench: fork a thread that waits for a specific event alongside a second thread that's just a fixed #delay, and whichever one finishes first — the event actually occurring, or the timeout elapsing — determines what happens next, without risking the simulation hanging forever if the event never comes.
Common Beginner Mistakes
Mistake #1: Assuming join_any waits for every thread, like plain join
join_any only waits for the first thread to finish, not all of them. Why? If you actually need to wait for every spawned thread, that's plain fork...join — using join_any when you meant join means your code continues far earlier than intended, while the "unfinished" threads are still quietly running.
Mistake #2: Forgetting that the remaining threads keep running
Once join_any lets the parent resume, every thread that hasn't finished yet keeps running independently in the background — nothing stops them automatically. Why? If you specifically need those leftover threads to stop (like the losing side of a timeout race), you have to terminate them explicitly with disable fork, covered later in this chapter.
Mistake #3: Assuming a nested join_any stops its own remaining threads once it's "done"
As the nested example shows, Thread1_1 keeps running and prints at 30ns even though the inner join_any was already considered complete at 20ns. Why? "Done" for a join_any block just means the parent (in this case, the outer fork) is allowed to stop waiting on it — it doesn't terminate whatever's still running inside that block.
Practice Exercise
Challenge: Build a Timeout Pattern
Write a fork...join_any that races a wait (done) statement against a #100 timeout, printing "Event occurred" if done goes high first, or "Timed out" if the 100ns delay elapses first.
Hint
Fork two threads: one that simply does wait (done); then displays a message, and one that does #100; then displays a different message. join_any resumes as soon as whichever one finishes first.
Solution
module tb;
bit done;
initial begin
fork
begin
wait (done);
$display ("[%0t] Event occurred", $time);
end
begin
#100;
$display ("[%0t] Timed out", $time);
end
join_any
end
endmodule
If done goes high before 100ns elapses, the first branch finishes first, join_any resumes right there, and "Event occurred" prints. If 100ns passes with done still low, the second branch finishes first instead, and "Timed out" prints — either way, the simulation never hangs waiting indefinitely for done.
Summary
In this tutorial, you learned:
- Plain
fork...joincan hang a simulation if any spawned thread never finishes;join_anyavoids that by only waiting for the first thread to complete - The main thread resumes as soon as the fastest of the forked threads finishes, not all of them
- Threads that haven't finished yet keep running in the background after the parent resumes — nothing stops them automatically
- A nested
fork...join_anyis itself treated as done once any one of its own threads finishes, which is what the outerjoin_anythen compares against its other threads fork...join_anyis the standard building block for a timeout: racing awaitagainst a fixed delay
Continuing the Threads & Processes Chapter
Continue with SystemVerilog disable fork join to terminate leftover background threads, and SystemVerilog wait fork to explicitly wait for them instead.
Quiz
No quiz questions available for this article.