Lecture 7 Packages
Lecture 7 Packages
and packages
2
Packages & Libraries
Frequently used pieces of VHDL code are usually
written in the form of COMPONENTS,
FUNCTIONS, or PROCEDURES.
Such codes are then placed inside a PACKAGE
and compiled into the destination LIBRARY
A package can be shared across many VHDL
models.
A package can also contains user defined data
types and constants.
A library is a collection of related packages.
Packages and libraries serve as repositories for
functions, procedures, and data types.
3
Packages & Libraries
4
Libraries
Library is a place to which design units may be
compiled.
5
Libraries (cntd.)
VHDL knows library only by logical name
6
Libraries (cntd.)
LIBRARY std; (do not need to call)
Contains the following packages
Standard (Types: Bit, Boolean,
Integer, Real, and Time. All operator
functions to support types)
Textio (File operations)
7
Libraries (cntd.)
LIBRARY ieee; (need to call)
Contains the following packages:
std_logic_1164 (std_logic types & related
functions)
std_logic_arith (arithmetic functions)
std_logic_signed (signed arithmetic
functions)
std_logic_unsigned (unsigned arithmetic
functions)
8
Libraries (cntd.)
How to call a library
LIBRARY <any_name>;
USE <any_name>.<package_name>.all;
Example
LIBRARY ieee;
USE ieee.std_logic_1164.all;
9
Packages
10
Packages
A VHDL package is simply a way of grouping a collection
of related declarations that serve a common purpose
11
Package body
The package declaration contains only the declarations of the various
items
The package body contains subprogram bodies and other declarations
use work.my_package.all;
... entity declaration ...
... architecture declaration ...
13
Subprograms
14
Subprograms
Subprograms are of two types :
functions and procedures
15
Subprograms
Full form of subprogram body is
subprogram-specification is
declarations
begin
statements
end identifier;
16
Functions
17
Subprograms (Functions)
A function computes and returns a value of
specified type using the input parameters.
Can be defined locally to an architecture or more
commonly in a package
Function parameters can only be inputs and
they cannot be modified.
I.e., you cannot change the value of inputs inside the
function body
Functions are restricted to substitute
components with only one output
Statements within a function must be sequential.
18
Function (Syntax)
Function declaration:
function rising_edge(signal clock: in std_logic) return Boolean;
20
Functions (III)
21
Functions (IV)
The function is called with the actual parameters.
Example: rising_edge(enable);
Types of formal and actual parameters must
match.
Actual parameters could be signal, constant or an
expression.
22
Function call (Examples)
23
Function: Examples (I)
function Min (X, Y : Integer) return Integer is
variable temp : integer;
begin
if (X < Y) then
temp:= X;
else
temp:= Y;
end if;
return temp; function inc (a: bit_vector) return bit_vector is
variable s: bit_vector (a'range);
end Min; variable carry: bit;
begin
carry := '1';
for i in a'low to a'high loop
s(i) := a(i) xor carry;
carry := a(i) and carry;
end loop
return (s);
end inc;
24
Pure vs. Impure Function
25
Pure vs. Impure Function
26
Pure vs. Impure function (I)
By default, functions are declared as pure;
In pure functions, the only accessible data are
the input arguments; and the only returned
information from this function is the returned
value. Pure functions do not have access to
objects outside the function.
Example:
29
Function Location
Function can also be located in the main code (either inside the
ARCHITECTURE or inside the ENTITY).
30
Function: Examples (Locate inside arch.)
library IEEE;
use IEEE.std_logic_1164.all;
entity dff is
port(d, clock: in std_logic; q, qbar: out std_logic);
end entity dff;
begin
output: process is
begin
wait until (rising_edge(clock)); Either True or False
q <= d after 5 ns;
qbar <= not d after 5 ns;
end process output;
end architecture beh;
31
Function: Examples (Locate inside
Entity)
library ieee;
use ieee.std_logic_1164.all;
entity test is
port(a,b: in integer;
C: out integer);
function Min (signal X, Y : Integer) return Integer is
variable temp : integer;
begin
Function if (X < Y) then Entity Part
declaration temp:= X;
and body else
temp:= Y;
end if;
return temp;
end Min;
end test;
32
FUNCTION Located in a Package
FUNCTION located in a PACKAGE can be
reused and shared by other projects.
33
Function Located inside package: Example
LIBRARY ieee;
USE ieee.std_logic_1164.all;
PACKAGE my_package IS
FUNCTION positive_edge(SIGNAL s: STD_LOGIC) RETURN BOOLEAN;
END my_package;
34
Function Located inside package: Example
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE work.my_package.all; Invoke the package
----------------------------------------------
ENTITY dff IS
PORT ( d, clk, rst: IN STD_LOGIC;
q: OUT STD_LOGIC);
END dff;
----------------------------------------------
ARCHITECTURE my_arch OF dff IS
BEGIN
PROCESS (clk, rst)
BEGIN
IF (rst='1') THEN q <= '0';
ELSIF positive_edge(clk) THEN q <= d;
END IF;
END PROCESS;
END my_arch;
35
Function Overloading
36
Subprogram Overloading
Subprograms
functions and procedures
Sometimes, it is convenient to have two or
more subprograms with the same name
function count (oranges: integer) return integer;
function count (apples: bit) return integer;
dff(clk, d, q, qbar);
dff(clk, d, q, qbar, reset, clear);
In this case, subprograms are overloaded.
compiler will decide which subprogram to call
based on the number and type of arguments.
37
Function Overloading-Example
library ieee;
use ieee.std_logic_1164.all;
entity overloading is
port(a: in std_logic_vector(7 downto 0);
b: in bit_vector(3 downto 0); Input:std_logic_vector
c1,c2: out integer range 0 to 30);
end entity;
architecture beh of overloading is
function f1 (x: std_logic_vector(7 downto 0)) return integer is
variable temp: integer:=0;
begin
for i in 0 to 7 loop
if (x(i)='1') then
temp := temp+1;
end if;
end loop;
return temp;
end f1;
function f1(x: bit_vector(3 downto 0)) return integer is
variable temp: integer:=0;
begin Input:bit_vector
for i in 0 to 3 loop
if x(i)='1' then
temp := temp+1;
end if;
end loop; If you change the bit_vector to
return temp; std_logic_vector in the second f1, you
end f1; will get the error of homograph of
begin
another object
c1<=f1(a);
c2<=f1(b); 38
end architecture;
Operator overloading using Functions
39
Example: Overloaded ‘‘+’’ Operator
Recall that the + operator accepts only
INTEGER, SIGNED, or UNSIGNED values
We are interested in writing a function
which should allow the sum of
STD_LOGIC_VECTOR values as well
Thus overloading the ‘‘+’’ operator
40
Example: Overloaded ‘‘+’’ Operator (II)
PACKAGE my_package IS
FUNCTION "+" (a, b: STD_LOGIC_VECTOR)
RETURN STD_LOGIC_VECTOR;
END my_package;
----------------------------------------------
PACKAGE BODY my_package IS
FUNCTION "+" (a, b: STD_LOGIC_VECTOR)
RETURN STD_LOGIC_VECTOR IS
VARIABLE result: STD_LOGIC_VECTOR;
VARIABLE carry: STD_LOGIC;
BEGIN
carry := '0';
FOR i IN a'REVERSE_RANGE LOOP
result(i) := a(i) XOR b(i) XOR carry;
carry := (a(i) AND b(i)) OR (a(i) AND carry) OR
(b(i) AND carry);
END LOOP;
RETURN result;
END "+";
END my_package; 41
Example: Overloaded ‘‘+’’ Operator (III)
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE work.my_package.all; Invoke the package
----------------------------------------------
ENTITY add_bit IS
PORT ( a: IN STD_LOGIC_VECTOR(3 DOWNTO 0);
y: OUT STD_LOGIC_VECTOR(3 DOWNTO 0));
END add_bit;
----------------------------------------------
ARCHITECTURE my_arch OF add_bit IS
CONSTANT b: STD_LOGIC_VECTOR(3 DOWNTO 0) := "0011";
CONSTANT c: STD_LOGIC_VECTOR(3 DOWNTO 0) := "0110";
BEGIN
y <= a + b + c; -- overloaded "+" operator
END my_arch;
42
Recursive Functions in VHDL
43
Recursive Functions in VHDL (I)
Recursion is a method of defining
functions in which the function being
defined calls itself.
The idea is to execute a task in a loop, in a
self similar way
Recursion is possible in VHDL
However, recursive logic has the disadvantage
of eating up lot of resources in FPGA
You cannot blindly write a recursive code in
your design
44
Recursive Functions in VHDL (II)
Example: Repetitive XOR operation on a 16 bit signal,
until the result obtained is 2 bit.
For example if the signal is "1111001100010101“
MSB 8 bits of signal : 11110011
LSB 8 bits of signal : 00010101
XOR operation : 11100110 ( call this signal as 'x1')
MSB 4 bits of x1 : 1110
LSB 4 bits of x1 : 0110
XOR operation : 1000 ( call this signal as 'x2')
MSB 2 bits of x2 : 10
LSB 2 bits of x2 : 00
XOR operation : 10 ( this should be the result obtained when the
code is simulated)
45
Recursive Functions in VHDL (III)
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
The num'length attribute is used to get the size of the
entity recursion is input argument
port ( num : in std_logic_vector(15 downto 0);
exor_out : out std_logic_vector(1 downto 0)
);
The signal on which XOR operation has to be
end recursion;
performed
architecture Behavioral of recursion is
begin
numf := num; the result of XOR operation
if (num'length = 4) then
exorf := numf(1 downto 0) xor numf(3 downto 2); the stop condition
else
exorf := exor(numf(num'length-1 downto num'length/2)) xor exor(numf((num'length/2)-1 downto 0));
end if;
return exorf;
end exor; The left half The right half
begin
exor_out <= exor(num); The recursive call is continued until the size of the
end Behavioral; signal becomes 4 46
Recursive Functions in VHDL (IV)
Let us see how this code is synthesized
The Technology schematic view of the design is
shown below
47
Recursive Functions in VHDL (V)
There are 4 LUT's used to implement the logic-Two 4 input LUT's and two
5 input LUT's.
48
Recursive Functions in VHDL (VI)
In C and other high level languages recursion is
implemented using stack and, there the main
issue is stack overflow
In a HDL like VHDL, the resources may get
heavily used for even simple codes
The synthesis tool implements the logic by
replicating the function in separate hardware
component.
If a function calls 10 times itself, then the resources
will be used nearly 10 times than that , when an
individual block is implemented
49
Some useful functions
50
Type conversion
VHDL is strongly depends to data types
Sometimes data type conversion is
necessary
For type conversion you should the
following library
Use ieee.std_logic_arith.all
Std_logic bit
Std_logic_vector bit_vector
Std_logic_vector integer
51
Type conversion
Functions:
Function to_stdlogicvector(s:bit_vector) return
std_logic_vector;
Function conv_integer(arg:std_logic_vector) return integer;
Function conv_std_logic_vector(arg:integer, size:integer)
return std_logic_vector;
Examples
1.
a,b : bit_vector(3 downto 0);
q : std_logic_vector(3 downto 0);
q<= a and b (error)
q<=to_stdlogicvector(a and b); (correct)
Convert to integer
2.
52
Procedures
53
Procedures (I)
Procedures are allowed to change the values of
the objects associated with its formal parameters
Parameters of procedures may of mode in, out
or inout
If no mode is specified the parameter is
interpreted as having mode in.
If no class is specified parameters of mode in are
interpreted as being of class constant and
parameters of mode out or inout are interpreted
as being of class variable.
A procedure can return more than one value
54
Procedure Syntax
55
Procedures (II)
Similar to functions, WAIT, SIGNAL
declarations, and COMPONENTS are not
synthesizable when used in a procedure
56
Procecdure:
Mode
Examples
Procedure ModTwo (X : inout Integer) is
begin Here, X is a variable
because class has
case X is
not been specified
When 0 | 1 => null;
When others => X := X mod 2;
end case;
end ModTwo;
57
Procedure Example
58
Procedures (III)
Signals can be passed to procedures and
updated within procedures
Signals cannot be declared within procedures
Visibility rules apply here (scope)
Procedure can update signals visible to it even if
these signals do not appear in the parameter list.
This is sometimes called as side effect of
procedure.
updating signals that do not appear in the
parameter list is a poor programming practice.
59
Procedure Call
60
Concurrent vs. Sequential Procedure Calls
Concurrent procedure call
Procedure calls can be made in concurrent
signal assignments
Execution of a procedure is concurrent with
other concurrent procedures, CSA statements,
or processes.
The procedure is invoked when there is an
event on a signal that is a parameter to the
procedure.
Sequential procedure call
Executed within the body of a process.
61
Example: Concurrent Procedure Call
entity serial_adder is
port(a, b, clk, reset: in std_logic; z: out std_logic);
end entity;
begin
C1: comb port(a=>a, b=>b, c_in=>s1, z=>z, carry=>s2);
process
begin
dff(clk=>clk, reset=>reset, d=>s2, q=>s1, qbar=>open);
wait on clk, reset, s2;
end process;
end architecture structural;
63
Procedure Location
See the figure presented for Functions
Procedure is usually placed in a PACKAGE
it can also be located in the main code (either
in the ENTITY or in the declarative part of the
ARCHITECTURE).
64
FUNCTION versus PROCEDURE
65
FUNCTION versus PROCEDURE (I)
A FUNCTION has zero or more input parameters
and a single return value. The input parameters
can only be CONSTANTS (default) or SIGNALS
(VARIABLES are not allowed).
70