Sunday, September 10, 2017

Randomizations and constraint overriding in SV : Part2

Pre_randomize and Post_randomize  :

1. Every class contains pre_randomize() and post_randomize() methods
2. which are automatically called by randomize() before and after computing new random values.
3. When randomize() is called it invokes three methods in below order :
    i. pre_randomize()
    ii. randomize()
    ii. post_randomize()


Note : post_randomize() will be only invoked if the randomize() was successful 

These methods can be used as hooks for the user to perform operations
for example : setting initial values and performing functions after assigning random variables.

///////////////////////////////////////
program pre_post_15;
class simple;
function void pre_randomize;
$display(" PRE_RANDOMIZATION ");
endfunction

function void post_randomize;
$display(" POST_RANDOMIZATION ");
endfunction

endclass
simple obj = new();
initial
void'(obj.randomize());
endprogram
////////////////////////////////////////////////////////


Output:

PRE_RANDOMIZATION
POST_RANDOMIZATION
$finish at simulation time 0
V C S S i m u l a t i o n R e p o r t

------------------------------------------------------------------------------------------------------------
NowI have changed a function with randomize in place of pre_randomize :
VCS compiler has given below error :

The user declared method 'randomize' in class 'simple' clashes with built-in
method name. Please change the name of the user declared method/identifier.

4. Overriding of pre_randomize and post_randomize functions is allowed by child class.


5. The pre_randomize() and post_randomize() methods are not virtual. However, because they are automatically called by the randomize() method, which is virtual, they appear to behave as virtual methods.


Question on constraint Overriding :

Can we override constraints of base class in extended class, by defining a new constraint with the same name in extended class.

In the code below, there are two constraint in "base" class for variable "a" & "b".

In "extended" class which is extended from base, I defined two more constraint which are in conflict with the constraint in base class. For variable "a" constraint I used the same name as in base class, and for "b" I used a different name.



When I randomized the extended class object, I expected randomization should fail for both the constraint in extended class. But the randomization only fail for the conflicting constraint with different name in base and extended class.
module junk () ;
   class base;
      rand bit [15:0] a,b;
      
      function new();
      endfunction // new
      
      constraint ct_a {a inside {[0:9]};} 
      constraint ct_b {b inside {[10:19]};}
    endclass // base
   
   class extended extends base;
      function new();
      endfunction // new

      constraint ct_a       {a inside {[10:19]};}
      constraint ct_b_new   {b inside {[0:9]};}
   endclass // extended
   
   initial begin
      extended extended_obj;
      extended_obj = new();

      if(!extended_obj.randomize()) $display("Randomization Failed");
   end
endmodule // junk


Answer :
1. That's expected as per LRM. With same names, you are "overriding" the base constraint and hence is not conflicting.

2. if u use the same name in extended class that means you are overriding that constraint in the derived class. so the constraint in the base class no more work for the extended class. so for 'a' there is no conflict and the constraint is easily overwritten in extended class.

Note : here we are randomizing the object of extended class

where as for 'b' both the constraints are working so conflicts occurs in that case.



No comments:

Post a Comment

Ethernet and more

Ethernet is a protocol under IEEE 802.33 standard User Datagram Protocol (UDP) UDP is a connectionless transport protocol. I...