Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Testbenches

Stimulus, Wait and Assert

Use wait for, wait until, wait on, assert and report with severity levels to drive inputs and check outputs automatically in a VHDL testbench.

A good testbench checks results itself and tells you what failed, so you never have to stare at waveforms for correct designs. This lesson covers the three tools for that: the wait statement in its several forms, the assert statement and the report statement. Real VHDL is shown read-only; C programs mimic the checking logic so you can run something.

The forms of wait

Statement Resumes when
wait for 10 ns; 10 ns of simulated time have passed
wait until clk = '1'; the condition becomes true (checked when a signal in it changes)
wait until rising_edge(clk); the clock has just risen
wait on a, b; any listed signal has an event
wait; never

wait until rising_edge(clk) is the workhorse for synchronous designs: the stimulus lines up with clock edges just like real hardware would. A common pattern is to change inputs just after the edge (for example, wait for 1 ns after the edge) so the DUT is never driven exactly at its sampling instant.

VHDL
-- Drive "enable" for three cycles, aligned to the clock
wait until rising_edge(clk);
enable <= '1';
for i in 1 to 3 loop
  wait until rising_edge(clk);
end loop;
enable <= '0';

assert: say what must be true

VHDL
assert sum = expected_sum
  report "sum wrong: got " & std_logic'image(sum) &
         " expected " & std_logic'image(expected_sum)
  severity error;

An assertion states a condition that should be true. If it is false, the simulator prints the report text with the severity and the simulation time. There are four severity levels:

Severity Meaning Default simulator reaction
note information continue
warning suspicious continue
error the design is wrong continue (most tools)
failure fatal, stop stop the simulation

Most simulators let you choose the level at which a run is aborted. assert false report "done" severity failure; was the traditional way to end a simulation, but in VHDL-2008 the cleaner way is std.env.stop; (or finish;).

report: plain messages

report without assert simply prints a message:

VHDL
report "starting test case " & integer'image(test_no);

Use integer'image(x), std_logic'image(s) or to_string(x) (VHDL-2008) to convert values to text, and & to concatenate.

Compare, do not eyeball

The usual structure is: set the inputs, wait for the logic to settle, compare the outputs with expected values.

VHDL
check : process
  type vec_t is record a, b, sum, carry : std_logic; end record;
  type vec_array is array (natural range <>) of vec_t;
  constant vectors : vec_array := (
    ('0','0','0','0'),
    ('0','1','1','0'),
    ('1','0','1','0'),
    ('1','1','0','1'));
begin
  for i in vectors'range loop
    a <= vectors(i).a;
    b <= vectors(i).b;
    wait for 10 ns;
    assert sum = vectors(i).sum and carry = vectors(i).carry
      report "vector " & integer'image(i) & " failed"
      severity error;
  end loop;
  report "half adder test finished";
  wait;
end process;

The table of vectors is data; the loop is code. Adding a case means adding one line.

A C model of the checking loop

C has assert too, but a testbench wants to count and report every failure rather than abort at the first. The program below uses a macro that behaves like VHDL's assert ... report ... severity error: it prints the message, counts the problem and keeps going. Run it, then change a vector to see a failure.

C C stand-in: table-driven check with severity counting

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.

Timing: when is it safe to look at outputs?

After a <= '1'; wait for 10 ns; the combinational logic has long settled in a zero-delay simulation, so the check is correct. For registered designs you must wait for a clock edge, and the output appears after the edge, so check one delta later or after a short wait for 1 ns. A test that checks at the exact edge often sees the old value, a source of many "off by one cycle" confusions.

C C stand-in: why a registered output shows the old value at the edge

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.

Learn the rule: compare registered outputs after the edge, with the inputs from the previous cycle.

Useful assertion habits

  • Write the message so it contains the inputs, actual and expected values.
  • Check only after the design has had time to respond (after reset, after the latency of a pipeline).
  • Keep a counter of errors in the testbench and finish with one summary line: "PASS" or "FAIL".
  • Use severity failure only when later testing is pointless (for example, the DUT never leaves reset).

Concurrent assertions (assert outside any process) check a condition whenever a signal in it changes. They are great for protocol rules such as "request is never high while reset is high". Full property languages (PSL, SVA in other languages) go beyond this track.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which wait resumes at the next rising clock edge?

    wait until with rising_edge pauses the process until the edge happens.
  2. 2. What does assert x = 5 report "bad" severity error; do when x is 3?

    An assertion fires when its condition is FALSE and prints the report string with the severity.
  3. 3. Which severity normally stops the simulation?

    failure is fatal in most simulators; error and warning let the run continue by default.
  4. 4. A flip-flop output checked exactly at the capturing clock edge usually shows

    The register updates in the delta cycle after the edge, so a check at the edge sees the previous value.

Summary

  • wait for, wait until and wait on suspend a process; a bare wait; ends it.
  • assert fires on a false condition; report prints text; severities are note, warning, error, failure.
  • Table-driven checks keep data separate from code.
  • Check registered outputs after the edge; include actual and expected values in messages.
  • End with a clear PASS/FAIL line.

Next: Clocks, Reset and Self-Checking Testbenches