When implementing Verilog tasks in modules, the best approach is to group tasks that have the same output signals into separate modules. If different modules control the same signal, then explicit arbitration logic is required to specify which module is controlling the signal at a given time. To put tasks in a separate module, you will require start and completion handshaking signals.
The problem in plain words
In a testbench or behavioral model, a Verilog task works like a subroutine: you call it, it runs, and it can even wait for something to happen before it returns. Hardware has no subroutines and no "wait here". Everything runs at once, clock edge by clock edge. So a task that waits (for example, until an input becomes non-zero) cannot be turned into gates as written. A synthesis tool can handle a task only if it contains synthesizable statements and is called from synthesizable code; waits and delays are not on that list, as the Utah State University course notes on tasks explain.
The fix is to turn the waiting task into a small state machine in its own module, and to have the caller talk to it with two signals: a start pulse ("please do the update") and a done pulse ("finished, results are ready"). The caller's own state machine sits in a waiting state until done arrives.
When to use this pattern
- When behavioral code has a task that waits for an event or spans several clock cycles.
- When several parts of a design need the same multi-cycle operation and you want one copy of the hardware.
- When you are moving a model written for simulation toward RTL that a synthesis tool will accept.
If the task is purely combinational (no waits, no clock edges inside), you can usually keep it as a task or a function, and synthesis will inline it.
The key design rule
Group tasks by the signals they drive. If two modules can both write the same register, you need arbitration logic to decide who owns it at each moment. Keeping each output owned by exactly one module avoids that, and it matches a common coding guideline: do not assign the same variable from more than one always block.
Deep dive: the numbers and details
This part holds the original behavioral code, two synthesizable translations, a cycle-by-cycle timing walk-through and a checklist. Skip it if the overview is all you need.
The behavioral starting point
task update_a_and_b; begin a = y; wait (z != 0) b = z; end endtask ... y = x + w; update_a_and_b; // calls the task x = a + b; @(posedge clock); x = b; if (y == x) update_a_and_b; @(posedge clock); command1;
Care must be taken when translating this into synthesizable Verilog, to preserve correct timing. In the behavioral code, a is updated at once, b and then x are updated the moment z becomes non-zero, and nothing after the call runs until then.
Translation 1: preserving the original timing
This version updates a, b and x immediately when z is already non-zero, and only hands off to the helper module when it must wait.
module update_a_and_b(do_update_a_and_b, clock, reset, a, z, done_update_a_and_b, b_temp, x_temp); input do_update_a_and_b; // this signal should go high for only one clock cycle input clock; input reset; // reset this module when reset goes high input [7:0] a; input [7:0] z; output done_update_a_and_b; output [7:0] b_temp; output [7:0] x_temp; reg done_update_a_and_b; reg [7:0] b_temp; reg [7:0] x_temp; reg state; always @(posedge clock or posedge reset) if (reset) begin state <= 0; done_update_a_and_b = 0; b_temp = 0; x_temp = 0; end else if (do_update_a_and_b && (state == 0)) begin done_update_a_and_b = 0; if (z != 0) begin b_temp = z; x_temp = a+b_temp; // x value is update inside this module, as it must occur immediately when z != 0 done_update_a_and_b = 1; // stay in state 0, we've finished end else state <= 1; end else begin case (state) 0 : done_update_a_and_b = 0; // do nothing, this is the idle state 1 : if (z != 0) begin b_temp = z; x_temp = a+b_temp; // x value is update inside this module, as it must occur immediately when z != 0 done_update_a_and_b = 1; state <= 0; // stay in state 0, we've finished end endcase end endmodule ... reg [2:0] top_state; ... case (top_state) 0 : begin y = x + w; a = y; // this must happen immediately if (z != 0) // we have to update x and b immediately if z != 0 begin b = z; x = a + b; top_state <= 2; end else begin do_update_a_and_b = 1; // call the task top_state <= 1; end end 1 : begin do_update_a_and_b = 0; // stays high for only one cycle if (done_update_a_and_b) begin // we assume the values of x and b weren't needed on the previous cycle, otherwise additional circuitry is needed // or x_temp and b_temp values need to be used on that cycle - it's very difficult to coordinate this correctly // in the general case x = x_temp; b = b_temp; top_state <= 2; end end 2 : begin x = b; if (y == x) begin a = y; // this must happen immediately if (z != 0) // we have to update x and b immediately if z != 0 begin b = z; x = a + b; top_state <= 4; end else begin do_update_a_and_b = 1; // call the task top_state <= 3; end end else top_state <= 4; end 3 : begin do_update_a_and_b = 0; // stays high for only one cycle if (done_update_a_and_b) begin // we assume the values of x and b weren't needed on the previous cycle, otherwise additional circuitry is needed // or x_temp and b_temp values need to be used on that cycle - it's very difficult to coordinate this correctly // in the general case x = x_temp; b = b_temp; top_state <= 4; end end 4: command1; endcase
Translation 2: simpler, with extra cycles
Now if we didn't care about having a couple of additional cycle delays between updates (that is, assuming nothing depends on the variable values immediately, and nothing else is changing variable values), we could implement this in a far simpler fashion:
module update_a_and_b(do_update_a_and_b, clock, reset, a, z, done_update_a_and_b, b_temp, x_temp); input do_update_a_and_b; // this signal should go high for only one clock cycle input clock; input reset; // reset this module when reset goes high input [7:0] a; input [7:0] z; output done_update_a_and_b; output [7:0] b_temp; output [7:0] x_temp; reg done_update_a_and_b; reg [7:0] b_temp; reg [7:0] x_temp; reg state; always @(posedge clock or posedge reset) if (reset) begin state <= 0; done_update_a_and_b = 0; b_temp = 0; x_temp = 0; end else if (do_update_a_and_b && (state == 0)) begin state <= 1; done_update_a_and_b = 0; end else begin case (state) 0 : done_update_a_and_b = 0; // do nothing, this is the idle state 1 : if (z != 0) begin b_temp = z; x_temp = a+b_temp; // x value is update inside this module, as it must occur immediately when z != 0 done_update_a_and_b = 1; state <= 0; // stay in state 0, we've finished end endcase end endmodule ... reg [2:0] top_state; ... case (top_state) 0 : begin y = x + w; do_update_a_and_b = 1; // call the task top_state <= 1; end 1 : begin do_update_a_and_b = 0; // stays high for only one cycle if (done_update_a_and_b) begin x = x_temp; b = b_temp; top_state <= 2; end end 2 : begin x = b; if (y == x) begin do_update_a_and_b = 1; // call the task top_state <= 3; end else top_state <= 4; end 3 : begin do_update_a_and_b = 0; // stays high for only one cycle if (done_update_a_and_b) begin x = x_temp; b = b_temp; top_state <= 4; end end 4: command1; endcase
Note: in general command1 refers to a block of commands. It is assumed there is an appropriate clock for the case statement state machines.
Two corrections were made to the code as first published: the always blocks were sensitive to refclk, which is not a port of the module, so they now use clock; and the second module mixed the names done_update_a_and_b and done_update_and_b, which would not compile, so one name is used throughout.
Cycle-by-cycle cost of the simpler version
Assume z is already non-zero when the caller starts. Count rising clock edges from the edge at which the caller is in top_state 0:
| Edge | Caller | Helper module |
|---|---|---|
| 1 | State 0: computes y, raises start, moves to state 1 | Idle, start not yet seen |
| 2 | State 1: lowers start, waits | Sees start, moves to state 1 |
| 3 | State 1: still waiting | z is non-zero: writes b_temp, x_temp, raises done |
| 4 | State 1: sees done, copies results, moves to state 2 | Back in idle |
So the simpler version costs about three extra cycles per call compared with the behavioral code, where the update happens with no clock edge at all when z is already non-zero. Translation 1 avoids that cost in the common case by doing the update inline, at the price of duplicating logic in the caller. That is the real trade-off: latency against area and complexity.
A cleaner modern version of the helper
The original code mixes blocking (=) and nonblocking (<=) assignments in one clocked always block. Clifford Cummings' widely used SNUG 2000 paper, Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill, gives the guidelines: use nonblocking assignments for sequential logic, and do not mix blocking and nonblocking assignments in the same always block. A version written that way:
module update_a_and_b (
input wire clock,
input wire reset,
input wire start, // one-cycle request pulse
input wire [7:0] a,
input wire [7:0] z,
output reg done, // one-cycle completion pulse
output reg [7:0] b_out,
output reg [7:0] x_out
);
localparam IDLE = 1'b0, WAIT_Z = 1'b1;
reg state;
always @(posedge clock or posedge reset) begin
if (reset) begin
state <= IDLE;
done <= 1'b0;
b_out <= 8'd0;
x_out <= 8'd0;
end else begin
done <= 1'b0; // default: done is a pulse
case (state)
IDLE: if (start) state <= WAIT_Z;
WAIT_Z: if (z != 8'd0) begin
b_out <= z;
x_out <= a + z; // equals a + b once b = z
done <= 1'b1;
state <= IDLE;
end
endcase
end
end
endmodule
Because nonblocking assignments read the old values, x_out uses a + z directly rather than a + b_out, which would add the previous value of b_out. Note also that the 8-bit sum wraps on overflow, exactly as the original 8-bit registers do.
Checklist for every handshaking state machine
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 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 outputs from it to appropriate idling 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. For example, a 2-bit register given the value 4 holds 0.
Common mistakes
- A start signal held for more than one cycle. The helper may run twice. Either make start a one-cycle pulse, as here, or have the helper accept a new request only in its idle state.
- Using results on the cycle done rises. The caller sees done one edge after the helper raises it; plan for that edge in the caller's state machine.
- Two modules driving one register. This is the arbitration problem from the start of the post. Give each register a single owner.
- Undeclared or misspelled signals. Older Verilog creates an implicit one-bit wire for an undeclared name, which can hide a typo; adding
`default_nettype noneat the top of each file turns that into an error.
Further reading
- Clifford E. Cummings, Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill (SNUG 2000), copy on an MIT course site
- Utah State University ECE 3700, handshaking in digital design
- Nandland, Task: Verilog example


Your comments will be moderated before it appears here.