What is "good RTL code"? The message of this post is not that one piece of code is always better than another, but that you should understand which trade-offs and choices are involved.
Programming languages always ask you to make choices, and whether a choice is right depends on what the result is for and where it will be used. HDLs are no different.
- Synthesis: do you reduce area and static power, or dynamic power?
- Maintenance: is the code easy for people to read, or easy for the synthesis tool to optimize?
Most of these choices are not either/or; you pick somewhere in between. And for the productivity of a whole organization, writing reusable code that follows an agreed guide is better than writing newly optimized code for every project.
This post takes Verilog code in a style common in the industry and changes it step by step according to 22 SystemVerilog coding guidelines for synthesis, and why, explaining the reason for each change.
The design
A small block that takes two 8-bit inputs and a mode and outputs AND/OR/XOR/NOR. It accepts inputs with an in_valid pulse and outputs the result with an out_valid pulse two clocks later. (It has the same function as the example in Getting started with Verilog digital logic.)
Starting point: typical Verilog code
`timescale 1ns/1ps
module example (
input wire clk, // 10MHz
input wire rst_n, // active-low reset
input wire [7:0] in_a,
input wire [7:0] in_b,
input wire [1:0] mode,
input wire in_valid,
output reg [7:0] out_o,
output reg out_valid
);
reg [7:0] wa, wb, tmp;
reg [1:0] wm;
reg [1:0] cState, nState;
parameter WAIT = 2'b00, INDATA = 2'b01, OUTDATA = 2'b10;
always @(posedge clk or negedge rst_n)
if (~rst_n) cState <= WAIT;
else cState <= nState;
wire inValid = (in_valid == 1'b1) ? 1'b1 : 1'b0;
wire outValid = (nState == OUTDATA) ? 1'b1 : 1'b0;
always @(cState or inValid)
case (cState)
WAIT: nState = inValid ? INDATA : WAIT;
INDATA: nState = OUTDATA;
OUTDATA: nState = WAIT;
default: nState = WAIT;
endcase
always @(posedge clk or negedge rst_n)
if (~rst_n) begin
wa <= 0; wb <= 0; wm <= 0;
end else if (in_valid) begin
wa <= in_a; wb <= in_b; wm <= mode;
end else begin
wa <= wa; wb <= wb; wm <= wm; // hold the value
end
always @(*)
case (wm)
2'b00: tmp = wa & wb;
2'b01: tmp = wa | wb;
2'b10: tmp = wa ^ wb;
2'b11: tmp = ~(wa | wb); // NOR
default: tmp = 8'h00;
endcase
always @(posedge clk or negedge rst_n)
if (~rst_n) out_valid <= 1'b0;
else if (outValid) out_valid <= 1'b1;
else out_valid <= 1'b0;
always @(posedge clk or negedge rst_n)
if (~rst_n) out_o <= 8'h00;
else if (outValid) out_o <= tmp;
else out_o <= out_o; // hold the value
endmodule
⚠️ A mistake you see now and then is writing NOR as
!wa & !wb.!is logical negation, so it turns the whole 8-bit value into a 1-bit true/false. A bitwise NOR is~(wa | wb)or~wa & ~wb.
Now let's change this code one piece at a time.
1. Remove `timescale
Change: delete `timescale 1ns/1ps from the design file.
Why: unless it is RTL that interfaces with a high-speed PHY (such as part of a DDR controller), `timescale has no physical meaning in digital design code. Used carelessly, it can actually slow simulation down a lot.
It was written in the days when simulators were less mature, and the habit stuck as boilerplate, but now it belongs in the testbench or in simulator options.
2. logic instead of wire/reg
Change
module example (
input logic clk,
input logic rst_n,
input logic [7:0] in_a,
input logic [7:0] in_b,
input logic [1:0] mode,
input logic in_valid,
output logic [7:0] out_o,
output logic out_valid
);
Why: wire and reg are the most criticized part of Verilog.
wireis borrowed from PCB wiring and allows multiple drivers. That does not match the "one driver" rule logic inside an SoC needs.reg, despite its name, does not mean a flip-flop.
logic has the property of a single driver, yet does not care whether that driver is a flip-flop or combinational logic, which is intuitive. Driving it from more than one place is caught as an error at elaboration.
3. A one-line comment on every variable declaration
Change
logic [7:0] wa; // input register for a
logic [7:0] wb; // input register for b
logic [1:0] wm; // input register for mode
logic [7:0] tmp; // calculated result (combinational)
Why: a variable name is the name the designer gave to a specific part of the circuit. Even a value used only briefly has an intent. Leaving one line about that intent helps a lot whoever analyzes the code later, including your future self.
4. States as an enum
Change
enum logic [1:0] {
WAIT = 2'b00, // waiting input
INDATA = 2'b01, // calculate
OUTDATA = 2'b10 // throw calculated result
} cState, nState; // current & next state
Why: writing states as names instead of numbers is almost universally preferred. But there are levels to how you name them.
| Method | Problem |
|---|---|
`define |
Its scope is the whole compilation unit, which is unclear. The larger the project, the higher the risk of collisions |
parameter |
The scope is clear, but it can be overridden by a parent module, which does not fit the "fixed constant" nature of a state |
localparam |
Cannot be overridden, so it fits as a constant. But you cannot tell when the variable holds a value that is not allowed |
enum |
Tells the tools that the state variable can hold only the defined values |
Declared as an enum, the simulator understands the design intent. It warns when an undefined value is assigned, and shows INDATA in the waveforms instead of 2'b01. That clearly makes debugging more efficient.
5. always_ff / always_comb instead of always
Change
always_comb begin
case (cState)
WAIT: nState = inValid ? INDATA : WAIT;
INDATA: nState = OUTDATA;
OUTDATA: nState = WAIT;
default: nState = WAIT;
endcase
end
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
wa <= '0; wb <= '0; wm <= '0;
end else if (in_valid) begin
wa <= in_a; wb <= in_b; wm <= mode;
end
end
Why: for synthesis, only two uses of always make sense: blocking assignment → combinational logic and nonblocking assignment → sequential logic. But syntactically, mixing the two or leaving a signal out of a combinational sensitivity list is not an error, because always is also used for behavioral modeling.
If such mistakes are found only at lint or synthesis, you may have to rerun all the related simulations after fixing them. Use nonblocking in always_comb or blocking in always_ff and the tool tells you right away, so you catch it while coding. always_comb also builds the sensitivity list for you.
6. unique on case
Change
always_comb begin
unique case (cState)
WAIT: nState = inValid ? INDATA : WAIT;
INDATA: nState = OUTDATA;
OUTDATA: nState = WAIT;
endcase
end
Why: Verilog's case has two issues.
- Semantically it is a priority structure that compares from the top down, so the synthesized logic can get deep.
- If no item matches, it does nothing, which is why you must always write a
default.
These used to be solved with synthesis-only pragmas (// synopsys parallel_case full_case). But pragmas are not standard and are interpreted only by synthesis tools, so they actually cause simulation and synthesis results to differ.
SystemVerilog's unique solves this in the standard.
- Declares that the items do not overlap → can be synthesized as a parallel structure without priority (the
parallel_caseeffect) - Warns in simulation when no item matches → you find omissions immediately (the
full_caseeffect)
Because it is standard, simulators and synthesis tools interpret it the same way. (Cliff Cummings, SystemVerilog's priority & unique)
7. No "hold the value" assignments in sequential logic
Change: delete assignments that just keep the value, such as else wa <= wa;. (This is the opposite of combinational logic, where you must assign in every case to avoid latches.)
Why: the most basic technique for reducing dynamic power is clock gating.

Synthesis tools recognize the pattern "the value changes only when the condition is true" and turn (a) into (b). But if you write the assignment explicitly as else wa <= wa;, the tool has to ignore it to convert it to clock gating. That conflicts with the general principle that tools put explicit user instructions first, so not writing such code at all makes it easier to get result (b).
How do you make design choices that have no right answer?
From here on, the topics go beyond a coding guide: they are choices that depend on your goals. The discussion assumes this situation.
- You are designing part of a large, complex SoC
- The process is more sensitive to dynamic power than static power
- Modules, once written, are reused across many projects
Two-always vs one-always FSMs
// Two-always: next state (combinational) and current state (sequential) separated
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) cState <= IDLE;
else cState <= nState;
always_comb begin
unique case (cState)
IDLE: nState = start ? BUSY : IDLE;
// ...
endcase
end
// One-always: current state and outputs in one block
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state <= IDLE;
result <= '0;
end else begin
unique case (state)
IDLE: begin
state <= start ? BUSY : IDLE;
result <= '0;
end
// ...
endcase
end
end
Coding guides on the internet often recommend two-always. Either works, but under the assumptions above these points are worth considering.
- For dynamic power, a structure that performs
cState <= nStateevery cycle can be a disadvantage - One-always generally tends to synthesize smaller (at the cost of more lines of code)
- In complex FSMs, having the state and outputs written close together can be easier to understand
- Fewer
alwaysblocks means fewer events for the simulator to manage, so simulation is slightly faster
(Cliff Cummings, SystemVerilog FSM Coding Styles (SNUG 2019))
Mealy vs Moore
| Mealy | Moore |
|---|---|
| Output = f(state, inputs) | Output = f(state) |
| Usually smaller logic | Good for registered outputs |
Mealy can create paths where inputs go straight through to outputs (feed-through); Moore makes it easy to have outputs come straight from registers, which keeps timing clean. In practice, Mealy often pairs with two-always and Moore with one-always.
One-hot state encoding
Four states fit in 2 bits, so binary encoding looks natural. But at the gate level, binary states need decoding logic, which adds logic depth and area.
One-hot assigns one bit per state and removes the decoding.
// define the bit positions (indexes) as constants
localparam int S_WAIT = 0, S_INDATA = 1, S_OUTDATA = 2;
logic [2:0] cState, nState; // one-hot: exactly one bit is 1
always_comb begin
nState = '0;
unique case (1'b1) // branch on which bit is 1
cState[S_WAIT]: nState[inValid ? S_INDATA : S_WAIT] = 1'b1;
cState[S_INDATA]: nState[S_OUTDATA] = 1'b1;
cState[S_OUTDATA]: nState[S_WAIT] = 1'b1;
endcase
end
// reset value is 3'b001 (WAIT)
It uses one more flip-flop, but without decoding it is faster, and it also helps later with fixes that change only metal layers (metal revisions). On FPGAs, where flip-flops are plentiful, synthesis tools often choose one-hot by default.
Blocking X (unknown) propagation
After reset, digital circuits should not output X. But if an input is X, the output becomes X too. If a little logic can keep X from spreading further, it helps in SoC verification.
logic inValid; // X-free input valid
assign inValid = (in_valid == 1'b1) ? 1'b1 : 1'b0;
In simulation, if in_valid is X, the comparison is not true, so inValid becomes 0. However, the MUX added this way may not be optimized away, so consider applying it only to control paths, not data paths.
Flip-flops without reset (no-reset flip-flops)
Flip-flops without reset are known to be roughly 30% smaller than ones with reset. Hence the idea: "reset only the flip-flops related to state, and do not reset the data path."
| For | Against |
|---|---|
| Less area, and correspondingly less static power | Stability comes first |
| It is often hard to separate control paths from data paths (command queues, descriptors and so on) |
Applications where control and data separate cleanly, such as ISPs (image signal processors), use it actively. Blocks that run by reading descriptors from external memory (OSD, GPU and so on) are more cautious, or spend a lot of effort separating the paths.
If you mainly build small common modules reused across many projects (bus components, I/O MUXes and so on), a policy of resetting every flip-flop for stability is a perfectly reasonable choice too.
// example: leave only the data path (out_o) without reset
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
out_valid <= 1'b0; // control signals are reset
end else if (outValid) begin
out_valid <= 1'b1;
out_o <= tmp; // data only matters together with valid
end else if (out_valid) begin
out_valid <= 1'b0;
end
end
Note that in this code
out_ois inside an asynchronous-reset block but is not assigned in the reset branch. Depending on the synthesis and lint tools, you may get warnings, so in a real design it is cleaner to put registers without reset in a separatealways_ff @(posedge clk)block.
Summary
| Change | Main reason |
|---|---|
Remove `timescale |
Meaningless in design, only slows simulation |
Use logic |
One-driver rule, removes the reg misunderstanding |
| A comment per declaration | Records the design intent behind the name |
enum states |
Restricts allowed values, shows names in waveforms |
always_ff / always_comb |
Catches assignment-style mistakes while coding |
unique case |
Parallel structure + omission detection, in the standard |
| Remove hold-value assignments | Helps automatic clock gating |
FSM style, Mealy/Moore, one-hot, X blocking and no-reset flip-flops are choices to make according to your goals and environment. What matters is being able to explain why you wrote it that way.
Further reading
- Cliff Cummings – SystemVerilog's priority & unique (SNUG 2005)
- Cliff Cummings – SystemVerilog FSM Coding Styles (SNUG 2019)
- IEEE Std 1800-2017, 9.2.2 (always_comb/always_ff), 12.5.3 (unique/priority case)
Design (5 posts)
- The SoC Development Flow at a Glance: From Concept to Chip
- Getting Started with Verilog Digital Logic: Combinational Circuits, Sequential Circuits and FSMs
- Learning RTL Coding by Rewriting Verilog in SystemVerilog Reading now
- 22 SystemVerilog Coding Guidelines for Synthesis, and Why
- Compilers 101 for Hardware Engineers: All the Way to Verilog
Leave a Reply