Style and Conventions
Register-transfer level (RTL) codebases remain in production for many years. Engineers must read, modify, and migrate designs across multiple silicon manufacturing processes. Clear style conventions enhance code readability and prevent known hardware defects.
Naming Standards
Use consistent naming conventions across all project modules:
`timescale 1ns / 1ps
`default_nettype none
module naming_demo (
input wire clk, // Primary clock; use clk_a, clk_b for multiple domains
input wire rst_n, // Suffix _n specifies active-low polarity
input wire cs_n, // Active-low chip select
input wire wr_en, // Active-high write enable (no suffix)
input wire [7:0] data_in, // Use _in and _out when direction is ambiguous
output wire [7:0] data_out,
output wire valid_out,
output wire error_flag
);
wire [7:0] data_byte; // Lowercase identifiers with underscores
reg [3:0] state_reg; // Suffix _reg identifies flip-flop outputs
reg [3:0] next_state; // Prefix next_ identifies combinational next-state values
wire pipeline_valid;
localparam IDLE = 2'd0; // Uppercase identifiers for constants and states
localparam ACTIVE = 2'd1;
endmodule
The suffix _n explicitly denotes active-low signal polarity. The identifier rst_n indicates that the active reset condition is !rst_n. The suffix _reg informs engineers that the signal originates from a sequential flip-flop. The prefix next_ distinguishes combinational next-state signals from registered current-state variables.
Module instance names require the prefix u_ followed by a descriptive function name:
adder #(.WIDTH(16)) u_alu_adder (/* Port connections */);
fifo #(.DEPTH(64)) u_tx_fifo (/* Port connections */);
uart_rx u_uart_rx (/* Port connections */);
pipeline_stage u_stage0 (/* Port connections */);
pipeline_stage u_stage1 (/* Port connections */);
The u_ prefix enables fast searching and filtering of instance names in synthesis gate netlists.
Reset Design Conventions
ASIC standard cell libraries use asynchronous active-low resets. At device power-up, clock generators may not run stably, but flip-flops must enter a known operational state. Physical standard cells contain active-low reset pins. Specifying active-high reset in RTL forces synthesis tools to insert inverters into every reset tree branch.
`timescale 1ns / 1ps
`default_nettype none
module reset_demo (
input wire clk,
input wire rst_n, // Asynchronous active-low reset
input wire [7:0] d,
output reg [7:0] q
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
q <= 8'h00;
else
q <= d;
end
endmodule
Observe three critical implementation practices:
- Include
negedge rst_nin the sensitivity list to infer an asynchronous reset pin. - Evaluate
!rst_nin the condition statement to match active-low polarity. - Assign an explicitly sized constant to the register reset value.
FPGA architectures often prefer synchronous resets. A synchronous reset block omits negedge rst_n from the sensitivity list. Synchronous resets eliminate reset removal timing violations. Select one reset architecture and apply that convention across the entire digital design.
One Register per Procedural Block
When two registers utilize different reset values or control enables, describe each register in an independent procedural block.
Do not combine unrelated registers into a single procedural block:
// Inflexible style: two unrelated registers combined into one procedural block
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
counter_reg <= 8'd0;
status_reg <= 8'hFF;
end else begin
if (count_en)
counter_reg <= counter_reg + 8'd1;
status_reg <= {error_flag, 7'b0};
end
end
Separate the registers into dedicated blocks:
`timescale 1ns / 1ps
`default_nettype none
module separate_always (
input wire clk,
input wire rst_n,
input wire count_en,
input wire error_flag,
output reg [7:0] counter_reg,
output reg [7:0] status_reg
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
counter_reg <= 8'd0;
else if (count_en)
counter_reg <= counter_reg + 8'd1;
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
status_reg <= 8'hFF;
else
status_reg <= {error_flag, 7'b000_0000};
end
endmodule
Both approaches generate identical gate netlists. Dedicated procedural blocks allow engineers to inspect reset and enable conditions for each register independently.
Source Code Formatting
Use four spaces for indentation and do not insert tab characters. Align signal directions, data types, bit ranges, and identifiers into clean vertical columns. Wrap lines at 100 characters. Organize modules into distinct commented sections:
module well_structured (/* Port declarations */);
// ---- Parameters ----
localparam IDLE = 2'd0;
localparam BUSY = 2'd1;
// ---- Signals ----
reg [1:0] state_reg;
reg [1:0] next_state;
// ---- State Register ----
always @(posedge clk or negedge rst_n) begin
/* Sequential state transition */
end
// ---- Next-State Logic ----
always @(*) begin
/* Combinational state calculation */
end
endmodule
Meaningful Code Documentation
Document design rationale rather than restating code syntax. A comment such as // Add a and b provides no value. A comment such as // Wraps on overflow; caller inspects carry flag explains functional behavior.
Avoid duplicate expressions. When identical logic appears in multiple places, encapsulate the expression into a function or a dedicated submodule.
Complete Verified RTL Example
// =============================================================================
// Module: debounce
// Description: Switch debouncer: outputs stable level after FILTER_BITS
// consecutive identical input samples.
// =============================================================================
`timescale 1ns / 1ps
`default_nettype none
module debounce #(
parameter FILTER_BITS = 16
)(
input wire clk,
input wire rst_n, // Asynchronous active-low reset
input wire btn_raw,
output reg btn_clean
);
// ---- Internal Signals ----
reg [FILTER_BITS-1:0] shift_reg;
wire all_ones;
wire all_zeros;
// ---- Stability Detection Logic ----
assign all_ones = &shift_reg;
assign all_zeros = ~(|shift_reg);
// ---- Input Sampling Shift Register ----
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
shift_reg <= {FILTER_BITS{1'b0}};
else
shift_reg <= {shift_reg[FILTER_BITS-2:0], btn_raw};
end
// ---- Filtered Output Register ----
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
btn_clean <= 1'b0;
else if (all_ones)
btn_clean <= 1'b1;
else if (all_zeros)
btn_clean <= 1'b0;
end
endmodule
`default_nettype wire
Practice Exercise
Identify the style and syntax errors in the following module and rewrite it to adhere to professional RTL standards:
module bad_style(input clk, input reset, input [7:0] D, output reg [7:0] Q);
always @(posedge clk) begin
if(reset) Q <= 0;
else Q <= D;
end
endmodule
Solution
Defects in original code:
- Missing
`timescaleand`default_nettype nonedirectives. - Port declarations omit net types (
wire). - Reset polarity lacks standard
_nnaming. - Literal
0is an unsized 32-bit integer. - Port names use uppercase characters (
D,Q). - Code lacks column alignment and standard indentation.
Standardized RTL implementation:
// =============================================================================
// Module: dff_byte
// Description: 8-bit register with asynchronous active-low reset
// =============================================================================
`timescale 1ns / 1ps
`default_nettype none
module dff_byte (
input wire clk,
input wire rst_n,
input wire [7:0] d,
output reg [7:0] q
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
q <= 8'h00;
else
q <= d;
end
endmodule
`default_nettype wire
This article completes the Verilog language basics. Section 4.3 examines Verilog operators, arithmetic logic synthesis, and silicon gate utilization.