This program defines a 4-bit down counter starting at 15. The testbench toggles the clock and releases reset after 10 time units. The count decreases every clock cycle until it reaches zero.
module down_counter(
input wire clk,
input wire reset,
output reg [3:0] count
);
always @(posedge clk or posedge reset) begin
if (reset) begin
count <= 4'b1111; // start from 15
end else if (count > 0) begin
count <= count - 1;
end
end
endmodule
// Testbench
module testbench();
reg clk = 0;
reg reset = 1;
wire [3:0] count;
down_counter dc(.clk(clk), .reset(reset), .count(count));
always #5 clk = ~clk; // clock toggles every 5 time units
initial begin
$monitor($time, " Count = %d", count);
#10 reset = 0; // release reset after 10 time units
#150 $finish;
end
endmodule