TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

Synthesizable Verilog from Behavioral Constructs, Part 1: Wait

The Editor's Desk
by
0
Share Facebook X LinkedIn WhatsApp Email

Modifying a behavioral Verilog wait statement to make it synthesizable.

A wait statement is easy to write in a testbench or a behavioral model, but RTL synthesis tools do not turn it into hardware. The fix is to replace the wait with a small state machine that checks the condition on every clock edge. This post shows the conversion, explains why it works, and then goes through a complete, clocked example with the reset, start and done handling the original checklist calls for.

The conversion

Behavioral:

command1;
wait (x != 0);
command3;

Synthesizable:

case (state)
0 : begin
      command1;
      if (x != 0) command3;
      else state <= 1;
    end

1 : if (x != 0) // wait until this is true
      command3;
endcase

You also need to add the variable state, reg state. If a cycle delay between command1 and command3 does not matter, then the following is simpler, but not identical to the original:

case (state)
0 : begin
      command1;
      state <= 1;
    end

1 : if (x != 0) // wait until this is true
      command3;
endcase

The latter approach is preferred in many cases for coding simplicity.

Note: in general commandi refers to a block of commands. It is assumed there is an appropriate clock for the case statement state machines.

Why wait does not synthesize

In simulation, wait (x != 0) suspends the process until the expression becomes true, however long that takes. If it is already true, execution continues at once. Hardware has no way to "suspend" a block of logic. What it can do is remember where it is (a state register) and look at the condition again on the next clock edge. The state machine above does exactly that: state 0 is "before the wait", state 1 is "waiting".

That is also why the first version checks x in state 0. If x is already non-zero, the behavioral code runs command3 straight away, without waiting; the first synthesizable version matches that. The second version always spends at least one clock in state 1, so command3 happens one cycle later. Whether that matters depends on the protocol around it.

Checklist for any state machine like this

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.

Deep dive: the numbers and details

This part holds a complete clocked module, its cycle-by-cycle timing and the common mistakes. Skip it if the overview is all you need.

A complete module

The fragments above leave out the clock, reset and what happens after command3. Here is a complete version with an idle state, a start input and a one-cycle done pulse. command1 and command3 are shown as register updates so that the timing is visible.

module wait_fsm (
  input  wire       clk,
  input  wire       rst_n,
  input  wire       start,
  input  wire [7:0] x,
  output reg        done,
  output reg  [7:0] a,      // written by command1
  output reg  [7:0] b       // written by command3
);
  localparam IDLE = 2'd0, WAIT_X = 2'd1;
  reg [1:0] state;

  always @(posedge clk or negedge rst_n) begin
    if (!rst_n) begin
      state <= IDLE;
      done  <= 1'b0;
      a     <= 8'd0;
      b     <= 8'd0;
    end else begin
      done <= 1'b0;                 // default: done is a pulse
      case (state)
        IDLE: if (start) begin
                a <= a + 8'd1;      // command1
                if (x != 0) begin
                  b    <= x;        // command3
                  done <= 1'b1;
                end else
                  state <= WAIT_X;
              end
        WAIT_X: if (x != 0) begin   // wait until this is true
                  b     <= x;       // command3
                  done  <= 1'b1;
                  state <= IDLE;
                end
        default: state <= IDLE;     // recover from illegal states
      endcase
    end
  end
endmodule

Points to note: the state register is 2 bits although only two states are used, which leaves room for more states without the width trap in the checklist; the default branch returns any illegal state to IDLE; every output has a reset value; and done defaults to 0 at the top of the clocked block so it can only be high for one cycle.

Cycle-by-cycle timing

Assume start is high in cycle 0, and x becomes non-zero in cycle 3.

Clock edgeState beforexActionState after
0IDLE0command1, x is zero so go to WAIT_XWAIT_X
1WAIT_X0waitWAIT_X
2WAIT_X0waitWAIT_X
3WAIT_Xnon-zerocommand3, done pulseIDLE

If x had been non-zero in cycle 0, command1 and command3 would both happen on edge 0, matching the behavioral code. With the simpler second version, command3 would happen on edge 1 at the earliest: one cycle of extra latency.

The latency trade-off in numbers

At a 200 MHz clock (5 ns period), the simpler version costs 5 ns per use. In a loop that runs this sequence a million times per second, that is 5 ms of extra time per second, or 0.5 percent of throughput. In a control path that runs once at start-up it is irrelevant. That is why the original note says the simpler form is preferred in many cases.

Common mistakes

  • Asynchronous input: if x comes from another clock domain, synchronize it first. Testing a multi-bit asynchronous bus with x != 0 can see a partly changed value.
  • Combinational loops: writing the wait as a while loop in an always @* block does not create a state machine; tools either reject it or build something unintended.
  • Blocking assignments in the clocked block: use non-blocking (<=) for registers so that the simulation matches the hardware.
  • Missing exit: forgetting to return to IDLE after command3, so the machine sits in WAIT_X and repeats command3 every time x is non-zero.

Interview angle

A common interview question is "How do you synthesize a wait or a delay?" A good answer says that time in hardware is counted in clock cycles, so a wait on a condition becomes a state that loops until the condition is true, and a fixed delay (for example #100) becomes a counter state that loops for N cycles, where N is the delay divided by the clock period.

The Editor's Desk
About the Author
The Editor's Desk
Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!