0
0
VHDLprogramming~5 mins

Why testbenches are needed in VHDL

Choose your learning style9 modes available
Introduction

Testbenches help check if your VHDL design works correctly before using it in real hardware.

When you want to make sure your digital circuit behaves as expected.
Before sending your design to be made into a chip or FPGA.
To find and fix mistakes early in your design process.
When you want to try different input signals and see the outputs.
To automate testing so you don't have to check outputs by hand.
Syntax
VHDL
entity testbench is
end testbench;

architecture behavior of testbench is
  -- component declaration
  -- signal declarations
begin
  -- instantiate the design under test
  -- apply test signals
end behavior;

A testbench is a VHDL file that does not have inputs or outputs.

It creates signals and applies them to your design to check outputs.

Examples
This testbench checks an AND gate with all input combinations.
VHDL
library ieee;
use ieee.std_logic_1164.all;

entity tb_and_gate is
end tb_and_gate;

architecture behavior of tb_and_gate is
  signal a, b, c : std_logic;
begin
  uut: entity work.and_gate port map(a => a, b => b, c => c);

  process
  begin
    a <= '0'; b <= '0'; wait for 10 ns;
    a <= '0'; b <= '1'; wait for 10 ns;
    a <= '1'; b <= '0'; wait for 10 ns;
    a <= '1'; b <= '1'; wait for 10 ns;
    wait;
  end process;
end behavior;
This testbench applies clock and data signals to a flip-flop to test its behavior.
VHDL
library ieee;
use ieee.std_logic_1164.all;

entity tb_flipflop is
end tb_flipflop;

architecture behavior of tb_flipflop is
  signal clk, d, q : std_logic := '0';
begin
  uut: entity work.flipflop port map(clk => clk, d => d, q => q);

  clk_process : process
  begin
    clk <= '0'; wait for 5 ns;
    clk <= '1'; wait for 5 ns;
  end process;

  stim_proc: process
  begin
    d <= '0'; wait for 10 ns;
    d <= '1'; wait for 10 ns;
    wait;
  end process;
end behavior;
Sample Program

This testbench tests a NOT gate by applying 0 and 1 to input 'a' and reporting output 'b'.

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity tb_not_gate is
end tb_not_gate;

architecture behavior of tb_not_gate is
  signal a, b : std_logic := '0';
begin
  uut: entity work.not_gate port map(a => a, b => b);

  stim_proc: process
  begin
    a <= '0'; wait for 10 ns;
    report "Input a=0, Output b=" & std_logic'image(b);
    a <= '1'; wait for 10 ns;
    report "Input a=1, Output b=" & std_logic'image(b);
    wait;
  end process;
end behavior;
OutputSuccess
Important Notes

Testbenches do not get synthesized into hardware; they are only for simulation.

Use wait statements to control timing of inputs in testbenches.

Reports or assertions help check if outputs are correct during simulation.

Summary

Testbenches let you check your VHDL design works before building hardware.

They apply inputs and observe outputs automatically in simulation.

Using testbenches helps find mistakes early and saves time and cost.