System Verilog Codes: Table of
System Verilog Codes: Table of
Table of
contents
1.SYSTEM VERILOG SCOPE
*TYPES OF SCOPE
1.GLOBAL SCOPE
2.LOCAL SCOPE
3.TASK AND FUMCTION SCOPE(NORMAL SCOPE)
2.KEYWORDS
1.EXTERN
2.THIS
3.VIRTUAL
4.PURE VIRTUAL
3.PASS BY ARGUMENTS
*TYPES
1.ARGUMENT PASS BY VALUE
2.ARGUMENT PASS BY REFERENCE
3.ARGUMENT PASS BY NAME
4.FORK CONCEPT
*TYPES OF FORKS
1.FORK - JOIN
2.FORK - JOIN_ANY
3.FORK - JOIN_NONE
4.WAIT - FORK
5.DISABLE-FORK
(1).SYSTEM VERILOG SCOPE:
System verilog scopes mainly used to access the properties which presents outside the class
(or) inside the class (or) inside function / task.
TYPES OF SCOPES:
1. GLOBAL SCOPE
2. LOCAL SCOPE
3. TASK AND FUNCTION SCOPE (NORAML SCOPE)
(1.1) GLOBAL SCOPE: Mainly used to access outside class properties.
(a)Code: Simple example.
class pkt;
endfunction
module top;
endclass
pkt p;
int a=7;
Outside class property initial begin
p=new();
By normally in this example,
the values of the properties p.randomize();
inside function or task get p.print();
updated and only that value
is resulted end
endmodule
OUTPUT: a=6
(b)Code: Example code to access global variable.
class pkt;
module top;
int a=5;
pkt p;
function void print();
initial begin
$display("GLOBAL=%0d",$unit::a);
p=new();
endfunction
p.randomize();
endclass
p.print();
int a=7;
end
Outside class property is accessed by
using global scope. endmodule
$unit::varaible_name
OUTPUT: GLOBAL=7
class pkt;
module top;
int a=5;
pkt p;
function void print();
initial begin
$display("LOCAL=%0d",$root.top.p.a);
p=new();
endfunction
p.randomize();
endclass
Inside class property is accessed p.print();
int a=7; through the local scope.
end
Syntax: endmodule
$root.top.hande.variable
OUTPUT: LOCAL=5
(d)Code: Example for system verilog scopes(LOCAL , GLOBAL, TASK/FUNCTION).
class pkt;
module top;
int a=5;
pkt p;
function void print();
initial begin
int a=6;
p=new();
$display("a=%0d",a);
p.randomize();
$display("local=%0d",$root.top.p.a);
p.print();
$display("global=%0d",$unit::a);
end
endfunction OUTPUT:
endmodule
endclass a=6
int a=7;
# local=5
# global=7
(e)Code: Any modification inside one class property will reflect inside another class property.
class gen;
module top;
int a=5;
gen p;
int b; Any changes inside
class bfm (variable bfm p1;
function void print(); c) ,then class gen (b
initial begin
int a=7; variable) gets updated
p=new();
b=$root.top.p1.c;
p1=new();
$display("local=%0d",$root.top.p.a);
p.randomize();
$display("global=%0d",$unit::a);
p.print();
$display("a=%0d",a);
end
$display("b=%0d",b);
endmodule
endfunction
class bfm;
endclass OUTPUT: local=5 #global=10
int c=8;
int a=10; a=7
endclass
b=8
(f)Code: SV scope example code with using polymorphism, interface block etc..
int b; pkt p;
$display("a=%0d",a); p=new();
$display("local=%0d",$root.top.p.a); p1=new();
$display("global=%0d",$unit::a); p=p1;
$display("b=%0d",b); p.print();
$display("arr=%0d",$unit::top.arr); end
$display("f=%0d",$unit::top.dut.f); endmodule
endfunction
endclass
OUTPUT: a=6
int a;
# local=5
endclass
(2)EXTERN: Mainly used to secure methods and constraints. This can achieved by using
EXTERN keyword.
class pkt;
module top;
rand bit[3:0] a;
pkt p;
extern function void print();
initial begin
endclass
p=new();
endfunction end
endmodule
class pkt;
module top;
rand bit[3:0] a;
pkt p;
extern function void print();
initial begin
extern constraint c1;
p=new();
endclass
p.randomize();
p.print();
function void pkt::print();
end
$display("EXTERN CONCEPT a=%0d",a);
endmodule
endfunction
constraint pkt::c1{
a==10; OUTPUT:
} EXTERN CONCEPT a=10
(c)Code: Example to secure methods.
class gen;
module top;
int a=5;
gen p;
int b;
bfm p1;
extern function void print();
initial begin
endclass
p=new();
int a=7;
p1=new();
class bfm;
p.randomize();
int c=8;
p.print();
endclass
end
endmodule
function void gen::print();
int a=7;
b=$root.top.p1.c;
$display("global=%0d",$unit::a);
$display("b=%0d",b);
$display("c=%0d",$root.top.p1.c);
endfunction
OUTPUT:
a=7
# local=5
# global=7
# b=8
# c=8
(d)Code: Example code for securing constraint.
class gen;
module top;
int a=5;
gen p;
int b;
bfm p1;
rand int d;
initial begin
extern function void print();
p=new();
extern constraint c1;
p1=new();
endclass
p.randomize();
p.print();
int a=7;
end
endmodule
class bfm;
int c=8;
endclass
$display("a=%0d",a); # local=5
$display("local=%0d",$root.top.p.a); # global=7
$display("global=%0d",$unit::a);
# b=8
$display("b=%0d",b);
# c=8
$display("c=%0d",$root.top.p1.c);
$display("d=%0d",d); # d=2
endfunction
constraint gen::c1{
d==2;
NOTE: Inside class, declaration of methods and constraints alone possible. And
implementations of methods or constraint done outside class or any were in the
environment.
(3)THIS KEYWORD: (this) keyword is mainly used to avoid hanging conditions. Without (this)
keyword tool get confused (which is class variable and which is argument type variable).
class pkt;
module top;
bit [3:0] a;
pkt p;
function new(bit [3:0] a);
initial begin
a=a;
p=new(5);
endfunction
$display("p=%0d",p.a);
endclass
In this case user knows, which is end
class variable (a) and which is endmodule
OUTPUT:
argument variable (a). But tools
(compiler) have no idea about this.
P=0;
So it moves to hanging state.
class pkt;
module top;
bit [3:0] a;
pkt p;
function new(bit [3:0] a);
initial begin
this.a=a;
p=new(5);
endfunction
$display("p=%0d",p.a);
endclass
end
endmodule
(this.a) indicates class
variable.
OUTPUT:
P=5;
NOTE: -By using (this) keyword it is very easy to find out which one is class variable and
which one is argument variable.
(4).ABSTRACT CLASS: Systemverilog class declared with VIRTUAL keyword is called abstract
class. An abstract class cannot be initialized, it can only be derived.
endfunction p=new();
endclass p.randomize();
endclass end
endmodule
OUTPUT: a= 3
(5).PURE VIRTUAL: "pure virtual" keyword only relation with task & function. Pure virtual
only supports inside abstract class.
OUTPUT: a=3;
(b)Code: Pure virtual is not supported inside normal class.
class pkt;
module top;
rand bit[3:0]a;
my_pkt p;
pure virtual function void print();
initial begin
endclass
p=new();
class my_pkt extends pkt;
p.randomize();
function void print();
p.print();
$display("a=%d",a);
end
endfunction
endmodule
endclass
OUTPUT: Pure Virtual method 'print' can exist only in abstract class
a==10;
} OUTPUT:
$display("PURE VIRTUAL::c=%0d",c);
endfunction
endclass
$display("EXTERN::a=%0d",a);
endfunction
OUTPUT:
# PURE VIRTUAL::c=5
(6)PASS BY ARGUMENTS:
return a+b;
endclass pkt p;
initial begin
Any change of value
inside subroutine p=new();
variable(a) is not p.a=20;
reflected to class
variable (a) p.b=30;
p.c=p.sum(10,30);
OUTPUT: $display("a=%0d b=%0d c=%0d",p.a,p.b,p.c);
endmodule
(b)Code: Example code for pass by value.
class pkt;
module top;
int a;
pkt p;
int b;
initial begin
int c;
p=new();
function int sum(int a,b);
p.a=20;
a=a+b;
p.b=30;
return a+b;
p.c=p.sum(50,50);
endfunction
p.print();
function void print();
end
$display("a=%0d,b=%0d,c=%0d",a,b,c); OUTPUT:
endmodule
endfunction a=20, b=30, c=150
endclass
class pkt;
module top;
int a;
pkt p;
int b;
initial begin
int c;
p=new();
function int sum(ref int a,b);
p.a=20;
a=this.a+this.b; Here variable (a and
b) act as reference to p.b=30;
return a+b;
class variable (a and p.c=p.sum(p.a,p.b);
endfunction b). In this case
memory is not $display("a=%0d b=%0d c=%0d",p.a,p.b,p.c);
endclass
created for reference end
variable
endmodule
OUTPUT:
class pkt;
module top;
int a;
pkt p;
int b;
initial begin
int c;
p=new();
function int sum(ref int a,b);
p.a=20;
a=a+b;
p.b=30;
b=a*b;
p.c=p.sum(p.a,p.b);
return a+b;
p.print();
endfunction
end
function void print();
endmodule
$display("a=%0d,b=%0d,c=%0d",a,b,c);
endfunction OUTPUT:
endclass
a=50,b=1500,c=1550
class pkt;
We are missing ref keyword, module top;
int a; so separate memory is
pkt p;
int b; created for this arguments
initial begin
int c;
p=new();
function int sum(int a,b);
p.a=20;
a=this.a+this.b;
p.b=30;
return a+b;
p.c=p.sum(p.a,p.b);
endfunction
$display("a=%0d b=%0d
endclass c=%0d",p.a,p.b,p.c);
end
OUTPUT:
endmodule
a=20 b=30 c=80
(d)Code: By using (const) keyword.
class pkt;
By using (const) keyword module top;
int a; along with ref, we can able
pkt p;
to avoid the modification of
int b;
class variable. initial begin
int c;
p=new();
function int sum(const ref int a,b);
p.a=20;
return a+b;
p.b=30;
endfunction
p.c=p.sum(p.a,p.b);
function void print();
p.print();
$display("a=%0d,b=%0d,c=%0d",a,b,c);
end
endfunction
endmodule
endclass OUTPUT:
a=20,b=30,c=50
class pkt;
module top;
int a;
pkt p;
int b;
initial begin
int c;
p=new();
function int sum(const ref int a,b);
p.a=20;
a=a+b;
p.b=30;
Illegal to use (const)
b=a-b;
type variable in LHS p.c=p.sum(p.a,p.b);
return a+b; side.
p.print();
endfunction
end
function void print();
endmodule
$display("a=%0d,b=%0d,c=%0d",a,b,c);
class pkt;
module top;
int a,b,c; OUTPUT:
pkt p;
function int sum(int a="10",b="20");
initial begin
name=vlsi
a=this.a+this.b;
return a+b;
p=new(); # a=20
p.a=20;
endfunction # b=30
p.b=30;
function void print(string name="vlsi");
p.c=p.sum(); # c=12898
$display("name=%0s",name);
p.print();
$display("a=%0d",a);
end
$display("b=%0d",b);
endmodule
$display("c=%0d",c);
endfunction
endclass
Fork join will start all the process inside it parallel and wait for the completion of all the
process.
(a)Code: Example for fork-join.
class pkt;
module top;
task run();
pkt p;
#5 $display("before fork block ....%0d",$time);
initial begin
fork
p=new();
#2 $display("process1..%0d",$time);
p.run();
#5 $display("process2..%0d",$time);
end
#7 $display("process3..%0d",$time);
endmodule
join
# before fork block ....5
$display (" outside fork block ...%0d",$time);
# process1..7
endtask
endclass
# process2..10
OUTPUT:
# process3..12
#10 $display (" outside fork block ...%0d",$time); # before fork block ....5
endtask # process1..7
endclass
# process2..10
OUTPUT: # process3..12
class pkt;
module top;
task run();
pkt p;
#5 $display("before fork block ....%0d",$time);
initial begin
fork
p=new();
begin
p.run();
#2 $display("process1..%0d",$time);
end
#5 $display("process2..%0d",$time);
endmodule
end
begin
#3 $display("process4..%0d",$time);
#7 $display("process3..%0d",$time);
end
join
begin
#3 $display("second statment..%0d",$time);
end OUTPUT:
endtask
# before fork block ....5
endclass
# process1..7
# process4..8
# process2..12
# process3..15
# second statment..28
(7.2)FORK-JOIN_ANY: For fork join_any will be unblocked after completion of any one of the
process between fork block.
Inside fork block if one statement completed its checks outside fork block also then only
check inside fork remaining process.
class pkt;
module top; OUTPUT:
task run();
pkt p;
before fork block ....5
#5 $display("before fork block ....%0d",$time);
initial begin
fork # process1..7
p=new();
#2 $display("process1..%0d",$time); # outside fork block ...7
p.run();
#5 $display("process2..%0d",$time);
end # process2..10
#7 $display("process3..%0d",$time);
endmodule
join_any
# process3..12
endtask
endclass
class pkt;
module top; OUTPUT:
task run();
pkt p;
before fork block ....5
#5 $display("before fork block ....%0d",$time);
initial begin
fork # process1..7
p=new();
#2 $display("process1..%0d",$time); # outside fork block ...7
p.run();
#5 $display("process2..%0d",$time);
end # process2..10
#7 $display("process3..%0d",$time);
endmodule
join_any
# process3..22
endtask
endclass
(c)Code: Example code for fork-join_any.
class pkt;
module top;
task run();
pkt p;
#5 $display("before fork block ....%0d",$time);
initial begin
fork
p=new();
begin
p.run();
#2 $display("process1..%0d",$time);
end
#5 $display("process2..%0d",$time);
endmodule
end
begin
#1 $display("process3..%0d",$time);
#10 $display("process4..%0d",$time);
end
join_any
endtask
endclass
OUTPUT:
# process3..6
# process1..7
# process2..12
# process4..16
(7.3)FORK-JOIN_NONE: In case fork join_none fork block always non-blocking statement.
endtask
endclass
class pkt;
module top;
task run();
pkt p;
#5 $display("before fork block ....%0d",$time);
initial begin
fork
p=new();
#2 $display("process1..%0d",$time);
p.run();
#5 $display("process2..%0d",$time);
end
#1 $display("process3..%0d",$time);
endmodule
join_none OUTPUT:
#10 $display (" outside fork block ...%0d",$time); # before fork block ....5
endtask
# process3..6
endclass
# process1..7
# process2..10
class pkt;
task run();
fork
begin
#2 $display("process1..%0d",$time);
#5 $display("process2..%0d",$time);
end
begin
#1 $display("process3..%0d",$time);
#10 $display("process4..%0d",$time);
end
join_none
initial begin
p=new();
OUTPUT: p.run();
# before fork block ....5 end
# process3..6 endmodule
# process1..7
# process2..12
# process4..16
(7.4)DISABLE FORK AND WAIT FORK: This two mainly used for join_any and join_none
process. Fork_join no need of disable and wait_fork.
(a)Code: Example code for disable-fork along with (fork and join_any).
class pkt; module top;
task run(); pkt p;
#5 $display("before fork block ....%0d",$time); initial begin
fork p=new();
#2 $display("process1..%0d",$time); p.run();
#5 $display("process2..%0d",$time); end
#1 $display("process3..%0d",$time); endmodule
#10 $display("process4..%0d",$time);
Killing a process in fork is
join_any
achieved by using disable OUTPUT:
disable fork; fork
# before fork block ....5
$display (" outside fork block ...%0d",$time);
# process3..6
endtask
(b)Code: Example code for disable-fork along with (fork and join_none).
class pkt;
module top;
task run();
pkt p;
#5 $display("before fork block ....%0d",$time);
initial begin
fork
p=new();
#2 $display("process1..%0d",$time);
p.run();
#5 $display("process2..%0d",$time);
end
#1 $display("process3..%0d",$time);
endmodule
#10 $display("process4..%0d",$time);
join_none
$display (" outside fork block ...%0d",$time); # before fork block ....5
class pkt;
module top; OUTPUT:
task run();
pkt p;
#5 $display("before fork block ....%0d",$time); # before fork block ....5
initial begin
fork # process3..6
p=new();
#2 $display("process1..%0d",$time);
p.run(); # process1..7
#5 $display("process2..%0d",$time);
end # process2..10
#1 $display("process3..%0d",$time);
endmodule
#10 $display("process4..%0d",$time); # outside fork block ...15
join_none
disable fork;
endtask
endclass
class pkt;
task run();
module top; OUTPUT:
pkt p; # before fork block ....5
#5 $display("before fork block ....%d",$time);
initial begin
fork # process3..6
p=new();
#2 $display("process1..%0d",$time); # process1..7
p.run();
#5 $display("process2..%0d",$time);
# process2..10
end
#1 $display("process3..%0d",$time);
endmodule # outside fork block ...15
#10 $display("process4..%0d",$time);
join_none
$finish;
Kill the execution and stop
endtask the stimulation.
endclass
(7.5)WAIT-FORK: Killing the process is avoided by using wait fork.
class pkt;
module top;
task run();
pkt p;
#5 $display("before fork block ....%0d",$time);
initial begin
fork
p=new();
#2 $display("process1..%0d",$time);
p.run();
#5 $display("process2..%0d",$time);
end
#1 $display("process3..%0d",$time);
endmodule
#10 $display("process4..%0d",$time);
join_any
wait fork;
By using wait fork we can avoid
$display (" outside fork block ...%0d",$time); killing of process
$finish;
endtask
endclass
OUTPUT:
# process3..6
# process1..7
# process2..10
# process4..15
class pkt;
#2 $display("process1..%0d",$time); p=new();
#5 $display("process2..%0d",$time); p.run();
#1 $display("process3..%0d",$time); end
join_any
wait fork;
disable fork;
endtask
endclass
OUTPUT:
# process3..6
# process1..7
# process2..10
# process4..15
$display("a=%b b=%b",p.a,p.b);
end
OUTPUT:
endmodule
a=00100110001100111001010010100001
b=01101111000111101101011011011111010110001
00101000000100111100000
(b)Code: parameterized class without default and without passing the constant.
p.randomize();
Here we are not declared
default value. And also not $display("a=%b b=%b",p.a,p.b);
passed any constant.
end
OUTPUT: endmodule
a=00100110001100111001010010100001 end
b=00000000000000000000000000000000 endmodule
rand T b; pkt#(integer) p;
p.b=32'hxxxxxxxx;
$display("a=%b b=%b",p.a,p.b);
OUTPUT:
end
a=0010011000110011100101001010000
endmodule
1 b=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
NOTE: In this example, we override the data type from (int to integer). Now variable (b)
supports value(x).
(e)Code: Overriding default data type (int) to logic.
p.randomize();
OUTPUT: p.b=32'hxxxxxxxx;
endmodule
rand T1 a;
module top;
rand T2 b;
pkt#(byte,integer) p;
endclass
initial begin
p=new();
p.randomize();
p.b=32'hxxxxxxxx;
$display("a=%b b=%b",p.a,p.b);
OUTPUT:
end
a=01100100 endmodule
b=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
(g)Code: Achieving both constant and data type using parameter.
p.randomize();
OUTPUT:
p.b=32'hxxxxxxxx;
a=00000000000000000000000000000000
p.a=32'hxxxxxxxx;
b=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$display("a=%b b=%b",p.a,p.b);
end
endmodule
p=new();
p.randomize();
We can able to skip the
overriding by just denoting p.b=32'hxxxxxxxx;
$display("a=%b b=%b",p.a,p.b);
end
endmodule
OUTPUT:
a=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
b=00000000000000000000000000000000
(i)Code: Including my_pkt class inside pkt class by using parameterized.
class my_pkt;
endclass
Class 2
Here (a) is handle of
my_pkt class
T a;
We are including (my_pkt) class in
function new(); pkt class by using parameter. And
we are overriding the value of
a=new();
variable (c and d) in pkt class.
a.c=10;
a.d=3;
endfunction
endclass
module top;
pkt p;
initial begin
p=new();
$display("c=%d",p.a.c);
OUTPUT: $display("d=%d",p.a.d);
c=10; end
endmodule
d=3;
(j)Code: Overriding the default class by another class.
class my_pkt;
endclass
endfunction
endclass
class my_pkt1;
p=new();
$display("c=%d",p.a.c);
OUTPUT: $display("d=%d",p.a.d);
end
C=12;
endmodule
d=12;
(k)Code: Child class(#parameterized) extends from Base class(#parameterized).
class my_pkt;
rand bit[3:0]c=5;
module top;
rand bit [3:0]d=5;
car#(5) p;
endclass
initial begin
class my_pkt1;
p=new();
rand bit[3:0]c=12;
$display("c=%d",p.a.c);
rand bit[3:0]d=12;
$display("d=%b",p.a.d);
endclass
$display("c=%d",p.c);
class pkt#(parameter type T);
$display("d=%d",p.d);
This (my_pkt)
T a;
reflected in place (T) end
function new();
endmodule
a=new();
a.c=10;
a.d=10;
endfunction
endclass
# d=1010
# c=20
# d=20
(l)Code: Example code for child class inherited from parameterized base class.
class my_pkt;
rand bit[3:0] c;
endclass
T a;
function new();
Parameterized base class
a=new();
a.c=10;
endfunction
endclass
module top;
class my_pkt1;
car#(5) p;
rand bit [3:0] c;
initial begin
endclass
p=new();
p.randomize();
$display("c=%d",p.a.c);
$display("d=%b",p.d);
end
OUTPUT: endmodule
# c=10
# d=100001
(9)TASK AND FUNCTION:
(9.1)FUNCTIONS: Function is also one kind of subroutine which is used to achieve reusable of
code.
Argument_list
function return_data_type function_name (argument_list) can be
SYNTAX:
endfunction 1.input
Return type can be with
void (or) no void type. 2.inout
3.output
module top;
int a;
initial begin
OUTPUT:
a=sum(10,10);
Value of a= 20
$display("\tValue of a= %0d",a);
end
endmodule
module top;
int a;
sum = a+b;
endfunction
initial begin
a=sum(10,8);
$display("\tValue of x = %0d",a);
end
endmodule
(c)Code: Calling function inside another function.
class pkt;
module top;
function void a();
pkt p;
$display("INSIDE FUNCTION A()");
initial begin
endfunction
p=new();
function void b();
p.b();
$display("BEFORE CALLING FUNCTION A() INSIDE FUNCTION B");
end
a();
endmodule
$display("AFTER CALLING FUNCTION A() INSIDE FUNCTION B");
endfunction
# BEFORE CALLING FUNCTION A() INSIDE FUNCTION B
endclass
# INSIDE FUNCTION A()
OUTPUT
# AFTER CALLING FUNCTION A() INSIDE FUNCTION B
NOTE: Calling a task inside a function is illegal, because task supports delay concept where
function executed in ZERO stimulation time.
(d)Code: Calling the task (with delay) inside function not possible.
class pkt;
module top;
task a;
pkt p;
#10;
initial begin
$display("Calling task inside function");
p=new();
endtask
p.b();
function void b();
Calling task inside end
a; function results in endmodule
endfunction error, because task a
contains delay (#10)
endclass
OUTPUT:
Error: example.sv(8): (vlog-2873) Task "a" can not be called in function "b" as
it has delay.
(e)Code: Calling task (no delay) inside function is possible.
OUTPUT:
(f)Code: By using fork join_none it is possible to call task (with delay) inside function.
class pkt;
module top;
task a;
pkt p;
#10;
initial begin
$display("IM IN TASK A\n");
p=new();
endtask
p.b();
function void b;
end
fork
endmodule
a;
join_none
module top;
int x;
return a+b;
endfunction
OUTPUT:
initial begin
end
endmodule
(h)Code: We are not assigning the return type to any other variable.
module top;
int x;
OUTPUT:
initial begin
sum(10,5);
end
We are not assigning the
endmodule returned value to any other
variable. So no (a+b) not get
updated and we encounter
(warning)
(i)Code: We can able to avoid the above example warning by using (explicit void).
module top;
int x;
return a+b;
endfunction
initial begin
endmodule
(9.2)TASK: Split the code into small parts and reusing this task wherever required.
module top;
int x;
c = a+b;
endtask
initial begin
sum(10,5,x);
$display("\tValue of x = %0d",x);
end
endmodule OUTPUT:
Value of x = 15
(b)Code: Arguments declaration inside task along with direction.
module top;
int x;
task sum;
output int c;
c = a+b;
endtask OUTPUT:
initial begin Value of x = 15
sum(10,5,x);
$display("\tValue of x = %0d",x);
end
endmodule
(c)Code: Both function and task can call inside another task is possible.
class pkt;
#10; p.run();
endtask endmodule
task run();
FUNCTION
print(); OUTPUT:
abc; # THIS IS PRINT METHOD
endtask TASK
# THIS IS TASK abc
endclass
(d)Code: Task not supports return keyword.
class pkt;
module top;
bit [3:0] a=10;
pkt p;
bit [3:0] b;
initial begin
bit [3:0] c;
p=new();
task abc;
p.abc;
#10;
p.def;
$display("THIS IS TASK abc");
$display("value of b=%0d",p.b);
endtask
end
task def;
endmodule
b=a;
Illegal to use return
return b; keyword inside task
OUTPUT:
endtask
endclass
OUTPUT:
class pkt;
module top;
bit [3:0] a=10;
pkt p;
bit [3:0] b;
initial begin
bit [3:0] c;
p=new();
task abc;
p.abc;
#10;
p.def;
$display("THIS IS TASK abc");
$display("value of b=%0d",p.b);
endtask
Returning is end
task def;
achieved only by
endmodule
b=a; return by statement
endtask
OUTPUT:
endclass
# THIS IS TASK abc
# value of b=10
TASK FUNCTION
Can call another task and function Unable to call task (by using fork_join
possible).But can call function
Task contain delay, event, @, timing control Function not supports delay, event, timing
statements. control statements.
Task will not supports void type, return Function supports void type, return keyword,
keyword (return can achieved by using return Return statement.
statement)
------------------------------------------------------xxxxxxxxxx--------------------------------------------------------