Building and Simulating a Binary Up-Down Counter Using Verilog/VHDL and Xilinx


Introduction
Binary counters are an essential component in digital circuits, used in various applications such as timers, frequency dividers, and sequential logic designs. A Binary Up-Down Counter is a type of counter that can increment (count up) or decrement (count down) its value based on a control input.
In this blog, we will design a Binary Up-Down Counter using Verilog and VHDL, simulate it in Xilinx Vivado/ISE, and analyze its working using a waveform viewer. If you're new to FPGA design, this tutorial will give you hands-on experience with designing and simulating sequential circuits.
How a Binary Up-Down Counter Works
A Binary Up-Down Counter follows these basic principles:
It counts up when a control signal (UP/DOWN) is HIGH (1).
It counts down when the control signal is LOW (0).
The counter wraps around when it reaches its maximum or minimum value.
Truth Table and State Transition
UP/DOWN | Previous State | Next State |
1 | 0001 [1] | 0010 [2] |
1 | 0010 [2] | 0011 [3] |
0 | 0011 [3] | 0010 [2] |
0 | 0010 [2] | 0001 [1] |
This logic helps the counter operate bidirectionally, making it useful for applications like digital clocks, event counters, and shift registers.
Choosing Verilog/VHDL
Key Differences Between Verilog and VHDL
Feature | Verilog | VHDL |
Syntax | Similar to C | Similar to Ada/Pascal |
Usage | Popular in industry | Preferred in academia |
Readability | Concise and compact | More verbose and strict |
Both languages are widely used in FPGA design, and in this blog, we will provide implementations in both Verilog and VHDL.
Why Use Xilinx for FPGA Implementation?
Xilinx provides industry-standard FPGA design tools like:
✅ Xilinx ISE (for older devices)
✅ Xilinx Vivado (for modern FPGAs)
✅ Built-in Simulation & Debugging Tools
Using Xilinx, we can design, simulate, and even implement our Binary Up-Down Counter on an FPGA board like the Xilinx Spartan-7 or Zynq-7000.
Writing the Code
Verilog Implementation of Binary Up-Down Counter
module up_down_counter (
input clk,
input reset,
input up_down, // 1 for Up, 0 for Down
output reg [3:0] count
);
always @(posedge clk or posedge reset) begin
if (reset)
count <= 4'b0000; // Reset counter to 0
else if (up_down)
count <= count + 1; // Count up
else
count <= count - 1; // Count down
end
endmodule
VHDL Implementation of Binary Up-Down Counter
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity up_down_counter is
Port ( clk : in STD_LOGIC;
reset : in STD_LOGIC;
up_down : in STD_LOGIC;
count : out STD_LOGIC_VECTOR (3 downto 0));
end up_down_counter;
architecture Behavioral of up_down_counter is
signal temp_count : STD_LOGIC_VECTOR (3 downto 0) := "0000";
begin
process (clk, reset)
begin
if reset = '1' then
temp_count <= "0000";
elsif rising_edge(clk) then
if up_down = '1' then
temp_count <= temp_count + 1;
else
temp_count <= temp_count - 1;
end if;
end if;
end process;
count <= temp_count;
end Behavioral;
Simulation and Testing in Xilinx
Setting Up the Project in Xilinx ISE/Vivado
Open Xilinx Vivado/ISE and create a new project.
Choose the FPGA board (e.g., Spartan-7, Artix-7, Zynq-7000).
Add the Verilog/VHDL source file.
Write the testbench to simulate the design.
Running Simulations and Observing Waveforms
Open the simulation tool and apply test inputs for clk, reset, and up_down.
Run the simulation and check the waveform output.
Ensure that the counter increments when UP/DOWN = 1 and decrements when UP/DOWN = 0.
Hardware Implementation (Optional)
Connect an FPGA board (e.g., Basys3 or Spartan-7) to your PC.
Load the .bit file onto the FPGA using Xilinx tools.
Use switches to control UP/DOWN counting and view the result on 7-segment displays or LEDs.
Troubleshooting and Optimizations
Common Errors and Fixes:
🔴 Counter not resetting? → Ensure reset
is properly initialized.
🔴 Counting direction incorrect? → Check up_down
logic.
🔴 Glitches in simulation? → Add debouncing for switch inputs.
Improving Efficiency:
Use synchronous reset for better clock management.
Add Enable signals for controlled counting.
Implement BCD Counters for decimal representation.
What’s Next?
Try modifying the counter to 8-bit or 16-bit.
Add a Synchronous Enable signal.
Implement a BCD Counter instead of Binary Counter.
GitHub Repository
🔗 Click Here for the Full Code and Simulations
Resources & References
Verilog Language Reference Manual – Download PDF
VHDL Standard Reference Manual – Download PDF
FPGA-Based Up-Down Counter Tutorial – FPGA4Student
Binary Counter Simulation Guide – EDA Playground
Circuit Design & Implementation – Fritzing
Have fun building and creating! :)
Subscribe to my newsletter
Read articles from Ayushi Lathiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
