C. Sisterna UNSJ Argentina. PACKAGE and SUBPROGRAMS

Similar documents
Subprograms 2 DSDA. Cristian Sisterna

Basic Language Concepts

Hardware Description Language VHDL (1) Introduction

Concurrent Signal Assignment Statements (CSAs)

JUNE, JULY 2013 Fundamentals of HDL (10EC45) PART A

Abi Farsoni, Department of Nuclear Engineering and Radiation Health Physics, Oregon State University

Digital Systems Design

COE 405 Design Methodology Based on VHDL

Getting Started with VHDL

ECE 545 Lecture 5. Data Flow Modeling in VHDL. George Mason University

UNIT I Introduction to VHDL VHDL: - V -VHSIC, H - Hardware, D - Description, L Language Fundamental section of a basic VHDL code Library :

CprE 583 Reconfigurable Computing

Building Blocks. Entity Declaration. Entity Declaration with Generics. Architecture Body. entity entity_name is. entity register8 is

EECE-4740/5740 Advanced VHDL and FPGA Design. Lecture 3 Concurrent and sequential statements

VHDL. Official Definition: VHSIC Hardware Description Language VHISC Very High Speed Integrated Circuit

Hardware Description Languages. Modeling Complex Systems

Review. LIBRARY list of library names; USE library.package.object; ENTITY entity_name IS generic declarations PORT ( signal_name(s): mode signal_type;

1. Using the for-generahon scheme, concurrent statements can be replicated a predetermined number of times.

Subprograms, Packages, and Libraries

Lecture 10 Subprograms & Overloading

Control and Datapath 8

Lecture 7. Standard ICs FPGA (Field Programmable Gate Array) VHDL (Very-high-speed integrated circuits. Hardware Description Language)

Contents. Appendix D VHDL Summary Page 1 of 23

Multi-valued Logic. Standard Logic IEEE 1164 Type std_ulogic is ( U, uninitialized

Digital Design Using VHDL Using Xilinx s Tool for Synthesis and ModelSim for Verification Part II

ECE U530 Digital Hardware Synthesis. Course Accounts and Tools

Introduction to VHDL #3

VHDL for FPGA Design. by : Mohamed Samy

Introduction to VHDL #1

Introduction to VHDL. Main language concepts

Review of Digital Design with VHDL

ECE 545 Lecture 8. Data Flow Description of Combinational-Circuit Building Blocks. George Mason University

What Is VHDL? VHSIC (Very High Speed Integrated Circuit) Hardware Description Language IEEE 1076 standard (1987, 1993)

VHDL 2 Combinational Logic Circuits. Reference: Roth/John Text: Chapter 2

CSCI Lab 3. VHDL Syntax. Due: Tuesday, week6 Submit to: \\fs2\csci250\lab-3\

Digital Design Using VHDL Using Xilinx s Tool for Synthesis and ModelSim for Verification

Hardware Modeling. VHDL Basics. ECS Group, TU Wien

Advanced Training Course on FPGA Design and VHDL for Hardware Simulation and Synthesis. 26 October - 20 November, 2009

Lab # 5. Subprograms. Introduction

310/ ICTP-INFN Advanced Tranining Course on FPGA and VHDL for Hardware Simulation and Synthesis 27 November - 22 December 2006

ECE 448 Lecture 3. Combinational-Circuit Building Blocks. Data Flow Modeling of Combinational Logic

ECE 448 Lecture 3. Combinational-Circuit Building Blocks. Data Flow Modeling of Combinational Logic

VHDL for Complex Designs

VHDL VS VERILOG.

Design units can NOT be split across different files

Lattice VHDL Training

IE1204 Digital Design L7: Combinational circuits, Introduction to VHDL

Outline. CPE/EE 422/522 Advanced Logic Design L05. Review: General Model of Moore Sequential Machine. Review: Mealy Sequential Networks.

VHDL Structural Modeling II

Introduction to VHDL. Yvonne Avilés Colaboration: Irvin Ortiz Flores Rapid System Prototyping Laboratory (RASP) University of Puerto Rico at Mayaguez

VHDL: A Crash Course

VHDL. ELEC 418 Advanced Digital Systems Dr. Ron Hayne. Images Courtesy of Cengage Learning

Modeling Complex Behavior

C-Based Hardware Design

Basic Language Elements

Chapter 6 Combinational-Circuit Building Blocks

Digital Systems Design

ENGIN 241 Digital Systems with Lab

Lecture 3: Modeling in VHDL. EE 3610 Digital Systems

EE434 ASIC & Digital Systems

EE261: Intro to Digital Design

EECS 373 Design of Microprocessor-Based Systems

Logic and Computer Design Fundamentals VHDL. Part 1 Chapter 4 Basics and Constructs

VHDL Sequential Statements. Cristian Sisterna

EE 595. Part II Design Units in VHDL. EE 595 EDA / ASIC Design Lab

VHDL/Verilog Simulation. Testbench Design

Lecture 12 VHDL Synthesis

COE 405, Term 062. Design & Modeling of Digital Systems. HW# 1 Solution. Due date: Wednesday, March. 14

Lecture 1: VHDL Quick Start. Digital Systems Design. Fall 10, Dec 17 Lecture 1 1

Lecture 5: State Machines, Arrays, Loops. EE 3610 Digital Systems

!"#$%&&"'(')"*+"%,%-".#"'/"'.001$$"

The block diagram representation is given below: The output equation of a 2x1 multiplexer is given below:

ELCT 501: Digital System Design

Basic Language Constructs of VHDL

cla library ieee; use ieee.std_logic_1164.all;

Introduction to VHDL

Lecture 3 Introduction to VHDL

VHDL Testbench Design. Textbook chapters 2.19, , 9.5

Digital Systems Design

EECS 373 Design of Microprocessor-Based Systems

[VARIABLE declaration] BEGIN. sequential statements

5. 0 VHDL OPERATORS. The above classes are arranged in increasing priority when parentheses are not used.

EECS 373 Design of Microprocessor-Based Systems

Embedded Systems CS - ES

Mridula Allani Fall Fall

5. VHDL - Introduction - 5. VHDL - Design flow - 5. VHDL - Entities and Architectures (1) - 5. VHDL - Entities and Architectures (2) -

Part 4: VHDL for sequential circuits. Introduction to Modeling and Verification of Digital Systems. Memory elements. Sequential circuits

Declarations of Components and Entities are similar Components are virtual design entities entity OR_3 is

1. Defining and capturing the design of a system. 2. Cost Limitations (low profit margin must sell millions)

Arithmetic Circuits. Nurul Hazlina Adder 2. Multiplier 3. Arithmetic Logic Unit (ALU) 4. HDL for Arithmetic Circuit

Outline CPE 626. Advanced VLSI Design. Lecture 4: VHDL Recapitulation (Part 2) Signals. Variables. Constants. Variables vs.

EEL 4783: Hardware/Software Co-design with FPGAs

VHDL Examples Mohamed Zaky

Computer-Aided Digital System Design VHDL

Tutorial 4 HDL. Outline VHDL PROCESS. Modeling Combinational Logic. Structural Description Instantiation and Interconnection Hierarchy

-- Fill in values for each generic. -- Fill in values for each signal. SIGNAL load_start : std_ulogic := '1'; SIGNAL clock : std_ulogic := '0';

Test Bench. Top Level Model Test Bench MUT

EE 459/500 HDL Based Digital Design with Programmable Logic. Lecture 4 Introduction to VHDL

Inthis lecture we will cover the following material:

VHDL: Code Structure. 1

Transcription:

C. Sisterna UNSJ Argentina PACKAGE and SUBPROGRAMS

Package Syntax 2 package <package_name> is [subprograma_declarations]; [constant_declarations]; [type_declarations]; [component_declarations]; [attribute_declarations]; [attribute_specifications]; end <package_name>; package body <package_name> is [subprogram_bodies]; [internal_subprogram_declarations]; [internal_constant_declaration]; [internal_type_declaration]; end <package_name>;

Package A package declaration contains a set of declarations that may possible be shared by many design units. Common declarations are constants, functions, procedures and types A package body contains the hidden details of a package, such as the behavior of the subprograms Items declared in a package can be accessed by other design unit by using the library and use clauses

Declaring Subprograms in Packages -- package declaration package my_package is constant word_size: positive:=16; subtype word is bit_vector (word_size-1 downto 0); subtype address is bit_vector(address_size-1 downto 0); type status_value is (halted, idle, fetch, mem_read, mem_wr, io_read); function int_to_bit_vector (int_valor: integer) return bit_vector; end my_package; Function Declaration 4 -- package body package body my_package is function int_to_bit_vector (int_vector: integer) return bit_vector is...... end int_to_bit_vector; end my_package; Function Body

Package.vhd File - Example library ieee; use ieee.std_logic_1164.all; -- package declaration package my_pack is constant width: integer := 8; function max(a, b: in std_logic_vector)return std_logic_vector; end; -- package body package body my_pack is function max(a, b: in std_logic_vector)return std_logic_vector is if a> b then return a; else return b; end if; end; end; 5

Package Usage library ieee; use ieee.std_logic_1164.all; use work.my_pack.all;-- make the package available 6 entity package_test_2 is port(d1,d2,d3: in std_logic_vector(width-1 downto 0); z: out std_logic_vector(width-1 downto 0); q: out std_logic_vector(width-1 downto 0)); end; architecture beh of package_test_2 is q <= max(d1,d2); -- concurrent call of the function process(d2,d3) z <= max(d3, d2); end process; end architecture ; -- sequential call of the function

Package Common Usage library ieee; use ieee.std_logic_1164.all; 7 package my_pack is -- constants declarations constant bus_width : natural:=2; -- component declarations component bcd_7segm is port( led_outs : out std_logic_vector(6 downto 0); bcd_inps : in std_logic_vector(3 downto 0) ); end component; component mux is port( inf_inputs: in std_logic_vector(3 downto 0); sel : in std_logic_vector(1 downto 0); output : out std_logic); end component; end package my_pack;

Package Common Usage 8 library ieee; use ieee.std_logic_1164.all; use work.my_pack.all;-- make the package available entity bcd_7seg_top is port (...... ); end; architecture beh of package_test_2 is U1: bcd_7segm port map (... ); U2: mux port map (... );... end beh;

Subprograms 9 F U N C T I O N S P R O C E D U R E S

Subprograms 10 Sequence of statements that can be executed from multiple locations in the design Provide a method for breaking large segments of code into smaller segments Types of subprograms: Functions: used to calculate and return one value Procedures: used to define an algorithm which affects many values or no values

Subprograms 11 Function - Return only one value - Executed in zero simulation time - Can be invoked from an expression Procedure - Return zero or several values - Can or can not be executed in zero simulation time (wait statement)

function 12 Provides a method of performing simple algorithms Produce a single return value Invoked by an expression Can not modify parameters. Only mode allowed: IN (read only) wait statements are NOT permitted in a function

function Syntax 13 Declarative part function function_name(parameters_list)return return_type is [function_declarations;] sequential_statements; return simple_value; end [function] [function_name]; Behavioral part

function Parts - Definitions 14 [function_declarations] -- the only class declarations allowed are: Variables Constants <parameters_list> - mode in only - signals only <sequential_statements> - sequential instructions only - always has to end with a return statement

function - Key Concepts Variables can be used to accumulate results or hold intermediate values. But, variables declared within the function do not keep the value between executions 15 Initial values assigned to variables are synthesizable Function can have more than one return, however just one is executed It is useful to declare the parameters as undefined for a general use of the function

function - Example 1 16 library ieee; use ieee.std_logic_1164.all; package my_pack is function max(a,b: in std_logic_vector)return std_logic_vector; end package; package body of my_pack is function max(a,b: in std_logic_vector)return std_logic_vector is if a> b then return a; else return b; end if; end function; end package body;

function Usage - Example 1 library ieee; use ieee.std_logic_1164.all; use work.my_pack.all;... entity example is port(...); end; architecture beh of example is... q_d1d2 <= max(d1,d2); process(d3,d4) q_d3d4 <= max(b=>d3, a=>d4); end process; end; Package holding function declaration and behavior Parameter passing list: by position -- concurrent call -- sequential call Parameter passing list: by name 17 Note: d1, d2, data and g have to be std_logic_vector type

function - Example 2 18 -- convert Boolean to Bit function bool_to_bit(b:boolean) return bit is if b then return '1'; else return '0'; end if; end function bool_to_bit;

function - Example 3 function sehctam_tnuoc (a, b: bit_vector) return natural is variable va : bit_vector(a'length-1 downto 0):= a; variable vb : bit_vector(b'length-1 downto 0):= b; variable cnt: natural := 0; assert va'length = vb'length report the vectors have different size severity failure; for i in va'range loop if va(i) = vb(i) then cnt := cta + 1; end if; end loop; return cnt; end funciton; 19

function Example 4 -- FUNCTION BIN2GRAY(VALUE) -- Used to convert Binary into Gray-Code function bin2gray(value:std_logic_vector)return std_logic_vector is variable grayval:std_logic_vector((value'length-1) downto 0); variable result: std_logic_vector((value'length-1) downto 0); l_oop:for i in 0 to (value'length-1) loop if(i=value'length-1) then grayval(i) := value(i); else grayval(i) := value(i+1) xor value(i); end if; end loop l_oop; result := grayval; return result; end function bin2gray; 20

-- integer to bit_vector conversion -- result'low is the lsb function to_vector (nbits: positive; int_val: natural) return bit_vector is variable m1: natural := 0; variable result:bit_vector(nbits-1 downto 0):=(others => '0'); m1 := int_val; for j in result'reverse_range loop if (m1/mod2) = 1 then result(j) := '1'; else result(j) := '0'; end if; m1 := m1/2; end loop m1; return result; end function to_vector; function - Example 5 21

procedure Provides a method of performing complex algorithms May produce multiple return (output) values Invoked by a statement Can affect input parameters Parameter s mode: IN: read only OUT: write only INOUT: read/write 22

procedure - Syntax 23 Declarative part procedure proced_name (parameter_list) is [procedure_declarations] sequential_statements; end [procedure] [proced_name]; Behavioral part

procedure - Definitions 24 <procedure_declarations> -- the only class declarations allowed are: Variables Functions Constants Types <parameter_list> in mode: to receive values out mode: to return values inout mode: to receive/return values Parameters can be signals, variables and constants. By default in = constant, out = variable Can return as many values as are needed using the out parameters

procedure - Calls Can be used in sequential or concurrent statements areas Concurrent calls are activated by changes in any signal associated with a parameter of mode IN or INOUT Sequential calls is executed whenever the procedure is encounter during the execution of the sequential code Passing parameter list: Association by name Association by position 25

procedure - Example 1 26 procedure calc(a,b: in integer; avg, max: out integer) is avg<= (a+b)/2; if a>b then max<=a; else max<=b; end if; end calc;

procedure - Example 2 27 -- sum, cout as variables procedure full_adder (a,b,c: in bit; sum, cout: out bit) is sum := a xor b xor c; cout := (a and b) or (a and c) or (b and c); end full_adder;

procedure - Example 3 28 -- sum, cout as variables procedure full_adder(a,b,c:in bit; signal sum, cout: out bit)is sum <= a xor b xor c; cout<= (a and b) or (a and c) or (b and c); end full_adder;

procedure - Example 3 entity adder4 is port ( a,b : in bit_vector(3 downto 0); cin : in bit; sum : out bit_vector(3 donwto 0); cout: out bit); end entity; architecture behavioral of adder4 is process (a, b, cin) variable result: bit vector(3 downto 0); variable carry : bit: full_adder(a(0), b(0), cin, result(0), carry); full_adder(a(1), b(1), carry, result(1), carry); full_adder(a(2), b(2), carry, result(2), carry); full_adder(a(3), b(3), carry, result(3), carry); sum <= result; cout <= carry; end process; end behavioral; 29

procedure - Example 4 architecture rtl of ex2 is procedure calc (a,b: in integer; avg, max: out integer) is avg:= (a+b)/2; if a>b then max:=a; else max:=b; end if; end calc; calc(d1,d2,d3,d4); -- concurrent call. ok? process(d3,d4) variable a,b: integer; calc(d3,d4,a,b); q3<=a; q4<=b;... end process; end; -- sequential call. ok? 30

procedure - Example 4 architecture rtl of ex3 is procedure calc(a,b:in integer; signal avg, max:out integer)is avg<= (a+b)/2; if a>b then max<=a; else max<=b; end if; end calc; calc(d1,d2,d3,d4); -- concurrent call. ok? process(d3,d4) variable a,b: integer; calc(d3,d4,a,b); -- sequential call. ok? q3<=a; q4<=b;... end process; end; 31

procedure - Example 5 32 procedure echo(constant str : in string) is variable l: line; write(l, string'("<")); write(l, time'(now)); write(l, string '("> ")); write(l, str); writeline(output,l); end echo;

procedure - Example 6 33 --------------------------------------------------- -- --------------------------------------------------- procedure echo_now(constant str : in string) is variable l : line; write(l, str); write(l, string'(" @ <--")); write(l, time'(now)); write(l, string'("--> ")); writeline(output, l); end echo_now;

AMBA Bus Write Operation 34

AMBA Bus Test Rd/Wr procedures package tc_amba_tb_pckg is procedure wr_amba_apb( signal pclock : in std_logic; constant addr : in std_logic; constant data : in std_logic_vector(7 downto 0); signal paddr : out std_logic; signal pselx : out std_logic; signal pwrite : out std_logic; signal pwdata : out std_logic_vector(7 downto 0); signal penable : out std_logic); procedure rd_amba_apb( signal pclock : in std_logic; constant addr : in std_logic; signal data : out std_logic_vector(7 downto 0); signal paddr : out std_logic; signal pselx : out std_logic; signal pwrite : out std_logic; signal prdata : in std_logic_vector(7 downto 0); signal penable : out std_logic); end package; 35

AMBA Bus Write procedure (1) 36... package tc_amba_tb_pckg is... -- AMBA APB Write ----- procedure wr_amba_apb( signal pclock : in std_logic; constant addr : in std_logic; constant data : in std_logic_vector(7 downto 0); signal paddr : out std_logic; signal pselx : out std_logic; signal pwrite : out std_logic; signal pwdata : out std_logic_vector(7 downto 0); signal penable : out std_logic);...

AMBA Bus Write procedure (2) 37 package body tc_amba_tb_pckg is wait until rising_edge(pclock); wait for 4 ns; paddr <= addr; pwrite <= '1'; pselx <= '1'; pwdata <= data; wait until rising_edge(pclock); wait for 4 ns; penable <= '1'; wait until rising_edge(pclock); wait for 4 ns; pselx <= '0'; penable <= '0'; pwrite <= '0'; end wr_amba_apb;

AMBA Bus Rd/Wr procedure (3) wr_rd_proc: process 38 -- initial values paddr <= '0'; pwrite <= '0'; pselx <= '0'; penable <= '0'; pwdata <= x"00"; rd_data <= x"00"; wait for 100 ns; wait until reset = '0'; wait for 14 ms; echo_now(" -> Set TC Mode to IDLE "); echo(" ------------------------------------------------"); -- Write '00' @ address '0' (CRA) wr_amba_apb(pclock,ctrlreg,tc_idle,paddr,pselx,pwrite,pwdata,penable); -- Read Control Register rd_amba_apb(pclock,ctrlreg,rd_data,paddr,pselx,pwrite,prdata,penable); wait for clock_12mhz_hperiod; -- Check Control Register value assert(rd_data=tc_idle) report "Error on reading Ctrl Reg, expected value & slv2str(tc_idle)& " read value "& slv2str(rd_data) severity FAILURE; echo (" -- Control Register succesfully read -- "); wait for 77 ms;

AMBA Bus Read procedure procedure rd_amba_apb( -- AMBA APB Read -- signal pclock: in std_logic;...) is wait until rising_edge(pclock); wait for 4 ns; paddr <= addr; pwrite <= '0'; pselx <= '1'; wait until rising_edge(pclock); wait for 4 ns; penable <= '1'; wait until rising_edge(pclock); wait for 4 ns; pselx <= '0'; penable <= '0'; pwrite <= '0'; data <= prdata; end rd_amba_apb; end tc_amba_tb_pckg 39

Subprograms Overloading 40 When two or more subprograms have the same name but different types in the list of parameters function max (a, b: in std_logic_vector) return std_logic_vector); function max (a, b: in bit_vector) return bit_vector); function max (a, b: in integer) return integer;