firmwareMIT

Counter in Verilog

breadboardhub/counter-in-verilog
verilogtestbenchcounter
ShareXLinkedInFacebookRedditWhatsApp
B
Mike M
v1.0.0 · UpdatedNov 24, 2026 ·0 downloads ·0 forks
Verilog, with a testOpen in the chip playground
// 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}
}