Thursday, March 17, 2016

constraints -- blessing or cursing ?


Sometimes, we might hit the constraint solver timeout error in our simulations. I was curious in knowing the reason. It has to do more with how we are coding the constraints.  I found the below link very interesting.


One classic example is below and I think we can relate that to our complex benches  :-
When iterative constraints are used on arrays, each element has a constraint on it. If the constraints are simple enough to implement with out using constraint block, simulation time may be saved. In the following example, there are two constraints blocks, one for size other for each element. In reality there may be more constraint blocks. 



EXAMPLE:
class Eth_pkt;
rand byte Payload[] ;
constraint size_c { Payload.size() inside {[46:1500]}; }
constraint element_c { foreach ( Payload[ i ] ) Payload[ i ] inside {[50:100]}; }
endclass

program iterative_68;
Eth_pkt obj
;
initial
begin
obj 
= new();
for(int i=0;i< 10000;i++)
begin
if(obj.randomize())
$display(" RANDOMIZATION DONE ");
end
$finish(2);
end
endprogram
RESULT:

# ** Note: Data structure takes 3407960 bytes of memory
# Process time 705.51 seconds


The above logic can implemented using post_randomize. Check how these two example with ur vendor tool and look at the simulation speed. You may find difference.
EXAMPLE:
class Eth_pkt;
rand integer length;
byte Payload[] ;
constraint size_c { length inside {[46:1500]}; }
function void post_randomize;
Payload 
= new[length];
for(int i=0;i< length;i++)
Payload
[ i ] = 50 + $urandom % 51 ;
endfunction
endclass

program iterative_69;
Eth_pkt obj
;
initial
begin
obj 
= new();
for(int i=0;i< 10000;i++)
begin
if(obj.randomize())
$display(" RANDOMIZATION DONE ");
end
$finish(2);
end
endprogram

# ** Note: Data structure takes 3539032 bytes of memory
# Process time 3.92 seconds 

I copy-pasted this code and ran in VCS simulator.
Look at the time difference J. I really got amazed.

CPU Time:    101.660 seconds;       Data structure size:   0.0Mb

CPU Time:      1.190 seconds;       Data structure size:   0.0Mb



Monday, March 14, 2016

be careful about passing objects to functions

Generally from uvm_monitor, we form the transactions and write them to one or more scoreboards. For example, if a monitor is writing an object to 2 scoreboards (or any uvm_components),  both the scoreboards will point to the same objects(even though we don't pass it as 'ref' arguments). If you anyone of the scoreboards change the object, other scoreboard will see that change. This is one of the common mistake people do in uvm benches. That's why it is always to recommend to clone the object before consuming that object in that component.

Here is an simple example to illustrate how it works:-

class C;
  int a;
endclass

program test;
  function F(C c2);
    //c2 = new();
    c2.a = 20;
  endfunction

  initial begin
   C c1;
   c1 = new();
   F(c1);
   $display("c1.a = %0d", c1.a);
  end

endprogram

Result:-
c1.a = 20

Please note here we didn't pass the object as ref but still it acts as if it is ref argument.
Then, what is the meaning of ref argument for the class objects?
Let me slightly change the code and see what happens.

program test;
  function F(C c2);
    c2 = new();
    c2.a = 20;
  endfunction

  initial begin
   C c1;
   //c1 = new();
   F(c1);
   $display("c1.a = %0d", c1.a);
  end

endprogram

Result:-
Null object error

In earlier case, c1 & c2 are 2 different object handles pointing to same object memory and hence any change in function is visible outside. Whereas in latter case, c1 points to null and c2 points to valid object memory. Hence we get null object error for c1.a.

if we use ref, it will behave differently.
program test;
  function F(ref C c2);
    c2 = new();
    c2.a = 20;
  endfunction

  initial begin
   C c1;
   //c1 = new();
   F(c1);
   $display("c1.a = %0d", c1.a);
  end

endprogram

Result:-
c1.a = 20

Now c1, c2 are same and hence any object allocation to c2 is same as to c1 itself. Most of the times, we play around with transaction objects and we might face potential issues if we miss this understanding.