Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Testbenches

Your First VHDL Testbench

Learn what a VHDL testbench is: an entity with no ports, a device under test, signals, stimulus and a simulation clock, with a runnable C model of simulated time.

Hardware cannot be debugged by printing in the middle of a wire. Instead you simulate the design: you drive its inputs with a program, advance simulated time and look at the outputs. The program that does this is called a testbench, and it is written in VHDL too. A testbench is not synthesized; it never becomes hardware, so it can use every feature of the language, including wait, text output and file I/O.

No VHDL simulator (GHDL, ModelSim, Vivado) is available on this site. VHDL is therefore shown read-only, and runnable C programs model the ideas, so you can still experiment with simulated time and checking.

The shape of a testbench

A testbench is an entity with no ports. Its architecture declares signals, plugs in the device under test (DUT) and runs processes that create stimulus.

VHDL
library ieee;
use ieee.std_logic_1164.all;

entity half_adder_tb is
end entity;

architecture sim of half_adder_tb is
  signal a, b, sum, carry : std_logic := '0';
begin

  dut : entity work.half_adder
    port map (a => a, b => b, sum => sum, carry => carry);

  stimulus : 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;   -- stop this process forever
  end process;

end architecture;

The DUT in this example is a half adder from an earlier chapter of the track (sum <= a xor b; carry <= a and b;).

What each part does

  • Empty entity. A testbench has no inputs or outputs: it is the whole world.
  • Signals with initial values. The testbench owns the wires that connect to the DUT. Initial values (:= '0') avoid the unknown value 'U' at time zero.
  • Direct instantiation. entity work.half_adder creates the DUT without a component declaration (VHDL-93 and later). Named association (a => a) is safer than positional.
  • Stimulus process. A process without a sensitivity list that contains wait statements. It runs top to bottom, pauses at each wait for, and then continues.
  • Final wait;. Without it the process would restart from the top forever and the simulation would never end on its own.

Simulated time and delta cycles

Simulation does not run in real time. The simulator keeps a time-ordered event queue. When a process says wait for 10 ns, it is put to sleep until time advances by 10 ns. Signal assignments such as a <= '1' do not take effect immediately: they are scheduled for the next delta cycle, an infinitesimal step at the same simulation time. After every process has run, the simulator applies the scheduled updates, wakes any process sensitive to a changed signal, and repeats until nothing changes. Only then does simulated time move forward.

That is why code after a <= '1' still sees the old a until the process next waits.

flowchart TD
  A["Run all awake processes"] --> B["Apply scheduled signal updates"]
  B --> C{"Did any signal change?"}
  C -->|"yes: next delta cycle"| A
  C -->|"no"| D["Advance time to next event"]
  D --> A

The simulation loop, simplified.

A C model of the loop

The program below is a toy simulator. It has an event list for the inputs a and b, applies them as time passes, recomputes the half adder, and prints a trace like a simulator log. It demonstrates two ideas: time advances in jumps, and outputs follow inputs after the update.

C C stand-in: stepping simulated time through a half adder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Watching the result

A simulator shows signals as a waveform: time on the horizontal axis, one line per signal. A passing half-adder run shows sum high only when exactly one input is high, and carry high only when both are. Reading waveforms is the first debugging skill; in the next lesson you also make the testbench check the values itself.

Running a testbench with GHDL (read-only)

On your own computer the open-source simulator GHDL is the easiest start:

TEXT
ghdl -a half_adder.vhd half_adder_tb.vhd      # analyze (compile)
ghdl -e half_adder_tb                          # elaborate
ghdl -r half_adder_tb --wave=out.ghw           # run, write a waveform file
gtkwave out.ghw                                # view it

Commercial tools (ModelSim/Questa, Vivado xsim, Riviera-PRO) do the same steps with different commands. The order matters: analyze files in dependency order (a package before the design that uses it, the DUT before the testbench).

Common beginner mistakes

  1. No final wait;: the stimulus loops forever, so the simulation never stops by itself.
  2. Forgetting initial values: signals begin as 'U', and the DUT's outputs stay 'U' or 'X' until driven.
  3. Expecting a signal assignment to be instant: read the value after a wait, not on the next line.
  4. Driving a signal from two processes without a resolution function: you get 'X' in the waveform.
  5. Using synthesizable style in the testbench: it is unnecessary; use wait for, assert and report freely.

A testbench that "runs without errors" proves nothing if it never checks values. The next lessons add automatic checking.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a testbench entity have no ports?

    The testbench is the top level of the simulation, so there is nothing to connect to.
  2. 2. What does a final 'wait;' in a stimulus process do?

    A process without sensitivity list loops forever unless it waits indefinitely at the end.
  3. 3. When does the assignment a <= '1' take effect?

    Signal assignments are scheduled; the new value appears after the processes suspend.
  4. 4. Which is NOT part of a typical testbench?

    Pin assignments belong to the synthesis and implementation flow, not to simulation.

Summary

  • A testbench is a port-less entity that instantiates the DUT and drives its inputs.
  • Stimulus processes use wait for to pass simulated time and end with wait;.
  • Signal updates happen in delta cycles; time only advances when nothing else changes.
  • Initialize signals to avoid 'U' at time zero.
  • Simulate with GHDL or a vendor tool; analyze files in dependency order.

Next: Stimulus, Wait and Assert