top of page

Binary Counter

Introduction

Counters are an essential component of digital circuits. They are used to count events, keep track of time, and generate signals of various frequencies. In this tutorial, we will learn how to create a simple counter program in Verilog and how to create a testbench to test it.

​

Design of the Counter

We will create a simple 4-bit counter that counts from 0 to 15 and then resets back to 0. The counter will increment on the positive edge of a clock signal, and it will have a synchronous reset signal that will reset the counter to 0 when it is asserted. Here is the Verilog code for the counter module:

​

In this module, we declare an input clk that represents the clock signal, an input reset that represents the reset signal, and an output count that represents the current count value. We use an always block to define the behavior of the counter. The always block is triggered on the positive edge of the clk input. If the reset signal is asserted, the counter is reset to 0. Otherwise, the counter is incremented by 1.

Creating the Testbench

To test the counter, we will create a testbench that generates a clock signal and a reset signal and checks the output of the counter. Here is the Verilog code for the testbench:

In this testbench, we declare the input clk and the input reset and the output count. We instantiate the counter module and connect its inputs and outputs to the testbench signals. We use an always block to generate a clock signal with a period of 10ns. We use an initial block to reset the counter after 10ns and to check the output of the counter after each 10ns period.

We are continuously looking for improvising this website. Hence, for any suggestions, any doubts/queries, please feel free to write to us at info@allaboutvlsi.com

To view this website on edaplayground: https://edaplayground.com/x/LLZr

​

Screenshot 2023-06-08 9.17.48 AM.png
bottom of page