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

HDL Coding Guidelines, Part 1: Why Coding Style Matters

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

Coding of circuit behavior and architecture is one of the most critical steps in the whole chip development project. It has a major impact on logic synthesis, routing results, timing robustness, verifiability and testability.

What HDL coding guidelines are for

The HDL Coding Guidelines help chip and macro developers and teams to rapidly understand each other's code. Macro-based designs integrate more easily if these common coding styles are followed. This also applies to externally developed soft cores. Code will not need modification if the simulator, synthesis tool or technology is exchanged. Code is invariant with respect to the synthesis tool as long as the tools support a similar HDL synthesis subset, and invariant with respect to technology as long as the libraries have similar performance and a similar cell set. In addition, the guidelines enable high synthesis quality and simulation performance.

Guidelines are a living document

The HDL Coding Guidelines need continuous adaptation to new tool properties and new methodologies. Please take part in this process with your design know-how. Contribute rules for HDL coding that turned out to prevent errors in the downstream flow, or recommendations that make further design, reuse or maintenance easier.

The HDL Coding Guidelines may be passed to subcontractors or cooperation partners. Ideally their code should comply with these guidelines, enabling rapid and safe integration with internally developed modules.

When to read them

Reading the HDL Coding Guidelines is most efficient at the beginning of a chip design project. Furthermore, a code review should be considered at a very early phase of HDL coding.

Why style is not just cosmetic

It is tempting to treat coding style as a matter of taste. In hardware it is not, for three reasons:

  • The same text means two things. HDL code is read by a simulator and by a synthesis tool. Some styles simulate one way and synthesize to something else, so the hardware does not match the verified model.
  • Code outlives its author. A block is reused, modified and debugged for years, often by other teams. Consistent names and structure make that work faster and safer.
  • Tools read conventions. Lint, clock domain crossing and equivalence-checking tools work better when clocks, resets and registers follow predictable names and patterns.

The rest of this series covers the guidelines themselves: compiling and partitioning VHDL, portability rules, common VHDL errors, critical policies, common warnings, and scope and unit size. Links are at the end.

Deep dive: the numbers and details

This part holds concrete example rules, code that shows why they exist, and a review checklist. Skip it if the overview is all you need.

What a modern style guide specifies

To show the level of detail a good guideline reaches, here are rules from the open lowRISC Verilog Coding Style Guide, used for open-source SystemVerilog projects:

TopiclowRISC rule
Line lengthWrap at 100 characters
IndentationTwo spaces per level, four for a continued line, no tabs
ClocksNames start with clk; the main clock is clk; others use a clk_ prefix, such as clk_dram
ResetsActive-low, asynchronous, default name rst_n
Active-low signals_n suffix, combined with direction as _ni or _no
Registers_d for the register input, _q for its output, _q2 and _q3 for later pipeline stages
Combinational logicalways_comb rather than always @*, and assign where practical
AssignmentsOnly non-blocking (<=) in sequential blocks, only blocking (=) in combinational blocks, never mixed
LatchesAvoid; if one is needed, use always_latch
Case statementsAlways a default item; no full_case or parallel_case pragmas
ParametersUpperCamelCase for tunable parameters, ALL_CAPS for constants

None of these rules is about taste. Each one prevents a class of bug or makes a tool's job easier.

Example 1: blocking assignments in a clocked block

Two registers meant to form a 2-stage pipeline:

// Wrong: blocking assignments in a clocked block
always @(posedge clk) begin
  q1 = d;
  q2 = q1;   // reads the NEW q1, so q2 gets d in the same cycle
end

// Right: non-blocking assignments
always @(posedge clk) begin
  q1 <= d;
  q2 <= q1;  // reads the OLD q1, so q2 lags q1 by one cycle
end

With blocking assignments the simulator updates q1 before q2 reads it, so the pipeline collapses to one stage. The "one assignment type per block" rule removes the question entirely. Clifford Cummings' SNUG 2000 paper "Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!" is the classic treatment of these races.

Example 2: an incomplete combinational block

// Wrong: y is not assigned when sel is 2'b11, so a latch is inferred
always @* begin
  case (sel)
    2'b00: y = a;
    2'b01: y = b;
    2'b10: y = c;
  endcase
end

// Right: a default assignment covers every path
always_comb begin
  y = '0;
  case (sel)
    2'b00:   y = a;
    2'b01:   y = b;
    2'b10:   y = c;
    default: y = '0;
  endcase
end

The first version must remember y when sel is 2'b11, so synthesis builds a latch. The second assigns y on every path. The full_case pragma is not a substitute: it tells synthesis to treat the missing case as "don't care" while the simulator still holds the old value, which creates a simulation and synthesis mismatch. That is why the lowRISC guide bans it.

Example 3: names that tools and reviewers can read

// Hard to review: what is the clock, which reset polarity, which value is registered?
always @(posedge c or negedge r) if (!r) s <= 0; else s <= n;

// Clear: clock, active-low reset, register input and output named by convention
always_ff @(posedge clk or negedge rst_ni) begin
  if (!rst_ni) count_q <= '0;
  else         count_q <= count_d;
end

A reviewer can check the second version for reset polarity and register boundaries at a glance, and a CDC or lint tool can identify the clock and reset by name.

How the guidelines support portability

The opening section promises that code will not need changes when the simulator, synthesis tool or technology changes. In practice that comes from a few habits:

  • Use only the synthesizable subset of the language that all target tools support, and keep testbench-only constructs (initial blocks, # delays) out of design files.
  • Do not instantiate technology cells directly in RTL; wrap any unavoidable ones (memories, clock gates, synchronizers) in a small library module that can be swapped per technology.
  • Avoid tool-specific pragmas in the logic itself, or keep them in one documented place.

A review checklist to start with

  1. Every clocked block uses non-blocking assignments only; every combinational block uses blocking only.
  2. Every combinational output is assigned on every path (no inferred latches).
  3. Every case statement has a default.
  4. Clock and reset names follow the convention, and every control register is reset.
  5. Widths and depths are parameters, not literal numbers.
  6. No technology cells are instantiated outside the wrapper library.
  7. The code is lint-clean, or every waiver is documented.

Running this checklist at the first code review, as the guidelines recommend, costs minutes. Finding the same problems after synthesis or in silicon costs far more.

Further reading

The Editor's Desk
About the Author
The Editor's Desk
Share Facebook X LinkedIn WhatsApp Email

Post a Comment

1 Comments

Your comments will be moderated before it appears here.

  1. I'm very sorry to quote you post without your permition, and I delete it now.sorry...

    ReplyDelete
Post a Comment

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!