Upload
kesireddy-naren-bittu
View
226
Download
0
Embed Size (px)
Citation preview
7/25/2019 Deep Copy Shallow Copy
1/28
Hi Sai,
Following are the differences between shallow copy and deep copy.
Shallow Copy
* Shallow copy is also called address copy.
*A shallow copy of an object copies all the member field values.
* This work well if the fields are values but may not be what you want for fields that point to
dynamically allocated memory. The pointer will be copied, but the memory it points to will not be
copied. The field in both original object and the copy will then point to the same dynamically
allocated memory, which is not usually what you want.
*Shallow copy is the standard pointer assignment, It just copies the address of the pointer. It
doesnt allocate any memory or copy the contents being pointed to.
Deep copy
* A Deep copy duplicates the object or variable being pointed to so that the destination(The
object being assigned to) receives its own local copy.
*This way, the destination can do whatever it wants to its local copy and the object that was
copied from will not be affected.
*Doing deep copy requires our own copy constructor.
EXAMPLES:
Class members are values only
SHALLOW COPY
class Base;
int a =45,b =18;
endclass
program shallow_cp;
Base b1,b2;
initial begin
b1 =new();
b2 =new();
b2 =b1;
7/25/2019 Deep Copy Shallow Copy
2/28
$display("Values after Copying");
$display("b1 Object Values =%p",b1);
$display("b2 Object Values =%p",b2);
b1.a =10;
b2.b =100; //b1 and b2 point to same memory location
$display("Values after Changing ");$display("b1 Object Values =%p",b1);
$display("b2 Object Values =%p",b2);
end
endprogram
OUTPUT
Values after Copying
b1 Object Values ='{a:45, b:18}b2 Object Values ='{a:45, b:18}
Values after Changing
b1 Object Values ='{a:10, b:100}
b2 Object Values ='{a:10, b:100}
DEEP COPY
class Base;
int a =45,b =18;
function copy(Base p1);
this.a = p1.a;
this.b =p1.b;
endfunction
endclass
program deep_cp;
Base b1,b2;initial begin
b1 =new();
b2 =new();
b2.copy(b1); //b1 and b2 point to different memory location
$display("Values after Copying");
$display("b1 Object Values =%p",b1);
7/25/2019 Deep Copy Shallow Copy
3/28
$display("b2 Object Values =%p",b2);
b1.a =10;
b2.b =100;
$display("Values after Changing ");
$display("b1 Object Values =%p",b1);
$display("b2 Object Values =%p",b2);
end
endprogram
OUTPUT
Values after Copying
b1 Object Values ='{a:45, b:18}
b2 Object Values ='{a:45, b:18}
Values after Changing
b1 Object Values ='{a:10, b:18}b2 Object Values ='{a:45, b:100}
When Class contains Objects of other class.
Each class should have its own copy method.
class Mega;
int a =2,b =4;
endclass
class Giga;
Mega b1;
int d=8,e=16;
function new();
this.b1 =new();
//This class doesn't have its own copy constructor.
endfunction
function copy(Giga p1);
this.d = p1.d;
this.e =p1.e;
this.b1 =p1.b1;
endfunction
7/25/2019 Deep Copy Shallow Copy
4/28
endclass
program deep_cp;
Giga p1,p2;
initial beginp1 =new();
p2 =new();
p2.copy(p1);
$display("Values after Copying");
$display("p1 Object Values d=%0d e=%0d",p1.d,p1.e);
$display("p2 Object Values d=%0d e=%0d",p2.d,p2.e);
$display("Mega p1 a=%0d b=%0d",p1.b1.a,p1.b1.b);
$display("Mega p2 a=%0d b=%0d",p2.b1.a,p2.b1.b);
p1.d =100;
p2.e =1000;
p1.b1.a =200; //p1.b1 and p1.b1 point to same memory location but p1 and p2 points to different
memory location
p2.b1.b =400;
$display("Values after Changing ");
$display("p1 Object Values d=%0d e=%0d",p1.d,p1.e);
$display("p2 Object Values d=%0d e=%0d",p2.d,p2.e);
$display("Mega p1 a=%0d b=%0d",p1.b1.a,p1.b1.b);$display("Mega p2 a=%0d b=%0d",p2.b1.a,p2.b1.b);
end
endprogram
OUTPUT
Values after Copying
p1 Object Values d=8 e=16
p2 Object Values d=8 e=16Mega p1 a=2 b=4
Mega p2 a=2 b=4
Values after Changing
p1 Object Values d=100 e=16
p2 Object Values d=8 e=1000
Mega p1 a=200 b=400
7/25/2019 Deep Copy Shallow Copy
5/28
Mega p2 a=200 b=400
Deep copy when class contains other class object.
class Mega;
int a =2,b =4;
function Mega copy();
copy = new();
this.a =copy.a;
this.b =copy.b;
endfunction
endclass
class Giga;Mega b1;
int d=8,e=16;
function new();
this.b1 =new();
endfunction
function Giga copy();
copy = new();
this.d = copy.d;this.e =copy.e;
this.b1 =b1.copy() ;
endfunction
endclass
program deep_cp;
Giga p1,p2;
initial beginp1 =new();
p2 =new();
p2 = p1.copy();
$display("Values after Copying");
$display("p1 Object Values d=%0d e=%0d",p1.d,p1.e);
7/25/2019 Deep Copy Shallow Copy
6/28
$display("p2 Object Values d=%0d e=%0d",p2.d,p2.e);
$display("Mega p1 a=%0d b=%0d",p1.b1.a,p1.b1.b);
$display("Mega p2 a=%0d b=%0d",p2.b1.a,p2.b1.b);
p1.d =100;
p2.e =1000;
p1.b1.a =200;
p2.b1.b =400;
$display("Values after Changing ");
$display("p1 Object Values d=%0d e=%0d",p1.d,p1.e);
$display("p2 Object Values d=%0d e=%0d",p2.d,p2.e);
$display("Mega p1 a=%0d b=%0d",p1.b1.a,p1.b1.b);
$display("Mega p2 a=%0d b=%0d",p2.b1.a,p2.b1.b);
endendprogram
OUTPUT
Values after Copying
p1 Object Values d=8 e=16
p2 Object Values d=8 e=16
Mega p1 a=2 b=4
Mega p2 a=2 b=4
Values after Changing
p1 Object Values d=100 e=16p2 Object Values d=8 e=1000
Mega p1 a=200 b=4
Mega p2 a=2 b=400
===============================================
Thanks,
Nithin
7/25/2019 Deep Copy Shallow Copy
7/28
//--------------------------------------------------------------------------------------------------------------------------
1. The "Deepness" of the copy always has to be thought about in two dimensions : deepness in
relation to inheritance and deepness in relation to handles ( ie IS-A vs HAS-A ). The deepness in
one dimension is entirely orthogonal to deepness in the other.
2. An explanation in terms of pointers isn't a lot of help, since SV uses handles not pointers.
Handles are effectively pointers with automatic garbage collection.
3. I'm no expert in the VMM, so I will answer in terms of the AVM.
Consider the following arrangement :
Code:
class A;
int a;
endclass
class B;
int b;
endclass
class C extends A;int c;
B b;
endclass
C c = new();
A a = c;
Now, what does it mean to "copy" the object pointed to by the handle "a" ?
It could mean :
(i) we create a new A which is ONLY an A ( ie, we do a copy which shallow in relation to the
inhertance hierarchy )
(ii) we create a new A which is ALSO a C, but we do not copy B ( ie, we do a copy which is
deep in relation to the inheritance hiearchy but shallow in relation to references )
7/25/2019 Deep Copy Shallow Copy
8/28
(iii) we create a new A which is ALSO a C, and we do copy B ( ie we do a copy which is deep
both in relation to the inheritance and the reference to B ).
The AVM has two methods which provide some kind of copying mechanism : copy and clone.
By definition, copy is shallow in relation to the inheritance hierarchy and does not assume thatthe target of the copy has been allocated. In contrast, clone ( which usually uses copy ) is deep in
relation to the inheritance hierarchy and does its own allocation.
Both copy and clone are undefinedin relation to references ( and in fact, they may well be deep
in relation to some references and shallow in relation to others ). In other words, the designer of
the class needs to decide for themselves which references should or should not be copied, and if
they are going to be copied how they should be copied.
Code:
class A extends avm_transaction;
int a;
function void copy( input A t );
a = t.a;
endfunction
function avm_transaction clone();
A t = new();
t.copy( this );
return t;
endclass
class B extends avm_transaction;
int b;
function void copy( input B t );
b = t.b;
endfunction
function avm_transaction clone();
B t = new();
t.copy( this );
return t;
endfunction
endclass
7/25/2019 Deep Copy Shallow Copy
9/28
class C extends A;
int c;
B b;
function void copy( input C t );
super.copy( this ); // copy contents of Ac = t.c; // copy contents of c
// pick one of :
// b = t.b; // OPTIONAL copy of reference OR
// b = new();
// b.copy( t.b ); // OPTIONAL copy of "ONLY B" OR
$cast( b , t.b.clone() ); // OPTIONAL deep clone of t's "B"
endfunction
function avm_transaction clone();
C t = new();
t.copy( this );
return t;
endfunction
endclass
C c = new();
A a = c;
A a1 = new();
A a2;
a1.copy( a ); // "ONLY A" - don't care that a is actually a C
$cast( a2 , a.clone() ); // a2 is now "really" a C
In the code above, a1 is shallow in relation to inheritance ie it ignores the fact that a is actually a
C.
7/25/2019 Deep Copy Shallow Copy
10/28
a2 is a clone of a - in other words, it is also a C. It is up to the designer of C whether this means
that B is copied by reference, by copy, or by clone.
The uncommented code above actually does a clone of B inside C - in other words, C chooses to
do a copy which is deep in relation to any inheritance hierarchy of "b". Some other alternatives
are in the code but commented out.
Someone more familiar with the VMM can comment on how these various kinds of copy can be
achieved with the VMM.
Adam.
//---------------------------------------------------------------------------------------
And I will answer in terms of the VMM.
Code:
class A extends vmm_data;
int a;
virtual function vmm_data copy(vmm_data to = null);
A cpy;
if (to == null) cpy = new;else $cast(cpy, to);
super.copy_data(cpy);
cpy.a = this.a;
return t;
endclass
7/25/2019 Deep Copy Shallow Copy
11/28
class B extends vmm_data;
int b;
virtual function vmm_data copy(vmm_data to = null);
B cpy;
if (to == null) cpy = new;else $cast(cpy, to);
super.copy_data(cpy);
cpy.b = this.b;
return t;
endfunction
endclass
class C extends A;
int c;
B b;
virtual function vmm_data copy(vmm_data to = null);
C cpy;
if (to == null) cpy = new;
else $cast(cpy, to);
super.copy( this ); // copy contents of A
c = this.c; // copy contents of c
// pick one of :
// b = this.b; // OPTIONAL copy of reference OR
// b = new this.b; // OPTIONAL copy of "ONLY B" OR
$cast(b , this.b.copy() ); // OPTIONAL deep copy of "B"
endfunction
endclass
C c = new();A a = c;
A a1 = new a; // "ONLY A" - don't care that a is actually a C
A a2;
$cast( a2 , a.clone() ); // a2 is now "really" a C
7/25/2019 Deep Copy Shallow Copy
12/28
VMM does not have a shallow-wrt-inheritance copy() method by default as the SystemVerilog
new operator does that natively.
//======================================================================
Deep versus Shallow Copy
I recently got an e-mail asking about copying objects and decided it would be a great blog post.
7/25/2019 Deep Copy Shallow Copy
13/28
Specifically, the question asked about the differences between deep versus shallow copy and
when one should choose one technique versus another.
The short answer is there is no right answer. The long answer, well, is a little longer
In any Object-based system, copying an object is not a trivial exercise. As we talked about in the
OOP Connections chapter, your verification system is a mesh of objects. Copying a piece of that
mesh is tricky. But even beyond that is the seemingly simple task of copying data.
Suppose you are trying to verify a SOC that moves data to and from some interfaces (RAM,
ethernet, PCI, etc). You probably have the classic generator connected to driver and checker
through a channel model.
Before you have to consider shallow versus deep copy, you have to decide if you want to copy
the data. In general, you do want to copy the data, but there is at least one case where you do not.This is the case where the driver and the checker need to communicate some information about
the data transaction in the channel. Perhaps its a read completion, or a write status word.
Lets assume our example should copy the data. So, what then is deep copy ? A deep copy is a
recursive replication of the members in the class/struct. For example, if our data between the
generator and driver/checker was a list of blocks of lower level random data to be sent, both the
list and the blocks would be copied.
A shallow copy, by contrast, would only copy the list. A shallow copy does not recurse beyond
the top level object (the one being copied). In our example, there would only be one instance
of the blocks of lower level data.
If your data only consists of integral types, there is no difference between deep and shallow
copy. If there are pointers in your class/struct then you need to think about what you want to
happen.
Now, if you data has ANY virtual functions (i.e. not just a clump of data), then you really need
to think. My general rule is that you are probably doing something wrong. Go back and rethink
why your design needs both virtual data members and needs to copy.
If you really do need to copy a class/struct with virtual methods, you generally end up with a two
method scheme. One method makes/news the right type of object (because this is a virtual
method) and the second method copies the data. Pretty darn horrible.
7/25/2019 Deep Copy Shallow Copy
14/28
My general advice is to use pointers for most things in a channel. Then you do not have to worry
about copying. But if you do have to copy, make sure you think about all the members, data and
methods.
7/25/2019 Deep Copy Shallow Copy
15/28
//===================================================================
SystemVerilog has two similar data types that allow variables to be grouped together in a
handy package: the struct and the class. Ive heard it often said, when explaining what a class (an
object-oriented data type) is, that it is just like a C struct with functions. I used to have no
problem with that, until, when reviewing and debugging testbench code, I started seeing some
problems related to the way classes have to be treated differently to structs. One of the most
common errors Ive found is when data structures composed of classes are copied.
Consider the following:
class FooDataClass; integer D1; integer D2;endclass
struct { integer D1; integer D2;} FooDataStruct;
program test; FooDataClass X[] = new[5]; // array of classes FooDataClass Y[]; // array of classes FooDataStruct A[] = new[5]; // array of structs FooDataStruct B[]; // array of structs ... Y = X; // copy array of classes
B = A; // copy array of structs ...endprogram
In the case of the struct, its possible to copy the dynamic array A[] to B[], sizing B to the same
as A automatically. Its equivalent to:
B = new[A.size()];foreach(A[i]) begin B[i].D1 = A[i].D1; // copy value of A[i].D1 to B[i].D1 B[i].D2 = A[i].D2; // copy value of A[i].D2 to B[i].D2end
Importantly, B[] has its very own copy of the values of variables D1 and D2 for each element in
the array. So, if A[2].D1 was modified, B[2].D1 would not be.
In the case of the class, when we copy the dynamic array X[] to Y[], Y is still sized to the same
as X automatically, but something subtly different happens (that often catches people out), with
7/25/2019 Deep Copy Shallow Copy
16/28
D1 and D2. When arrays of classes are copied, the references (aka handles) to the class are
copied, not the values of the class members themselves. Its equivalent to:
Y = new[X.size()];foreach(X[i]) begin
Y[i] = X[i]; // copy reference to class X[i] into Y[i]end
This means that Y[] does not have its own copy of the values of variables D1 and D2 for each
element in the array. If X[2].D1 was modified Y[2].D1 would also be modified. In fact they are
the same variable, pointed to by the reference Y[2], which is equal to X[2]. Thats, most often
times, not the desired behavior of the code. Coding errors like this can be tricky to track down
and may stay hidden for some time.
Once youve created a class, people might start using it all over the place. People, who might not
have access to modify your class code, only use it. This has an impact on the above mentioneddifference between classes and structs. If you want to enable people to make copies of the data
values, as opposed to just the references, then you (as the developer of the class), should provide
a mechanism to deep copy the class. So with FooDataClass we might do:
function void FooDataClass::copy(FooDataClass c); this.D1 = c.D1; this.D2 = c.D2;endfunction
Here we copy the values of another class of the same type into our class. It is now possible to
make Y[] have its very own copy of the values of variables D1 and D2 for each element in the
array. So, if X[2].D1 was modified, Y[2].D1 would not be. However, we still have to do
something different for classes than we do for structs. Something like:
// copy of values of values in X[] to Y[]foreach(X[i]) begin Y[i] = new; // create new instance of the class Y[i].copy(X[i]); // copies *values* in class from X to Yend
So, when you are reviewing code, its always prudent to look for places where classes or things
containing classes are explicitly or implicitly copied.
This entry was posted by Jason Sprott on Sunday, January 20, 2008 at 4:24 am and is filed under News,
SystemVerilog. You can follow any responses to this entry through the RSS 2.0feed. Both comments and pings are
currently closed.
http://www.verilab.com/blog/category/news/http://www.verilab.com/blog/category/systemverilog/http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/feed/http://www.verilab.com/blog/category/news/http://www.verilab.com/blog/category/systemverilog/http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/feed/7/25/2019 Deep Copy Shallow Copy
17/28
2 Responses to SystemVerilog Gotcha: (when copying) a struct is not a class by
another name
1. Daniel PrschSays:January 22nd, 2008 at 7:37 am
Hi Verilab,
just a short note, you define A,B as classes and X,Y as structs in the code, but refer to
A,B as structs and X,Y as classes in the text. This is confusing, I guess the text is
correct?!
What would happen if I didnt have arrays of classes/structs but a single class and a
single struct, I guess the same rule would apply, i.e. copy-by-reference for classes and
copy-by-value for structs?
Theres no Copy-Constructor in SV, is there? Would be handy
Rgds,
Daniel
2. Jason SprottSays:January 23rd, 2008 at 2:28 am
Hi Daniel
Thanks for pointing out that error (now corrected). I fixed the variable names and also
added the new() of the elements in Y[], inside the foreach loop, for clarity.
Yes, when a struct is copied the values are copied. When a class is copied the reference is
copied, or a shallow copy is taken (as described in IEEE1800-2005, Section 7.11). I find
people typically remember that when they are dealing with the class itself. When classes
are inside something else, its easy to forget, because we are copying the thing and may
not even have visibility of its contents. Arrays of classes fall into that category, but a
class composed of other classes is another big one to watch.
There is no method for a deep copy/clone defined in the SystemVerilog language. Both
the AVM and VMM define methods for copying objects as part of the methodology. In
Vera we had a built-in method called object_copy() as part of the language. This
provided a deep copy of any object, without you having to write anything yourself. I
http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-154http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-154http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-155http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-155http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-154http://www.verilab.com/blog/2008/01/gotcha-when-copying-a-struct-is-not-a-class-by-a-different-name/#comment-1557/25/2019 Deep Copy Shallow Copy
18/28
found it useful, but the problem was that there was no control, so you could end up
copying something *very* large.
Of course when writing custom copy functions we sometimes put bugs in them too.
Jason
//====================================================================
SystemVerilog Interview Questions
1. How many array types in SystemVerilog? How do you use them?
array_name[ ] dynamic array
e.g . dyna_arr_1 =new[100](dyna_arr_1);
dyna_arr_2 =new[4]('{4,5,6});// elements are {4,5,6,0}
array [5] fixed array
e.g. register1 [6][7:0]=`1;
array[$] queue
e.g. intq[$]={2,4,8};
q ={}; // clear the queue (delete all items)
e =q[0]; // read the first (leftmost) item
e =q[$]; // read the last (rightmost) item
array[string] or array[%] associate array
e.g. //associative array of 4-state integers indexed by strings, default is '1.
integertab [string]='{"Peter":20,"Paul":22,"Mary":23,default:-1};
2) What is the Polymorphism?
7/25/2019 Deep Copy Shallow Copy
19/28
Polymorphism allows an entity to take a variety of representations. Polymorphism means the
ability to request that the same Operations be performed by a wide range of different types of
things. Effectively, this means that you can ask many different objects to perform the same
action. Override polymorphism is an override of existing code. Subclasses of existing classes
are given a "replacement method" for methods in the superclass. Superclass objects may also
use the replacement methods when dealing with objects of the subtype. The replacementmethod that a subclass provides has exactly the same signature as the original method in the
superclass.
EXAMPLE: with virtual
classA ;
virtual taskdisp ();
$display(" This is class A ");
endtask
endclass
classEAextendsA ;
taskdisp ();
$display(" This is Extended class A ");
endtask
endclass
programmain ;
EAmy_ea;
A my_a;
initial
begin
my_a =new();
my_a.disp();
my_ea =new();
my_a =my_ea;
my_a.disp();
end
endprogram
RESULTS
This is class A
This is Extended class A
7/25/2019 Deep Copy Shallow Copy
20/28
3) how the copy works?
Answers:
There are 2 types of copy. Show copy or deep copy
For example:
class B;
int
endclass
programmain;
initial
begin
B b1;
B b2;
b1 =new();
b1.i =123;
b2 =b1; // b1 and b2 point to the same memory. The properties did not getcopied.
$display(b2.i );
end
endprogram
RESULTS:
123
A shallow copy of an object copies all of the member field values.programmain;
initial
begin
B b1;
B b2;
b1 =new();
7/25/2019 Deep Copy Shallow Copy
21/28
b1.i =123;
b2 =newb1; // shallow copy of b1
b2.i =321;
$display(b1.i );
$display(b2.i );
end endprogram
RESULTS:
123
321
If the value of b1 change, it will also change the value of b1. It's because it's pointing to the
same memory.
To avoid this, we need to use the deep copy.
Deep Copy
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by
the fields. To make a deep copy, you must write a copy constructor and overload the
assignment operator, otherwise the copy will point to the original, with disasterous
consequences.
EXAMPLE:
classA;
inti;
endclass
classB;
A a;
taskcopy(A a);
this.a =newa;
endtask
endclass
7/25/2019 Deep Copy Shallow Copy
22/28
programmain;
initial
begin
B b1;
B b2;
b1 =new(); b1.a =new();
b1.a.i =123;
b2 =newb1;
b2.copy(b1.a);
$display(b1.a.i );
$display(b2.a.i );
b1.a.i =321;
$display(b1.a.i );
$display(b2.a.i );
end
endprogram
RESULTS:
123
123
321
123
7/25/2019 Deep Copy Shallow Copy
23/28
//=========================================================
Hi everyone,
please look at the following code.
class obj_change;int i;
endclass
class dsp;
task display(obj_change oj);
#2 $display("&&&&&&&&&&&&&&&&obj.i = %d",oj.i);
endtask
endclass
module obj_change_mod;
obj_change obj ;dsp dsp_obj;
initial
begin
dsp_obj = new;
7/25/2019 Deep Copy Shallow Copy
24/28
obj = new;
obj.i = 10;
$display("$$$$$$$$$obj.i = %d",obj.i);
fork
dsp_obj.display(obj);
join_none
#1 obj.i = 40;
$display("*********obj.i = %d",obj.i);
end
endmodule
OBSERVED OUTPUTS:
$$$$$$$$$obj.i = 10
*********obj.i = 40&&&&&&&&&&&&&&&&obj.i = 40
why I am getting these outputs ? Shallow copy from actual argument(obj) to formal argument(oj)
is not occurring.
I think this comes under "PASS by VALUE" function calling type.
//=======================================================================
====
The UVM clone method is used to provide a deep copy of an object. We can call clone() on any
object, and it will use the clone() method of its actual class, without the calling code needing to
know what that class is.
Clone first allocates new memory for the object, then copies over each field to the new object. If
a field is an object handle, then instead of copying the handle (which would do a "Shallow"
copy) you would call fieldname.clone() to recursively allocate memory for that field (a "Deep"copy).
7/25/2019 Deep Copy Shallow Copy
25/28
[B]clone[/B]() means construct followed by a call to [B]copy[/B](). [B]clone[/B]() is virtual so that
you can construct the same object type as the one you started with. [B]clone[/B]() calls
[B]copy[/B]() to perform a deep copy of the object type you just created. The [B]copy[/B]()
method by itself just copies class member values from one object to another. Normally, the
copy() method only copies the class members for the current class type, then it calls
super.copy() to copy the class members of the base class.
Interview Questions
1. What's difference between static and automatic task/program?If you use static task for multiple places in your testbench, the local variables will share
the common, static storage. In this case, different threads will step on each other'svalues.By using atuotmic storage, it will make a copy of local variables and use them.Not a common static storage any more. e.g. program automatic initialization; ...... endprogram
2. What's the packed array and unpacked array?unpacked array is an array with gap between variables.
7/25/2019 Deep Copy Shallow Copy
26/28
e.g. bit[7:0] b_unpack[3]; // unpackedTe system verilog simulators store each element on a 32-bit word boundary. In otherwords, you are using only lower 8 bits, the other 24 bits per word space is wasted.Packed array is an array without gap. Unpacked array is good for local individualvariable access.
e.g. bit[3:0] [7:0] bytes; // 4 bytes packed into 32 bits In this case, all 32 bits word are packed with 4 bytes. A packed array is handy if youneed to convert to and from scalars.
3. What's different between logic and wire?Logic and wire are almost the same except wire can be driven by multiple sources. Logiccan only driven by single source.
4. How the copying of objects works?
There are two types of copy 1)Shallow Copy2) Deep Copy
typedef enum {READ,WRITE}rw_t;
class ID;
string id;
endclass
class packet;
ID id;
rand bit [7:0]data;
rand bit [7:0]address;
rw_t rw;
function new();
id=new; data=0;
address=0;
rw=READ;
endfunction:new
function packet deep_copy;
deep_copy=new;
deep_copy.data=this.data;
deep_copy.address=this.address; deep_copy.rw=this.rw;
deep_copy.id.id=this.id.id;
endfunction
function void print (string str);
7/25/2019 Deep Copy Shallow Copy
27/28
$display("*************************%s**************************",str);
$display("data:%d -> address:%d-->rw:%s-->id:%s",this.data,this.address,this.rw,this.id.id);
endfunction
endclass:packet
program prg;
packet p1,p2;
initial begin
p1=new;
p1.data=10; p1.rw=WRITE; p1.id.id="P1";
//shallow copy will copy class variables and handles(pointers) in the class
p2=new p1;
p2.print("p2");
p2.data=30; p2.rw=READ;p2.id.id="P2";
p2.print("p2");
p1.print("p1");
//Deep Copy:all variable and variable in the object also copied not handle
p3=p1.deep_copy();
end
endprogram:pr
Why we do Gate level simulation ?
With wide acceptance of STA and Formal verification tools by the industry one question still
arises in the minds of many, "Why do we need gate level simulation?"
The common reasons quoted by many engineers are simple..
1. To check if reset release, initialization sequence and boot-up is proper.2. Since Scan insertions occur during and after synthesis, they are not checked by
simulations.3. STA does not analyze asynchronous interfaces.4. Unwarranted usage of wild cards in static timing constraints set false and multi cycle
paths where they dont belong. This can also be due to design changes, mis-understanding or typos.
5. Usage of create_clock instead of using create_generated_clock between clock domains.
http://digitalelectronics.blogspot.com/2006/10/gate-level-simulation-introduction.htmlhttp://digitalelectronics.blogspot.com/2006/10/gate-level-simulation-introduction.html7/25/2019 Deep Copy Shallow Copy
28/28
6. For switching factor to estimate power.7. X's in RTL sim can be pessimistic or optimistic. Any unintended dependencies on initial
conditions can be found through GLS.8. Design changes, wrong understanding of the design can lead to incorrect false paths or
multicycle paths in the constraints.9. Can be used to study the activity factor for power estimation.10. It's an excellent feel good quotient that the design has been implemented correctly.
Some design teams use GLS only in a zero-delay, ideal clock mode to check that the design can
come out of reset cleanly or that the test structures have been inserted properly. Other teams do
fully back annotated simulation as a way to check that the static timing constraints have been set
up correctly.
In all cases, getting a gate level simulation up and running is generally accompanied by a series
of challenges so frustrating that they precipitate a shower of adjectives as caustic as those
typically directed at your most unreliable internet service provider. There are many sources oftrouble in gate level simulation. This series will look at examples of problems that can come
from your library vendor, problems that come from the design, and problems that can come from
synthesis. It will also look at some of the additional challenges that arise when running gate level
simulation with back annotated SDF.