Subprograms Lecture 6. Subprograms A subprogram defines a sequential algorithm that performs some computations. Subprograms can be: –1. functions –2.

Slides:



Advertisements
Similar presentations
COE 405 VHDL Basics Dr. Aiman H. El-Maleh Computer Engineering Department King Fahd University of Petroleum & Minerals Dr. Aiman H. El-Maleh Computer Engineering.
Advertisements

1 Introduction to VHDL (Continued) EE19D. 2 Basic elements of a VHDL Model Package Declaration ENTITY (interface description) ARCHITECTURE (functionality)
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
1/8/ L17 Resolved SiganlsCopyright Joanne DeGroat, ECE, OSU1 Resolved Signals What are resolved signals and how do they work. Resolution???
COE 405 Basic Concepts in VHDL Dr. Aiman H. El-Maleh Computer Engineering Department King Fahd University of Petroleum & Minerals Dr. Aiman H. El-Maleh.
Sistemas Digitais I LESI - 2º ano Lesson 5 - VHDL U NIVERSIDADE DO M INHO E SCOLA DE E NGENHARIA Prof. João Miguel Fernandes Dept.
Why Behavioral Wait statement Signal Timing Examples of Behavioral Descriptions –ROM.
SubprogramsSubprograms. SubprogramsSubprograms ä Similar to subprograms found in other languages ä Allow repeatedly used code to be referenced multiple.
ELEN 468 Lecture 191 ELEN 468 Advanced Logic Design Lecture 19 VHDL.
Topics of Lecture Structural Model Procedures Functions Overloading.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
ECE C03 Lecture 141 Lecture 14 VHDL Modeling of Sequential Machines Hai Zhou ECE 303 Advanced Digital Design Spring 2002.
VHDL. What is VHDL? VHDL: VHSIC Hardware Description Language  VHSIC: Very High Speed Integrated Circuit 7/2/ R.H.Khade.
Package with 4-valued logic Signal Attributes Assertion Data Flow description.
Today’s Lecture Process model –initial & always statements Assignments –Continuous & procedural assignments Timing Control System tasks.
Introduction to VHDL (part 2)
1 Data Object Object Types A VHDL object consists of one of the following: –Signal, Which represents interconnection wires that connect component instantiation.
1 H ardware D escription L anguages Basic Language Concepts.
1 Data Object Object Types A VHDL object consists of one of the following: –Signal, Which represents interconnection wires that connect component instantiation.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
IAY 0600 Digitaalsüsteemide disain Event-Driven Simulation Alexander Sudnitson Tallinn University of Technology.
Introduction to VHDL Arab Academy for Science, Technology & Maritime Transport Computer Engineering Department Magdy Saeb, Ph.D.
A VHDL Tutorial ENG2410. ENG241/VHDL Tutorial2 Goals Introduce the students to the following: –VHDL as Hardware description language. –How to describe.
ECE 368 CAD-Based Logic Design Shantanu Dutt Lecture 11 File I/O and Textio in VHDL (courtesy of Karam Chatha, ASU)
VHDL IE- CSE. What do you understand by VHDL??  VHDL stands for VHSIC (Very High Speed Integrated Circuits) Hardware Description Language.
7/10/2007DSD,USIT,GGSIPU1 Basic concept of Sequential Design.
2-Jun-16EE5141 Chapter 3 ä The concept of the signal ä Process concurrency ä Delta time ä Concurrent and sequential statements ä Process activation by.
CPS120: Introduction to Computer Science Functions.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Fall 2004EE 3563 Digital Systems Design EE 3563 VHDL – Basic Language Elements  Identifiers: –basic identifier: composed of a sequence of one or more.
Copyright(c) 1996 W. B. Ligon III1 Getting Started with VHDL VHDL code is composed of a number of entities Entities describe the interface of the component.
Fall 2004EE 3563 Digital Systems Design EE 3563 VHSIC Hardware Description Language  Required Reading: –These Slides –VHDL Tutorial  Very High Speed.
VHDL Very High Speed Integrated Circuit Hardware Description Language Shiraz University of shiraz spring 2011.
Electrical and Computer Engineering University of Cyprus LAB 1: VHDL.
Topics AliasesSubprograms Generics & Configurations.
Generate Statement A generate statement provides a mechanism for iterative or conditional elaboration of a portion of description. The iterative elaboration.
(1) Basic Language Concepts © Sudhakar Yalamanchili, Georgia Institute of Technology, 2006.
L13 – VHDL Language Elements. VHDL Language Elements  Elements needed for FPGA design Types  Basic Types  Resolved Types – special attributes of resolved.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
L19 – Resolved Signals. Resolved Signals  What are resolved signals In systems In VHDL Resolution – Isn’t that for resolving conflicts?  Ref: text Unit.
Advanced topics Lecture 7. Outline 7.1. Resolution functions 7.2. Guarded signals. Disconnection 7.3. GENERATE statement.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Structural modelling. Configurations Lecture 5. Outline 5.1. Structural modelling 5.2. Generics 5.3. Configurations.
VHDL Discussion Sequential Sytems. Memory Elements. Registers. Counters IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
04/26/20031 ECE 551: Digital System Design & Synthesis Lecture Set : Introduction to VHDL 12.2: VHDL versus Verilog (Separate File)
VHDL Discussion Subprograms IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
Relational Operators Result is boolean: greater than (>) less than (=) less than or equal to (
IAY 0600 Digital Systems Design Event-Driven Simulation VHDL Discussion Alexander Sudnitson Tallinn University of Technology.
Slide 1 3.VHDL/Verilog Description Elements. Slide 2 To create a digital component, we start with…? The component’s interface signals Defined in MODULE.
Case Study: Xilinx Synthesis Tool (XST). Arrays & Records 2.
Dataflow modelling Lecture 4. Dataflow modelling Specifies the functioning of a circuit without explicitly refer to its structure Functioning is described.
EGRE 6311 LHO 04 - Subprograms, Packages, and Libraries EGRE 631 1/26/09.
1 Introduction to Engineering Spring 2007 Lecture 18: Digital Tools 2.
Introduction To VHDL 홍 원 의.
Basic Language Concepts
IAY 0600 Digitaalsüsteemide disain
Chapter 2. Introduction To VHDL
ECE 434 Advanced Digital System L15
CPE 528: Lecture #4 Department of Electrical and Computer Engineering University of Alabama in Huntsville.
CPE/EE 422/522 Advanced Logic Design L11
ECE 434 Advanced Digital System L10
VHDL Discussion Subprograms
CPE 528: Lecture #3 Department of Electrical and Computer Engineering University of Alabama in Huntsville.
VHDL Discussion Subprograms
IAS 0600 Digital Systems Design
Data Object By E. Thirumeni Department of Electronics
Design units Lecture 2.
EEL4712 Digital Design (VHDL Tutorial).
Presentation transcript:

Subprograms Lecture 6

Subprograms A subprogram defines a sequential algorithm that performs some computations. Subprograms can be: –1. functions –2. procedures Functions: –Compute a single value –Execute in zero simulation time => do not contain WAIT statements. Procedures: –May return zero or more values –May contain WAIT statements In that case do not execute in zero simulation time NOT RECOMMENDED !! –May be called sequentially or concurrently.

Subprograms: specification Subprogram-specification: subprogram-specification IS subprogram-item-declarations BEGIN subprogram-statements END [FUNCTION | PROCEDURE] [subprogram-name]; Subprogram-specification: - gives the name of the subprogram - defines the interface of the subprogram, i.e. the formal parameters. Subprogram-item-declarations: the declarations from the subprogram Subprogram-statements: sequential statements and the RETURN statement Subprogram-name at the end: if it appears, is the same as the name from subprogram-specification Symbol ; at the end is mandatory.

Specification: parameters Formal parameters have: –name –Type –class: constant, variable, signal, FILE –mode: IN, OUT, INOUT The mode of the formal parameters: –IN – may be read, but not modified –OUT –may be modified, but may not be read –INOUT: may be both read and modified –Files don’t have mode, they may be read or written, depending on how they are opened. –Functions may have only parameters of mode IN, procedures may have parameters of any mode.

Subprogram parameters Subprogram calls use actual parameters. Associations: –To a formal parameter of class signal it may be associated only an actual parameter of class signal. –To a formal parameter of class variable it may be associated only an actual parameter of class variable. –To a formal parameter of class file it may be associated only an actual parameter of class file. –To a formal parameter of class constant it may be associated an expression. –The type of the formal and actual parameter must be the same –If a formal parameter is an unconstrained array, its size will be given by the size of the actual parameter Signals: –In functions we MAY NOT assign values to signals –If inside a procedure there is a signal assignment statement, then the signal driver is affected immediately, no matter if the procedure ends or not. –In procedures (and in functions) it is not allowed to use attributes that generate new signals: ‘STABLE, ‘QUIET, ‘TRANSACTION, ‘DELAYED

Subprograms: declarations Subprogram-item-declarations: –May be type declarations or object declarations (constants, variables, but NOT signals!) –May not be component declarations ! –These declarations are visible only inside subprograms and become effective only at the subprogram call. –Hence, a variable declared inside a subprogram is initialized at every call of the subprogram and It exists only during the execution of the subprogram. (A variable declared inside a process is initialized at the beginning of the simulation, exists for the entire duration of the simulation and it maintains its value from one execution of the process to another)

Specification: statements The statements from subprograms can be: –Sequential statements (like in processes) –RETURN statement: Syntax: RETURN [expression]; It may exist only in subprograms When it is executed the subprogram ends and the control is given back to the program that called the subprogram. All functions must contain RETURN expression; –Value of the expression is returned to the program that called the function In procedures the OUT and INOUT parameters return values, too. Procedures may have side effects, i.e. they can affect objects that are declared globally to the procedure -> NOT recommended !!

Procedures Procedures permit the de-composing of long code sequences into sections. Unlike a function, a procedure may return zero or more values. Syntax for procedure specification: PROCEDURE procedure_name(parameter_list) where parameter_list specifies the list of formal parameters. Parameter classes: constant, variable, signals (and files) Parameter mode: IN, OUT, INOUT. If the parameter class is not specified, then it will be constant for the parameters of mode IN and variable for the parameters of mode OUT or INOUT. It it is not specified the mode of a parameter, then implicitly it will be IN. Example of procedure: TYPE op_code IS (add, sub, mul, div, lt, le, eq); …

Procedures PROCEDURE alu_proc IS (a,b: IN INTEGER; op: op_code; z: OUT INTEGER; zcomp: OUT BOOLEAN) IS BEGIN CASE op IS WHEN add => z:=a+b; WHEN sub=> z:=a-b; WHEN mul=> z:=a*b; WHEN div=> z:=a/b; WHEN lt=> zcomp := a<b; WHEN le=> zcomp:= a<=b; WHEN eq=> zcomp:=a=b; END CASE; END PROCEDURE alu_proc;

Procedures Procedure call can be sequential or concurrent. Syntax: [label:] procedure_name(list_of_actuals); The concurrent procedure call is equivalent with a process that contains the sequential call and that has a sensitivity list containing the parameters belonging to class signal and having the mode IN or INOUT. Actual parameters can be specified by positional or named association. Example: alu_proc(d1,d2,add, sum, comp); -- positional association alu_proc(z=>sum, b=>d2, a=>d1, op=>add, zcomp=>comp);-- named association If a procedure contains WAIT statements, then it may not be called from a process that has a sensitivity list. A procedure may be postponed, being declared in this way: POSTPONED PROCEDURE a_procedure(list_of_parameters) IS …

Declaration versus specification A procedure may be only declared, without specifying its body. The same is true for functions: - for example in PACKAGE DECLARATION -it is useful at a recursive call (see example) Syntax for a subprogram declaration: subprogram_specification Example with two subprograms: PROCEDURE p(…) IS VARIABLE a, b:… BEGIN a:=q(b); … END p;

Declaration versus specification FUNCTION q(…) BEGIN … p(); … END q; The example is wrong, it will not compile. The correct form is: PROCEDURE p(); FUNCTION q(); PROCEDURE p() IS … END p; FUNCTION q() IS … END FUNCTION q;

Functions Specification: [PURE|IMPURE] FUNCTION function_name (parameter_list) RETURN return_type A function is pure if it returns the same value every time when it is called with the same set of actual parameters. An impure function may return different values when it is called with the same set of actual parameters. Implicitly a function is pure. Example of impure functions: - functions that generate random numbers - The function NOW (returns the current simulation time). Parameter_list describes the list of formal parameters of the function. They can be only of mode IN and may belong to constant or signal classes, being implicitly constants. Function call can be done only in expressions, according to the following syntax: function_name(list_of_actuals)

Functions For a constant, the actual parameter may be a constant or an expression, for a signal, the actual may be only a signal. Association between formal and actual parameters can be positional or by name. Functions MAY NOT contain WAIT statements and may not assign values to signals. In general functions do not have side effects. Example of a function that detects the rising edge of a signal: FUNCTION rising_edge (SIGNAL s: BIT) RETURN BOOLEAN IS BEGIN RETURN (s=‘1’ AND s’EVENT AND s’LAST_VALUE=‘0’); END FUNCTION rising_edge; Most frequent utilization of functions: - for conversions - resolution functions

Conversion functions In VHDL there are few cases when we can use cast-type conversion, like (INTEGER) x or (REAL) y. In general conversion functions are needed. Example: TYPE mvl IS (‘X’, ‘0’, ‘1’, ‘Z’); TYPE fourval IS (X, L, H, Z); FUNCTION mvl_to_fourval(v: mvl) RETURN fourval IS BEGIN CASE v IS WHEN ‘X’ => RETURN X; WHEN ‘0’ => RETURN L; WHEN ‘1’ => RETURN H; WHEN ‘Z’ => RETURN Z; END CASE; END FUNCTION;

Conversion with look-up table The conversion is more efficient if look-up tables are used instead of functions (the function calls introduce an overhead), like in the following example: entity look_up_table_2 is generic (tp: time:=5ns); end; architecture a of look_up_table_2 is type mvl is ('Z', '0', '1', 'X'); type fourval is (Z, L, H, X); type look_up is array(mvl) of fourval; CONSTANT to_fourval: look_up:=('Z'=>Z, '0'=>L,'1'=>H,'X'=>X); SIGNAL fv0,fv1,fv2: fourval; SIGNAL mvl1,mvl2: mvl; SIGNAL s1, s2: look_up; BEGIN Process(mvl1) Begin fv1 <= to_fourval(mvl1) after 1ns; end process;

process(mvl2) begin fv2<= to_fourval(mvl2) after 1ns; end process; fv0 <= to_fourval(mvl'('Z')) after tp, to_fourval('0') after 2*tp, to_fourval('1') after 3*tp, to_fourval('X') after 4*tp; mvl1 <= 'X' after 20ns, '1' after 30ns, '0' after 40ns, 'Z' after 50ns; mvl2 <= '0' after 40ns, '1' after 70ns; s1 X, '1'=>H, '0'=>L, 'Z'=> Z) after tp, ('X'=>Z, 'Z'=>X, '0'=>H, others=>L) after 2*tp; s2 L) after tp, (L,H,X,Z) after 2*tp; end architecture; Conversion with look-up table

Signal diagram Fig 10. Signal diagram for the previous example of conversion with look-up table.

Resolution functions Are used in order to resolve the value of a signal when the signal has more than one source (driver). In VHDL is illegal if a signal with more than one driver is not resolved. The resolution function: –Is written by the programmer –Is called by the simulator: When at least one of the signal’s driver has an event. The resolution function will return a value obtained from the values of all signal’s drivers. Unlike other HDLs, in VHDL there aren’t predefined resolution functions.

Overloading Sometimes we want to have two subprograms with the same name, case when we say that the subprograms are overloaded –For example a subprogram named count, which counts bits, and another subprogram, named also count, which counts integers A call to such subprograms is ambiguous, hence wrong, if we cannot identify the called subprogram. Subprogram identification can be based on: –Subprograms names –The number of actual parameters –Type and order of the actual parameters –The name of formal parameters (if the call uses the association by name) –The type of result (at functions)

Operator overloading When a standard VHDL operator is made to behave differently we say that we have an operator overloading –Usually we want to extend the operator for operands of different types than in the case of the standard operator –When the new operator is declared, its name is put between citation marks, in order to be able to call it as an operator The operator may be called as a function, too.

Example of operator overloading --Operator definition FUNCTION “+” (op1, op2: BIT_VECTOR) RETURN BIT_VECTOR; FUNCTION “-” (op1, op2: BIT_VECTOR) RETURN BIT_VECTOR; TYPE mvl IS (X, L, H, Z); FUNCTION “OR”(l, r: mvl) RETURN mvl; FUNCTION “AND”(l, r: mvl) RETURN mvl; … --Operator call VARIABLE x,y,z1,z2: BIT_VECTOR(3 DOWNTO 0); VARIABLE a1, b1, c1,c2: mvl; z1:=x+y;-- standard notation for operators z2:=“-”(x,y);-- standard notation for functions c1:=a1 AND b1;-- standard notation for operators c2:= “OR”(a1,b1);--standard notation for functions