LHO 04 - Subprograms, Packages, and Libraries EGRE

LHO 04 - Subprograms, Packages, and Libraries EGRE

LHO 04 - Subprograms, Packages, and Libraries EGRE 631 1/26/09 EGRE 631 1 HW 03 due Wednesday Add the function TO_ASCII to pkg_cnt1s.vhd in HW 03.zip. The function TO_ASCII should convert an input Hex nibble to an 8-bit ASCII byte. For example if the input is 1010 the returned value is 01000001. Use tb_ascii_func.vhd in HW 03.zip to verify the TO_ASCII function.

EGRE 631 2 Packages A module that can be shared across many different VHDL modules. Typically contains: Related groups of functions and procedures. User defined types and constants. Packages can be placed in Libraries. Libraries are repositories for VHDL units. Packages, entities, architectures, and configurations. Collectively procedures, functions, packages, and libraries provide facilities for creating and maintaining modular and

reusable VHDL programs. EGRE 631 3 Subroutines VHDL subroutines are either functions or procedures. Functions: Procedures: EGRE 631 4 Functions

Functions compute a value bases on values of input parameters. All parameters are of mode in and cannot be modified by the function. Not necessary to specify mode in. Functions execute in 0 simulation time, and thus cannot contain wait statements. The function parameter is an unconstrained array. i.e. If the parameter is a std_logic_vector we do not need to specify the size. EGRE 631 5 Procedures Signals cannot be declared within procedures.

Signals can be passed into procedures as parameters. Unlike functions a procedure can modify its parameters. Specify mode in, out, or inout for parameter. Mode in is assumed. Procedures can make assignments to a signal that is within scope even though the signal is not on the procedures parameter list. This is not good programming practice and should be avoided. Unlike processes variables within a procedure are not static. EGRE 631 6 Local function example using majority gate entity

use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; use work.all; -- This is necessary to find pkg_cnt1s use pkg_cnt1s.all; -- Make items in pkg_cnt1s available entity majority is Port ( x : in std_logic; y : in std_logic; z : in std_logic; f : out std_logic); end majority; EGRE 631 7 Local function example (continued) -- Architecture using local function

architecture LFUNC of majority is function Lcnt1s(x: std_logic_vector) return integer is -- Use 32 bits as maximum word size variable count: integer range 0 to 32; Begin count := 0; for i in x'right to x'left loop if X(i) = '1' then count := count + 1; end if; end loop; return count; end function; signal w: std_logic_vector(2 downto 0); EGRE 631 8

Begin W <= X&y&z; process(w) Begin if Lcnt1s(w) > 1 then f <= '1' after 2 ns; else f <= '0' after 2 ns; end if; end process; end architecture LFUNC; EGRE 631 9

Same example using a global function Create a package (pkg_cnt1s.vhd) Create function gcnt1 in package body. Declare function gcnt1s in package to make it visible. In VHDL code that uses function specify that package will be used. Call function in architecture. EGRE 631 10 Package Pkg_cnt1s.vhd -- Package containg func.vcd V 1.3 8/3/08 library IEEE; use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; package pkg_cnt1s is function gcnt1s(x: std_logic_vector) return integer; end pkg_cnt1s; library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; end function; end pkg_cnt1s; EGRE 631 11 Package Pkg_cnt1s.vhd (continued)

package body pkg_cnt1s is ---------------------------------------------------------------- Function: gcnt1s - Global function to count #1's in x -Maximum size of x is 32 bits. ---------------------------------------------------------------function gcnt1s(x: std_logic_vector) return integer is -- Use 32 bits as maximum word size variable count: integer range 0 to 32; begin count := 0; for i in x'right to x'left loop if X(i) = '1' then count := count + 1; end if; end loop; return count; end function; EGRE 631 end pkg_cnt1s;

12 Majority.vhd using global function. library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; use work.all; -- This is necessary to find pkg_cnt1s use pkg_cnt1s.all; -- Make items in pkg_cnt1s available entity majority is Port ( x : in std_logic; y : in std_logic; z : in std_logic; f : out std_logic); end majority;

EGRE 631 13 -- Architecture using global function in pkg_cnt1s.vhd architecture GFUNC of majority is signal w: std_logic_vector(2 downto 0); begin W <= X&y&z; process(w) begin if Gcnt1s(w) > 1 then f <= '1' after 2 ns; else f <= '0' after 2 ns; end if;

end process; end architecture GFUNC; EGRE 631 14

Recently Viewed Presentations

  • Fluids - Mrs. Haug&#x27;s Website

    Fluids - Mrs. Haug's Website

    11.1 Mass Density. In this chapter, we discuss the behavior of fluids.. Fluids . by definition, include both . gases. and . liquids. Mass density . is the mass per unit volume of a liquid or gas
  • Future voice deep dive

    Future voice deep dive

    DSLAM. Voice. OLT. e.g. supports BT Consumer All-IP Voice service, over BroadBand. 21CN 21st Century Network. AN Access Network. BB Broadband. D Side Distribution Side. DLE Digital Local Exchange. E Side Exchange Side. DSLAM Digital Subscriber Line Access Multiplexer.
  • Chapter 1 Chapter Part One: Individuals As2Leaders Who

    Chapter 1 Chapter Part One: Individuals As2Leaders Who

    Leadership Theory Paradigm. Leadership trait theories attempt to explain distinctive characteristics accounting for leadership effectiveness. Behavioral leadership theories . attempt to explain distinctive styles used by effective leaders, or to define the nature of their . work. Contingency leadership theories
  • Parts of the Brain - Faculty of Medicine, University of ...

    Parts of the Brain - Faculty of Medicine, University of ...

    Association fibers. Confined to a hemisphere and connects cortical areas within the hemisphere. Transverse or Commissural fibers. Connects 2 hemispheres. Axons runs in corpus callosum and anterior commissure, Projection fibers. Connect cerebral cortex with subcortical structures, brain stem and spinal...
  • United Kingdom - Mrs. Silverman: Social Studies

    United Kingdom - Mrs. Silverman: Social Studies

    Neo-corporatism: To be able to establish and maintain a neo-corporatist interest group system, a country has to have peak associations that are able to enforce the agreements between business, labour, and the government. Consequently, in Scandinavia, Germany, Austria, and Switzerland,...
  • Win32 Programming - malware.org

    Win32 Programming - malware.org

    Sharing Data. There are lots of different ways to share data between processes. But the "lowest level" way is really in memory, via a memory-mapped file. Two or more processes map the same base address - hence they are sharing...
  • Presentation Objectives - ORNL DAAC

    Presentation Objectives - ORNL DAAC

    * Metadata Pecha Kucha * Metadata Pecha Kucha * Metadata Pecha Kucha * Metadata Pecha Kucha * Test * City of Salem 44,-123 44.992575,-123.056853 Organize your information Review for accuracy and completeness Do not use jargon Use informative title that...
  • Evidence-Based Blood Transfusion - Stanford University

    Evidence-Based Blood Transfusion - Stanford University

    Evidence-Based Blood Transfusion. Blood transfusion does not simply involve the anesthesiologist hanging pRBCs once 1000 ml of blood are in the suction container! ... "6-pack" used to refer to pooling of platelets from 6 donors which is rarely done anymore.