Packages in VHDL

A package provides a convenient mechanism to store and share declarations that are common across many design units. Packages in VHDL consists of two parts:

  1. Package declaration section
  2. Package body.

The package declaration defines the interface for the package, much the same way that the entity defines the interface for a model. The package body specifies the actual behavior of the package in the same method that the architecture statement does for a model.

Package Declaration

A package declaration contains a set of declarations that may possibly be shared by many design units. It defines the interface to the package, that is, it defines items that can be made visible to other design units, for example, a function declaration.

The package declaration section can contain the following declarations:

  • Subprogram declaration
  • Type, subtype declaration
  • Constant, deferred constant declaration
  • Signal declaration creates a global signal
  • File declaration
  • Alias declaration
  • Component declaration
  • Attribute declaration, a user-defined attribute
  • Attribute specification
  • Disconnection specification
  • Use clause

All of the items declared in the package declaration section are visible to any design unit that uses the package with a USE clause. The interface to a package consists of any subprograms or deferred constants declared in the package declaration.

The subprogram and deferred constant declarations must have a corresponding subprogram body and deferred constant value in the package body or an error results.

Syntax:

package package-name is

package-item-declarations

end [ package-name ]

Example:

package PACKAGE_EXAMPLE is

constant CLOCKIN: TIME := 10ns:

type MATH_OPERATION is (ADD, SUB, MUL, DIV, EQL);

attribute FUN: BOOLEAN;

type LOGIC is (‘U’, ‘0’, ‘1’, ‘Z’);

subtype MY_ALU_OP is ALU_OP range ADD to DIV;

component AND2

port (A, B: in LOGIC; y: out MVL);

end component;

end PACKAGE_EXAMPLE;

Deferred Constants

Deferred constants are constants that have their name and type declared in the package declaration section but have the actual value specified in the package body section.

Example:

PACKAGE tpack IS

CONSTANT timing_mode : t_mode;

END tpack;

This example shows a deferred constant called timing_mode being defined as type t_mode. The actual value of the constant is specified when the package body for package tpack is compiled.

This feature allows late binding of the value of a constant so that the value of the constant can be specified at the last possible moment and can be changed easily.

Any design unit that uses a deferred constant from the package declaration need not be recompiled if the value of the constant is changed in the package body. Only the package body needs to be recompiled.

Package Body

A package body primarily contains the behavior of the subprograms and the values of the deferred constants declared in a package declaration.

The package body can also contain the following declarations:

  • Subprogram declaration
  • Subprogram body
  • Type, subtype declaration
  • Constant declaration, which fills in the value for the deferred constant
  • File declaration
  • Alias declaration
  • Use clause

Syntax:

package body package-name is

package-body-item-declarations

end [ package-name ];

The package name must be the same as the name of its corresponding package declaration. A package body is not necessary if its associated package declaration does not have any subprogram or deferred constant declarations.

All of the declarations in the package body, except for the constant declaration that is specifying the value of a deferred constant and the subprogram body declaration, are local to the package body.

Example:

package body PROGRAM_BODY is

constant DELAY_TIME: TIME := 15ns;

function “and” (L, R: MVL) return MVL is

begin

return TABLE_AND(L, R);

— TABLE_AND is a 2-D constant defined elsewhere.

end “and”;

procedure IN_SIGNAL (signal ARRAY_NAME: inout MVL_VECTOR;

START, STOP, INT_VALUE: in INTEGER) is

— Local declarations here.

begin

— Procedure behavior here.

end IN_SIGNAL;

end PROGRAM_BODY;

Recent posts

1 thought on “Packages in VHDL”

  1. constаntly i used to read smаller articles which as well clear their motіve,
    and that is also hapрening with this post which I am reading here.

Comments are closed.