Chuyển đến nội dung

Bài 26 · Testbenches

Clocks, Reset and Self-Checking Testbenches

Generate a clock, apply reset, drive a synchronous design and compare it with a reference model in a self-checking VHDL testbench that ends with PASS or FAIL.

Most real designs are synchronous: they need a clock and a reset before they do anything useful. In this lesson you build the standard testbench skeleton for such a design: a clock generator, a reset sequence, a stimulus process, a reference model and a checker that prints one PASS or FAIL line. The example device is a 4-bit counter with enable. VHDL appears read-only, with C stand-ins that run.

The DUT

VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity counter4 is
  port (clk, rst, en : in std_logic;
        q : out unsigned(3 downto 0));
end entity;

architecture rtl of counter4 is
  signal cnt : unsigned(3 downto 0);
begin
  process (clk)
  begin
    if rising_edge(clk) then
      if rst = '1' then cnt <= (others => '0');
      elsif en = '1' then cnt <= cnt + 1;
      end if;
    end if;
  end process;
  q <= cnt;
end architecture;

Clock generation

The simplest clock is a concurrent assignment with a delay:

VHDL
constant T_CLK : time := 10 ns;          -- 100 MHz
signal clk : std_logic := '0';
...
clk <= not clk after T_CLK / 2;

It toggles forever, so the simulation never runs out of events. You must therefore stop the simulation yourself. A more controllable form uses a process and a done flag:

VHDL
signal done : boolean := false;
...
clk_gen : process
begin
  while not done loop
    clk <= '0'; wait for T_CLK / 2;
    clk <= '1'; wait for T_CLK / 2;
  end loop;
  wait;
end process;

When done becomes true the clock stops, no events remain and the simulator ends cleanly. In VHDL-2008 you may instead call std.env.finish; from the stimulus process.

Reset sequence

VHDL
rst <= '1';
wait for 3 * T_CLK;        -- hold reset for a few cycles
wait until rising_edge(clk);
rst <= '0' after 1 ns;

Always start a test with reset. After reset, the first thing a checker should do is confirm the reset state (the counter reads 0).

A reference model and a checker

The reference model is a few lines of behavior, written the simplest way possible. It need not be synthesizable; it must be obviously correct. The testbench steps both with the same inputs and compares.

VHDL
check : process (clk)
  variable expected : unsigned(3 downto 0) := (others => '0');
begin
  if rising_edge(clk) then
    -- compare the DUT output (from before this edge) with the model
    if q /= expected then
      errors <= errors + 1;
      report "mismatch at " & time'image(now) severity error;
    end if;
    -- advance the model with this edge's inputs
    if rst = '1' then expected := (others => '0');
    elsif en = '1' then expected := expected + 1;
    end if;
  end if;
end process;

errors is a signal (or shared variable) counted across the test. At the end:

VHDL
if errors = 0 then report "TEST PASSED"; else report "TEST FAILED" severity failure; end if;

Because the checker reads q at the edge, it sees the value from before the edge, which is exactly what expected holds before it is updated. This is the same off-by-one idea you met in the previous lesson.

C model of the whole bench

The program below contains a DUT (a register updated at the "edge"), a reference model, a pseudo-random stimulus generator (a fixed seed so results repeat) and a checker. It also has a switch to inject a bug, so you can watch the bench catch it.

C C stand-in: self-checking counter bench with a bug switch

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.

The first run passes; the second reports mismatches as soon as the counter reaches the buggy value. A test that cannot fail is worthless: always inject a bug once to see that your checker catches it.

Random and directed tests

  • Directed tests hit specific corners: reset in the middle of counting, wrap-around from 15 to 0, enable toggling every cycle.
  • Random tests use a pseudo-random generator for many cycles and rely on the reference model. In VHDL use ieee.math_real.uniform with seed variables, or in VHDL-2008 the OSVVM or random packages. Always keep the seed fixed so a failure can be reproduced.

Mix both: directed for known corners, random for surprises.

C C stand-in: coverage of the wrap-around corner

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.

Even this crude counter shows what coverage means: knowing which corner cases your stimulus actually reached.

Finishing cleanly

  1. Set done <= true (or call std.env.finish).
  2. Print the summary line with the error count.
  3. Let the simulation end with no pending events. A run that must be killed by a time limit hides problems.

Put the reference model in a separate process or package. The day you change the DUT, the model stays the same, and any mismatch tells you the DUT changed behavior.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does the statement clk <= not clk after 5 ns; produce?

    Each change of clk schedules the next toggle 5 ns later, creating a free-running clock.
  2. 2. Why should every testbench start with a reset?

    Without reset the flip-flops hold 'U' and the outputs stay unknown.
  3. 3. Why use a fixed seed in random stimulus?

    The same seed gives the same stimulus, so any failure can be re-run and debugged.
  4. 4. How do you know a checker works?

    A checker proves its value only when it can fail. Inject a bug at least once.

Summary

  • Generate the clock with a delayed inverting assignment or a process with a done flag.
  • Reset first, then confirm the reset state.
  • A reference model plus a checker makes the bench self-checking; end with PASS or FAIL.
  • Mix directed tests for corners with seeded random tests for breadth.
  • Inject a bug once to prove the checker works.

Next: Procedures, Test Vectors and Debugging

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.