Professional Documents
Culture Documents
program automatic;
myPacket p1=new();
myPacket p2;
initial begin
p2.encode=1’b0; // runtime Error
end class myPacket;
endprogram bit [2:0] header;
bit encode;
bit [2:0] mode;
bit [7:0] data;
bit stop;
endclass
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
4
Review, This keyword
class Packet;
bit [31:0] addr;
this.addr = addr;
endfunction
endclass
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
5
Review, Local class ABC;
byte public_var;
Is available only to the local byte local_var;
methods of the same
class function void display();
Is not accessible by child $display (public_var, local_var);
endfunction endclass
classes
Nonlocal methods that module tb;
access local members initial begin ABC abc = new();
can be inherited and abc.display();
overridden by child class abc.public_var = 1;
abc.local_var = 1; //compilation error
end
endmodule
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
6
Review, Static
class Packet;
bit [15:0] addr;
bit [7:0] data;
Static int ctr=0;
module tb;
initial
begin Packet p1, p2, p3;
p1 = new (16'hdead, 8'h12);
p2 = new (16'hface, 8'hab);
p3 = new (16'hcafe, 8'hfc);
www.chipverify.com end endmodule
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
7
Constraints
Constraints use to generate random values that satisfy all conditions
class Pkt;
rand bit [7:0] addr;
randc bit [7:0] data;
endclass
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
8
rand
class Packet;
Can be used on rand bit [2:0] data; endclass
Normal variables
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
10
Soft constraint
constraint c_data { soft data >= 4; ncsim> run
data <= 12; } abc = 0x4
… abc = 0x8
abc.randomize(); abc = 0x4
… abc = 0x7
abc = 0x7
abc.randomize() with { data == 2; };
…
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
14
Assign Child Class to Base Class (2)
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
15
Assign Base Class to Child Class
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
16
Assign Base Class to Child Class (2)
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
17
Assign Base Class to Child Class (3)
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
18
Virtual Methods
class Base;
endclass
class Child extends Base;
rand bit en;
function void display(string tag="Thread1");
$display (tag, addr, data, en);
endfunction
endclass
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
19
Virtual Methods
Virtual function can change inside child class
and called as child method
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
20
Without virtual keyword
class Packet;
int addr;
function new (int addr);
this.addr = addr;
endfunction
module tb;
Packet pkt [6];
initial begin
for (int i = 0; i < $size(pkt); i++)
pkt[i] = new;
Packet::get_pkt_ctr(); // Static call
pkt[5].get_pkt_ctr(); // Normal call
end
endmodule
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
24
Static function (2)
class Packet;
static int ctr=0;
bit [1:0] mode;
function new ();
ctr++;
endfunction
static function
get_pkt_ctr ();
$display ("ctr=%0d mode=%0d", ctr, mode);
endfunction
endclass
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
25
Shallow and Deep Copy
Shallow copy
pkt = new;
pkt2 = new pkt;
Deep copy
Packet p1 = new;
Packet p2 = new;
p2.copy (p1);
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
26
Shallow copy
class Header;
int id;
function new (int id);
this.id = id;
endfunction
function showId();
$display ("id=0x%0d", id);
endfunction
endclass
class Packet;
int addr;
int data;
Header hdr;
function new (int addr, int data, int id);
hdr = new (id);
this.addr = addr;
this.data = data;
endfunction
function display (string name);
$display ("[%s] addr=0x%0h data=0x%0h id=%0d", name, addr, data,
hdr.id);
endfunction
endclass
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
27
Shallow copy (2)
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
28
Deep copy
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
29
Parameterized Classes
class something #(int size = 8);
bit [size-1:0] out;
endclass
module tb;
something #(16) sth1; // pass 16 as "size"
something #(.size (8)) sth2; // pass 8 as "size"
typedef something #(4) td_nibble; // create an alias
td_nibble nibble;
initial begin
sth1 = new;
sth2 = new;
nibble = new;
$display ("sth1.out = %0d bits", $bits(sth1.out));
$display ("sth2.out = %0d bits", $bits(sth2.out));
$display ("nibble.out = %0d bits", $bits(nibble.out));
end
endmodule
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
30
Pass datatype as a parameter
class stack #(type T = int);
T item;
function T add_a (T a);
return item + a;
endfunction
endclass
module tb;
stack st;
stack #(bit[3:0]) bs;
stack #(real) rs;
initial begin
st = new; bs = new; rs = new;
st.item = -456;
$display ("st.item = %0d", st.add_a (10));
bs.item = 8'hA1;
$display ("bs.item = %0d", bs.add_a (10));
rs.item = 3.14;
$display ("rs.item = %0.2f", rs.add_a (10)); end
endmodule
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
31
Extern keyword
class ABC;
endclass
module tb;
initial begin
ABC abc = new();
abc.display();
end
endmodule
www.chipverify.com
Synopsys University Courseware
Copyright © 2020 Synopsys, Inc. All rights reserved.
SystemVerilog
Lecture - 5
Developed By: Vazgen Melikyan
32