Skip to content

Lesson 31 ยท Synthesis and Verification

The Synthesizable Subset and Common Pitfalls

Learn which Verilog constructs synthesize into hardware and which are simulation-only, plus classic bugs: latches, blocking versus non-blocking, width and sign mismatches.

Verilog is two languages in one. One half describes hardware that a synthesis tool turns into gates and flip-flops (RTL, register-transfer level). The other half describes tests and models that only a simulator can run. Writing code that simulates correctly is not enough: if you use the wrong half in a design module, the synthesis tool will ignore it, reject it, or, worst of all, build something different from what you simulated.

This lesson gives you a map of what is synthesizable and walks through the bugs that catch almost everyone. The Verilog is read-only (no simulator or synthesis tool runs on this site); the C programs are labelled stand-ins that reproduce the arithmetic and ordering behaviour behind the bugs.

What synthesizes

Construct Synthesizable? Notes
module, ports, wire, reg, parameter Yes
assign, operators (+ - & | ^ << >> ?:) Yes * becomes a multiplier; / and % are costly or restricted
always @(posedge clk), always @* Yes Standard RTL
if, case, for with constant bounds Yes Loops are unrolled
generate, function, task without delays Yes
Memories as reg [W-1:0] mem [0:N-1] Yes Maps to RAM when the access pattern fits
initial Sometimes FPGA tools use it to set initial register or RAM values; ASIC flows ignore it
#delay No Ignored; never design with them
$display, $finish, $random, file I/O No Simulation only
fork/join, wait, events, force/release No Testbench constructs
real and time types No (usually)
Four-state compare ===, !== No Depends on x and z
SystemVerilog classes, queues, randomize() No Verification only

When a design is meant for synthesis, restrict yourself to the "Yes" rows. A good habit is to lint (run Verilator --lint-only, or your vendor's check tool) before simulating.

Bug 1: unintended latches

Combinational logic must assign every output on every path. A missing else or default makes the tool infer a latch that remembers the old value:

VERILOG
// BUG: when en is 0, y keeps its old value: a latch
always @* begin
  if (en) y = d;
end

// FIX: assign on every path
always @* begin
  y = 1'b0;           // default
  if (en) y = d;
end

Latches also appear from incomplete case statements with no default, and from outputs assigned in only some branches. always_comb lets the compiler flag this for you.

Bug 2: blocking versus non-blocking assignments

The two assignment types have different scheduling:

  • Blocking = executes immediately, in order, like C. Use it in combinational always blocks.
  • Non-blocking <= evaluates the right-hand side now but updates the left-hand side at the end of the time step. Use it in sequential (clocked) always blocks.

Mixing them in a clocked block is how you accidentally get a shift register that has the wrong length:

VERILOG
// Intended: a 3-stage delay line
always @(posedge clk) begin
  q1 = d;     // blocking: q1 changes immediately
  q2 = q1;    // sees the NEW q1 -> q2 == d in the same cycle
  q3 = q2;    // so all three equal d after one clock!
end

With non-blocking assignments (q1 <= d; q2 <= q1; q3 <= q2;) each stage reads the old value of the previous one, which is how real flip-flops behave. The first stand-in shows the difference by emulating both:

C Stand-in: blocking versus non-blocking in a 3-stage delay line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The blocking column simply equals d (a wire, not a delay line), while the non-blocking column lags by two cycles at the third stage. The old rule of thumb stays true: <= in clocked blocks, = in combinational blocks, never both in one block.

Bug 3: width mismatches

Verilog extends or truncates silently when widths differ. These bugs compile cleanly and give wrong numbers:

VERILOG
wire [7:0] a, b;
wire [7:0] sum8   = a + b;          // carry lost: 200 + 100 = 44
wire [8:0] sum9   = a + b;          // OK: context width is 9, so the carry is kept
wire [8:0] bad    = {1'b0, a + b};  // BUG: a + b is still computed in 8 bits first (see below)

The rule: the width of an expression is the largest width among the operands and the assignment target (the context width). In sum9 = a + b, the add is evaluated in 9 bits because the target is 9 bits wide, so the carry survives. In {1'b0, a + b} the concatenation operands are self-determined, so a + b is evaluated in 8 bits and the carry is gone before the concatenation. The portable fix is to widen an operand: {1'b0, a} + {1'b0, b}.

Turn on lint warnings for width mismatch; most tools detect them.

Bug 4: signed versus unsigned

If any operand in an expression is unsigned, the whole expression is treated as unsigned, and signed operands are zero- or sign-extended according to that:

VERILOG
wire signed [7:0] x = -8'sd3;
wire        [7:0] y = 8'd5;
wire              lt = (x < y);    // BUG: unsigned compare, x is 253, so lt = 0

Mark both operands signed (or cast with $signed) to compare as signed. The second stand-in reproduces this with uint8_t and int8_t:

C Stand-in: -3 compared with 5, signed and unsigned

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The unsigned compare prints 253 < 5 is 0, the signed compare prints -3 < 5 is 1, and the sums show 44 versus 300: the 8-bit sum loses the carry (300 mod 256 is 44), while the 9-bit sum keeps it.

Bug 5: sensitivity lists and multiple drivers

  • Incomplete sensitivity lists: always @(a or b) that also reads c simulates as if c never changes the output, but synthesizes as pure logic. Always write always @* (or always_comb).
  • Multiple drivers: assigning the same reg from two always blocks (or assign plus an always block) simulates unpredictably and synthesizes to a conflict error. One signal, one driver.

Bug 6: asynchronous resets and reset release

Async-reset flip-flops are written always @(posedge clk or negedge rst_n). Asserting reset is safe at any time, but releasing it near a clock edge can leave flip-flops in different states (metastability). The standard fix is to assert asynchronously and release synchronously through a two-flop synchronizer, covered in timing and clock domains.

Bug 7: x and initialisation

Simulation starts every reg at x. Real hardware powers up to 0 or 1 randomly. A design that "works" because you reset nothing and the simulator x-propagation happens to hide it will fail on silicon. Always reset registers that control state, and run gate-level or x-propagation checks on critical blocks.

A pre-synthesis checklist

  1. Run a linter and clear all warnings (latches, widths, multiple drivers, unconnected ports).
  2. No # delays, no $ tasks in design files (keep them in testbenches, or under `ifdef SIMULATION).
  3. Every combinational block has defaults; every FSM has default: and reset.
  4. Registers: <=, combinational: =.
  5. Read the synthesis log for warnings; "inferred latch" and "signal has no driver" are red flags.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these is NOT synthesizable in a design module?

    Delays are simulation-only. Synthesis tools ignore or reject them and the hardware will not match the simulation.
  2. 2. What does a missing else branch in an always @* block usually create?

    If an output is not assigned on some path it must keep its value, so the tool infers a latch.
  3. 3. Why use non-blocking assignments in a clocked always block?

    Non-blocking assignments update at the end of the time step, so all right-hand sides see the pre-edge values.
  4. 4. wire signed [7:0] x = -3; wire [7:0] y = 5; What does x < y evaluate to?

    If any operand is unsigned the whole expression is unsigned, so x is compared as 253.

Summary

  • Design modules use only the synthesizable subset; delays, $ tasks, ===, and classes are for simulation.
  • Latches come from incomplete assignments; give defaults or use always_comb.
  • <= in clocked blocks, = in combinational blocks; never mix.
  • Know the width and sign rules: context width, self-determined operands, unsigned wins.
  • Reset state registers, avoid multiple drivers, and lint before you simulate.

Next: Timing, Metastability and Clock Domain Crossing