// A 4-bit counter. On each rising clock edge it adds 1,
// wrapping from 15 back to 0. rst (synchronous) sets it to 0.
module counter (
input wire clk,
input wire rst,
output reg [3:0] count
);
always @(posedge clk) begin
if (rst)
count <= 4'd0;
else
count <= count + 4'd1;
end
endmodule
Description
A 4-bit counter that wraps from 15 to 0. It comes with a test that builds with Yosys and runs in the browser. count starts unknown (x), like a real register at power-on. Set rst to 1 and press Step clock to clear it, set rst back to 0, then keep stepping.
Cite this design
@design{breadboardhub/counter-in-verilog,
title = {Counter in Verilog},
author = {breadboardhub},
url = {https://www.breadboardhub.com/breadboardhub/counter-in-verilog},
version = {1.0.0}
}