RTL Design / Module 3: Verilog from Scratch
Lesson 3.8

Coding style synthesizers love (and hate)

Synthesis tools are picky parsers with a netlist printer attached. The habits you build now — full case, one driver per signal, sync resets, no # delays — are what separate "it simulates" from "it ships."

Core idea

Write RTL as if a human reviewer and a lint tool will read it tomorrow. Clarity is not optional — ambiguous code becomes ambiguous gates.

1

The synthesizer's mental model

RTL to gates flow

Good style reduces surprise between simulation and synthesis.

HabitSynthesizer lovesSynthesizer hates
case statementscase + default (full)Missing default → latch inference
Constantslocalparam / parameterMagic numbers scattered in code
AssignmentsOne driver per signalMultiple always driving same reg
Timingalways @(posedge clk)#10 delays in RTL
ResetsSync or async — but consistentMixing styles randomly per flop
2

Full case / default — avoid latches

In combinational always @(*) or case, every output must have a defined value for every input combination. Missing branches infer latches.

bad_case.v
// BAD — latch inferred when sel=2'b11
always @(*) begin
  case (sel)
    2'b00: out = a;
    2'b01: out = b;
    2'b10: out = c;
    // no default — out holds previous value → latch
  endcase
end
good_case.v
// GOOD — full case with safe default
always @(*) begin
  case (sel)
    2'b00:   out = a;
    2'b01:   out = b;
    2'b10:   out = c;
    default: out = {WIDTH{1'b0}};  // or explicit 2'b11 branch
  endcase
end
Trap

casez / casex with don't-cares can hide coverage holes. In RTL, prefer full binary case unless you truly need priority encoding and document it.

3

Named constants — no magic numbers

named_constants.v
// BAD
if (state == 3'd5) next_state = 3'd7;

// GOOD
localparam ST_IDLE  = 3'd0;
localparam ST_READ  = 3'd1;
localparam ST_WRITE = 3'd2;
localparam ST_DONE  = 3'd3;

if (state == ST_READ) next_state = ST_WRITE;
Analogy

Magic numbers are like unlabeled buttons in a cockpit. localparam names are the labels — future you (and lint) will thank you.

4

One driver per signal

Every wire or reg should be assigned in exactly one place. Multiple drivers create multiply-driven nets — synthesis errors or silicon fights.

✅

Good pattern

One always @(posedge clk) block owns all sequential outputs in a module section.

❌

Bad pattern

Two separate always blocks both assigning count <= ... — illegal for synthesis.

one_driver.v
// GOOD — single sequential block owns state
always @(posedge clk or negedge rst_n) begin
  if (!rst_n) begin
    count <= 8'd0;
    done  <= 1'b0;
  end else begin
    if (en) count <= count + 8'd1;
    done   <= (count == MAX);
  end
end
5

Synchronous style & no # delays in RTL

Default to always @(posedge clk) with non-blocking <= for registers. Reserve blocking = for combinational always @(*).

Hard rule: Never put # delay literals in modules targeted for synthesis. Delays are for testbench timing only — they do not become physical delay elements in modern ASIC/FPGA flows.

ConstructRTL (synth)Testbench (sim)
#N delayForbiddenClock generation, stimulus spacing
initialReset/config only (tool-dependent)Stimulus, $finish
posedge clkPrimary sequential templateAlso used in TB sync
6

Readable FSMs — preview for Module 4

State machines are everywhere. The style you learn now carries into Module 4's deep FSM lessons: two-process or one-process — but always named states and full transitions.

fsm_preview.v
localparam S_IDLE = 2'd0, S_RUN = 2'd1, S_DONE = 2'd2;
reg [1:0] state, next_state;

// Sequential: state register only
always @(posedge clk or negedge rst_n)
  if (!rst_n) state <= S_IDLE;
  else        state <= next_state;

// Combinational: next-state logic — full case!
always @(*) begin
  next_state = S_IDLE;  // default assignment prevents latch
  case (state)
    S_IDLE: next_state = start ? S_RUN : S_IDLE;
    S_RUN:  next_state = done  ? S_DONE : S_RUN;
    S_DONE: next_state = ack   ? S_IDLE : S_DONE;
    default: next_state = S_IDLE;
  endcase
end
Lint-friendly FSM habits

Use localparam state encodings. Assign a default next_state before the case. Document unreachable states. Avoid overlapping state bit patterns.

7

Bad vs good — side by side

TopicBadGood
ResetSome flops async, some sync, undocumentedOne reset policy per module, documented header comment
Widthsassign y = a + b; (implicit widths)assign y = a + b; with declared widths + overflow plan
Combo logicMissing default in if/else chainDefault else branch or default assignment at top
File splitTB initial mixed into DUTdut.v + dut_tb.v separation
Comments"Fix later" with no ticketHeader: clock, reset type, interface protocol
module_header.v
// uart_tx_byte — byte serializer
// Clock: 50 MHz, sync active-low reset
// Interface: valid/ready handshake on byte_in
// States: IDLE → SHIFT → DONE (see fsm_preview pattern)
module uart_tx_byte (
  input  wire       clk,
  input  wire       rst_n,
  // ...
);
▶ Refactor RTL in the simulator
8

Lint-friendly habits checklist

  • Every case has default or is provably full
  • No multiply-driven signals — one assign/always owner
  • No # delays, $display, or TB tasks in synth files
  • States and opcodes use localparam names
  • Sequential blocks use <=; combo blocks use =
  • Module header documents clock frequency and reset type
  • Run lint before every review — warnings are free PPA insurance

Module 4 preview: You have the Verilog vocabulary. Next we build real blocks — decoders, muxes, counters, and full FSMs — using these style rules as non-negotiable defaults.

9

Module 3 checkpoint

  • I can write a module with ports and instantiate it in hierarchy
  • I know wire vs reg / logic and when each is appropriate
  • I choose continuous assign vs always blocks deliberately
  • I use blocking (=) in combo and non-blocking (<=) in sequential
  • I wield operators, concat, replicate, and part-select confidently
  • I parameterize modules and use generate for structure
  • I keep functions in RTL and tasks with delays in the testbench
  • I write full-case, one-driver, lint-clean RTL by default

🎯 What to remember

  • Full case + default prevents accidental latches.
  • Named constants beat magic numbers in every review.
  • One driver per signal — synthesis is not forgiving here.
  • No # delays in RTL; sync style is the default.
  • Module 3 complete — you're ready for combinational building blocks.
Advertisement