Docsity

Subprograms and Packages - Computer Science - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Subprograms and Packages - Computer Science - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Packages

  • The primary purpose of a package is to encapsulate elements that can be shred (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.

Package (cont..)

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

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;

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

(cont..)

For I in s’range loop if s(I) /= undriven then drive_count := drive_count + 1; if drive_count = 1 then result := a (I); else result : = undriven end if; End loop Return result; -- return value End resolve_cluster; -- end function End cluspack; -- end package

Subprograms

  • Subprograms consist of
    • Procedure
    • Function

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.

Function syntax

  • To construct and use of a function, two

parts are necessary:

  • The function body and
  • A call to the function

Function body (cont..)

  • = [CONSTANT] constant_name: constant_type; or
  • = 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.

Example of function body

FUNCTION f

(a,b : integer; signal c : std_logic_vector ) return BOOLEAN is

Begin

(sequential statements)

END f1;

Function Location

Function/Procedure location

Package Main code

(+Package body)

Architecture Entity

Library

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;

Architecture behave of convert is function vector_to_int (s : log8) return integer is variable result : integer := 0; begin for I in 0 to 7 loop result : = result * 2: if s(I) = ‘1’ then result := result + 1; end if; end loop; return result; end vector_to_int; Begin o1 <= vector_to_int(i1); End behave; Docsity.com

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.