VHDL Lecture 3 PDF
VHDL Lecture 3 PDF
5051158-3002
Lecture 3
VHDL Basics – part 2
Test benches
Contents of this lecture
• VHDL Basics – part 2
• Arrays and indexing, user types
• Generics
• Using generics in hierarchical design
• Test bench (for simulation)
Arrays in VHDL
signal b_bus: std_logic_vector (3 downto 0);
• 2 ways to combine “bits” to signal x,y,z,w: std_logic;
“arrays”
b_bus <= x & "00" & w;
• Concatenation
• Aggregate b_bus <= (2=>x,1=>y,3=>z,0=>d);
• Named association
b_bus <= (x,y,z,d);
• Positional association
• Indexing signal z_bus: std_logic_vector(3 downto 0);
• Accessing a “slice” of an array signal a_bus: std_logic_vector (1 to 4);
• The direction of the slice (i.e. a_bus <= z_bus; -- ok
to or downto) must match z_bus(3 downto 2) <= "00"; --ok
the direction in which the a_bus(2 to 4) <= z_bus(3 downto 1); --ok
array is declared z_bus(2 to 3) <= "00"; -- NOT OK
Multi-dimensional Arrays and User Types
• An array contains multiple
elements of same type
• Note: std_locic_vector is type MY_BUS is array (3 downto 0) of std_logic_vector(15 downto 0);
also an array, composed of type RAM is array (0 to 31) of integer range 0 to 255;
several std_logic types – signal A_BUS : MY_BUS;
defined in std_logic_1164- signal RAM_0 : RAM;
package
• When any array object is
declared, an existing array
type must be used
• Multi-dimensional arrays
are especially useful, when
using generate loops (we’ll
see that later)
Entity - Generics
• To improve the reusability and flexibility of your entity entity_name is
code (especially true for components), you can
use generics generic (generic list);
port (port list);
end entity_name;
• generic is a parameter- or a “specification”,
which value is evaluated during component component_name
compilation/synthesis
• NOTE: it does not “change” your block run-time generic (generic_list);
port (port_list);
• Generics are listed in entity, before port list.
Similarly, component needs to have a generic list end component;
as well, as it represents the “interface” of an
entity instance_label: component_name
• On instantiation, values are associated to generic map (generic_association_list)
generics – the default value of generic (in a port map (port_association_list);
component) is overridden
Generics – an example
• Consider a clocked 4:1 bus multiplexer architecture rtl of busmux4to1 is
• What if the width of the data bus begin
sync_mux_p: process(clk, n_Reset)
changes? begin
• In this case the architecture does not if n_Reset = '0' then
require any change (note how we reset the Y <= (others => '0');
elsif rising_edge(clk) then
output), but entity need to be changed case S is
when "00" => Y <= A;
entity busmux4to1 is
when "01" => Y <= B;
port (
when "10" => Y <= C;
clk, n_Reset: in std_logic;
when others => Y <= D;
A,B,C,D : in std_logic_vector(7 downto 0);
end case;
S : in std_logic_vector(1 downto 0);
end if; --clk/rst
Y : out std_logic_vector(7 downto 0)
end process sync_mux_p;
);
end architecture rtl;
end busmux4to1;
Generics – an example (cont.)
• Added a generic
• Default value: 8 architecture rtl of gen_busmux4to1 is
begin
• Note: No change in architecture (in this sync_mux_p: process(clk, n_Reset)
case) begin
if n_Reset = '0' then
entity gen_busmux4to1 is Y <= (others => '0');
generic ( elsif rising_edge(clk) then
DATA_WIDTH : integer := 8 case S is
); when "00" => Y <= A;
port ( when "01" => Y <= B;
clk, n_Reset: in std_logic; when "10" => Y <= C;
A,B,C,D : in std_logic_vector(DATA_WIDTH-1 downto 0); when others => Y <= D;
S : in std_logic_vector(1 downto 0); end case;
Y : out std_logic_vector(DATA_WIDTH-1 downto 0) end if; --clk/rst
); end process sync_mux_p;
end gen_busmux4to1; end architecture rtl;
Generics – an example (cont.)
• How about making it 16-bits wide? Or 32-bits?
• Change the value of generic
• But, we really don’t want to change it every time – better to use it as a component
and override the default value when instantiating the component