SystemVerilog- Difference between Class assignment , Shallow copy and Deep copy

SystemVerilog- Difference between Class assignment , Shallow copy and Deep copy

https://www.youtube.com/channel/UCEt3Jnel-o1KCz1RuUTD2ZQ

Subscribe to our channel for VLSI Stuff.

VLSI SAINT

Hello folks, lets jump into the code first and lets understand the flow one by one.


Class Definitions (tr and packet):

The tr class represents a transaction with an address (addr) and data (data).The packet class contains a tr object (tr_h), along with additional properties like strobe and en.

Class Methods:

Both classes have a display method to print their details. The copy method in both classes creates a copy of the object.

Module ( module_name ):

Instantiates objects of the packet class (cl_a1, cl_a2, sh_c1, sh_c2, dp_c1, dp_c2).Demonstrates class assignment, shallow copy, and deep copy with appropriate modifications and displays.

Transaction class

here in this transaction class we have two properties addr and data, one constructor(to initialise the current class properties) and two functions one for to display the current class addr and data, and another function for to copy the class. ignore this in case of Shallow copy and class assignment this will be useful function for Deep copy.

Packet Class

In this Packet Class we have the handle of the transcation class as tr_h, strobe and en as properties.

one constructor for to initialize the object for tr_h handle and to initialize the strobe and en properties

one display function to display all the tr_h properties and the current class properties.

Similar to the above transaction class here also we have the copy function to copy the inner object, so in deep copy inner objects will be copied with the help of this methos only as we can see in this method we will do the copy of the copy.tr_h = this.tr_h , and remember one thing that is for the copy function keenly observe that the return type is the class name in the packet class packet is the return type, we can observe that in the copy function declaration like function packet copy(); this packet is the return type of the function that is class is the return type.


Module Name

in side module i have created two handles for each 1. class assignment 2.shallow copy 3.deep copy

in total 6 handle of packet class type handles.

Class Assignment

i have explained all 3 concepts with the same similar method that is we have two handles right. in all 3 types initially for the first handle we will create the memory/object so when we initialize the memory internally transaction object will also be created. by the time memory created with the first handle we can access

the below properties can be accessed. here observe one object inside another, means transaction object(tr_h) inside packet object (c1_a1)

now observe the code that i gave in the top, So if we can see in the in class assignments both handles(cl_a1,cl_a2) are pointing to the same objects, so that is why after copying that means

cl_a2= cl_a1;

when we tried to change the properties of packet object and properties of transaction object inside the packet object. we can see the same change will happen on the properties of packet object and properties of transaction object inside the packet object.

This is what happens in class assignments.


Shallow copy

now in this case if we can see inner object (transaction object) wont be copied, only inner handle will be copied, so any change happen on the inner transaction object properties in one handle will also we can see in another class inner transaction object.

sh_a2 = new sh_a1;


Deep Copy

in conclusion we could say

Class Assignment - Both handles will points to the same objcet.

Shallow copy - inner objects wont copied , only handles will copied.

Deep copy - objects and handle both will be completely compied.

Follow our page for more such interesting Stuff related to VLSI

And Subscribe to our VLSI SAINT YouTube Channel Aswell

https://www.youtube.com/channel/UCEt3Jnel-o1KCz1RuUTD2ZQ

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics