[Design 4] 22 SystemVerilog Coding Guidelines for Synthesis, and Why

Summary of the 22 SystemVerilog coding rules for synthesis

Written by

in

SystemVerilog is a big language covering both design and verification. That makes it important for a team to agree on which features to use, and how, when writing RTL for synthesis.

This guide has three goals.

  1. Code that shows the design intent accurately
  2. Code that can be reused across projects
  3. Code that minimizes differences between synthesis results and RTL simulation results

The rules are as specific as possible, but only as detailed as people can actually follow. They target synthesizable RTL modules; only some apply to simulation models. Naming rules are a separate topic.

Summary of the 22 SystemVerilog coding rules for synthesis
Figure 1. The 22 rules in this post, grouped into five sets: file organization, declarations, structure, behavioral description, and separating simulation from synthesis.

You can see this guide applied step by step to real code in Learning RTL coding by rewriting Verilog in SystemVerilog.


File organization

1. Design files are .sv, headers are .svh

Some EDA tools tell Verilog and SystemVerilog apart by file extension, so design files use .sv. Headers get a different extension to keep them easy to manage; .svh is recommended because the most editors support syntax highlighting for it.

2. An `ifndef guard in any file that may be included

// rules
// - define names in upper case
// - guard name: __GUARD_ + name + __ (words joined with _, e.g. DesignName → DESIGN_NAME)
// - next to `endif, a comment saying which guard it closes

`ifndef __GUARD_DESIGN_NAME__
`define __GUARD_DESIGN_NAME__

module DesignName ( /* ... */ );
  // ...
endmodule

`endif // __GUARD_DESIGN_NAME__

Why: some code, such as a class, must be declared before it is referenced. With a guard, it is compiled only the first time it appears, so you do not have to worry about the compile order of files.

3. One module per file

Each file holds only one module (or class). The only exception is something like a built-in testbench.

4. File name = module name

Name the file after the module/class/package inside it. You can then find the module you want right away among hundreds of files.

5. Indent with spaces, not tabs

Tab width differs between editors and settings, so indentation aligned with tabs breaks depending on where you view it. The main reason for using tabs used to be smaller files, but source code size is no longer a concern.


Declarations

6. logic instead of wire/reg

// bad
wire intr;    // combinational logic via assign
reg  intrSt;  // combinational logic via always
reg  intrEn;  // flip-flop via always

// good
logic intr;   // always_comb (or assign)
logic intrSt; // always_comb
logic intrEn; // always_ff

Why: in Verilog, wire and reg are only a syntactic distinction: one changes by continuous assignment, the other by procedural assignment. But the physical nuance of the names causes misunderstandings. In particular, a reg does not necessarily become a flip-flop.

logic was introduced with digital circuits in mind, and the standard says driving it from more than one place is an error at elaboration. That is different from wire, which allows multiple drivers, and reg, where multiple drivers are caught only at lint.

7. Ports in Verilog-2001 style, with direction and type together

// good
module M (input logic A, output logic B);

// bad (Verilog-1995 style)
module M (A, B);
  input  logic A;
  output logic B;

8. Group ports by function, not by direction

// good: grouped by channel
module A (
  // incoming channel
  input  logic       rxValid,    // incoming valid
  output logic       rxReady,    // incoming acknowledge
  input  logic [7:0] rxPayload,  // incoming data
  // outgoing channel
  output logic       txValid,    // outgoing valid
  input  logic       txReady,    // outgoing acknowledge
  output logic [7:0] txPayload   // outgoing data
);

If you group all inputs together and all outputs together, you have to jump up and down to understand one interface. Keep signals that work together, together.

9. A one-line comment on every variable declaration

logic       valid;    // active-high valid
logic       ready_n;  // active-low ready
logic [7:0] payload;  // data transferred when valid & !ready_n

A variable name carries the designer's intent. Leaving that intent in one line makes the code much easier to analyze. (See 5 coding guidelines that work in any language.)

10. States as an enum

typedef enum logic [1:0] {
  ST_IDLE   = 2'b00,  // idle state
  ST_DECODE = 2'b01,  // decode opcode
  ST_EXEC   = 2'b10   // execute opcode
} t_state;

t_state state;   // state of function
t_state st_d1;   // 1-cycle delayed version of state

// shown as 'ST_DECODE' in waveforms instead of '01'
if (A) state <= ST_DECODE;

You can also do this with localparam and logic [1:0], but an enum lets the tools know the allowed values and shows names in waveforms, which makes debugging much easier.

11. No hard-coded constants

// bad
assign A = B + 3;

// good
localparam INC = 3;   // amount to increase each time
assign A = B + INC;

Give constants names with parameter, localparam or enum. Write constant names in upper case and leave a comment on their purpose.


Structure

12. Connect ports by name

// good
M u_M (.a(a), .b(b));

// bad
M u_M (a, b);   // positional: silently miswired if the port order changes
M u_M (.*);     // implicit: you cannot see in the code which signals are connected

Use named connections in design code. In testbenches .* is fine, and positional connections are only for very exceptional cases.

13. Name or comment begin/end

if (A) begin : p_chk_A
  if (B) begin
    // ... 100 lines ...
  end // if B          ← pair marked with a comment
end : p_chk_A          ← pair marked with a name

Once blocks are nested or long, it is hard to see which end closes which begin. A name also appears in the simulator's hierarchy path, which helps debugging.

14. Always name generate blocks; no stand-alone generate blocks

// bad: unnamed generate block
generate if (A < 100) begin   // ← a name is needed here
  // ...
end endgenerate

// good
generate if (A < 100) begin : g_small
  // ...
end endgenerate

The rules for unnamed generate blocks only became clear in Verilog-2005, and stand-alone generate blocks were allowed in Verilog-2001 but dropped in 2005. Avoid both so you do not depend on a particular language version. With a name, the hierarchy path (u_top.g_small.xxx) is predictable too.


Behavioral description

15. always_ff / always_comb instead of always

Write flip-flops with always_ff and combinational logic with always_comb (or assign). A plain always does not give a syntax error when blocking and nonblocking assignments are mixed, which blurs the design intent, and the mistake is found only at lint. The dedicated constructs let the tools catch it right away.

16. unique instead of full_case/parallel_case pragmas

unique case (A)
  // ...
endcase // A

Semantically, Verilog's case is a priority comparison (an if-else chain). This used to be patched with tool-specific pragmas, but pragmas are interpreted only by synthesis tools, so simulation and synthesis results can differ. SystemVerilog's unique gives the same effect in the standard, and also warns about overlaps and omissions in simulation. You can use unique on if as well when needed.

17. Asynchronous active-low reset + a single rising-edge clock

always_ff @(posedge clk, negedge rst_n) begin : p_eg
  if (!rst_n) begin
    // reset values
  end
  else begin
    // normal operation
  end
end : p_eg

Following the most common clock and reset scheme means fewer problems when moving IP to another project.

18. No "hold the value" assignments in sequential logic

// bad
always_ff @(posedge clk, negedge rst_n) begin : p_A
  if (!rst_n) A <= '0;
  else if (B) A <= A + 1;
  else        A <= A;       // unnecessary
end : p_A

// good
always_ff @(posedge clk, negedge rst_n) begin : p_A
  if (!rst_n) A <= '0;
  else if (B) A <= A + 1;
end : p_A

When assignments happen only when the value changes, it is easier for the synthesis tool to insert clock gating automatically, which reduces dynamic power.


How do you separate simulation from synthesis?

let instead of `define functions; keep `define/`ifdef to a minimum

// bad
`define MIN(X,Y) (((X) > (Y)) ? (Y) : (X))

// good
let MIN(X, Y) = ((X) > (Y)) ? (Y) : (X);

`define is plain text substitution, so it has no scope, and tools sometimes interpret it differently. (See 3 SystemVerilog portability traps: VCS vs Xcelium.) let is a scoped expression definition.

⚠️ let was added in IEEE 1800-2009, so support varies by synthesis tool and version. Before using it, check that your synthesis and lint tools support it, and if not, use a function automatic instead.

Log the parameters actually in use during simulation

module A #(
  parameter Q_DEPTH = 32,
  parameter MAX_PKT = 8
) ( /* ... */ );

  logic [Q_DEPTH-1:0] pQueue;  // processing queue

  B #(.MAX_PKT(MAX_PKT)) u_B ( /* ... */ );

  // synthesis translate_off
  initial begin : rpt_param
    $display("[%m] Q_DEPTH: %0d", Q_DEPTH);
    // MAX_PKT is not used directly in this module, so it is not printed
  end : rpt_param
  // synthesis translate_on
endmodule

Why: logging the parameter values actually used helps a lot later. The full waveforms of a three-week simulation, for example, are too big to keep for long, but if you have to revisit an issue six months after the project ends, one line in the log is the most reliable evidence.

  • Do not print parameters that are only passed down to submodules, to avoid duplicates.
  • There may be several instances, so include the hierarchy path with %m.
  • In a UVM environment, `uvm_info is better than $display.

Testbench code gets translate pragmas; design logic gets the SYNTHESIS define

Sometimes design code needs to tell simulation and synthesis apart.

// code that is not synthesized → translate pragma
// synthesis translate_off
`include "uvm_pkg.sv"
// synthesis translate_on
// synthesized code that should be implemented differently from simulation → SYNTHESIS define
module AND2 (output logic Y, input logic A, B);
`ifdef SYNTHESIS
  AND2X4_RVT u_and2 (.Y(Y), .A1(A), .A2(B));   // a specific library cell
`else
  assign Y = A & B;
`endif
endmodule

The difference is whether the user can control it.

Synthesis tool Simulator
SYNTHESIS define Defined automatically Not defined (the user can turn it on with +define+SYNTHESIS)
translate pragma Interpreted; the section is ignored Treated as an ordinary comment

Synthesized logic must be simulatable with SYNTHESIS turned on, so it uses the define. If you wrap testbench code in the SYNTHESIS define instead, then the moment you turn on the define to verify the design in synthesis mode, the testbench disappears too and you cannot verify anything. That is why testbench code uses translate pragmas.

No `timescale in design files

In synthesized digital designs, time resolution has no meaning, and misusing it can slow simulation or cause delta-delay problems. Set it in the testbench or simulator options. The only exception is when interfacing with analog modules.


Summary

Group Rules
Files .sv/.svh, include guards, one module per file, file name = module name, space indentation
Declarations logic, ANSI ports, ports grouped by function, declaration comments, enum states, named constants
Structure Named port connections, named begin/end, named generate blocks
Behavior always_ff/always_comb, unique, asynchronous active-low reset, no hold-value assignments
Sim vs synthesis Prefer let (check support), log parameters, translate pragma vs SYNTHESIS, no `timescale

In the end, the rules have one purpose: conveying the design intent accurately to the tools and to your colleagues.

Further reading

Design (5 posts)

  1. The SoC Development Flow at a Glance: From Concept to Chip
  2. Getting Started with Verilog Digital Logic: Combinational Circuits, Sequential Circuits and FSMs
  3. Learning RTL Coding by Rewriting Verilog in SystemVerilog
  4. 22 SystemVerilog Coding Guidelines for Synthesis, and Why Reading now
  5. Compilers 101 for Hardware Engineers: All the Way to Verilog

See the full Tech Notes index

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *