16/11/2006dsd,usit,ggsipu1 packages the primary purpose of a package is to encapsulate elements that...

29
16/11/2006 DSD,USIT,GGSIPU 1 Packages • The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design units. • A package is a common storage area used to hold data to be shared among a number of entities • Declaring data inside of a package allows the data to be referenced by other entities; thus data can be shared.

Upload: stewart-stafford

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 1

Packages

• The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design units.

• A package is a common storage area used to hold data to be shared among a number of entities

• Declaring data inside of a package allows the data to be referenced by other entities; thus data can be shared.

Page 2: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 2

Package (cont..)

• A package consists of two parts:– A package declaration section – A package body

Page 3: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 3

Package Declaration

• The package declaration section can contain the following declaration:– Subprogram declaration– Type, Subtype declaration– Constant, deferred constant– Component declaration– Attribute declaration– File declaration

Page 4: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 4

Package declaration (example)Package example is

type nineval is (z0,z1,z2,r0,r1,r2,f0,f1,f2);type t_cluster is arary (0 to 15) or nineval;type t_clus_vec is array (natural range <>) of t_cluster;function resolve (s : t_clus_vec)

return t_cluster;subtype t_wclus is resolve_cluster t_cluster;constant undriven : t_wclus;

End example;

Page 5: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 5

Package body

• The package body can also contain the following declaration:– Subprogram declaration/body– Type, subtype declaration– Constant declaration– File declaration– Alias declaration– Use clause

Page 6: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 6

Package body (example)

Package body cluspack isconstant undriven : t_wclus:= (zx,zx);function resolve_cluster (s: t_clus_vec)

return t_cluster isvariable result : t_cluster;variable drive_const: integer;

beginif s’length = 0 then return undrivenend if;

Page 7: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 7

(cont..)For I in s’range loop

if s(I) /= undriven thendrive_count := drive_count + 1;

if drive_count = 1 thenresult := a (I);

elseresult : = undriven

end if;End loopReturn result; -- return valueEnd resolve_cluster; -- end functionEnd cluspack; -- end package

Page 8: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 8

Subprograms

• Subprograms consist of– Procedure– Function

Page 9: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 9

Subprogram properties

• Similar to PROCESS statements• IF, CASE, and LOOP, WAIT is not allowed.• Fundamental difference at the level of application.• Process is intended for immediate use in the main

code, the others are intended mainly for Library allocation, I.e. their purpose is to store commonly used pieces of code, so they can be reused or shared by other projects.

• If, desired, a Function or Procedure can also be installed in the main code itself.

Page 10: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 10

Procedure and Function

• A procedure can return more than one argument.

• A procedure can have input parameters, output parameters and inout parameters

• A function always returns just one.

• All parameters are input parameters

• E.g. data type conversions, logical operations, arithmetic computations, new operator and attributes

• Signal declaration and component instantiation is not allowed.

Page 11: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 11

Function syntax

• To construct and use of a function, two parts are necessary:– The function body and– A call to the function

Page 12: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 12

Function body

FUNCTION function_name [<parameter list>] RETURN data_type is

[declaration]

Begin

[sequential statements]

END function_name;

Page 13: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 13

Function body (cont..)

• <parameter list> = [CONSTANT] constant_name: constant_type; or

• <parameter list>= SIGNAL signal_name:signal_type;

• VARIABLES are not allowed.

• There can be any type of synthesizable data types.

• No range specification should be included (e.g. do not enter range when using INTEGER or TO/Down to when using std_logic_vector)

• There is only one return value, whose type is specified by data_type.

Page 14: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 14

Example of function body

FUNCTION f1 (a,b : integer; signal c : std_logic_vector)

return BOOLEAN isBegin

(sequential statements)END f1;

Page 15: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 15

Function call

• A function is called as par of an expression. The expression can obviously appear by itself or associated to a statement (either concurrent or sequential).

• Example of function calls:X <= conv_integer (a); -- converts a to an integer

Y <= maximum (a,b); --returns the largest of a and b;

If x > maximum (a,b); --compares x to the largest of a,b

Page 16: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 16

Function Location

Function/Procedure location

Package Main code

(+Package body)

ArchitectureEntity

Library

Page 17: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 17

Function

Use library ieee;

Use ieee.std_logic_1164.all;

package num_type is

type log8 is array (0 to 7) of std_logic;

end num_type;

Page 18: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 18

Use library ieee;

Use ieee.std_logic_1164.all;

Use work.num_type.all;

Entity convert is

port (i1 : in log8;

o1 : out integer);

End convert;

Page 19: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 19

Architecture behave of convert is function vector_to_int (s : log8)

return integer isvariable result : integer := 0;begin

for I in 0 to 7 loopresult : = result * 2:

if s(I) = ‘1’ thenresult := result + 1;

end if;end loop;return result;

end vector_to_int;Begin

o1 <= vector_to_int(i1);End behave;

Page 20: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 20

Conversion Function

• Conversion Function are used to convert an object of one type to another.

• They are used in component instantiation statement to allow mapping of signals and ports of different types.

Page 21: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 21

Conversion function (example)

Function conversion4val (s: fourval)return fourvalue is

Begincase s is

when x => return ‘x’;when L => return ‘0’;when H => return ‘1’when z => return ‘z’;

end case;End convert4val;

Page 22: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 22

Resolution Function

• A resolution function is used to return the value of a signal when the signal is driven by multiple drivers.

• A resolution function consists of a function that is called whenever one of the drivers for the signal has an event occur on it.

• The resolution function is executed and returns a single value from all of the driver values; this value is the new value of the signal.

Page 23: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 23

Operator Overloading

• When a standard operator symbol is made to behave differently based on the type of its operands, the operator is said to be overloaded.

• Function bodies are written to define the behavior of overloaded opertors.

• The number of parameters in such a function must match in cardinality with that of the predefined operator.

Page 24: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 24

Procedure

• A procedure is very similar to a function.

• A procedure can return more than one value

• Two parts are required to construct and use a procedure– Procedure body– Procedure call

Page 25: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 25

Procedure body

Procedure procedure_name [<parameter list>] is

[declarations]

Begin

(sequential statements)

End procedure_name;

Page 26: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 26

Procedure (example)

• Procedure my_procedure( a : in std_logic;

signal b,c : in std_logic;

signal x : out std_logic_vector(7 downto 0);

signal y : inout integer range 0 to 99) is

Begin

------

End my_procedure;

Page 27: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 27

<parameter list>

• <parameter list> = [CONSTANT] constant_name: mode type

• <parameter list> = SIGNAL signal_name: mode type; or

• <parameter list> = VARIABLE variable_name : mode type

• A procedure can have any number of IN,OUT or INOUT parameters,

Which can be SIGNALS, VARIABLES, or CONSTANTS.

Page 28: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 28

Procedure call

• Procedure call is a statement on its own.

• It can appear by itself or associated to a statement (either concurrent or sequential)

• Ex.– Compute_min_max(in1,in2,out1,out2)

Page 29: 16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design

16/11/2006 DSD,USIT,GGSIPU 29

Procedure Location

• The typical locations of a procedure are the same as those of a function.