TEST AND VERIFICATION SOLUTIONS
Training
SYSTEM VERILOG ASSIGNMENT-5
Questions (Class)
1) Declare eth_pkt class with following fields
o DA, Len, Payload, CRC
o Declare DA, Len, Payload as random (ex. rand DA)
o Declare Payload as dynamic array or queue
Instantiate eth_pkt inside module top and create a object for it.
Print the contents of eth pkt using $display
o Write print function inside eth pkt and call the function in module
-> Pass string name as an argument, name will tell who is printing
packet
Randomize pkt (ex. Pkt.randomize), with assert
Print eth_pkt contents by calling print method
o Apply constraint for payload
(ex. Constraint payload_c {payload.size() >10 ; payload.size() <20 ;}) in
class
Randomize again the print the elements
https://edaplayground.com/x/SHN9
2) Declare the property len in eth_pkt as local/protected and try to access
from module top (pkt.len=100) and display it. // Complier Error
o Write a method to assign values for local / protected variable in eth_pkt
Now assign value for top using the method which is available in eth_pkt
and print it
https://edaplayground.com/x/XgA_
3) Derive new_pkt from eth_pkt , Override print method in derived class
Instantiate derived class new_pkt and create object for it
From module top call print method of derived class
Use super keyword to print the eth_pkt print while call print method of
new_pkt (use super.print() inside print method of new_pkt)
Check which one able to access from derived class either protected or
local property
https://edaplayground.com/x/G2Zp
4) Declare a variable ‘Count’ as static in eth_pkt class
-> Used to count total eth_pkt instances created in the TB module
-> Count will be incremented as part function new
//Write new function in eth_pkt and inside that increment count by 1.
Create 3 instances of eth_pkt in module top (pkt1, pkt2, pkt3)
Create object for these instances. (ex. pkt1=new () ;)
Print the each eth_pkt instance content
Assign count=5 in one of the eth_pkt instance and print all eth_pkt
instance content
Declare the count property as automatic and do the same. // Default
every property in class is automatic
https://edaplayground.com/x/d5ws
5) Handle Copy:
o Declare the following fields in eth_pkt class
-> DA, Len, Payload as random (ex. Rand DA)
-> Payload is a dynamic array
o Write a constraint for payload size
o Write a print method to print the properties in this class
In module top create two instance of eth_pkt as pkt1 and pkt2
Create an object for pkt1 only
Using randomize method randomize pkt1
Print pkt1 and pkt2 fields
Assign pkt2=pkt1;
Print pkt1 and pkt2 fields
Change pkt1.len value (pkt1.len=5 ;)
Print pkt1 and pkt2 fields
https://edaplayground.com/x/PA7i
6) Shallow Copy:
In the above question instead of pkt2=pkt1 assign pkt2=new pkt1; in
module
To understand the difference between handle copy and shallow copy,
compare the outputs Q.No.5 and 6
https://edaplayground.com/x/iUii
7) Deep Copy: (Update above code with following instructions)
Declare a class named my_pkt with a property m_count of int type
Instantiate this my_pkt inside eth_pkt // my_pkt m_pkt;
Create an object for my_pkt inside function new() of eth_pkt
//function new() m_pkt=new(); endfunction
Create copy method //function copy(output eth_pkt pkt)
-> inside that copy the field of this instance to another// pkt= new this
-> copy the nested handle in this instance to another //pkt.m_pkt=new
this.m_pkt
In print method add m_pkt.m_count to display
Do the following steps in module Top
Instantiate two eth_pkt as pkt1 and pkt2
Create a object for pkt1
Randomize pkt1 (pkt1.randomize () ;)
Assign m_count value as 5 (pkt1.m_pkt.m_count=’h5)
Call print method of pkt1 // pkt1.print();
Create object for pkt2 // pkt2=new();
Call print method of pkt2 // pkt1.print();
Call copy method of pkt1 and keep pkt2 as argument // pkt1.copy(pkt2)
Call print method of pkt2 // Everything get copied
Change values of the properties in pkt1
// pkt1.m_pkt.m_count='h4;pkt1.DA='h1;pkt1.len='h5;
Again call print pkt1 and pkt2 by calling print method
// Nested handle of a class also get copied in another memory location
For better understanding try this question using shallow copy instead of
copy method (deep copy)
https://edaplayground.com/x/ptWa
8) o Declare eth_pkt class with following properties
-> sa, preamble, sof, len and payload[], declare all these with rand
o Write a constraint as follows
-> preamble should be 10
-> len should be between 5 and 8
-> payload size should be equal to len
o Write print method to display all the properties in this class
o Write compare function two compare two instances
In module top instantiate pkt1 and pkt2
Create object for pkt1, randomize and print
Do shallow copy
Print pkt2
Try by comparing pkt1 and pkt2 by using if condition // Won’t work
Call compare method and compare the 2 pkts
Use ‘extern’ to implement compare method outside the class and do
the comparison.
https://edaplayground.com/x/Acmx