SystemVerilog supports parallel, concurrent threads through the fork...join construct. Multiple procedural blocks can be spawned off at the same time using fork and join, and different variations of the construct (covered later in this chapter) let the parent thread continue at different points depending on when the spawned threads finish. This article covers the plain fork...join form, which waits for every spawned thread to complete.
7 min read | Intermediate Level
What You'll Learn
- How long the parent thread stays suspended at a
fork...join - The basic
fork...joinsyntax, and what counts as a single spawned thread - How nested
fork...joinblocks resolve their timing - Why a task called concurrently from multiple threads needs to be declared
automatic
Syntax
fork
// Thread 1
// Thread 2
// ...
// Thread 3
join
A Basic fork...join Example
In the example below, three threads are forked using fork...join. The main thread stays suspended until every thread spawned by the fork completes. Any block of code within begin and end counts as a single thread — here, that's Thread2:
module tb;
initial begin
$display ("[%0t] Main Thread: Fork join going to start", $time);
fork
// Thread 1
#30 $display ("[%0t] Thread1 finished", $time);
// Thread 2
begin
#5 $display ("[%0t] Thread2 ...", $time);
#10 $display ("[%0t] Thread2 finished", $time);
end
// Thread 3
#20 $display ("[%0t] Thread3 finished", $time);
join
$display ("[%0t] Main Thread: Fork join has finished", $time);
end
endmodule
Output
ncsim> run [0] Main Thread: Fork join going to start [5] Thread2 ... [15] Thread2 finished [20] Thread3 finished [30] Thread1 finished [30] Main Thread: Fork join has finished ncsim: *W,RNQUIE: Simulation is complete.
All three threads are forked at time 0ns. Thread2 is a block of procedural code that finishes only after executing every statement inside its begin/end, so it completes at 5ns + 10ns = 15ns — the first thread to finish. Thread3 finishes at 20ns, and Thread1 takes the longest, finishing at 30ns. Since plain fork...join waits for every thread, the main thread doesn't resume until 30ns — whichever thread finishes last.
Quick Check
Q: If Thread1's delay above were changed from #30 to #8, at what time would the main thread resume after the join?
Show Answer
20ns. fork...join always waits for the slowest thread to finish — with Thread1 now finishing at 8ns, Thread2 at 15ns, and Thread3 at 20ns, the latest of the three is Thread3 at 20ns.
Where Is fork...join Used?
fork...join is a natural fit for synchronization points in a testbench — spinning up several independent stimulus generators or agents in parallel, then waiting for every one of them to finish before moving on to checking results or starting the next test phase.
Nested fork...join
A fork...join can be nested inside another fork...join.
Example #1
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
print (10, "Thread2");
join
$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 [20] Thread1_0 [30] Thread1_1 [30] Main Thread: Fork join has finished ncsim: *W,RNQUIE: Simulation is complete.
The outer fork...join spawns two things: the inner fork...join block itself (which spawns Thread1_0 and Thread1_1 concurrently, finishing once both complete at 30ns) and Thread2 directly (finishing at 10ns). The outer join treats the entire inner block as one of its own threads, so it waits for the later of the two: the inner block's 30ns, versus Thread2's 10ns — landing on 30ns.
print is called concurrently from three separate threads in this example, so it must be declared automatic. Without it, every concurrent call would share a single copy of _time and t_name, and one call could silently overwrite another's arguments mid-execution — automatic gives each call its own independent copy of the task's local variables and arguments.
Example #2
module tb;
initial begin
$display ("[%0t] Main Thread: Fork join going to start", $time);
fork
fork // Thread 1
#50 $display ("[%0t] Thread1_0 ...", $time);
#70 $display ("[%0t] Thread1_1 ...", $time);
begin
#10 $display ("[%0t] Thread1_2 ...", $time);
#100 $display ("[%0t] Thread1_2 finished", $time);
end
join
// Thread 2
begin
#5 $display ("[%0t] Thread2 ...", $time);
#10 $display ("[%0t] Thread2 finished", $time);
end
// Thread 3
#20 $display ("[%0t] Thread3 finished", $time);
join
$display ("[%0t] Main Thread: Fork join has finished", $time);
end
endmodule
The main thread stays suspended until every nested fork is over, at 110ns:
Output
ncsim> run [0] Main Thread: Fork join going to start [5] Thread2 ... [10] Thread1_2 ... [15] Thread2 finished [20] Thread3 finished [50] Thread1_0 ... [70] Thread1_1 ... [110] Thread1_2 finished [110] Main Thread: Fork join has finished ncsim: *W,RNQUIE: Simulation is complete.
The inner fork (labeled Thread 1) spawns three threads of its own: one finishing at 50ns, one at 70ns, and a begin/end block finishing at 10ns + 100ns = 110ns. The inner join waits for the slowest of those three — 110ns. The outer join then waits for the slowest of its three threads: the inner block (110ns), Thread2 (15ns), and Thread3 (20ns) — landing on 110ns overall.
Common Beginner Mistakes
Mistake #1: Assuming fork...join lets the main thread continue immediately
Plain fork...join does the opposite — it blocks the parent thread until every spawned thread finishes. Why? If you need the parent to continue as soon as just one thread finishes, or not wait at all, those are different constructs (fork...join_any and fork...join_none, covered later in this chapter) — plain join is the one that waits for everything.
Mistake #2: Forgetting automatic on a task called from multiple concurrent threads
As the note above the print task shows, a task called concurrently by more than one thread needs to be declared automatic. Why? Without it, all concurrent calls share one copy of the task's local variables and arguments, and one thread's call can silently corrupt another's in-progress execution — a bug that's easy to miss since it depends on the exact timing of concurrent calls.
Mistake #3: Assuming a nested fork...join finishes independently of the outer one
The outer join treats an entire nested fork...join block as just one of its own threads. Why? The outer thread doesn't resume until the inner block itself has finished, which means the outer join's total wait time is determined by whichever thread — inner or outer — actually finishes last, not by the outer threads alone.
Practice Exercise
Challenge: Predict the Finish Time
A fork...join spawns three threads: one with a #15 delay, one with a #40 delay, and a begin/end block that waits #5 and then #25 more before finishing. At what simulation time does the main thread resume after the join?
Hint
Work out each thread's own finish time first, then take the latest one — fork...join always waits for the slowest thread.
Solution
40ns. The three threads finish at 15ns, 40ns, and 5ns + 25ns = 30ns respectively. The latest of the three is 40ns, so that's when the main thread resumes.
Summary
In this tutorial, you learned:
fork...joinspawns multiple threads concurrently, and the parent thread waits until every one of them finishes- A
begin/endblock inside aforkcounts as a single thread, finishing only once every statement inside it has executed - A
fork...joincan be nested inside another; the outerjointreats the entire inner block as one of its own threads - The total wait time for any
fork...joinis always determined by its slowest thread - A task called concurrently from multiple threads must be declared
automatic, or concurrent calls will share and potentially corrupt the same local variables
Functional coverage is built from two closely related pieces: coverpoint, which does the actual work of tracking what's been exercised, and covergroup, which is the container that holds one or more coverpoints together. This article starts with the coverpoint, since that's the piece that actually answers the "have we tested everything?" question, then shows how covergroups bring several of them together into one coverage model.
8 min read | Intermediate Level
What You'll Learn
- What a
coverpointis and why it's the fundamental unit of functional coverage - How to declare a coverpoint, and how its legal values get grouped into bins
- What a
covergroupis and how it brings multiple coverpoints together - How a
covergroupgets sampled, and how to read its reported coverage percentage
What Is a Coverpoint, and Why Does It Matter?
A coverpoint watches a single integral variable or expression and records which of its legal values have actually occurred during the test. Every time it's sampled, a coverpoint checks the current value of whatever it's watching and marks that value as "hit."
This is the piece that does the real measuring in functional coverage — it's the difference between assuming your random stimulus was thorough and actually knowing it was. Each value (or group of values) a coverpoint tracks lives in a bin. By default, SystemVerilog automatically creates one bin per legal value of the variable being covered — a 2-bit variable gets 4 automatic bins, one per possible value. SystemVerilog Coverpoint Bins covers how to group values into custom bins yourself, which matters once a variable is too wide for one bin per value to be practical.
How a Coverpoint Is Written
A coverpoint can optionally be given a name (a label) before a colon:
[label :] coverpoint <variable_or_expression>;
Parameters:
label— an optional name for the coverpoint, followed by a colonvariable_or_expression— the signal, bit-slice, or expression the coverpoint tracks
A coverpoint never appears on its own, though — it always lives inside a covergroup, which is the container for the overall coverage model.
What Is a Covergroup?
A covergroup is a user-defined type that encapsulates a coverage model — it can be defined once and instantiated multiple times through its new() function, just like a class. A covergroup can be defined inside a package, module, program, interface, or class, and typically brings together:
- One or more coverpoints — the individual signals or expressions being tracked
- Cross coverage between coverage points — tracking combinations of values across two or more coverpoints, rather than each one in isolation (covered in its own article later in this chapter)
- An event that defines when the covergroup is sampled — a clock edge or any other SystemVerilog event, deciding how often every coverpoint inside it checks its current value
- Other options to configure the coverage object — things like per-coverpoint weight or a target coverage goal
How a Covergroup Is Written
A minimal covergroup declaration wraps one or more coverpoints between covergroup and endgroup:
covergroup <covergroup_name> [(arguments)] [@ (sampling_event)];
[label :] coverpoint <variable_or_expression>;
...
endgroup
// Instantiate the covergroup, similar to instantiating a class
<covergroup_name> <instance_name> = new();
Parameters:
<covergroup_name>— the name of the covergroup type being defined(arguments)— optional arguments passed in when the covergroup is instantiated (optional)@ (sampling_event)— the event that triggers sampling, such as a clock edge (optional, but almost always used)
A Single Coverpoint in Action
Here's that skeleton filled in with a minimal working example — one covergroup with one coverpoint tracking the variable mode. The group is sampled on every posedge clk, while mode and cfg are assigned fresh random values on every negedge clk — so by the time each sample happens, mode already holds that cycle's random value:
module tb;
// Declare some variables that can be "sampled" in the covergroup
bit [1:0] mode;
bit [2:0] cfg;
// Declare a clock to act as an event that can be used to sample
// coverage points within the covergroup
bit clk;
always #20 clk = ~clk;
// "cg" is a covergroup that is sampled at every posedge clk
covergroup cg @ (posedge clk);
coverpoint mode;
endgroup
// Create an instance of the covergroup
cg cg_inst;
initial begin
// Instantiate the covergroup object similar to a class object
cg_inst= new();
// Stimulus : Simply assign random values to the coverage variables
// so that different values can be sampled by the covergroup object
for (int i = 0; i < 5; i++) begin
@(negedge clk);
mode = $random;
cfg = $random;
$display ("[%0t] mode=0x%0h cfg=0x%0h", $time, mode, cfg);
end
end
// At the end of 500ns, terminate test and print collected coverage
initial begin
#500 $display ("Coverage = %0.2f %%", cg_inst.get_inst_coverage());
$finish;
end
endmodule
Output
ncsim> run [40] mode=0x0 cfg=0x1 [80] mode=0x1 cfg=0x3 [120] mode=0x1 cfg=0x5 [160] mode=0x1 cfg=0x2 [200] mode=0x1 cfg=0x5 Coverage = 50.00 % Simulation complete via $finish(1) at time 500 NS + 0
mode is 2 bits wide, so it has 4 legal values (0 through 3), and by default a coverpoint creates one automatic bin per legal value, as described earlier. Across the 5 samples shown, only 0x0 and 0x1 ever appeared — 2 out of 4 possible values — which is exactly why get_inst_coverage() reports 50.00%.
Quick Check
Q: If a 3-bit coverpoint variable has hit 5 of its 8 possible values by the end of a test, what coverage percentage would you expect?
Show Answer
62.5% — 5 out of 8 possible values, the same "values hit ÷ total legal values" calculation used in the example above.
Multiple Coverpoints in One Covergroup
A covergroup can contain more than one coverpoint, and each one can optionally be given a name followed by a colon. mode can take any value from 0 to 3, and cfg can take any value from 0 to 7 — the question functional coverage answers is: out of all the values each of these could legally take, how many actually showed up during the test?
module tb;
bit [1:0] mode;
bit [2:0] cfg;
bit clk;
always #20 clk = ~clk;
// "cg" is a covergroup that is sampled at every posedge clk
// This covergroup has two coverage points, one to cover "mode"
// and the other to cover "cfg". Mode can take any value from
// 0 -> 3 and cfg can take any value from 0 -> 7
covergroup cg @ (posedge clk);
// Coverpoints can optionally have a name before a colon ":"
cp_mode : coverpoint mode;
cp_cfg_10 : coverpoint cfg[1:0];
cp_cfg_lsb : coverpoint cfg[0];
cp_sum : coverpoint (mode + cfg);
endgroup
cg cg_inst;
initial begin
cg_inst= new();
for (int i = 0; i < 5; i++) begin
@(negedge clk);
mode = $random;
cfg = $random;
$display ("[%0t] mode=0x%0h cfg=0x%0h", $time, mode, cfg);
end
end
initial begin
#500 $display ("Coverage = %0.2f %%", cg_inst.get_inst_coverage());
$finish;
end
endmodule
Output
ncsim> run [40] mode=0x0 cfg=0x1 [80] mode=0x1 cfg=0x3 [120] mode=0x1 cfg=0x5 [160] mode=0x1 cfg=0x2 [200] mode=0x1 cfg=0x5 Coverage = 68.75 % Simulation complete via $finish(1) at time 500 NS + 0
Note that a coverpoint doesn't have to track a plain variable — cp_cfg_10 covers a bit-slice (cfg[1:0]), cp_cfg_lsb covers a single bit (cfg[0]), and cp_sum covers an entire expression (mode + cfg). get_inst_coverage() reports the overall coverage across all four coverpoints combined, which is why the second run's 68.75% differs from the single-coverpoint example's 50.00% — it's now an aggregate across every coverpoint in the group.
Where Is Functional Coverage Used?
Functional coverage is how verification teams decide when random testing has actually done its job — tracking which opcodes, address ranges, or protocol states have been exercised lets you spot gaps a purely random test plan might otherwise miss for a long time, and coverage reports are commonly part of the sign-off criteria before a design is considered adequately verified.
Common Beginner Mistakes
Mistake #1: Forgetting the sampling event
A covergroup only updates when its sampling event occurs — @ (posedge clk) in the examples above. Why? If that event never fires (a clock that's never toggled, or an event that's misspelled), the covergroup silently reports 0% coverage forever, with no error to warn you something's wrong.
Mistake #2: Assuming high coverage means the design is bug-free
Coverage only measures which values or scenarios your stimulus produced — it says nothing about whether the design responded correctly to them. Why? A test can reach 100% functional coverage on a coverpoint while a completely broken scoreboard or missing checker lets every incorrect result slip through unnoticed.
Mistake #3: Expecting automatic bins to scale to wide variables
By default, a coverpoint creates one bin per legal value, which works fine for a 2-bit or 3-bit variable like the examples above. Why? For something wider, like a 32-bit address, that default would try to create billions of bins — you need to explicitly group values into ranges, which is exactly what SystemVerilog Coverpoint Bins covers next.
Practice Exercise
Challenge: Add a Coverpoint for cfg
Starting from the single-coverpoint example, add a second coverpoint that tracks cfg directly (not a bit-slice). Given the sampled values in the output log (cfg = 0x1, 0x3, 0x5, 0x2, 0x5), how many of the 8 possible values were hit, and what coverage percentage would that coverpoint alone report?
Hint
List out the distinct cfg values from the log first, then divide the count of distinct values by 8.
Solution
covergroup cg @ (posedge clk);
coverpoint mode;
coverpoint cfg;
endgroup
The sampled cfg values are 0x1, 0x3, 0x5, 0x2, and 0x5 — four distinct values (0x1, 0x2, 0x3, 0x5) out of 8 possible. That coverpoint alone would report 4/8 = 50.00% coverage.
Summary
In this tutorial, you learned:
- A
coverpointis the piece that actually measures coverage — it tracks one variable or expression and records which legal values have occurred - Each value (or group of values) a coverpoint tracks lives in a bin; by default, one automatic bin is created per legal value
- A
covergroupis the container that groups one or more coverpoints together, along with a sampling event, optional cross coverage, and configuration options - A
covergroupis sampled whenever its declared event occurs; if that event never fires, coverage stays at 0% get_inst_coverage()reports the aggregate coverage across every coverpoint in the group- High coverage confirms stimulus diversity, not correctness — it's not a substitute for checking results
Continuing the Functional Coverage Chapter
Continue with SystemVerilog Functional Coverage for a broader look at the coverage flow, Coverpoint Bins for grouping wide ranges of values together, and Cross Coverage for tracking combinations between multiple coverpoints.
Quiz
No quiz questions available for this article.