S35 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S3 · Lesson 5 · Simulation and synthesis pitfalls

Verilog that simulates one way and builds another: assignments, sensitivity, latches, widths and X

Verilog describes hardware but runs as a simulation program, and the two readings can disagree. Code that passes every simulation can synthesize into different hardware: a shift register that collapses to one flip-flop, a "combinational" block that grows latches, an adder that silently drops its carry. Lessons 2 to 4 wrote correct RTL; this lesson collects the classic ways to write incorrect RTL that looks correct, each with the rule that prevents it. Lint tools check most of these rules automatically, but only for a designer who knows what they mean.

  • A three-stage shift register written with nonblocking assignments (<=). On each clock every flip-flop takes its neighbor's old value, so a pulse on D walks through Q0, Q1, Q2 one clock at a time.
  • Written with blocking assignments (=) in the order Q0, Q1, Q2, each line would see the value just assigned above it, and D would reach Q2 in the same clock: the three flip-flops would act as one, in simulation and, often, in synthesis.

Predict

In Verilog, reg [7:0] s; is assigned s = a + b; with 8-bit a = 200 and b = 100. What does s hold?

Name it

Blocking and nonblocking assignments

In a clocked always block, use nonblocking (<=) assignments: every right-hand side is evaluated with the values from before the clock edge, then all registers update together, as real flip-flops do. Blocking (=) assignments update immediately, so later lines see new values, and the result depends on statement order. In combinational blocks, use blocking assignments. Mixing them, or assigning the same variable from two blocks, creates simulation races.

Sensitivity lists

A combinational block must re-evaluate whenever any input changes. Old-style lists that omit a signal make the simulation hold a stale output, while synthesis builds the full logic: simulation and hardware disagree. Use always @* (or SystemVerilog's always_comb).

Inferred latches

If a combinational block doesn't assign an output on every path, such as an if without an else or a case missing values without a default, the output must keep its old value, and synthesis builds a latch. Latches bring timing problems and are almost never intended. Give every output a default at the top of the block.

Widths and signedness

Verilog silently truncates and zero-extends. An expression's width is set by its largest operand and its destination, so carries vanish and signed values lose their sign when mixed with unsigned ones. Size every result deliberately.

X-optimism

In simulation, if (sel) with sel unknown (x) takes the else branch, hiding the fact that the hardware's behavior is undefined. Uninitialized registers and floating inputs can pass simulation this way and fail in silicon. Resets on all control state, and assertions that check for x, guard against it.

Worked example

A designer writes a 3-stage shift register as always @(posedge clk) begin q0 = d; q1 = q0; q2 = q1; end. What does it do?

Show the solution
  1. Follow the blocking assignments in order. q0 takes d, then q1 takes the new q0, which is d, then q2 takes d.
  2. What that builds. After each clock all three equal d: a single flip-flop with three copies, and a delay of one clock instead of three.
  3. Reversed order. Writing the lines as q2 = q1; q1 = q0; q0 = d; happens to work, which is exactly why the rule is to use nonblocking assignments: correctness shouldn't depend on line order.

Answer it acts as one flip-flop copied three times, delaying d by one clock instead of three.

Worked example

A combinational block decodes a 2-bit sel with case (sel) 2'b00: y = a; 2'b01: y = b; 2'b10: y = c; endcase and no default. What does synthesis build?

Show the solution
  1. Find the unassigned case. For sel = 2'b11 the block doesn't assign y, so y must hold its previous value.
  2. What synthesis infers. A latch on y, enabled when sel isn't 11.
  3. The fix. Adding default: y = 1'b0; (or y = 1'b0; before the case) makes it a pure 4-to-1 multiplexer with a constant input. One missing line turns combinational logic into a level-sensitive storage element with its own timing hazards.

Answer a latch on y, enabled whenever sel isn't 11; a default assignment turns it back into a multiplexer.

Worked example

wire signed [7:0] a = -8'sd3; and wire [7:0] b = 8'd2;. What is a < b in Verilog, and why?

Show the solution
  1. The comparison's type. Mixing a signed and an unsigned operand makes the whole comparison unsigned.
  2. Read −3 as unsigned. As an 8-bit pattern, −3 is 256−3=253 so the comparison is 253 < 2, which is false.
  3. The fix. Make both operands signed, or compare explicitly sized values:
a < $signed(b)
  1. Why it slips through. Bugs like this survive simulation when test values never go negative.

Answer a < b is false, because the comparison is unsigned and −3 reads as 253.

Check

A 6-bit register is assigned the sum of two values, 200 and 9000 (wider operands, already in range of their own widths). What does it hold after truncation?

Check

A 4-stage shift register is written with blocking assignments in the order stage 0, stage 1, …, stage 4 − 1 inside one clocked block. How many clock cycles of delay does it give from input to the last stage?

Check

A combinational block has 5 outputs. A case statement without a default leaves 1 of them unassigned in some branch; the rest are assigned in every branch. How many latches does synthesis infer?

Check

In an 8-bit signed-unsigned comparison, the signed operand holds −3. Verilog treats the comparison as unsigned. What unsigned value does −3 become?

Check

A combinational block written always @(a or b) y = a & b | c; simulates correctly in every test, yet the gate-level simulation disagrees with RTL simulation when only c changes. Why?

Check

Which coding style prevents inferred latches in a combinational always @* block?

Check

Write two Python models of an n-stage (8) shift register's clock edge, given the list of stage values q and input d: clock_nonblocking(q, d) (all stages update from the old values) and clock_blocking(q, d) (statements in order stage 0, 1, …, each seeing updates above it). Both return the new list. Starting from all zeros, feed the input sequence 1, 0, 0, … for n + 2 clocks and set out_nb and out_b, the lists of the last stage's value after each clock.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

A team's design passes all RTL simulations but fails on the FPGA: a state machine occasionally jumps to an undefined state after power-up. The state register has no reset, and the RTL simulation started with it at x. Explain how X-optimism let the bug through, and the fix.

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.