VHDL Tutorial

Subprograms in Package declarations
Fundamental concepts
Modelling concepts
Elements of behaviour
Elements of structure
Analysis elaboration
Lexical elements
Characters and strings 
Syntax descriptions
Constants and variables
Scalar type
Integer types
Floating point types
Time type
Enumeration types
Character types
Boolean type 
Bits type
Standard logic
Sequential statements
Case statements
Loop and exit statements
Assertion statements
Array types & array operations
Architecture bodies
Entity declarations
Behavioral descriptions 
Wait statements
Delta delays
Process statements
Conditional signal assignment 
Selected signal assigment
Structural descriptions
Library and library clauses
Procedure parameters
Signal parameters
Default values
Unconstrained array parameter
Package declarations and bodies
Subprograms in package

Use clauses
Resolved signals and subtypes
Resolved signals and ports
Parameterizing behavior
Parameterizing structure

Subprograms in Package Declarations


Another kind of declaration that may be included in a package declaration is a sub- program declaratioeither a procedure or a function declaration.  An important as- pect  of  declaring  a  subprogram  in  a  package  declaration  is  that  we  only  write  the header of the subprogram, that is, the part that includes the name and the interface list defining the parameters (and result type for functions).  We leave out the body of the subprogram.   For example, suppose we have a package declaration that defines

a bit-vector subtype:

Want To have highly paid VLSI jobs ?? then you may contact at

Contact : webmaster@freehost7com

subtype word32 is bit_vector(31 downto 0);


We can include in the package a procedure to do addition on word32 values that rep- resent signed integers.   The procedure declaration in the package declaration is


procedure add ( a, b : in word32;

result : out word32;  overflow : out boolean );


Note  that  we  do  not  include  the  keyword  is  or  any  of  the  local  declarations  or statements needed to perform the addition; these are deferred to the package body. Each package declaration that includes subprogram declarations must have a corre- sponding package body to fill in the missing details.   However, if a package declara- tion only includes other kinds of declarations, such as types, signals or fully specified constants, no package body is necessary.  The syntax rule for a package body is sim- ilar to that for the interface, but with the inclusion of the keyword body:



package body identifier is

{ package_body_declarative_item }

end [ package body ] [ identifier ] ;


The items declared in a package body must include the full declarations of all sub- programs defined in the corresponding package declaration.   These full declarations must include the subprogram headers exactly as they are written in the package dec- laration.  A package body may also include declarations of additional types, subtypes, constants and subprograms.  These items are used to implement the subprograms de- fined in the package declaration Note that the items declared in the package decla- ration  cannot  be  declared  again  in  the  body  (apart  from  subprograms  and  deferred constants, as described above), since they are automatically visible in the body.




Figure 6-2 shows outlines of a package declaration and a package body declaring arithmetic functions for bit-vector values.   The functions treat bit vectors  as representing signed integers in binary form.  Only the function headers are in- cluded in the package declaration.   The package body contains the full function bodies.  It also includes a function, mult_unsigned, not defined in the package dec- laration.   It is used internally in the package body to implement the signed mul- tiplication operator.





package bit_vector_signed_arithmetic is

function add ( bv1, bv2 : bit_vector ) return bit_vector; function sub ( bv : bit_vector ) return bit_vector; function mult ( bv1, bv2 : bit_vector ) return bit_vector;


end package bit_vector_signed_arithmetic;

–– ––– –––– ––– –––– –––– ––– –––– ––– –––– ––– –––– –––– ––– ––––

package body bit_vector_signed_arithmetic is

function add ( bv1, bv2 : bit_vector ) return bit_vector is …

function sub ( bv : bit_vector ) return bit_vector is …

function mult_unsigned ( bv1, bv2 : bit_vector ) return bit_vector is




end function mult_unsigned;

function mult ( bv1, bv2 : bit_vector ) return bit_vector is begin

if bv1(bv1'left) = '0' and bv2(bv2'left) = '0' then

return mult_unsigned(bv1, bv2);

elsif bv1(bv1'left) = '0' and bv2(bv2'left) = '1' then return –mult_unsigned(bv1, –bv2);

elsif bv1(bv1'left) = '1' and bv2(bv2'left) = '0' then return –mult_unsigned(–bv1, bv2);





return mult_unsigned(–bv1, –bv2);


end if;

end function mult;


end package body bit_vector_signed_arithmetic;


An outline of a package declaration and body that define signed arithmetic functions on integers rep-

resented as bit vectors.