Avlsi M4
Avlsi M4
Avlsi M4
and Routines
Introduction
• As you verify your design, you need to
write a great deal of code, most of which is
in tasks and functions.
• System Verilog introduces many
incremental improvements to make this
easier by making the language look more
like C, especially around argument passing.
Procedural Statements
New procedural statements and operators
Procedural Statements (cont)
Using break and continue while reading a file
8
Tasks
• Tasks are declared with 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.
11
FA using two HA’s
• Verilog Full Adder Using Task
• module Full_add (x, y, cin, sum, cout);
• //The full adder is built from two half adders
• input x, y, cin;
• output sum, cout;
• reg sum, sum1, c1, c2, cout;
• always @ (x, y, cin)
• begin
•
• Haddr (sum1, c1, y, cin);
• Haddr (sum, c2, sum1, x);
• //The above two statements are calls to the task Haddr.
• cout = c1 | c2;
• end
•
•
12
FA using two HA’s
• task Haddr;
• //This task describes the half adder
• output sh, ch;
• input ah, bh;
• begin
• sh = ah ^ bh;
• ch = ah & bh;
• end
• endtask
• endmodule 13
Verilog N-Bit Ripple Carry Adder Using Task
• module adder_ripple (x, y, cin, sum, cout);
• parameter N = 3;
• input [N:0] x, y;
• input cin;
• output [N:0] sum;
• output cout;
• reg [N+1:0] cint;
• reg [N:0] sum;
• reg cout;
• integer i;
• always @ (x, y, cin)
• begin
• cint[0] = cin;
• for (i = 0; i <= N; i = i + 1)
• begin
• Faddr (sum[i], cint[i+1], x[i], y[i], cint[i]);
• cout = cint[N+1];
• end
14
•
•
TASK FULL ADDER
• task Faddr;
• //The task describes a full adder
• output sf, cof;
• input af, bf, cinf;
• begin
• sf = af ^ bf ^ cinf;
• cof = (af & bf) | (af & cinf) | (bf & cinf);
• end
• endtask
•
• endmodule
15
Functions
• Functions are declared with keywords function and
endfunction.
• Functions are used if all of the following conditions are true
for the procedure.
There are no delay, timing, or event control constructs in the
procedure.
The procedure returns a single value.
There is at least one input arguments.
16
17
Verilog Function That Calculates exp = a XOR b
• module Func_exm (a1, b1, d1);
• input a1, b1;
• output d1;
• reg d1;
• always @ (a1, b1)
• begin
• /*The following statement calls the function exp
• and stores the output in d1.*/
• d1 = exp (a1, b1);
• end
• function exp ;
• input a, b;
• begin
• exp = a ^ b;
• end
• endfunction
• endmodule
18
Verilog Function to Find the Greater of Two Signed Numbers
• module greater_2 (x, y, z);
• input signed [3:0] x;
• input signed [3:0] y;
• output signed [3:0] z;
• reg signed [3:0] z;
• always @ (x, y)
• begin
• z = grt (x, y); //This is a function call.
• end
• function [3:0] grt;
• input signed [3:0] a, b;
• begin
• if (a>= b)
• grt = a;
• else
• grt = b;
• end
• endfunction
• endmodule 19
Differences between Tasks and Functions
20
Tasks, Functions, and Void Functions
Tasks, Functions, and Void Functions
Routine Arguments
Routine Arguments
Argument Direction