Chap8_Tasks and Functions
Chap8_Tasks and Functions
Digital Design
1
2024-12-26
2
2024-12-26
3
2024-12-26
Task
Tasks are declared with the keywords task and endtask. Tasks
must be used if any one of the following conditions is true for
the procedure:
There are delay, timing, or event control constructs in the procedure.
The procedure has zero or more than one output arguments.
The procedure has no input arguments.
I/O declaration use keywords input, output or input, based on the type
of argument declared.
Input and output arguments are passed into the task.
Input arguments are processed in the task statements.
Output and inout argument values are passed back to the variables in
the task invocation statement when the task is completed.
Task can invoke other tasks or functions.
Ports are used to connect external signals to the module.
I/O arguments in a task are used to pass values to and from the task.
uments are processed in the task statements.
4
2024-12-26
task_item_declaration ::=
block_item_declaration
| { attribute_instance } tf_input_declaration ;
| { attribute_instance } tf_output_declaration ;
| { attribute_instance } tf_inout_declaration ;
task_port_list ::= task_port_item { , task_port_item }
task_port_item ::=
{ attribute_instance } tf_input_declaration
| { attribute_instance } tf_output_declaration
| { attribute_instance } tf_inout_declaration
tf_input_declaration ::=
input [ reg ] [ signed ] [ range ]
list_of_port_identifiers
| input [ task_port_type ] list_of_port_identifiers
tf_output_declaration ::=
output [ reg ] [ signed ] [ range ]
list_of_port_identifiers
| output [ task_port_type ] list_of_port_identifiers
tf_inout_declaration ::=
inout [ reg ] [ signed ] [ range ]
list_of_port_identifiers
| inout [ task_port_type ] list_of_port_identifiers
task_port_type ::=
time | real | realtime | integer
5
2024-12-26
Task Examples
Use of input and output arguments
//Define a module called operation that contains the task
bitwise_oper
module operation;
...
parameter delay = 10;
reg [15:0] A, B;
reg [15:0] AB_AND, AB_OR, AB_XOR;
always @(A or B) //whenever A or B changes in value
begin
//invoke the task bitwise_oper. provide 2 input arguments A, B
//Expect 3 output arguments AB_AND, AB_OR, AB_XOR
6
2024-12-26
7
2024-12-26
8
2024-12-26
//Initialization sequence
task init_sequence;
begin
clock = 1'b0;
end
endtask
//define task to generate asymmetric sequence
//operate directly on the clock defined in the module.
task asymmetric_sequence;
begin
#12 clock = 1'b0;
#5 clock = 1'b1;
#3 clock = 1'b0;
#10 clock = 1'b1;
end
endtask
Endmodule
Functions
Functions are declared with the keywords function
and endfunction.
Functions are used if all of the following conditions
are true for the procedure:
1. There are no delay, timing, or event control
constructs in the procedure.
2. The procedure returns a single value.
3. There is at least one input argument.
4. There are no output or inout arguments.
5. There are no nonblocking assignments.
9
2024-12-26
function_item_declaration ::=
block_item_declaration
| tf_input_declaration ;
function_port_list ::= { attribute_instance }
tf_input_declaration {,
{ attribute_instance } tf_input_declaration }
range_or_type ::= range | integer | real | realtime
| time
10
2024-12-26
Function Examples
Parity calculation
//Define a module that contains the function calc_parity
module parity;
...
reg [31:0] addr;
reg parity;
//Compute new parity whenever address value changes
always @(addr)
begin
parity = calc_parity(addr); //First invocation of calc_parity
$display("Parity calculated = %b", calc_parity(addr) );
//Second invocation of calc_parity
end
11
2024-12-26
12
2024-12-26
13
2024-12-26
Constant Functions
A constant function[1] is a regular Verilog HDL function, but with
certain restrictions.
These functions can be used to reference complex val ues and can
be used instead of constants
Example:-Constant Functions-shows how a constant function
can be used to compute the width of the address bus in a module.
//Define a RAM model
module ram (...);
parameter RAM_DEPTH = 256;
input [clogb2(RAM_DEPTH)-1:0] addr_bus; //width of bus
computed
//by calling constant//function defined below//Result of clogb2 = 8
//input [7:0] addr_bus;
--
--
14
2024-12-26
//Constant function
function integer clogb2(input integer depth);
begin
for(clogb2=0; depth >0; clogb2=clogb2+1)
depth = depth >> 1;
end
endfunction
--
--
endmodule
Signed Functions
Signed functions allow signed operations to be
performed on the function return values.
Example 8-12 shows an example of a signed function.
Example 9-12. Signed Functions
module top;
//Signed function declaration
//Returns a 64 bit signed value
function signed [63:0] compute_signed(input [63:0]
vector);
--
--
endfunction
15
2024-12-26
Recommended Questions
1. Describe the following statements with an example: initial and
always
2. What are blocking and non-blocking assignment statements?
Explain with examples.
3. With syntax explain conditional, branching and loop
statements available in Verilog HDL behavioural description.
4. Describe sequential and parallel blocks of Verilog HDL.
5. Write Verilog HDL program of 4:1 mux using CASE statement.
6. Write Verilog HDL program of 4:1 mux using If-else statement.
7. Write Verilog HDL program of 4-bit synchronous up counter.
8. Write Verilog HDL program of 4-bit asynchronous down
counter.
9. Write Verilog HDL program to simulate traffic signal controller
16
2024-12-26
Q&A
17