Here is a well-structured SystemVerilog implementation of a 4-bit counter that increments on each rising edge of the clock. The counter includes a synchronous active-high reset to initialize the count to 0 when asserted.
✅ 4-Bit Counter in SystemVerilog
systemverilog
module counter_4bit (
input clk, // Clock input
input reset, // Active-high reset
output reg [3:0] count // 4-bit output register
);
// Sequential logic block: triggered on the positive edge of the clock
always_ff @(posedge clk) begin
if (reset) begin
count <= 4'b0000; // Reset the counter to 0
end else begin
count <= count + 1; // Increment the counter
end
end
endmodule
🔍 Explanation
Inputs:
clk: Clock signal. The counter updates on the rising edge.
reset: Active-high signal. When reset == 1'b1, the counter is reset to 0.
Output:
count: A 4-bit register that holds the current count value (from 0000 to 1111).
Behavior:
On each clock cycle:
If reset is high, the counter is set to 0.
Otherwise, it increments by 1.
The counter wraps around from 1111 (15) back to 0000 (0) due to the 4-bit width.
🧪 Example Usage (Testbench)
You can test this counter using a simple testbench like the following:
systemverilog
module tb_counter_4bit;
reg clk;
reg reset;
wire [3:0] count;
// Instantiate the counter
counter_4bit uut (
.clk(clk),
.reset(reset),
.count(count)
);
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk; // 10ns period
end
// Test sequence
initial begin
reset = 1;
#10;
reset = 0;
#50; // Let the counter run for 5 clock cycles
$stop;
end
// Monitor the count
initial begin
$monitor("Time=%0t | count = %0b", $time, count);
end
endmodule
📌 Notes
This implementation assumes a synchronous reset, meaning the reset is only active during a clock edge.
If you need an asynchronous reset, you can modify the always block to include the reset in the sensitivity list, but that is generally less common for standard counters.
The counter uses non-blocking assignments (<=) to ensure correct sequential behavior.
This counter is suitable for applications such as timing, state machines, or counting events in digital circuits.