Hierarchy and Generate
Digital systems organize hardware as a hierarchical tree. A top-level module instantiates major functional subsystems. Each subsystem instantiates smaller submodules down to fundamental gate primitives. This modular hierarchy isolates design complexity and enables independent module verification.
Benefits of Hierarchical Decomposition
Consider a 16-bit arithmetic logic unit (ALU). Describing the ALU as a single flat module requires hundreds of interconnected logic gates. In a flat netlist, debugging is difficult.
Hierarchical decomposition structures the ALU cleanly:
- Construct a half adder from two basic gates.
- Construct a 1-bit full adder from two half adders and an OR gate.
- Cascade four full adders into a 4-bit adder block.
- Cascade four 4-bit blocks into the complete 16-bit ALU datapath.
Engineers test each submodule independently. When an error occurs in the full adder, fixing the submodule corrects all instances throughout the hierarchy.
Explicit 4-Bit Ripple-Carry Adder
The following module interconnects four 1-bit full adder instances using named internal carry wires:
`timescale 1ns / 1ps
`default_nettype none
module rca_4bit (
input wire [3:0] a,
input wire [3:0] b,
input wire cin,
output wire [3:0] sum,
output wire cout
);
wire c1, c2, c3;
full_adder u_fa0 (.a(a[0]), .b(b[0]), .cin(cin), .sum(sum[0]), .cout(c1));
full_adder u_fa1 (.a(a[1]), .b(b[1]), .cin(c1), .sum(sum[1]), .cout(c2));
full_adder u_fa2 (.a(a[2]), .b(b[2]), .cin(c2), .sum(sum[2]), .cout(c3));
full_adder u_fa3 (.a(a[3]), .b(b[3]), .cin(c3), .sum(sum[3]), .cout(cout));
endmodule
Internal wires c1, c2, and c3 link the carry-out of each stage to the carry-in of the next stage.
Parameterized Generate Loops
Explicit manual instantiation becomes inefficient for wide data buses. The generate loop creates parameterized hardware structures during design elaboration:
`timescale 1ns / 1ps
`default_nettype none
module rca #(
parameter WIDTH = 4
)(
input wire [WIDTH-1:0] a,
input wire [WIDTH-1:0] b,
input wire cin,
output wire [WIDTH-1:0] sum,
output wire cout
);
wire [WIDTH:0] carries;
assign carries[0] = cin;
assign cout = carries[WIDTH];
genvar i;
generate
for (i = 0; i < WIDTH; i = i + 1) begin : gen_fa
full_adder u_fa (
.a (a[i]),
.b (b[i]),
.cin (carries[i]),
.sum (sum[i]),
.cout(carries[i+1])
);
end
endgenerate
endmodule
The genvar keyword declares an elaboration-time loop index. A genvar is not a physical register or net. During elaboration, the compiler unrolls the loop into WIDTH distinct instances.
The named block label gen_fa is mandatory. The compiler names individual instances systematically: gen_fa[0].u_fa, gen_fa[1].u_fa, and gen_fa[i].u_fa.
Conditional Elaboration with generate
Use conditional generate blocks (if-else or case) to select between different circuit architectures based on parameter values:
generate
if (FAST_ADDER) begin : gen_cla
cla_adder #(.WIDTH(WIDTH)) u_adder (
.a(a), .b(b), .cin(cin), .sum(sum), .cout(cout)
);
end else begin : gen_rca
rca #(.WIDTH(WIDTH)) u_adder (
.a(a), .b(b), .cin(cin), .sum(sum), .cout(cout)
);
end
endgenerate
The synthesis tool evaluates the parameter condition and compiles only the selected branch. Unselected branches generate zero hardware.
Hierarchical Signal Probing in Testbenches
Simulation testbenches can inspect internal signals using dot-separated hierarchical paths:
`timescale 1ns / 1ps
`default_nettype none
module tb_rca;
reg [3:0] a, b;
reg cin;
wire [3:0] sum;
wire cout;
rca #(.WIDTH(4)) u_dut (.a(a), .b(b), .cin(cin), .sum(sum), .cout(cout));
initial begin
$dumpfile("rca_waves.vcd");
$dumpvars(0, tb_rca);
a = 4'd5; b = 4'd3; cin = 1'b0; #20;
// Hierarchical path probing into internal DUT structures
$display("Carry into stage 2 = %b", tb_rca.u_dut.carries[2]);
$display("Input a of stage 2 adder = %b", tb_rca.u_dut.gen_fa[2].u_fa.a);
$finish;
end
endmodule
[!CAUTION] Use hierarchical references only in testbenches. Synthesizable RTL modules must never access external signals through hierarchical paths. Synthesis tools reject hierarchical path references in RTL.
Architectural Best Practices
- Single Responsibility: Dedicate each module to a single functional task (such as a FIFO buffer or a UART receiver).
- Module Sizing: Maintain module lengths under 300 lines of code. Split larger files into logical sub-modules.
- Thin Top-Level Routing: The top-level chip module should instantiate major functional blocks and route I/O pads without containing procedural logic.
module chip_top (
input wire clk, rst_n,
input wire rx_pin,
output wire tx_pin,
output wire [7:0] led
);
wire [7:0] rx_data;
wire rx_valid;
uart_rx u_rx (.clk(clk), .rst_n(rst_n), .rx(rx_pin), .data(rx_data), .valid(rx_valid));
led_ctrl u_led (.clk(clk), .rst_n(rst_n), .data(rx_data), .valid(rx_valid), .leds(led));
uart_tx u_tx (.clk(clk), .rst_n(rst_n), .data(rx_data), .start(rx_valid), .tx(tx_pin));
endmodule
Practice Exercise
Implement a 16-bit adder module rca_16bit by instantiating rca #(.WIDTH(16)). Then construct a 32-bit adder rca_32bit by cascading two rca_16bit instances.
Solution
`timescale 1ns / 1ps
`default_nettype none
module rca_16bit (
input wire [15:0] a, b,
input wire cin,
output wire [15:0] sum,
output wire cout
);
rca #(.WIDTH(16)) u_rca16 (
.a (a),
.b (b),
.cin (cin),
.sum (sum),
.cout(cout)
);
endmodule
`timescale 1ns / 1ps
`default_nettype none
module rca_32bit (
input wire [31:0] a, b,
input wire cin,
output wire [31:0] sum,
output wire cout
);
wire mid_carry;
rca_16bit u_low (
.a (a[15:0]),
.b (b[15:0]),
.cin (cin),
.sum (sum[15:0]),
.cout(mid_carry)
);
rca_16bit u_high (
.a (a[31:16]),
.b (b[31:16]),
.cin (mid_carry),
.sum (sum[31:16]),
.cout(cout)
);
endmodule
The internal net mid_carry connects the carry-out of the lower 16-bit slice to the carry-in of the upper 16-bit slice.
The final article in Section 4.4 presents a worked structural modeling example: constructing a complete 4-bit ALU from primitive gates.