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 Muxes, Assignments and Coding

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

These Verilog awareness questions, first posted here in December 2005, are the kind an interviewer uses to check whether you understand how Verilog simulates and how it turns into hardware. They cover multiplexers with unknown selects, blocking and nonblocking assignments, data types, coding styles, operators, delays and simulator types. The full list is kept below, followed by short model answers and worked code.

The questions

  • Consider a 2:1 mux, what will be the output F if the Select (sel) is "X"?
  • What is the difference between blocking and nonblocking assignment? Explain with a simple example.
  • What is the difference between wire and a reg data type?
  • Write code for async reset D-Flip-Flop, Shift Register.
  • Write code for 2:1 MUX using different coding styles.
  • Write code for parallel encoder and priority encoder.
  • Different "case" usage styles! Explain.
  • What is the difference between === and ==?
  • What is defparam used for?
  • What is the difference between unary operator and logical operator?
  • What is the difference between task and function?
  • What is the difference between transport and inertial delays?
  • What is the difference between casex and case statements?
  • What is the difference between $monitor and $display?
  • What is the difference between compiled, interpreted, event based and cycle based simulator?

What the interviewer is looking for

Most of these questions test one idea: Verilog is a simulation language first, and only part of it describes hardware. A good candidate can say what the simulator does with a line of code, what the synthesis tool builds from it, and when those two can disagree. The unknown value X, the two kinds of assignment and the case variants are where simulation and hardware most often part ways, so they come up again and again.

Keep your answers short, give one small example, and say what hardware results. If you mention a rule, give the reason behind it.

Quick answers

  • Mux with sel = X: it depends on how the mux is written. A continuous assignment with the conditional operator gives X only on bits where the two inputs differ; an if-else treats X as false and passes the else input.
  • Blocking versus nonblocking: blocking (=) updates immediately, in order; nonblocking (<=) samples all right-hand sides first and updates at the end of the time step. Use nonblocking for flip-flops.
  • wire versus reg: a wire is a net driven by a continuous assignment or a port; a reg is a variable that keeps the last value assigned in a procedural block. A reg does not always become a register.
  • === versus ==: case equality compares X and Z literally and always returns 0 or 1; logical equality returns X if either side has an X or Z bit.
  • Task versus function: a function runs in zero time and returns one value; a task can contain delays and timing controls and can return values through output arguments.

Deep dive: the numbers and details

This part holds the code, the rules and the reasoning for each question. Skip it if the overview is all you need.

1. A 2:1 mux with an unknown select

assign f1 = sel ? a : b;          // conditional operator

always @(*)
  if (sel) f2 = a;                // if-else
  else     f2 = b;

With sel = X, the conditional operator compares a and b bit by bit: where the bits match, the result is that value; where they differ, the result is X. So with a = 4'b1100 and b = 4'b1010, f1 = 4'b1xx0. The if-else version treats an X condition as false and sets f2 = b = 4'b1010. Real hardware does neither: the output is whichever value the gates settle to. This is a classic source of simulation and synthesis mismatch, because the if-else model can hide an X that real silicon would turn into a wrong value.

2. Blocking and nonblocking assignments

// Nonblocking: a two-stage shift register
always @(posedge clk) begin
  q1 <= d;
  q2 <= q1;     // takes the OLD q1
end

// Blocking: q2 gets d in the same edge, one flop is lost
always @(posedge clk) begin
  q1 = d;
  q2 = q1;      // takes the NEW q1
end

Nonblocking assignments evaluate every right-hand side at the clock edge and update the left-hand sides later in the same time step, so the order of the statements does not matter. Clifford Cummings' SNUG 2000 paper, Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!, sets out eight guidelines. The first five are the ones to quote in an interview: use nonblocking for sequential logic and latches, use blocking for combinational logic in an always block, use nonblocking when one always block mixes both, and do not mix blocking and nonblocking assignments in the same always block. See also Verilog Blocking vs Non-Blocking Assignments: Myths and Facts on this blog.

3. wire and reg

Propertywirereg
Assigned byassign, port connection, gate outputinitial or always block
Holds a valueNo, follows its driverYes, until the next assignment
Default valueZ (undriven)X
Multiple driversAllowed, resolvedLast assignment wins
HardwareWireFlip-flop, latch or plain logic, depending on the block

4. Asynchronous reset flip-flop and shift register

module dff_ar (input clk, input rst_n, input d, output reg q);
  always @(posedge clk or negedge rst_n)
    if (!rst_n) q <= 1'b0;
    else        q <= d;
endmodule

module shift8 (input clk, input rst_n, input din, output reg [7:0] q);
  always @(posedge clk or negedge rst_n)
    if (!rst_n) q <= 8'h00;
    else        q <= {q[6:0], din};
endmodule

The reset must appear in the sensitivity list for it to be asynchronous, and the first if must test it. A full shift register with a self-checking test bench is in Verilog Shift Register with Test Bench.

5. Three ways to write a 2:1 mux

assign y = sel ? b : a;                               // dataflow

always @(*) case (sel) 1'b0: y_r = a; default: y_r = b; endcase   // behavioral

wire nsel, t0, t1;                                    // gate level
not g0 (nsel, sel);
and g1 (t0, a, nsel);
and g2 (t1, b, sel);
or  g3 (y_g, t0, t1);

A related 3:1 example is in Verilog Question: 3:1 Multiplexer in Dataflow and Other Styles.

6. Parallel (one-hot) encoder and priority encoder

// Parallel encoder: assumes exactly one input bit is high
always @(*)
  case (req)
    4'b0001: idx = 2'd0;
    4'b0010: idx = 2'd1;
    4'b0100: idx = 2'd2;
    4'b1000: idx = 2'd3;
    default: idx = 2'bxx;
  endcase

// Priority encoder: the highest set bit wins
always @(*)
  if      (req[3]) pidx = 2'd3;
  else if (req[2]) pidx = 2'd2;
  else if (req[1]) pidx = 2'd1;
  else             pidx = 2'd0;

The parallel version builds flat OR logic; the priority version builds a chain, and its delay grows with the number of inputs.

7. case styles, casex and casez

  • case matches 0, 1, X and Z exactly. In simulation an X on the select matches only an X item.
  • casez treats Z (or ?) in the select or the items as a do-not-care.
  • casex treats both X and Z as do-not-care. It can hide an unknown select in simulation, which is why many teams ban it and use casez instead.
  • Always add a default item, or assign a default value before the case, so the synthesis tool does not infer a latch.

8. Operators

Expression (a = 4'b10x1, b = 4'b10x1)Result
a == b1'bx
a === b1'b1
&4'b1101 (unary reduction AND)1'b0
|4'b0100 (unary reduction OR)1'b1
!4'b0100 (logical NOT)1'b0
4'b0100 && 4'b0010 (logical AND)1'b1

A unary reduction operator works on every bit of one operand and returns one bit. A logical operator treats each whole operand as true (non-zero) or false (zero) and returns one bit. === is for test benches; it does not synthesize to anything meaningful.

9. defparam

defparam overrides a parameter of a module instance from anywhere in the hierarchy, for example defparam top.u_fifo.DEPTH = 32;. It makes the design hard to read and many tools warn against it. Pass parameters at instantiation instead: fifo #(.DEPTH(32)) u_fifo (...);.

10. Task and function

functiontask
TimeZero simulation time, no delays or event controlsMay contain #, @ and wait
OutputsOne return valueAny number of output and inout arguments
Can callOther functionsFunctions and tasks
UseIn an expressionAs a statement

11. Transport and inertial delay

An inertial delay rejects pulses shorter than the delay, as a real gate does; Verilog continuous assignments such as assign #5 y = a; behave this way. A transport delay passes every pulse, however short, as a long wire would; in Verilog it is modeled with a nonblocking assignment with an intra-assignment delay, y <= #5 a;. The full treatment is in Transport, Inertial and Delta Delay in VHDL and Verilog.

12. $monitor and $display

$display prints once, when the statement executes. $monitor prints whenever any of its arguments changes, and only one $monitor is active at a time. $strobe prints at the end of the time step, after nonblocking updates, which is why Cummings recommends it for showing values assigned with nonblocking assignments.

13. Simulator types

  • Interpreted: reads and executes the source directly. Quick to start, slow to run.
  • Compiled: translates the design into machine code or C first. Slower to start, much faster on long runs.
  • Event based: evaluates a block only when one of its inputs changes, and keeps full timing (delays, glitches). This is how standard Verilog simulators work; see Event-Driven Simulation: How Verilog Simulators Work.
  • Cycle based: evaluates the logic once per clock cycle and ignores delays within the cycle. Very fast for synchronous designs, but it cannot show glitches or check timing.

Common mistakes in these answers

  • Saying a reg is always a flip-flop.
  • Using blocking assignments in clocked blocks and relying on statement order.
  • Forgetting the default in a combinational case and getting a latch.
  • Using casex in RTL and missing an X on the select in simulation.
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!