Selbstprüfenden VHDL-Testbench erstellen – Praxisleitfaden
Das Device Under Test (DUT)
Als Beispiel dient ein Binär-zu-Gray-Code-Konverter. Gray-Code ändert zwischen aufeinanderfolgenden Werten nur ein Bit – vorteilhaft für serielle Kommunikation und Zähler.
library ieee;
use ieee.std_logic_1164.all;
entity gray_converter is
port (
bin : in std_logic_vector;
gray : out std_logic_vector
);
end gray_converter;
architecture rtl of gray_converter is
begin
process(bin) is
begin
gray(gray'high) <= bin(bin'high);
for i in bin'high - 1 downto bin'low loop
gray(i) <= bin(i + 1) xor bin(i);
end loop;
end process;
end architecture;
4-Bit-Mapping-Tabelle:
| Dezimal | Binär | Gray |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Testbench-Gerüst
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use std.env.finish;
entity gray_converter_tb is
end gray_converter_tb;
architecture sim of gray_converter_tb is
signal bin : std_logic_vector(3 downto 0) := (others => '0');
signal gray : std_logic_vector(3 downto 0);
begin
DUT : entity work.gray_converter(rtl)
port map (bin => bin, gray => gray);
end architecture;
Hinweis: In ModelSim Datei auf VHDL-2008 setzen (Properties → VHDL → Use 1076-2008) für std.env.finish.
Exhaustive Eingabegenerierung
PROC_SEQUENCE : process
begin
for i in 0 to 2**bin'length - 1 loop
bin <= std_logic_vector(to_unsigned(i, bin'length));
wait for 10 ns;
end loop;
bin <= (others => '0');
wait for 10 ns;
report "Test: OK";
finish;
end process;
10 ns Wartezeit ist willkürlich; der DUT ist rein kombinatorisch. finish beendet die Simulation sauber.
DUT-Ausgabe verifizieren
PROC_CHECKER : process
variable prev : std_logic_vector(gray'range);
variable count : integer;
begin
wait on bin;
prev := gray;
wait for 1 ns; -- Kombinatorik auflösen lassen
count := 0;
for i in gray'range loop
if gray(i) /= prev(i) then
count := count + 1;
end if;
end loop;
assert count = 1
report integer'image(count) & " bits changed, should have been 1"
severity failure;
end process;
Der Checker überwacht bin, merkt den vorherigen Gray-Wert, zählt unterschiedliche Bits und fordert genau einen Bitwechsel. Bei Verfehlung stoppt der Simulator sofort – „Test: OK“ erscheint nicht.
Testbench ausführen
In ModelSim: run -all. Erfolgreicher Lauf endet mit:
VSIM 1> run -all # ** Note: Test: OK # Time: 170 ns Iteration: 0 Instance: /gray_converter_tb
Robustheit prüfen
Fehler im DUT injizieren (z. B. gray(3) <= '0' erzwingen). Der Assertion-Fehler meldet die tatsächliche Anzahl geänderter Bits:
VSIM 2> run -all # ** Failure: 0 bits changed, should have been 1 # Time: 81 ns Iteration: 0 Process: /gray_converter_tb/PROC_CHECKER # Break in Process PROC_CHECKER at gray_converter_tb.vhd line 61
Einstieg in selbstprüfende Testbenches
Gewöhnen Sie sich an, für jedes VHDL-Modul einen selbstprüfenden Testbench zu schreiben. Sie fangen Regressionen früh ab und dokumentieren Design-Absicht. Testbenches müssen nicht synthetisierbar sein, aber klar, wartbar und moderne VHDL-Konstrukte nutzen.
Für tieferes Lernen: Fortgeschrittene Kurse, die den gesamten Design-Flow (Spezifikation bis FPGA-Prototyp) mit rigoroser Verifikation abdecken.
Aktualisiert am 12. Oktober 2020: VHDL/FPGA-Kurs abgeschlossen. Bild anklicken für Infos.
Mehr zum Dot-Matrix-VHDL/FPGA-Kurs erfahren!
Start:
Noch offen.
VHDL
- Ihr erstes AWS CloudFormation Template erstellen – Ein praktischer Leitfaden
- Cloud Center of Excellence aufbauen: Strategien für erfolgreiche Migration und Customer Success
- Strategischen Cloud-Adoptionsplan erstellen
- Reibungslose Nutzererfahrungen schaffen: Ein UX-Design-Blaupause
- String-Listen in VHDL erstellen: Best Practices & Beispiel
- Tcl-gesteuerter Testbench für ein VHDL Code-Lock-Modul erstellen
- VHDL-Testbench-Simulation sauber beenden
- Dynamische verkettete Liste in VHDL mit Protected Types und Access-Pointern
- Menschzentrierte Smart Cities bauen: Ein strategischer Blueprint
- Array von Objekten in Java erstellen: Schritt-für-Schritt-Anleitung