Register Design Patterns
Digital designs use standard register configurations repeatedly. The six patterns in this article share a common reset-and-enable structure. They differ only in the combinational logic that computes the next state value.
Binary Up-Counter
An up-counter increments its state by one on every active clock edge when enabled:
module up_counter #(
parameter N = 8
) (
input wire clk, rst_n, en,
output reg [N-1:0] count
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n) count <= {N{1'b0}};
else if (en) count <= count + 1'b1;
end
endmodule
The condition count == {N{1'b1}} detects the terminal count before the counter overflows to zero.
Bidirectional Up/Down Counter
An up/down counter adds direction control:
always @(posedge clk or negedge rst_n) begin
if (!rst_n) count <= {N{1'b0}};
else if (en) begin
if (up) count <= count + 1'b1;
else count <= count - 1'b1;
end
end
The control signal up drives a multiplexer that selects between increment and decrement operations.
Modulo-N Counter
A modulo-N counter clears to zero when it reaches a configured threshold:
always @(posedge clk or negedge rst_n) begin
if (!rst_n) count <= {W{1'b0}};
else if (en) begin
if (count == M - 1) count <= {W{1'b0}};
else count <= count + 1'b1;
end
end
Parameter M sets the sequence length. Parameter W defines the register bit width ($W = \lceil \log_2 M \rceil$). The comparison checks for M - 1 so that the reset occurs after exactly $M$ clock cycles.
Serial-In Parallel-Out (SIPO) Shift Register
A SIPO register captures serial input data and presents parallel output words:
always @(posedge clk or negedge rst_n) begin
if (!rst_n) shift_reg <= {N{1'b0}};
else shift_reg <= {shift_reg[N-2:0], serial_in};
end
Concatenation shifts existing bits left by one position and inserts serial_in at bit index 0. After $N$ clock edges, the register contains the complete $N$-bit parallel word. Receiver circuits in UART and SPI interfaces use this configuration.
Parallel-In Serial-Out (PISO) Shift Register
A PISO register captures parallel data words and transmits individual bits serially:
always @(posedge clk) begin
if (load)
shift_reg <= data_in;
else if (shift_en)
shift_reg <= {shift_reg[N-2:0], 1'b0};
end
assign serial_out = shift_reg[N-1];
The load control signal samples the parallel bus. When shift_en asserts, the register shifts bits out through the most significant bit. Transmitter interfaces use this configuration.
Saturating Accumulator
Standard binary accumulators wrap to zero upon arithmetic overflow. In digital signal processing and audio systems, overflow wrapping creates audible distortion. A saturating accumulator clamps the output at maximum value:
wire [N:0] sum = {1'b0, acc} + {1'b0, data_in};
always @(posedge clk) begin
if (add_en)
acc <= sum[N] ? {N{1'b1}} : sum[N-1:0];
end
The addition uses an $(N+1)$-bit intermediate wire. Bit sum[N] represents the arithmetic carry-out. When carry asserts, the register clamps to {N{1'b1}}.
Verification Testbench for SIPO Register
The following testbench verifies the SIPO shift register:
`timescale 1ns / 1ps
`default_nettype none
module sipo_shift_reg #(
parameter N = 8
) (
input wire clk, rst_n, serial_in,
output reg [N-1:0] shift_reg
);
always @(posedge clk or negedge rst_n) begin
if (!rst_n) shift_reg <= {N{1'b0}};
else shift_reg <= {shift_reg[N-2:0], serial_in};
end
endmodule
module tb_sipo_shift_reg;
parameter N = 8;
reg clk = 0, rst_n, serial_in;
wire [N-1:0] shift_reg;
sipo_shift_reg #(.N(N)) dut (.clk(clk), .rst_n(rst_n), .serial_in(serial_in), .shift_reg(shift_reg));
always #5 clk = ~clk;
reg [N-1:0] test_byte = 8'b1011_0010;
integer i;
initial begin
$dumpfile("tb.vcd");
$dumpvars(0, tb_sipo_shift_reg);
rst_n = 0; serial_in = 0;
@(negedge clk); rst_n = 1;
for (i = N - 1; i >= 0; i = i - 1) begin // Drive MSB first
serial_in = test_byte[i];
@(negedge clk);
end
if (shift_reg !== test_byte)
$display("FAIL: shift_reg=%b (expected %b)", shift_reg, test_byte);
else
$display("PASS: shift_reg=%b after %0d cycles", shift_reg, N);
$finish;
end
endmodule
The test stimulus injects a known test byte across eight consecutive clock cycles. The testbench verifies that the parallel output matches the injected pattern.
These patterns form the foundation of sequential digital logic. The next article demonstrates how to arrange registers into high-throughput hardware pipelines.