13. Beispiele

13.1 Multiplexer (strukturales Modell)

Folgendes Schaltbild gibt den Aufbau eines 2:1 Multiplexers wieder:

Eine Möglichkeit, diese Schaltung zu modellieren, wird in dem VHDL-Modell auf den nach-folgenden Seiten demonstriert. Zunächst die Schnittstellenbeschreibung (Entity):

---------------------------------------------------------------- 
-- filename: mux21.vhd
-- title: structural model of 2:1-MUX
-- author: B. Wunder, ITIV
----------------------------------------------------------------

LIBRARY ieee;
USE ieee.std_logic_1164.all;

ENTITY mux21 IS
PORT ( i1: IN std_ulogic:='0'; -- data 1
i2: IN std_ulogic:='0'; -- data 2
sel: IN std_ulogic:='1'; -- select
result: OUT std_ulogic -- output
);
END mux21;

Dieser noch allgemeinen Schnittstellenbeschreibung wird nun eine strukturale Architektur mit entsprechender Configuration zugeordnet:

ARCHITECTURE structural OF mux21 IS
-- components to use in mux21
COMPONENT and2_component
PORT (a,b: IN std_ulogic; y: OUT std_ulogic);
END COMPONENT;
COMPONENT or2_component
PORT (a,b: IN std_ulogic; y: OUT std_ulogic);
END COMPONENT;
COMPONENT inv_component
PORT (i: IN std_ulogic; o: OUT std_ulogic);
END COMPONENT;
-- internal signals
SIGNAL selbar,s1,s2: std_ulogic;

BEGIN
in_gate_1: and2_component PORT MAP (i1,sel,s1);
in_gate_2: and2_component PORT MAP (selbar,i2,s2);
sel_inverter: inv_component PORT MAP (sel,selbar);
out_gate: or2_component PORT MAP (s1,s2,result);
END structural;

CONFIGURATION mux21_config OF mux21 IS
FOR structural
FOR all: and2_component
USE CONFIGURATION work.and2_config;
-- Default-Port MAP (identical port names)
END FOR;
FOR sel_inverter: inv_component
USE CONFIGURATION work.inverter_config;
-- Default-Port MAP (identical port names)
END FOR;
FOR out_gate: or2_component
USE CONFIGURATION work.or2_config;
-- Default-Port MAP (identical port names)
END FOR;
END FOR;
END mux21_config;

13.2 Stimuli und Antwortkontrolle für den 2:1-Multiplexer

Folgendes VHDL-Modul enthält die Stimulierzeugung und die Antwortkontrolle für die Multiplexer-Testbench. Da in der Testbench-Configuration (siehe weiter unten) die Form USE ENTITY dir_name.entity_name (architecture_name) verwendet wird, ist für das Stimuli-Modul nicht unbedingt eine Configuration erforderlich.

---------------------------------------------------------------
-- filename: mux21_stim.vhd
-- title: stimuli for 2:1-MUX
-- author: B. Wunder, ITIV
---------------------------------------------------------------

LIBRARY ieee;
USE ieee.std_logic_1164.all;

ENTITY mux21_stim IS
PORT ( in1,in2,in3: OUT std_ulogic; -- data, select
res: IN std_ulogic ); -- data output
END mux21_stim;

ARCHITECTURE behavioral OF mux21_stim IS
SUBTYPE t3 IS std_ulogic_vector (1 TO 3);
BEGIN
PROCESS
BEGIN
-- in2 = 0 => in3 selected, otherwise in1 is selected
-- t3 is a qualified expression to indicate that "000"
-- is of type std_ulogic_vector!

(in1,in2,in3) <= t3'("000") AFTER 0 ns,
t3'("001") AFTER 10 ns,
t3'("100") AFTER 20 ns,
t3'("101") AFTER 30 ns,
t3'("010") AFTER 40 ns,
t3'("011") AFTER 50 ns,
t3'("110") AFTER 60 ns,
t3'("111") AFTER 70 ns,
t3'("X01") AFTER 80 ns,
t3'("10X") AFTER 90 ns,
t3'("X11") AFTER 100 ns,
t3'("11X") AFTER 110 ns;

-- describe exp. resp.; check 1 ns after input changes
-- a warning will be displayed if condition is false!

WAIT FOR 1 ns; ASSERT (res = '0')
REPORT "result isn't 0"
SEVERITY WARNING; -- absolute 1 ns
WAIT FOR 10 ns; ASSERT (res = '1')
REPORT "result isn't 1"
SEVERITY WARNING; -- absolute 11 ns
WAIT FOR 10 ns; ASSERT (res = '0')
REPORT "result isn't 0"
SEVERITY WARNING; -- absolute 21 ns
WAIT FOR 10 ns; ASSERT (res = '1')
REPORT "result isn't 1"
SEVERITY WARNING; -- absolute 31 ns
WAIT FOR 10 ns; ASSERT (res = '0')
REPORT "result isn't 0"
SEVERITY WARNING; -- absolute 41 ns
WAIT FOR 10 ns; ASSERT (res = '0')
REPORT "result isn't 0"
SEVERITY WARNING; -- absolute 51 ns
WAIT FOR 10 ns; ASSERT (res = '1')
REPORT "result isn't 1"
SEVERITY WARNING; -- absolute 61 ns
WAIT FOR 10 ns; ASSERT (res = '1')
REPORT "result isn't 1"
SEVERITY WARNING; -- absolute 71 ns
WAIT FOR 10 ns; ASSERT (res = '1') R
REPORT "result isn't 1"
SEVERITY WARNING; -- absolute 81 ns
WAIT FOR 10 ns; ASSERT (res = 'X')
REPORT "result isn't X"
SEVERITY WARNING; -- absolute 91 ns
WAIT FOR 10 ns; ASSERT (res = 'X')
REPORT "result isn't X"
SEVERITY WARNING; -- absolute 101 ns
WAIT FOR 10 ns; ASSERT (res = '1')
REPORT "result isn't 1"
SEVERITY WARNING; -- absolute 111 ns
WAIT;
END PROCESS;
END behavioral;

13.3 Testumgebung für den 2:1-Multiplexer

Die Testbench für den Multiplexer enthält nur eine Zusammenschaltung von MUT und Stimulimodul.

---------------------------------------------------------------
-- filename: mux21_tb.vhd
-- title: Testbench for 2:1-MUX
-- author: B. Wunder, ITIV
---------------------------------------------------------------

LIBRARY ieee;
USE ieee.std_logic_1164.all;

ENTITY mux21_tb IS
END mux21_tb;

ARCHITECTURE structural OF mux21_tb IS
COMPONENT mux21_stim -- stimuli for mux21
PORT ( in1,in2,in3: OUT std_ulogic;
res: IN std_ulogic);
END COMPONENT;
COMPONENT mux21 -- block to test
PORT ( i1,i2,sel: IN std_ulogic;
result: OUT std_ulogic);
END COMPONENT;

SIGNAL x1,x2,x3,o: std_ulogic; -- internal signals

BEGIN
-- declaration of driving part of testbench (i.e. stimuli)
stimuli: mux21_stim PORT MAP (x1,x2,x3,o);
-- declaration of driven part of testbench (i.e. circuit)
circuit: mux21 PORT MAP (x1,x3,x2,o);
END structural;

CONFIGURATION mux21_tb_config OF mux21_tb IS
FOR structural
FOR stimuli: mux21_stim
USE ENTITY work.mux21_stim (behavioral);
END FOR;
FOR circuit: mux21
USE CONFIGURATION work.mux21_config;
END FOR;
END FOR;
END mux21_tb_config;

13.4 Verhaltensbeschreibung eines D-Flip-Flops

Als zweites Beispiel sei ein D-Flip-Flop, diesmal in Verhaltenssichtweise, modelliert:

---------------------------------------------------------------
-- file name : dff.vhd
-- title : D Flip Flop / behavioral model
-- author : G. Lehmann, ITIV
---------------------------------------------------------------

ENTITY dff is
generic ( t_clk_q : time := 5 ns;
t_res_q : time := 2 ns );
port ( d : IN bit := '0'; -- data
clk : IN bit := '0'; -- clock
res : IN bit := '1'; -- reset
q : OUT bit; -- dff-out
qn : OUT bit); -- dff-out-inv
END dff;

ARCHITECTURE behavior OF dff IS

BEGIN
PROCESS (clk, res)
BEGIN
IF res = '0' THEN
q <= '0' AFTER t_res_q;
qn <= '1' AFTER t_res_q;
ELSIF clk = '1' AND clk'EVENT THEN
q <= d AFTER t_clk_q;
qn <= NOT d AFTER t_clk_q;
END IF;
END PROCESS;
END behavior;

CONFIGURATION dff_conf OF dff IS
FOR behavior
END FOR;
END dff_conf;

13.5 Verhaltensbeschreibung eines Automaten

Als letztes Beispiel sei das Vorgehen bei der Modellierung eines taktflankengetriggerten Auto-maten mit asynchronem Reset aufgezeigt. Bild 14 zeigt die Schnittstelle eines abstrakten Moore-Automaten mit zwei Eingangs-, drei Ausgangssignalen, Takt und asynchronem Rücksetzsignal.

Der Automatengraph für dieses Beispiel ist in Bild 15 aufgeführt. Es handelt sich um einen synchronen, vorderflankengetriggerten Moore-Automaten mit vier Zuständen. Die jeweiligen Eingangskombinationen in_1 und in_2 sind an den Übergangspfeilen vermerkt, in den Zuständen selbst sind die Ausgangsvariablen out_1, out_2 und out_3 definiert. Ein asynchrones, low-aktives Rücksetzsignal (reset = '0') bewirkt ein sofortiges Wechseln in den Zustand init. Dies ist gleichzeitig der Anfangszustand.

Um ein solchen Verhalten mit VHDL zu modellieren, ist folgendes zu beachten:

  • Für die vier möglichen Zustände kann ein eigener Aufzähltyp für ein internes "Zustandssignal" deklariert werden, der die einzelnen Zustände als mögliche Signalwerte enthält.
  • Die Flankentriggerung der Zustandswechsel erfolgt innerhalb eines Prozesses, der durch clk und reset aktiviert wird. Die Abfrage der aktiven Signalflanke erfolgt mit Hilfe von Signalattributen. Wichtig ist dabei, daß das Rücksetzsignal Priorität hat!
  • Da es sich um einen Automaten vom Moore-Typ handelt, werden ahängig vom Zustand die Ausgangsvariablen zugewiesen. Dies kann im gleichen Prozess oder getrennt als Prozess bzw. nebenläufige Signalzuweisung erfolgen.

Der das beschriebene Automatenverhalten realisierende VHDL-Code lautet:

----------------------------------------------------------------
--
-- filename: fsm.vhd
-- title: Verhaltensmodell des einfachen Automaten
-- author: B. Wunder, ITIV
--
----------------------------------------------------------------

ENTITY fsm IS
PORT (clk, reset : IN bit ;
in_1, in_2 : IN bit ;
out_1, out_2, out_3 : OUT bit) ;
TYPE fsm_state IS (init, state_1, state_2, state_3) ;
END fsm ;

----------------------------------------------------------------

ARCHITECTURE behavioral OF fsm IS
SIGNAL state : fsm_state := init ;

BEGIN

new_state : PROCESS (clk, reset)
BEGIN
IF (reset = '0') THEN
state <= init ;
ELSIF (clk = '1' AND clk'EVENT) THEN
CASE state IS
WHEN init =>
IF in_1 = '0' AND in_2 = '1' THEN
state <= state_1 ;
ELSIF in_1 = '1' AND in_2 = '1' THEN
state <= state_2 ;
END IF ;
WHEN state_1 =>
IF in_1 = '1' THEN
state <= init;
ELSIF in_1 = '0' AND in_2 = '1' THEN
state <= state_2 ;
END IF ;
WHEN state_2 =>
IF in_1 = '1' AND in_2 = '1' THEN
state <= init ;
ELSIF (in_1 = '0' AND in_2 = '1')
OR (in_1 = '1' AND in_2 = '0') THEN
state <= state_3 ;
END IF ;
WHEN state_3 =>
IF in_1 = '1' AND in_2 = '1' THEN
state <= init ;
END IF ;
END CASE ;
END IF ;
END PROCESS ;

output_assignment : PROCESS (state)
BEGIN
CASE state IS
WHEN init =>
out_1 <= '0' ;
out_2 <= '0' ;
out_3 <= '0' ;
WHEN state_1 =>
out_1 <= '0' ;
out_2 <= '0' ;
out_3 <= '1' ;
WHEN state_2 =>
out_1 <= '0' ;
out_2 <= '1' ;
out_3 <= '1' ;
WHEN state_3 =>
out_1 <= '1' ;
out_2 <= '0' ;
out_3 <= '0' ;
END CASE ;
END PROCESS ;

END behavioral ;

Hinweis: Die Synthesewerkzeuge erfordern einen bestimmten, werkzeugspezifischen VHDL-Modellierungsstil. Dies ist insbesondere bei der Beschreibung von Automaten der Fall. Der im Beispiel (VHDL-Quellcode) verwendete Modellierungsstil ist z.B. für das Synthesewerkzeug der Firma SYNOPSYS geeignet.