VHDL - Concurrent Statements, Processes and Testbenches
In questa pagina 4
This note shows how to write the behaviour of combinational circuits and how to verify them in simulation with a testbench. The same constructs, with a clock, serve for sequential circuits (VHDL - Latches, Flip-Flops and ResetsIn VHDL a memory element is a process in which some branch does not assign the signal: the D latch is process(D,C) with if C='1' then Q<=D, the positive-edge-triggered flip-flop is process(CLK) with if CLK'event and CLK='1'. Asynchronous reset: RST is in the sensitivity list and tested first (if RST='1' ... elsif clock edge); synchronous reset: only CLK in the list and RST tested inside the clock-edge branch, so a clock edge is needed for it to act. The testbench of a synchronous circuit has a process generating the clock and one applying inputs away from the active edge.VHDL - Latches, Flip-Flops and Resets →). The block structure is in VHDL - Entity, Architecture, Types and Description LevelsVHDL is a hardware description language, not a programming language: its statements are concurrent and describe blocks of circuit, used to document, simulate and synthesise. A block has an entity (name and in/out ports) and at least one architecture (what is inside) written in structural style with components and port map, dataflow style with Boolean equations, or behavioral style. Types include std_logic with values 0, 1, X, Z, U and -, std_logic_vector, bit, boolean and integer; signal is used for internal connections and constant for fixed values.VHDL - Entity, Architecture, Types and Description Levels →.
Concurrent statements
In the body of an architecture (between begin and end) statements are concurrent: all active in parallel.
Simple assignment
Definition (concurrent assignment).
y <= (a and b) or c;is a Boolean equation. Whenever a signal on the right changes,yis re-evaluated. With several assignments to the same signal in concurrent statements there are several drivers: in simulation, if the values differ, the signal becomes'X'(conflict), a design error.
when ... else (priority logic)
Y <= I0 when S = "00" else
I1 when S = "01" else
I2 when S = "10" else
I3;Property. Conditions are evaluated in order: if the first is true, the first value is chosen and later conditions do not matter. Used for priorities, and allows decisions on different signals under different conditions.
Example. With S = "01" the first condition is false and the second true: Y <= I1.
with ... select (parallel logic)
with S select
Y <= I0 when "00",
I1 when "01",
I2 when "10",
I3 when others;Property. A single selection expression (
S) is evaluated; all choices have equal priority, every possible value must be covered by exactly one choice, andwhen othersis required for unlisted values. Simpler thanwhen-elsefor selection circuits (multiplexers, decoders, truth tables). Several values are listed with|:'1' when "0100" | "1000".
Example: 2-to-4 decoder with enable, dataflow:
library IEEE;
use IEEE.std_logic_1164.all;
entity decoder2to4 is
port ( A : in std_logic_vector(1 downto 0);
EN : in std_logic;
D : out std_logic_vector(3 downto 0) );
end decoder2to4;
architecture dataflow of decoder2to4 is
begin
D(0) <= EN and not A(1) and not A(0);
D(1) <= EN and not A(1) and A(0);
D(2) <= EN and A(1) and not A(0);
D(3) <= EN and A(1) and A(0);
end dataflow;With EN = '0' all outputs are '0'.
The process
A process is a concurrent statement (parallel to the others in the architecture) inside which statements are sequential: executed one after the other, in written order, in zero simulated time (unless there is a wait). All processes of an architecture work simultaneously.
label : process ( sensitivity_list )
-- local declarations
begin
-- sequential statements: assignments, if, case, for ...
end process;Definition (sensitivity list). The signals whose change activates the process. To describe a combinational circuit it must contain all inputs (in VHDL-2008
process (all)). It may contain only readable signals (inputs or internalsignals, not outputs). A process without a list starts at the beginning of the simulation and repeats forever unless there is await: the pattern of testbenches.
Example. process (a, b) begin y <= a and b; end process; is re-evaluated at every change of a or b.
Property (signal update in a process). A value assigned to a signal inside a process is updated only when the process suspends (end of statements or a
wait). If the same signal is assigned several times in one pass, the last assignment wins. Soprocess (b) begin a <= b; a <= not b; end process;describes : an inverter, combinational logic, not a flip-flop.
Sequential if and case
if is the sequential analogue of when-else (conditions in order, priority); case is the analogue of with-select (all choices covered, not overlapping, when others required; null for "no action"):
-- 4-input priority encoder with if
process (D)
begin
if D(3) = '1' then A <= "11"; V <= '1';
elsif D(2) = '1' then A <= "10"; V <= '1';
elsif D(1) = '1' then A <= "01"; V <= '1';
elsif D(0) = '1' then A <= "00"; V <= '1';
else A <= "--"; V <= '0'; -- no input at 1: A is don't care
end if;
end process;-- 4-bit prime number (or 1) with case
process (input)
begin
case input is
when x"1" | x"2" | x"3" | x"5" | x"7" | x"b" | x"d" => isPrime <= '1';
when others => isPrime <= '0';
end case;
end process;Unwanted memory
Property (unwanted memory). If in an
if(orcase) not all conditions are specified, the signal keeps its previous value for those not covered: the synthesiser must introduce a memory element (latch). It is the same mechanism used on purpose to describe a latch or flip-flop; in a circuit that must be combinational it is an error.
Example. 3-bit primes ():
-- CORRECT: all conditions (else)
process (input)
begin
if input = "001" or input = "010" or input = "011" or input = "101" or input = "111" then
isPrime <= '1';
else
isPrime <= '0';
end if;
end process;
-- WRONG: no case for "000" and "011"
process (input)
begin
if input = "001" or input = "010" or input = "101" or input = "111" then
isPrime <= '1';
elsif input = "100" or input = "110" then
isPrime <= '0';
end if; -- for "000" and "011" isPrime keeps its old value: memory
end process;In the second code, if "011" arrives after "100" the output stays '0', after "101" it stays '1': the circuit has memory. Another typical case: process(A) begin if A = '1' then Q <= B; end if; end process; describes a latch, not a gate.
Rule of thumb: for combinational logic every output must be assigned in every branch of if and case, and all inputs belong in the sensitivity list.
Testbench
Definition (testbench). VHDL code that simulates a circuit: it applies input signals to the circuit under test (DUT, device under test) and observes the outputs. It is not synthesised, only simulated.
Structure:
- an empty entity (no ports);
- in the architecture: signals driving the DUT inputs and reading its outputs;
- the DUT instance (with port map);
- one or more processes applying stimuli sequentially, with
wait forto let time pass (wait;with no argument suspends the process forever).
Example: majority function ( if at least two inputs are 1):
library IEEE;
use IEEE.std_logic_1164.all;
entity majority is
port ( a, b, c : in std_logic;
y : out std_logic );
end majority;
architecture maj_dataflow of majority is
begin
y <= (a and b) or (a and c) or (b and c);
end maj_dataflow;library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
entity tb_majority is -- empty entity
end tb_majority;
architecture test of tb_majority is
signal inp : std_logic_vector(2 downto 0) := "000";
signal y : std_logic;
begin
DUT : entity work.majority(maj_dataflow)
port map (a => inp(2), b => inp(1), c => inp(0), y => y);
stimuli : process
begin
for i in 0 to 7 loop -- all 8 combinations
inp <= std_logic_vector(to_unsigned(i, 3));
wait for 10 ns; -- let the output settle
report "in = " & integer'image(i) & " -> y = " & std_logic'image(y);
end loop;
wait; -- end of stimuli
end process;
end test;Versione ripasso
- Concurrent assignment:
y <= (a and b) or c;re-evaluated when a right-hand signal changes; several drivers on one signal conflict ('X'). when ... else: conditions in order, priority;Y <= I1forS="01"in the multiplexer example.with ... select: one expression, all values covered exactly once,when othersrequired;|lists values. Decoder 2-to-4:D(i) <= EN and (minterm of A);EN='0'gives all zeros.- Process: concurrent statement with sequential body; sensitivity list = signals whose change wakes it (all inputs for combinational logic,
process (all)in 2008); without list it runs from the start (testbench). Signals update at suspension; last assignment wins, soa <= b; a <= not b;is an inverter. if/case:if= priority (likewhen-else),case= parallel (likewith-select,others,null). Priority encoder withifandA <= "--"when no input is 1; primes withcase.- Unwanted memory: an
iforcasethat does not assign in all branches infers a latch (if A='1' then Q <= B; end if;). Assign every output in every branch. - Testbench: empty entity, signals, DUT with port map, stimulus process with
wait for, finalwait;. Majority: loopiin 0 to 7,to_unsigned(i,3),wait for 10 ns,report.