Contact me for VHDL projects or assignments

Wednesday, March 3, 2010

Usage of Packages and functions

Packages are the only language mechanism to share objects among different design units. Usually, they are designed to provide standard solutions for specific problems, e.g. data types and corresponding subprograms like type conversion functions for different data types, procedures and functions for signal processing purposes, etc.A package is declared in the following format :
 package package_name is
     -- Declaration of
          -- types and subtypes
          -- subprograms
          -- constants, signals etc.
 end package_name;


package body package_name is
     -- Definition of previously declared
        -- constants
        -- subprograms
     -- Declaration/definition of additional
        -- types and subtypes
        -- subprograms
        -- constants, signals and shared variables
end package_name;


--An example for a module using package..
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

--note this line.The package is compiled to this directory by default.
--so don't forget to include this directory.
library work;
--this line also is must.This includes the particular package into your program.
use work.test_pkg.all;

entity test is
port (clk : in std_logic;
      a1 : in t1;
      b1 : in t1;
      c1: out t1
    );
end test;

architecture Behavioral of test is
begin
process(clk)
begin
if(clk'event and clk='1') then
c1<=add(a1,b1);   --for doing xor operation at every positive edge of clock cycle.
end if;
end process;
end Behavioral;



--Package declaration for the above program
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;

package test_pkg is
type t1 is  --a data type(totally 32 bits contains 3 different fields)
    record
        a :std_logic_vector(11 downto 0);
        b :std_logic_vector(15 downto 0);
        c :std_logic_vector(3 downto 0);
    end record;

 --function declaration.
function add (a2 : t1; b2: t1) return t1;
end test_pkg;   --end of package.

package body test_pkg is  --start of package body
--definition of function
function  add (a2 : t1; b2: t1) return t1 is
variable sum : t1;
begin -- Just name the fields in order...
sum.a:=a2.a xoR b2.a;
sum.b:=a2.b xoR b2.b;
sum.c:=a2.c xoR b2.c;
return sum;
end add;
--end function
end test_pkg;  --end of the package body

      The above program calculates the 'xor' of a 32 bit data type containing 3 fields.If you don't use a function then it is not possible to directly apply the 'xor' function to different data types like 't1'. If you want to use this function at many places in your program then it is advisable to use packages(or functions).
      Another advantage of packages is that by just editing the data types or functions in the package body alone you can do some minor modifications in your design.Otherwise you may need to change your code at lot of places.

1 comment:

  1. In the package outline you can define constants at the package declaration and also at the body (or declaration) part of the package.
    So, What is the difference between define a constant at the declaration of the package and define the same constant at the body of package?

    Thanks!

    ReplyDelete

Related Posts with Thumbnails

Download this article as PDF