Procedure and Function
Procedure in VHDL
In VHDL, a "procedure" is a language construct used to group multiple statements and specific tasks into a single block of code. Procedures help organize and understand complex VHDL designs.
Procedure Declaration
A procedure is defined using a procedure declaration. This declaration specifies the name of the procedure, the required parameters (if any), and the type of data returned (if any). Here is an example of a procedure declaration in VHDL:
procedure procedure_name(param1: in type1; param2: out type2) is
begin
-- Procedure body
end procedure_name;
Procedures can accept parameters as arguments. These parameters are used to send data into the procedure for processing. The required parameters can be defined in the procedure declaration.
The code block in a procedure is where the tasks to be performed by the procedure are placed. You can write statements in the procedure code block to perform various operations. These statements can include calculations, tests, data manipulation, etc.
Procedure Call
To use a procedure, you can call it from the main part of the design or another procedure. Calling a procedure is done by providing arguments that match the parameters defined in the procedure declaration. Here is an example of using a procedure:
variable_name := procedure_name(arg1, arg2);
In this example, arg1
and arg2
are the arguments passed to the procedure, and the result of the procedure is assigned to variable_name
. Here's an example of calling a procedure:
procedure add_numbers(a: in integer; b: in integer; sum: out integer) is
begin
sum := a + b;
end add_numbers;
-- Calling the procedure
variable_name := add_numbers(5, 3);
Function in VHDL
In VHDL (VHSIC Hardware Description Language), "function" and "impure function" are two concepts used to create subprograms that can be used in hardware descriptions. The following is an explanation of both:
Function
Procedures in VHDL do not return values directly. Instead, they can modify the values of their parameters or variables within their scope. If a return value is needed, a function should be used instead.
Functions in VHDL are subprograms used to perform calculations or data processing that return a value as a result. You can think of them as mathematical functions in programming. Functions can have input arguments (parameters) that are used in the calculation. The result of the function will depend on the values of the input arguments provided. Here is an example of function usage in VHDL:
function function_name(param1: in type1; param2: in type2) return type3 is
begin
-- Function body
return result;
end function_name;
The code above just shows the basic structure of a function in VHDL. The actual implementation of the function will depend on the specific task it is designed to perform. Here's an example of a function that calculates the sum of two numbers:
function add_numbers(a: in integer; b: in integer) return integer is
begin
return a + b;
end add_numbers;
-- Using the function
variable_name := add_numbers(5, 3);
Impure Function
An impure function is a function that can have properties that are unpredictable or changeable when executed. This means that the result of an impure function can depend on external factors unknown to the program, such as the time at which it is executed, random values, or global variables that can change.
An impure function is usually used when its result depends on values outside the input arguments and may change over time. Impure functions cannot be used in hardware descriptions that are deterministic or synchronous, as is commonly expected in VHDL.
function random_number return integer is
begin
return integer'image(random(0, 100));
end random_number;