Verilog's fork and join runs several blocks of statements in parallel and waits for all of them to finish. It is a simulation construct: synthesis tools do not accept it. This post shows how to modify a behavioral Verilog fork and join statement to make it synthesizable, by turning it into a small state machine with "done" flags.
What fork and join does
Statements between fork and join start at the same simulation time and run independently. The statement after join waits until every one of them has finished. In a testbench this is the natural way to say "drive these two interfaces at once, then check the result".
Hardware has no notion of "start these, then wait for all of them". It has registers that update on clock edges and logic between them. So to synthesize the same behavior, you make the waiting explicit: one flag per parallel branch remembers whether that branch has finished, and a state machine moves on only when all the flags are set.
The behavioral code
command1;
fork
// start of fork block 1
begin
wait (y != 0);
a = y;
end
// start of fork block 2
begin
wait (z != 0);
b = z;
end
join
command2;
command2 will execute only after fork blocks 1 and 2 have finished.
The synthesizable version
case (state)
0 : begin
command1;
done_fork_block_1 = 0;
done_fork_block_2 = 0;
if (y != 0)
begin
a = y;
done_fork_block_1 = 1;
end
if (z != 0)
begin
b = z;
done_fork_block_2 = 1;
end
if (done_fork_block_1 & done_fork_block_2) command2;
else state <= 1;
end
1 : begin
if ((y != 0) && !done_fork_block_1)
begin
a = y;
done_fork_block_1 = 1;
end
if ((z != 0) && !done_fork_block_2)
begin
b = z;
done_fork_block_2 = 1;
end
if (done_fork_block_1 & done_fork_block_2) command2;
// else state <= 1;
end
endcase
In some special cases it may not be necessary to have done signals, but in general the blocks of commands being executed in parallel by fork may finish at different times.
A simpler version, if one cycle of delay is acceptable
If a cycle of delay between command1 and the other commands is acceptable, this code is simpler:
case (state)
0 : begin
command1;
done_fork_block_1 = 0;
done_fork_block_2 = 0;
state <= 1;
end
1 : begin
if ((y != 0) && !done_fork_block_1)
begin
a = y;
done_fork_block_1 = 1;
end
if ((z != 0) && !done_fork_block_2)
begin
b = z;
done_fork_block_2 = 1;
end
if (done_fork_block_1 & done_fork_block_2) command2;
// else state <= 1;
end
endcase
As y or z may have different values after a clock cycle passes, care is needed in choosing the simpler alternative, because it does not exactly implement the behavioral code.
Things to check in any such state machine
Note: in general, commandi refers to a block of commands. It is assumed there is an appropriate clock for the case-statement state machines. Care is required in setting appropriate reset states, initialization, and completion of use of a state machine:
- Is there a signal to tell the state machine to begin?
- Does a done signal go high, signaling that the state machine has finished?
- When it is not in operation, does the state machine idle correctly? Does it change signal values shared with other code? Does it set its outputs to appropriate idle values?
- Is the state machine reset to the idle state by a reset signal?
- Ensure that you initialize all registers.
- Ensure that your state register has the correct bit width. If it is too small, assigning a larger state value will just return it to an earlier state.
Deep dive: the numbers and details
This part walks through the generated hardware, a cycle-by-cycle trace, the gaps the code leaves open, and the interview angle. Skip it if the overview is all you need.
What the synthesizable code turns into
| RTL element | Hardware |
|---|---|
state | A state register; one bit is enough for states 0 and 1 |
done_fork_block_1, done_fork_block_2 | One flip-flop each, set when its branch completes and cleared in state 0 |
a = y guarded by y != 0 | A register for a with a load enable driven by a comparator (OR-reduction of y) |
b = z guarded by z != 0 | The same for b |
done_fork_block_1 & done_fork_block_2 | An AND gate that enables command2 and the state change |
Each wait in the behavioral code became an if that is re-tested every clock. The cost of "waiting" in hardware is one flag per branch plus the comparator; for N parallel branches you need N done flags and an N-input AND.
Cycle-by-cycle trace of the first version
Illustrative values: y becomes nonzero in cycle 2 and z in cycle 4.
| Cycle | State | y | z | Action | done1 | done2 |
|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | command1; neither branch can finish; go to state 1 | 0 | 0 |
| 2 | 1 | 5 | 0 | a = 5 | 1 | 0 |
| 3 | 1 | 7 | 0 | nothing; done1 already set, so a keeps 5 | 1 | 0 |
| 4 | 1 | 7 | 3 | b = 3; both done, command2 | 1 | 1 |
Cycle 3 shows why the done flags matter: without them, a would be overwritten with 7, which the behavioral code never does, because each fork branch runs only once.
Gaps to close before using this code
- What happens after command2. In state 1 the code runs command2 but never leaves state 1, so on the next clock command2 would run again. A real design adds a transition back to an idle state, which is the "does it idle correctly" check in the list above.
- Restart from state 0. In state 0, if both branches finish at once, command2 runs but the state stays 0, so command1 would run again on the next clock unless an idle state or a start signal stops it.
- Blocking and nonblocking assignments. The done flags use blocking assignments (
=) so that the test at the end of the same branch sees the new value in the same cycle, whilestateuses a nonblocking assignment (<=). Mixing them in one clocked block works in this pattern, but many coding guidelines prefer computing next-state values in a separate combinational block. - Reset. The done flags,
a,bandstateall need a defined reset value.
Latency cost of the two versions
| Case | First version | Simpler version |
|---|---|---|
| y and z already nonzero when command1 runs | command2 in the same cycle as command1 | command2 one cycle later |
| y ready after k cycles, z after m cycles | command2 after max(k, m) cycles | command2 after max(k, m, 1) cycles |
| y or z changes between cycle 0 and cycle 1 | Captures the cycle-0 value | Captures the cycle-1 value, which may differ from the behavioral result |
Fork and join in the standards
Parallel blocks are defined in IEEE 1364-2005 (Verilog). IEEE 1800 SystemVerilog keeps fork ... join and adds join_any (continue when any branch finishes) and join_none (continue at once). These are widely used in testbenches and are not synthesizable either; the same done-flag pattern applies, with an OR instead of an AND for join_any.
Interview angle
- "Why is fork and join not synthesizable?" It describes concurrency in simulation time with no clock; hardware needs explicit storage to remember which branch has finished.
- "How many flip-flops does the conversion add?" One per parallel branch for the done flags, plus the state bits.
- "When is the one-cycle-late version wrong?" When the inputs it samples can change in that cycle.
Further reading
- Synthesizable Verilog from Behavioral Constructs, Part 1: Wait (this blog)
- Synthesizable Verilog from Behavioral Constructs, Part 4: Tasks (this blog)
- Synthesizable Verilog from Behavioral Constructs, Part 5: Delay Statements (this blog)
- Clifford E. Cummings, "Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!", SNUG 2000 (Internet Archive copy)


Your comments will be moderated before it appears here.