Valid args to randomize() Method

Hi all ,

I am trying to figure out what is a valid arg. to randomize() method .


// Code I
class Main ;

   rand bit [1:0] b [2] ;

 endclass

 Main m ;

 initial begin

   m = new() ;

   if ( m.randomize( b[1] ) )    // legal ??
    $display("Success with %p",m);
   else
    $display("Fails");  


 end


I Observe Compilation Error with call to randomize() .
[Q1] Is element of unpacked not considered valid ?


// Code II
class A;

 rand bit [1:0] a ;

endclass

class B extends A;

 rand bit [1:0] a; // Overridden property . Obj of B will have 2 properties named 'a' !!

endclass

B b1;

initial begin

b1 = new();

if ( b1.randomize( super.a ) )     // Legal ??
$display("Success with %p ",b1 );

 end

[Q2] Some simulators allow this whereas others flash compilation error .
Is super.overridden_property Legal based on LRM ?

In reply to Have_A_Doubt:

Code I is illegal, the arguments to the randomize method must be variable names. Adding an index makes it a select expression.

Code II is not as clear, but I see no reason to code like this. You wind up with two active random variables with the same name.

In reply to dave_59:

Hi Dave ,

Code 2 is addition from Ahmed Yehia’s Paper on Randomization .

**Object of class B will have 2 properties named ‘a’ .

I understand that Code 2 isn’t a correct Coding practice .

The intention is to try out various code to understand the LRM better .**

If I modify Code2 above to ::


B b1;

initial begin

b1 = new();

if ( b1.randomize() with {super.a == 3 ;} ) 
$display("Success with %p ",b1 );  

end

This works and it basically constraints the property ‘a’ inherited by class B to value 3

The Output would now be ::

Success with ‘{super:’{a:3}, a:2}

The 2nd ‘a’ has no constraint ( i.e any value b/w 0 to 3 )

With the modified code , how is that super::a works within an inline-constraint but
doesn’t work as an argument to randomize() method ?

As super.a is considered a variable , isn’t it considered a Valid argument to randomize() based on LRM ??

In reply to Have_A_Doubt:

As you have already seen, some tools accept it, other tools don’t.

The built-in randomize is a peculiar construct in that its actual arguments are looked at from the perspective of the object it it is being called on, which is the same as how the with clause works, so I would expect it to be considered already inside the context of randomize method. But the LRM is not very clear on exactly what the arguments to the randomize method could be, just that they are variable names.

In reply to dave_59:

Thank you for the clear explanation .

I have a final question to end this post .


 // Code 3 
  class Base ;

 rand bit [1:0] a ;

 constraint aa { a == 0 ; }

endclass

class Ext1 extends Base;

 rand bit [1:0] a;

 constraint bb { a == 1 ; }

endclass

class Ext2 extends Ext1;

 rand bit [1:0] a;

 constraint cc { a == 2 ; }

// Override gparent Constraint aa
 constraint aa { Base::a == 2 ; }

// Override parent Constraint bb
 constraint bb { Ext1::a == 2 ; }

endclass

Ext2 e2;

 initial begin

  e2 = new();
  
  if ( e2.randomize() ) 
  $display("Success with %p ",e2 );  

 end


O/P is :: Success with ‘{super:’{super:'{a:'h2}, a:'h2}, a:'h2}

Class Scope resolution operator helps to unambiguously refer to correct variable even though none are static

However if I try to constraint via in-line Constraint ::


  // Code 4

  class Base ;

 rand bit [1:0] a ;

 constraint aa { a > 0 ; }

endclass

class Ext1 extends Base;

 rand bit [1:0] a;

 constraint bb { a > 1 ; }

endclass

class Ext2 extends Ext1;

 rand bit [1:0] a;

 constraint cc { a == 2 ; }


endclass

Ext2 e2;

 initial begin

  e2 = new();
  
  // TO_DO :: Below in-line Constraint throws Compilation Error !! 
  // Why does the Class Scope Resolution Operator work in Constraint Override but doesn't work within in-line Constraint
  
  if ( e2.randomize() with { Ext1::a == 2 ; Base::a == 2 ; } ) // Valid ??
  $display("Success with %p ",e2 );  

 end


**Since unrestricted Constraint block searches for variables in Class Object Scope first ,
shouldn’t this be considered a Valid Syntax ( even though these are non-static variables ) ??
**

In reply to Have_A_Doubt:

You are dealing with such poor programming practices that tools have probably never exercised those features. Please contact your tool vendor to report the problem.