Designing a counter in Verilog means creating a circuit that changes its value (either increasing or decreasing) with each clock pulse.
What is Counter?
A counter is a logic circuit that counts the incidences of particular events; it is identified traditionally by the frequencies of a clock pulse. Counters are employed in digital electronics in order to count the number of clocks which correspond to time frames, occurrences or particular operations. Counters are primitive blocks used in many systems and subsystems like digital clocks, timers, frequency counters, and memory address counters.
Counters can operate in different modes, primarily categorized into:
- Up Counters: Increase the count value +1 for each of the clock pulse.
- Down Counters: Subtract one from the count value with every clock pulse.
- Up-Down Counters: In this, it can add or subtract with the help of an additional control signal which is unusual.
What is Verilog?
Verilog is an HDL used in describing the electronic systems to work on. Digital circuits and systems are the most common areas that apply this graphical language. Verilog permits engineers to describe the organization and operation of working and electrical circuits naturally using textual language that could later get compiled to the hardware or simulated on the computers. It supports one or multi-dimensional abstraction that ranges from high abstraction level behavioral descriptions to the lower abstraction level- gate level implementations. Synthesis and testing of digital systems like processors, memory modules and other kinds of integrated circuits can be achieved through Verilog.
Problem
Design 4 bit up-down synchronous counter Verilog Hardware Description Language along with Testbench
Design Specification

Block diagram of design requirements : 4-bit counter
The counter is a digital sequential circuit and here it is a 4 bit counter, which simply means it can count from 0 to 15 and vice versa based upon the direction of counting (up/down).
- The counter (“count“) value will be evaluated at every positive (rising) edge of the clock (“clk“) cycle.
- The Counter will be set to Zero when “reset” input is at logic high.
- The counter will be loaded with “data” input when the “load” signal is at logic high. Otherwise, it will count up or down.
- The counter will count up when the “up_down” signal is logic high, otherwise count down.
Verilog HDL Code :
Design –
// Here we will learn to write a verilog HDL to design a 4 bit counter
module counter(clk,reset,up_down,load,data,count);
//define input and output ports
input clk,reset,load,up_down;
input [3:0] data;
output reg [3:0] count;
//always block will be executed at each and every positive edge of the clock
always@(posedge clk)
begin
if(reset) //Set Counter to Zero
count <= 0;
else if(load) //load the counter with data value
count <= data;
else if(up_down) //count up
count <= count + 1;
else //count down
count <= count - 1;
end
endmodule :counter
Testbench :
// Code your testbench here
module counter_tb;
reg clk,reset,load,ud;
reg [3:0] data;
wire [3:0] count;
// instance counter design
counter ct_1(.ud(up_down),.*);
//clock generator
initial begin clk = 1'b0; repeat(30) #3 clk= ~clk;end
//insert all the input signal
initial begin reset=1'b1;#7 reset=1'b0; #35 reset=1'b1;end
initial begin #12 load=1'b1; #5 load=1'b0;end
initial begin #5 ud=1'b1;#24 ud=1'b0;end
initial begin data=4'b1000;#14 data=4'b1101;#2 data=4'b1111;end
//monitor all the input and output ports at times when any inputs changes its state
initial begin $monitor("time=%0d,reset=%b,load=%b,ud=%b,data=%d,count=%d",
$time,reset,load,ud,data,count);end
endmodule :counter_tb
Expected Output :
time=0,reset=1,load=x,ud=x,data= 8,count= x
time=3,reset=1,load=x,ud=x,data= 8,count= 0
time=5,reset=1,load=x,ud=1,data= 8,count= 0
time=7,reset=0,load=x,ud=1,data= 8,count= 0
time=9,reset=0,load=x,ud=1,data= 8,count= 1
time=12,reset=0,load=1,ud=1,data= 8,count= 1
time=14,reset=0,load=1,ud=1,data=13,count= 1
time=15,reset=0,load=1,ud=1,data=13,count=13
time=16,reset=0,load=1,ud=1,data=15,count=13
time=17,reset=0,load=0,ud=1,data=15,count=13
time=21,reset=0,load=0,ud=1,data=15,count=14
time=27,reset=0,load=0,ud=1,data=15,count=15
time=29,reset=0,load=0,ud=0,data=15,count=15
time=33,reset=0,load=0,ud=0,data=15,count=14
time=39,reset=0,load=0,ud=0,data=15,count=13
time=42,reset=1,load=0,ud=0,data=15,count=13
time=45,reset=1,load=0,ud=0,data=15,count= 0
Note : Follow this link to online simulate this design.
Advantages of Counters design using Verilog HDL
- High-Level Abstraction: Verilog as a hardware description language offers an above register transfer level abstraction of the hardware designs which makes it easier to model complex circuits.
- Modularity: Similar to any other software solution, design can be divided into modules and it is easier to test them and reuse them again.
- Simulation and Verification: Simulation and verification in Verilog is greatly supported thus enabling the detection of errors and to ensure that the designed hardware works as intended.
- Synthesis Support: It can be synthesizable into gate level representations so that it is useful for generation of actual hardware different from higher level descriptions.
- Industry Standard: Verilog is most widely recognized as well as employed in the industry to support communication and interconnection with other designs, tools and systems.
Disadvantages of Counters design using Verilog HDL
- Learning Curve: Verilog is relatively difficult when it comes to writing and understanding the code because this language has a steep learning curve.
- Design Complexity: Simplification of such complex and bigger patterns becomes very cumbersome and perplexing when there are no annotations or maintainable plans to facilitate their identification and rectification.
- Hardware-Specific Issues: They may cause difference in simulation results with the real hardware due to variation in the tools used in the synthesis and actual implementation.
- Limited High-Level Features: Verilog is very good at describing hardware, it may lack some high level constructs of other programming languages, thereby resulting in verbose code.
- Tool Dependence: The synthesis and simulation can in turn depend on the tools that are applied in the development and these tools can differ in capabilities and functionality.
Conclusion
In this article, the design and implementation of a 4-bit up-down synchronous counter using Verilog HDL was covered. It also gave information about the counters, description of the Verilog code and pros and cons of adopting Verilog HDL for counters. This is a short and easy-to-read guide that helps the readers to understand the basic principles of designing digital counters and implement them in various projects.
Frequently Asked Questions on Counter Design using Verilog HDL- FAQs
Is it possible to perform analog circuit design by using Verilog?
Verilog is mainly used for description of digital circuits. For analog circuits Verilog-A or other tools are used much frequently, in many cases Verilog-A is used.
What is the difference between a synchronous and an asynchronous counter?
Synchronous counter counts by the same clock signal while an asynchronous counter count independently and may lag behind.
Can the simulation of Verilog code be done without having to use hardware?
Indeed, it is possible to simulate Verilog code by using appropriate software tools to ensure the adequacy of the design before it is actually turned into hardware.