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

Verilog Awareness: Interview Questions on Delays and FSM Styles

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

Here is a set of Verilog interview questions on delays, state machine styles and a few tricky pieces of code. Try them on your own first. The answers follow, first in plain words and then in detail with the timing worked out step by step.

The questions

  1. Differentiate between intra-assignment delay and inertial delay.
  2. What are the different state machine styles? Which is better? Why and when do we use one over the other? Explain the advantages and disadvantages.
  3. What is the difference between the following lines of code?
    reg1 <= #10 reg2;
    reg3 = #10 reg4;
  4. What is the value of Var1 after the following assignment?
    reg Var1;
    initial begin
      Var1 <= "-";
    end
  5. What is the output of the code below?
    module quest_for_out();
    
      integer i;
      reg clk;
    
      initial begin
        clk = 0;
        #4 $finish;
      end
    
      always #1 clk = !clk;
    
      always @ (posedge clk)
      begin : FOR_OUT
        for (i=0; i < 8; i = i + 1) begin
          if (i == 5) begin
            disable FOR_OUT;
          end
          $display ( "Current i : %g" ,i);
        end
      end
    endmodule
  6. What is the output of the code below?
    module quest_for_in();
    
      integer i;
      reg clk;
    
      initial begin
        clk = 0;
        #4 $finish;
      end
    
      always #1 clk = !clk;
    
      always @ (posedge clk)
      begin
        for (i=0; i < 8; i = i + 1) begin : FOR_IN
          if (i == 5) begin
            disable FOR_IN;
          end
          $display ( "Current i : %g" ,i);
        end
      end
    endmodule

The short answers

  • Delays: an intra-assignment delay reads the right-hand side now and writes the result later. An inertial delay swallows any pulse shorter than the delay, the way a real gate does. They answer different questions: when is the value sampled, and do short pulses survive.
  • State machines: the two machine types are Moore (outputs depend only on the state) and Mealy (outputs also depend on the inputs). The coding styles are one, two or three always blocks. A two-block style with registered state and a separate next-state block is the common safe choice; registered outputs are better when glitch-free outputs matter.
  • reg1 and reg3: both sample the right-hand side at once and assign 10 time units later. The nonblocking line lets the block carry on immediately; the blocking line stops the block for 10 units.
  • Var1: it ends up as 1, because the one-bit register keeps only the lowest bit of the ASCII code for "-".
  • quest_for_out: prints i from 0 to 4, twice. Disabling the outer block ends the whole loop, like a break.
  • quest_for_in: prints 0, 1, 2, 3, 4, 6, 7, twice. Disabling the block inside the loop skips only the rest of that iteration, like a continue.

Why interviewers ask these

These questions test whether you understand how a Verilog simulator schedules events, not just the syntax. Getting delays and blocking versus nonblocking assignments wrong produces testbenches that pass in one simulator and fail in another, and RTL that simulates differently from the gates synthesis builds. The disable questions check that you can read control flow that has no break or continue keyword, since Verilog-2001 has neither.

Deep dive: the numbers and details

This part holds the timing, the event scheduling and the step-by-step answers. Skip it if the short answers are all you need.

1. Intra-assignment delay versus inertial delay

An intra-assignment delay sits on the right of the assignment operator, as in a = #5 b; or a <= #5 b;. The simulator evaluates b immediately and schedules the update of a 5 time units later. Compare an inter-assignment (regular) delay, #5 a = b;, which waits 5 units first and only then reads b.

Inertial delay describes how a delay treats short pulses. A continuous assignment such as assign #5 y = a & b; is inertial: if the result changes and changes back within 5 units, the output never moves. Transport delay keeps every transition and simply shifts it in time. The UMBC lecture on Verilog events, timing and testbenches (sections 17 to 26) shows both, and notes that a nonblocking assignment with an intra-assignment delay, y <= #D a & b;, models transport delay.

FormWhen the right-hand side is readShort pulses
#5 a = b;After the 5 unit waitMissed if they happen during the wait
a = #5 b;ImmediatelyMissed: the block is stuck for 5 units and cannot see new changes
a <= #5 b;ImmediatelyKept (transport behavior)
assign #5 a = b;On every changeFiltered (inertial behavior)

Delays are for simulation only. Synthesis tools ignore them, so a delay in RTL never creates a real timing guarantee.

3. reg1 <= #10 reg2 versus reg3 = #10 reg4

Suppose both lines run at time 20, and the registers hold reg2 = 1 and reg4 = 1 at that moment.

  • reg1 <= #10 reg2; reads reg2 (1) at time 20, schedules reg1 = 1 for time 30 in the nonblocking update region, and the procedural block continues with its next statement at time 20.
  • reg3 = #10 reg4; reads reg4 (1) at time 20, then the block waits. At time 30, reg3 becomes 1 and only then does the next statement run.

If reg2 changes at time 25, reg1 still gets the old value at time 30, because the value was sampled at time 20. The practical rule from Clifford Cummings' paper "Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!" (SNUG 2000) is to use nonblocking assignments for sequential logic and blocking assignments for combinational logic. (The original post wrote the second line as reg 4 with a space, which would not compile; it is shown here as reg4.)

4. The value of Var1

The string literal "-" is one 8-bit ASCII character, hex 2D, which is binary 0010_1101. Assigning it to a one-bit reg truncates to the least significant bit, which is 1. The update is nonblocking, so Var1 is still X during the rest of time step 0 and becomes 1 in the nonblocking update region of that same time step. Anything that reads Var1 after time 0 sees 1. Many lint tools warn about the width mismatch, which is the real lesson: size your literals.

5. quest_for_out: disabling the outer block

The clock starts at 0 and toggles every time unit, so it rises at time 1 and time 3 and falls at times 2 and 4. $finish runs at time 4, so there are exactly two rising edges.

On each edge the loop prints i = 0 to 4. When i reaches 5, disable FOR_OUT stops the named block that contains the whole loop, so control jumps to the end of the always block and nothing more is printed for that edge. The output is:

Current i : 0
Current i : 1
Current i : 2
Current i : 3
Current i : 4
Current i : 0
Current i : 1
Current i : 2
Current i : 3
Current i : 4

The %g format prints the integer as a real number in its shortest form, so the values appear as 0, 1, 2 and so on.

6. quest_for_in: disabling the inner block

Here the named block FOR_IN is the body of the loop. Disabling it ends the current iteration only. The loop then runs its increment, i = i + 1, and carries on with i = 6 and i = 7. On each of the two rising edges the output is:

Current i : 0
Current i : 1
Current i : 2
Current i : 3
Current i : 4
Current i : 6
Current i : 7

So a disable of the block around the loop acts like C's break, and a disable of the block inside the loop acts like continue. SystemVerilog later added break and continue keywords, but this idiom still appears in older code.

2. State machine styles in detail

There are two separate questions hidden in "state machine styles": the machine type and the coding style.

Machine typeOutputs depend onStrengthWeakness
MooreCurrent state onlyOutputs change only on clock edges; easy to timeOften needs more states; outputs react one cycle later
MealyCurrent state and inputsFewer states; outputs react in the same cycleInput glitches pass to outputs; longer combinational paths across modules

For coding, Cummings' paper "State Machine Coding Styles for Synthesis" (SNUG 1998) compares one-always-block and two-always-block coding. A three-block variant is also widely used:

  • One always block: state and outputs in a single clocked block. Compact, but every output becomes a register whether you want it or not, and the next-state logic is harder to read.
  • Two always blocks: a clocked block that only updates the state register, and a combinational block that computes the next state and outputs. This is the style most teams treat as the default, because it maps cleanly onto the hardware and is easy to review.
  • Three always blocks: the two-block style plus a separate clocked block for registered outputs. Useful when outputs must be glitch free, for example when they drive another clock domain or a chip pin.

State encoding is a related choice: binary encoding uses the fewest flip-flops, one-hot uses one flip-flop per state and usually gives simpler, faster next-state logic, which suits FPGAs with plenty of registers. Gray encoding changes one bit per transition, which helps when the state is read across a clock boundary.

Common mistakes in answers

  • Saying a nonblocking assignment with a delay "waits" before reading the right-hand side. It does not; it reads now and writes later.
  • Forgetting a default assignment in the combinational next-state block, which infers latches.
  • Confusing disable on the loop's own block with disable on the enclosing block.
  • Counting the clock edges wrong: always #1 clk = !clk; gives a 2 unit period, so only two rising edges fit before time 4.
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!