The Wayback Machine - https://web.archive.org/web/20241123000318/https://www.geeksforgeeks.org/counter-design-using-verilog-hdl/
Open In App

Counter Design using verilog HDL

Last Updated : 09 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Previous Article
Next Article

Similar Reads

4-bit Ripple Carry Counter in Verilog HDL
To proceed with Verilog Code, we shall first understand the structure of the 4-bit Ripple Counter. The top design block consists of four T-Flip Flop. For time being ignore the input and output of T-Flip Flop. Let us consider the overall outside structure of Ripple Counter. We have two inputs i.e., clock and reset and q is output. The output q is in
3 min read
Multiplexer Design using Verilog HDL
The Verilog Hardware Description Language is an important language in the world of digital design where electronic systems are modeled using it. It provides a way of describing and simulating both the behavior and structure of digital circuits; hence, it is an important tool for engineers in hardware design and verification. Verilog HDL provides fo
7 min read
Full Adder using Verilog HDL
A full adder is a digital circuit in Verilog HDL that adds three binary numbers. It has two inputs for the numbers to be added, A and B, and one Carry-In input, Cin. The outputs are Sum, S, and Carry-Out, Cout. A full adder has been implemented using Verilog as shown in the previously shared code snippet. Problem StatementWrite a Verilog HDL to des
5 min read
2 to 4 Decoder in Verilog HDL
In this article, we will implement the 2:4 Decoder using all levels of abstraction in Verilog HDL with a step-by-step procedure. Before proceeding to code we shall look into the truth table and logic symbol of the 2:4 Decoder. 2:4 Decoder A decoder is a combinational logic circuit that has ‘n’ input signal lines and 2n output lines. In the 2:4 deco
5 min read
Difference between Straight Ring Counter and Twisted Ring Counter
Ring counters, essential in digital circuits, come in two main types: Straight and Twisted Ring Counters. Therefore differ with respect to their functionality and usage in the sequential logic design. It is essential to understand what each of them entails in order to select the appropriate one when designing systems for instance state machines or
4 min read
HDL Model of Combinational Circuits
Any language from a class of computer languages and/or programming languages used for the formal description of digital logic and electronic circuits is known as a hardware description language, or HDL, in the field of electronics. To create executable hardware specifications, HDLs are employed. The capacity to simulate a piece of hardware before i
4 min read
HDL Model of Sequential Circuits
Digital electronics is the study of electronic circuits that are used to process digital signals. In this article, we will focus on the HDL model of sequential circuits. Sequential circuits are those in which the output of the circuit depends on the current input and the previous inputs. HDL is a hardware description language that can be used to de
5 min read
Half Adder Using Verilog
A Half Adder is probably one of the simplest digital circuits you can use in addition of two single-bit binary numbers. The circuit has two inputs: the bits A and B, and two outputs: Sum (S) and Carry (C). The sum represents the addition of the two bits; carry shows the overflow into the next higher bit. The Half Adder used XOR gate for Sum and AND
6 min read
Verilog Data Types
The data storage and transmission elements found in digital hardware are represented using a set of Verilog Hardware Description Language (HDL) data types. The purpose of Verilog HDL is to design digital hardware. Data types in Verilog are divided into NETS and Registers. These data types differ in the way that they are assigned and hold values, an
2 min read
Verilog Priority Encoder
In Digital System Circuit, an Encoder is a combinational circuit that takes 2n  input signal lines and encodes them into n output signal lines. When the enable is true i.e., the corresponding input signal lines display the equivalent binary bit output.  What is Priority Encoder?A Priority Encoder is a digital circuit which encodes the input signals
8 min read
Difference between Verilog and SystemVerilog
A Verilog and a System Verilog are hardware description languages used in the digital circuit design. Verilog is developed earlier focuses on a basic circuit description. A System Verilog is an extension of the Verilog offering additional features for the advanced design and verification including the enhanced data types, object-oriented programmin
6 min read
Design Mod - N synchronous Counter
The value of N can be different from power of 2. Also, the counting sequence may be random for example some cyclic code (8421, 2423 etc). The following method is applied for designing for mod N and any counting sequence. Design for Mod-N counter :The steps for the design are - Step 1 : Decision for number of flip-flops -Example : If we are designin
3 min read
Design asynchronous Up/Down counter
Asynchronous counters are a kind of digital circuitry which may count either upwards or downwards while not adhering to the timing pulses from an oscillator. As a result, state transitions are determined by extra inputs rather than by time frames at fixed intervals. The flip-flops and logic gates are utilized for designing the asynchronous counter
9 min read
Design counter for given sequence
A Counter is a device which stores (and sometimes displays) the number of times a particular event or process has occurred, often in relationship to a clock signal. Counters are used in digital electronics for counting purpose, they can count specific event happening in the circuit. For example, in UP counter a counter increases count for every ris
3 min read
Control Signals Generation using Counter
In various digital applications(For example : hardwired control unit) control signals are needed to start, execute and step various operations in a particular time sequence.For this control signals are required and for the generation of control signals, a counter circuit is designed whose outputs are connected to a decoder. The decoder provides the
2 min read
Amortized analysis for increment in counter
Amortized analysis refers to determining the time-averaged running time for a sequence (not an individual) operation. It is different from average case analysis because here, we don't assume that the data arranged in average (not very bad) fashion like we do for average case analysis for quick sort. That is, amortized analysis is worst case analysi
4 min read
Ring Counter in Digital Logic
A ring counter is a typical application of the Shift register. The ring counter is almost the same as the shift counter. The only change is that the output of the last flip-flop is connected to the input of the first flip-flop in the case of the ring counter but in the case of the shift register it is taken as output. Except for this, all the other
3 min read
Differences between Synchronous and Asynchronous Counter
There are two types of counters in digital logic circuit that are used to count the numbers of bits and these types depends upon the clock pulse applied to the flip flops. 1. Asynchronous Counter: These are the counters in which we do not use universal clock, main clock is only applied to the first flip flop and then for rest of flip flops the outp
2 min read
Mod 6 Johnson Counter (with D flip-flop)
Johnson counters are one of the most important applications of shift registers. They are created by connecting multiple flip-flops to one another (such that the output of one flip-flop is the input for another), and by connecting the complement of the output of the last flip-flop to the input of the first flip-flop. For a mod 6 Johnson counter, 3 f
1 min read
Mod 2 Ring Counter (with D flip-flop)
Ring counters are one of the most important applications of shift registers. They are created by connecting multiple flip-flops to one another (such that the output of one flip-flop is the input for another), and by connecting the output of the last flip-flop to the input of the first flip-flop. For a mod 2-ring counter, two flip-flops will be requ
1 min read
Non-binary Counter in Digital Logic
A counter is a circuit that counts the number of occurrences of an input. The circuit consists of flip-flops which along with combinational elements are used for generation of control signals. If M = Total number of states, and n = Total number of flip-flop Then, M <= 2n If M = 2n ; Binary counter and M < 2n ; Non-binary counter Block Diagram
2 min read
Synchronous Series Carry Counter
Synchronous Series Carry Counter is such a synchronous counter where inputs of flip-flops are connected in such a manner that only those flip-flops that are toggle on a given clock will have input value as the logic 1. The advantage of this counter is that it reduces the decoding error. Block Diagram of Synchronous Series Carry Counter: Here is the
2 min read
Self Starting Counter
A counter is a device which stores the number of times a particular event or process has occurred (according to Wikipedia). As the name suggests, a counter is a device which is used for "counting" - mostly in relation to a clock signal. A counter is said to be self-starting if it is possible to enter a counter loop irrespective of the initial state
2 min read
Free Running Counter
Counter is a device which stores the number of times a particular event or process has occurred. As the name suggests, a counter is a device which is used for "counting" - mostly in relation to a clock signal. A counter is said to be free-running if it contains all possible states in the counter loop. The longest loop in the sequence of numbers gen
2 min read
Synchronous Parallel-Carry Binary Counter
Synchronous Counter: It is a digital circuit that performs counting in binary numbers with the help of flip-flops and all flip-flops triggered simultaneously. Synchronous Counter design procedure for a given counting sequence: Identify the numbers of flip-flops(FFs), inputs, and outputs required for the count sequence.Select the type of FF going to
5 min read
3 bit Synchronous Down Counter
Prerequisite : Counter , Synchronous counter. 3 bit Synchronous Down Counter : In synchronous counter clock is provided to all the flip-flops simultaneously.Circuit becomes complex as the number of states increases.Speed is high. Design : The steps involves in design are 1. Decide the number of Flip flops - N number of Flip flop(FF) required for N
2 min read
Synchronous Controlled Counter
In this article, we will discuss the overview of the Synchronous controlled counter and will discuss its circuit diagram, circuit excitation table, timing diagram in detail. Let's discuss it one by one. Overview :Toggle or D-type flip-flops can be used to make synchronous counters, and It is easy to design than asynchronous counters. Due to the clo
5 min read
Synchronous 3 bit Up/Down counter
Prerequisite : 3 bit down counter. Overview: These types of counters fall under the category of synchronous controller counter.Here the mode control input is used to decide whether which sequence will be generated by the counter.In this case, mode control input is used to decide whether the counter will perform up counting or down counting.Designin
3 min read
Ripple Counter in Digital Logic
Counters play a crucial role in digital logic circuits, enabling tasks such as clock frequency division and sequencing. This article explores the concept of ripple counters, a type of asynchronous counter, their operation, advantages, and disadvantages in digital logic design. What is a Counter?Counter is basically used to count the number of clock
5 min read
Asynchronous Down Counter
In asynchronous/ripple counter output of the first flip-flop is provided as the clock to the second flip-flop i.e flip-flop(FF) are not clocked simultaneously. Circuit is simpler, but speed is slow. What is Asynchronous Counter?An offbeat counter, otherwise called a wave counter, is a computerized counter where going back and forth is associated wi
7 min read
Article Tags :
three90RightbarBannerImg