0% found this document useful (0 votes)
54 views45 pages

System Verilog Codes: Table of

The document provides an overview of System Verilog, focusing on various scopes, keywords, argument passing methods, and concepts like forks and abstract classes. It explains the different types of scopes (global, local, task/function), the use of keywords such as 'extern' and 'this', and how to implement polymorphism and pure virtual functions. Code examples illustrate these concepts, demonstrating their application and output results.

Uploaded by

aknown8421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views45 pages

System Verilog Codes: Table of

The document provides an overview of System Verilog, focusing on various scopes, keywords, argument passing methods, and concepts like forks and abstract classes. It explains the different types of scopes (global, local, task/function), the use of keywords such as 'extern' and 'this', and how to implement polymorphism and pure virtual functions. Code examples illustrate these concepts, demonstrating their application and output results.

Uploaded by

aknown8421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

SYSTEM VERILOG CODES

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;

int a=5; Inside class property

function void print();

int a=6; Inside function / task


property
$display("a=%0d",a);

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

(1.2) LOCAL SCOPE: Mainly used to access inside class properties.

(C)Code: Example code to access local variable (Inside class property).

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..

class pkt; module top;

bit[3:0]a=5; int arr=11;

int b; pkt p;

virtual function void print(); my_pkt p1;

int a=6; op dut();

b=$root.top.p1.a; initial begin

$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

interface op; # global=7


Variable f is outside
int f=50; class property so we # b=10
use GLOBAL SCOPE # arr=11
endinterface
# f=50
class my_pkt extends pkt; # c=11
int c;

function void print(); Any changes


inside module top
super.print(); variable (arr) will
c=$unit::top.arr; reflect inside
my_pkt (c
$display("c=%0d",c); variable)
endfunction

endclass
(2)EXTERN: Mainly used to secure methods and constraints. This can achieved by using
EXTERN keyword.

(a)Code: Simple example for securing methods.

class pkt;
module top;
rand bit[3:0] a;
pkt p;
extern function void print();
initial begin
endclass
p=new();

function void pkt::print(); p.randomize();

$display("EXTERN CONCEPT a=%0d",a); p.print();

endfunction end

endmodule

OUTPUT: This implementation of print


method can done anywhere
EXTERN CONCEPT a=3
in the environment.

(b)Code: Simple example for securing constraints.

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("a=%0d",a); Implemented anywhere in


the environment.
$display("local=%0d",$root.top.p.a);

$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

function void gen::print();


OUTPUT:
int a=7;
# a=7
b=$root.top.p1.c;

$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).

(a)Code: Without (this) keyword.

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.

(b)Code: with (this) keyword.

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.

(a)Code: Abstract class initiated inside module (error).

virtual class pkt;


Handle creation of abstract module top;
rand bit[3:0]a; class is possible
pkt p;
function void print();
initial begin
Memory creation, and calling
$display("a=%0d",a);
other methods of abstract p=new();
endfunction class inside module will result
p.randomize();
in fatal error.
endclass
p.print();
OUTPUT: Fatal error in Module top at example.sv line 11 end

Class allocator method 'new' called on abstract class 'pkt'. endmodule

(b)Code: Instance of abstract class inside another class is not possible.


virtual class pkt;
class my_pkt; module top;
rand bit[3:0]a;
pkt p; my_pkt p1;
function void print();
function new(); initial begin
$display("a=%0d",a);
p=new(); p1=new();
endfunction
endfunction p1.randomize();
endclass
function void print(); p1.print();

Object creation of abstract $display("a=%0d",p.a); end


class inside another class is endfunction endmodule
not possible.
endclass

OUTPUT: Fatal error in Function example_sv_unit/my_pkt::new at example.sv line 11

Class allocator method 'new' called on abstract class 'pkt'.


(c)Code: By using child class (derived class) we can able to call abstract class methods.

virtual class pkt;

rand bit [3:0] a; Abstract class (pkt) is module top;


derived in child my_pkt p;
function void print();
class(my_pkt)
$display("a=%d",a); initial begin

endfunction p=new();

endclass p.randomize();

class my_pkt extends pkt; p.print();

endclass end

endmodule

OUTPUT: a= 3

(5).PURE VIRTUAL: "pure virtual" keyword only relation with task & function. Pure virtual
only supports inside abstract class.

Pure virtual concept only supports for methods(task/functions)

(a)Code: Pure virtual supports only inside abstract class.

Virtual class pkt;


Only declaration of module top;
rand bit [3:0] a; abstract class print my_pkt p;
pure virtual function void print(); method is done inside
abstract class. initial begin
endclass Implementation done p=new();
class my_pkt extends pkt; inside child class
p.randomize();
function void print();
p.print();
$display("a=%d",a);
end
endfunction
endmodule
endclass

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

(c)Code: Pure virtual is not applicable for constraints.

virtual class pkt;


module top;
rand bit [3:0] a;
pkt p;
pure virtual function void print();
my_pkt p1;
pure virtual constraint c1;
initial begin
endclass
p1=new();
class my_pkt extends pkt;
Pure virtual p1.randomize();
function void print(); concept is not p1.print();
$display("PURE VIRTUAL::a=%0d",a); supported for
constraint end
endfunction
endmodule
constraint c1{

a==10;

} OUTPUT:

endclass near "constraint": syntax error, unexpected


constraint, expecting function or task.
(d)Code: Achieving Polymorphism in Abstract class along with extern and pure virtual
concept
virtual class pkt;
polymorphism module top;
rand bit [3:0]a;
pkt p;
bit [3:0]c=5;
my_pkt p1;
extern virtual function void print();
initial begin
pure virtual function void print1(); Pure virtual print p1=new();
endclass method implemented
inside child class p=p1;
class my_pkt extends pkt;
p.randomize();
rand bit[3:0]b;
p.print();
function void print();
p.print1();
super.print();
Child class print method. end
$display(“b=%0d",b);
endmodule
endfunction

function void print1();

$display("PURE VIRTUAL::c=%0d",c);

endfunction

endclass

function void pkt::print();

$display("EXTERN::a=%0d",a);

endfunction

OUTPUT:

# EXTERN::a=3 By using polymorphism we achieved


both (a and b).
#b=8

# PURE VIRTUAL::c=5
(6)PASS BY ARGUMENTS:

TYPES OF PASS BY ARGUMENTS:

1. ARGUMENT PASS BY VALUE


2. ARGUMENT PASS BY REFERENCE
3. ARGUMENT PASS BY NAME

(6.1)ARGUMENT PASS BY VALUE: If any changes to arguments within the subroutine


(function..endfunction), those changes will be not visible outside subroutine(class variable).
New memory is created for the subroutine arguments.
(a)Code: Example code for pass by value.
class pkt;

int a,b,c; New memory is


created for
function int sum( int a,b);
argument variable
a=this.a+this.b; (a and b)

return a+b;

endfunction module top;

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);

a=20 b=30 c=80 end

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

(6.2)ARGUMENT PASS BY REFERENCE: If any changes to arguments within the subroutine,


those changes will be visible outside subroutine.

(a)Code: Example code for pass by reference.

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:

a=50 b=30 c=80


(b)Code: Pass by reference example.

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

(c)Code: Without (ref) keyword.

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

(e)Code: (const) variable not allowed to use in LHS.

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);

endfunction OUTPUT: Illegal LHS of assignment.


endclass
(6.3)ARGUMENT PASS BY NAME:

(a)Code: Example code for pass by name.

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

(7)FORK CONCEPT: In system verilog 5 different types of forks are available.

TYPES OF FORKS: 1.FORK - JOIN


2. FORK - JOIN_ANY
3. FORK - JOIN_NONE
4. WAIT-FORK
5. DISABLE - FORK
(7.1)FORK-JOIN: Blocks until execution of all process, when all process completed then only
fork moving to unblocking state.

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

# outside fork block ...12

(b)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

#10 $display (" outside fork block ...%0d",$time); # before fork block ....5

endtask # process1..7
endclass
# process2..10
OUTPUT: # process3..12

# outside fork block ...22


(C)Code: Fork-join example.

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

#10 $display (" outside fork block ...%0d",$time);

#3 $display("second statment..%0d",$time);

end OUTPUT:
endtask
# before fork block ....5
endclass

# process1..7

# process4..8

# process2..12

# process3..15

# outside fork block ...25

# 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.

(a)Code: Example code for fork-join_any.

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

$display (" outside fork block ...%0d",$time);

endtask

endclass

(b)Code: Example for fork-join_any.

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

#10 $display (" outside fork block ...%0d",$time);

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

$display (" outside fork block ...%0d",$time);

endtask

endclass

OUTPUT:

# before fork block ....5

# process3..6

# process1..7

# process2..12

# outside fork block ...12

# process4..16
(7.3)FORK-JOIN_NONE: In case fork join_none fork block always non-blocking statement.

(a)Code: Example code for fork-join_none.


class pkt;
OUTPUT:
task run(); module top;
#5 $display("before fork block ....%0d",$time); pkt p;
before fork block ....5
fork initial begin # outside fork block ...5
#2 $display("process1..%0d",$time); p=new();
# process3..6
#5 $display("process2..%0d",$time); p.run();
# process1..7
#1 $display("process3..%0d",$time); end
join_none endmodule # process2..10
$display (" outside fork block ...%0d",$time);

endtask

endclass

(b)Code: Fork-join_none coding examples.

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

# outside fork block ...15


(c)Code: Example code for Fork-join_none.

class pkt;

task run();

#5 $display("before fork block ....%0d",$time);

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

#10 $display (" outside fork block ...%0d",$time);

endtask module top;


endclass pkt p;

initial begin

p=new();

OUTPUT: p.run();
# before fork block ....5 end

# process3..6 endmodule

# process1..7

# process2..12

# outside fork block ...15

# 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

endclass # outside fork block ...6

(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

disable fork; OUTPUT:

$display (" outside fork block ...%0d",$time); # before fork block ....5

endtask # outside fork block ...5


endclass
(c)Code: Example code for disable-fork along with (fork and join_none).

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

#10 $display (" outside fork block ...%0d",$time);

disable fork;

endtask

endclass

(d)Code: Example code for $finish.

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

#10 $display (" outside fork block ...%0d",$time);

$finish;
Kill the execution and stop
endtask the stimulation.

endclass
(7.5)WAIT-FORK: Killing the process is avoided by using wait fork.

(a)Code: Example code for wait fork along with $finish.

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:

# before fork block ....5

# process3..6

# process1..7

# process2..10

# process4..15

# outside fork block ...15


(b)Code: Example code wait fork along with disable fork condition.

class pkt;

task run(); module top;

#5 $display("before fork block ....%0d",$time); pkt p;

fork initial begin

#2 $display("process1..%0d",$time); p=new();

#5 $display("process2..%0d",$time); p.run();

#1 $display("process3..%0d",$time); end

#10 $display("process4..%0d",$time); endmodule

join_any

wait fork;

$display (" outside fork block ...%0d",$time);

disable fork;

endtask

endclass

OUTPUT:

# before fork block ....5

# process3..6

# process1..7

# process2..10

# process4..15

# outside fork block ...15

NOTE: Killing process($finish , disable fork) is avoided by using wait fork.


(8).PARAMETRIZED CLASS: Mainly used to assign some constant values.

Declaration of data types also possible through parameter.

Denoted by (#) symbol.

(a)Code: Parameterized class by using default value.

class pkt #(parameter int ADDR_WIDTH=32, ADDR_DEPTH=32); module top;


rand bit[ADDR_WIDTH-1:0] a; pkt#(32,64) p;
rand bit[ADDR_DEPTH-1:0] b; initial begin
Default is 32, But we
endclass can override that p=new();
default value.
p.randomize();

$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.

class pkt #(parameter int ADDR_WIDTH, ADDR_DEPTH);


module top;
rand bit[ADDR_WIDTH-1:0] a;
pkt p;
rand bit[ADDR_DEPTH-1:0] b;
initial begin
endclass
p=new();

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

The parameter 'ADDR_WIDTH' has not been given a


value.

The parameter 'ADDR_DEPTH' has not been given a


value.
(c)Code: Data type achieved by using parameter.

class pkt #(parameter type T=int );


module top;
rand T a;
pkt p;
rand T b;
Both (a and b) initial begin
endclass
variable are int
p=new();
datatype.
INT datatype is p.randomize();
2 sate. It not
supports (x) p.b=32'hxxxxxxxx;

OUTPUT: $display("a=%b b=%b",p.a,p.b);

a=00100110001100111001010010100001 end
b=00000000000000000000000000000000 endmodule

(d)Code: Default data type can be overridden.

class pkt#(parameter type T=int );

rand T a; module top;

rand T b; pkt#(integer) p;

endclass initial begin


We can able to override the p=new();
default data type.
p.randomize();

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.

class pkt#(parameter type T=int );


module top;
rand T a;
pkt#(logic[31:0]) p;
rand T b;
initial begin
endclass
p=new();

p.randomize();
OUTPUT: p.b=32'hxxxxxxxx;

a=00100110001100111001010010100001 $display("a=%b b=%b",p.a,p.b);


b=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx end

endmodule

(f)Code: We can achieve data types separately by using parameter.

class pkt#(parameter type T1=int,T2=int );

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.

class pkt#(parameter ADDR_WIDTH=32,type T1=bit,T2=logic);


module top;
rand T1[ADDR_WIDTH-1:0] a;
pkt p;
rand T2[ADDR_WIDTH-1:0] b;
initial begin
endclass
p=new();

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

(h)Code: Example code to skip the overriding.

class pkt#(parameter ADDR_WIDTH=32,type T=bit,T1=logic);

rand T[ADDR_WIDTH-1:0] a; module top;


rand T1[ADDR_WIDTH-1:0] b; pkt#(" ",logic,bit) p;
endclass initial begin

p=new();

p.randomize();
We can able to skip the
overriding by just denoting p.b=32'hxxxxxxxx;

(“ “) double quotes p.a=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;

rand bit[3:0] c=5;


Class 1(Base class)
rand bit [3:0] d=5;

endclass

Class 2
Here (a) is handle of
my_pkt class

class pkt#(parameter type T=my_pkt);

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;

rand bit[3:0] c=5;

rand bit [3:0] d=5;

endclass

class pkt#(parameter type T=my_pkt);


Here class pkt is parameterized.
T a;
And the default parameterized
function new(); class is (my_pkt)
a=new();

endfunction

endclass

We are overriding the class

class my_pkt1;

rand bit [3:0] c=12; module top;


rand bit [3:0] d=12; pkt#(my_pkt1) p;
endclass initial begin

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

class car#(parameter ADDR=4) extends pkt #(my_pkt);

rand bit [ADDR:0] c=20;

rand bit [ADDR:0] d=20; OUTPUT:


endclass
# c=10

# 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;

rand bit [3:0] d;

endclass

class pkt#(parameter type T);

T a;

function new();
Parameterized base class
a=new();

a.c=10;

endfunction

endclass

class car#(parameter ADDR=4) extends pkt#(my_pkt); Parameterized child


rand bit [ADDR:0] d; class extended from
parameterized base
endclass class

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

(a)Code: Example code for function arguments in parentheses. 4.ref

module top;

int a;

function int sum(input int a,b);

sum = a+b; Function with arguments


endfunction

initial begin
OUTPUT:
a=sum(10,10);
Value of a= 20
$display("\tValue of a= %0d",a);

end

endmodule

(b)Code: Arguments declaration inside function along with direction.

module top;

int a;

function int sum;

input int a,b;

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.

class pkt; module top;


task a; pkt p;
$display("Calling task inside function"); Here task a
initial begin
contain to
endtask delay p=new();
statement
function void b(); p.b();
a; end
endfunction endmodule
endclass

OUTPUT:

Calling task inside function

(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

$display("IM IN FUNCTION B\n"); OUTPUT:


endfunction #IM IN FUNCTION B
endclass
# IM IN TASK A
(g)Code: Function with return type.

module top;

int x;

function int sum;

input int a,b;

return a+b;

endfunction
OUTPUT:
initial begin

$display("Calling function with void"); # Calling function with void


x=sum(10,5); # value of x=15
$display("value of x=%0d",x);

end

endmodule

(h)Code: We are not assigning the return type to any other variable.

module top;

int x;
OUTPUT:

function int sum; Warning: example.sv(11): (vlog-2240) Treating


stand-alone use of function 'sum' as an implicit
input int a,b;
VOID cast.
return a+b;
#Calling function with void.
endfunction

initial begin

$display("Calling function with void");

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;

function int sum;

input int a,b;

return a+b;

endfunction

initial begin

$display("Calling function with void");


OUTPUT:
void'(sum(10,5));

end # Calling function with void

endmodule

(9.2)TASK: Split the code into small parts and reusing this task wherever required.

(a)Code: Task argument in parentheses.

module top;

int x;

task sum(input int a,b,output int c);

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;

input int a,b;

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;

function void print(); module top;

$display("THIS IS PRINT METHOD"); pkt p;

endfunction initial begin

task abc; p=new();

#10; p.run();

$display("THIS IS TASK abc"); end

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 Error: example.sv(33): return value


not allowed within tasks.

(e)Code: Task not supports void type.


class pkt; module top;
bit [3:0] a=10; pkt p;
bit [3:0] b; initial begin
task void abc; p=new();
#10; p.abc;
$display("THIS IS TASK abc"); p.def;
endtask $display("value of b=%0d",p.b);
task def; end
b=a; endmodule
endtask

endclass
OUTPUT:

Error: (vlog-13069) example.sv(27): near "void":


syntax error, unexpected void.
(f)Code: Inside task returning is achieved by using return by statement.

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

(9.3)DIFFERENCE BETWEEN TASK AND FUNCTION:

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--------------------------------------------------------

You might also like