Generic Package Example - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

203


57
 
1
-- THE EXAMPLES ARE DIRECTLY BELOW
2
                                              library IEEE;
3
                                              use IEEE.std_logic_1164.all;
4
                                              use IEEE.NUMERIC_STD.all;
5
----------------------------------------
6
7
-- use clause for generic package instance
8
use work.small.all;
9
10
----------------------------------------
11
                                              entity testbench is
12
                                              end entity testbench;
13
14
                                              architecture BENCH of testbench is
15
16
                                                signal CLK, WR_EN : STD_LOGIC;
17
                                                signal WR_ADDR, RD_ADDR : addrTp := (others => '0');
18
                                                signal WR_DATA, RD_DATA : busTp;
19
                                                signal Stop : BOOLEAN;
20
21
                                              begin
22
23
                                                ClockGenerator: process
24
                                                begin
25
                                                  while not Stop loop
26
                                                    CLK <= '0';
27
                                                    wait for 5 NS;
28
                                                    CLK <= '1';
29
                                                    wait for 5 NS;
30
                                                  end loop;
31
                                                  wait;
32
                                                end process ClockGenerator; 
33
34
                                                Stim : process
35
                                                begin
36
                                                  WR_EN <= '1';
37
                                                WR_DATA <= STD_LOGIC_VECTOR(TO_UNSIGNED(1, dataWidth));
38
                                                  WR_ADDR <= STD_LOGIC_VECTOR(TO_UNSIGNED(1, addressWidth));
39
                                                wait until RISING_EDGE(CLK);
40
                                                wait until FALLING_EDGE(CLK);
41
                                                WR_DATA <= STD_LOGIC_VECTOR(TO_UNSIGNED(2, dataWidth));
42
                                                  WR_ADDR <= STD_LOGIC_VECTOR(TO_UNSIGNED(2, addressWidth));
43
                                                wait until FALLING_EDGE(CLK);
44
                                                  WR_EN <= '0';
45
                                                  RD_ADDR <= STD_LOGIC_VECTOR(TO_UNSIGNED(1, addressWidth));
46
                                                wait until FALLING_EDGE(CLK);
47
                                                  RD_ADDR <= STD_LOGIC_VECTOR(TO_UNSIGNED(2, addressWidth));
48
                                                wait until FALLING_EDGE(CLK);
49
                                                Stop <= TRUE;
50
                                                  wait;
51
                                                end process Stim;
52
53
                                                DUT: entity work.EX_GENERIC_MAP_PACKAGE_2008(A1)
54
                                                  port map (CLK, WR_EN, WR_ADDR, RD_ADDR, WR_DATA, RD_DATA);
55
56
                                              end architecture BENCH;
57
48
 
1
-- THE EXAMPLES ARE DIRECTLY BELOW
2
                                              library IEEE;
3
                                              use IEEE.STD_LOGIC_1164.all;
4
----------------------------------------
5
6
-- generic package mypack
7
package mypack is
8
  generic (dataWidth, addressWidth : INTEGER);
9
  subtype busTp is STD_LOGIC_VECTOR(dataWidth-1 downto 0);
10
  subtype addrTp is STD_LOGIC_VECTOR(addressWidth-1 downto 0);
11
  type memTp is array (0 to 2**addressWidth-1) of busTp;
12
end package mypack;
13
14
-- Instance of the generic package mypack
15
package small is new work.mypack
16
  generic map (dataWidth => 16, addressWidth => 4);
17
18
----------------------------------------                                                
19
                                              library IEEE;
20
                                              use IEEE.STD_LOGIC_1164.all;
21
                                              use IEEE.NUMERIC_STD.all;
22
----------------------------------------
23
24
-- use clause for generic package instance
25
use work.small.all;
26
27
----------------------------------------
28
                                              entity EX_GENERIC_MAP_PACKAGE_2008 is
29
                                                port(CLK, WR_EN       :  in Std_logic;
30
                                                     WR_ADDR, RD_ADDR :  in addrTp;
31
                                                     WR_DATA          :  in busTp;
32
                                                     RD_DATA          : out busTp);
33
                                              end entity EX_GENERIC_MAP_PACKAGE_2008;
34
35
                                              architecture A1 of EX_GENERIC_MAP_PACKAGE_2008 is
36
                                                signal RAM: memTp;
37
                                              begin
38
                                                process( CLK )
39
                                                begin
40
                                                  if Rising_edge( CLK ) then
41
                                                    if WR_EN = '1' then
42
                                                      RAM(To_integer(Unsigned(WR_ADDR))) <= WR_DATA;
43
                                                    end if;
44
                                                    RD_DATA <= RAM(To_integer(Unsigned(RD_ADDR)));
45
                                                  end if;
46
                                                end process;
47
                                              end architecture A1;
48
8759 views and 0 likes     
 
# Generic Package Example
A package contains common definitions that can be shared across a VHDL design or even several designs. See the example at https://www.edaplayground.com/x/2A47.
VHDL-2008 adds package generics, which can be used to parameterize a package and the ability to declare packages locally in the declaration region of a process, subprogram, protected body type, or another package. A generic package is declared by adding a _generic_ statement to the package. A generic package then needs to be instantiated with a _generic map_, which enables specific values to be given to the generics.
The first example in the **design.vhd** tab shows the declaration of a generic package and its subsequence instantiation. The second example in the **design.vhd** tab and the example in the **testbench.vhd** tab show use clauses for the generic package instance.

Generic Package Example

A package contains common definitions that can be shared across a VHDL design or even several designs. See the example at https://www.edaplayground.com/x/2A47.

VHDL-2008 adds package generics, which can be used to parameterize a package and the ability to declare packages locally in the declaration region of a process, subprogram, protected body type, or another package. A generic package is declared by adding a generic statement to the package. A generic package then needs to be instantiated with a generic map, which enables specific values to be given to the generics.

The first example in the design.vhd tab shows the declaration of a generic package and its subsequence instantiation. The second example in the design.vhd tab and the example in the testbench.vhd tab show use clauses for the generic package instance.

61420:0