Saturday, 16 April 2016

SystemVerilog FAQ1

What happens if I randomize variable var1 using constraint cn {0 < var1 < 100;}?
The solver considered the this constraint as (0 < var1) || (var1 < 100), so result will not be as expected.
Ex1: value 500: It is not less than 100 but it is greater than 0,
Ex2: value -1: It is not greater than 0 but it is less than 100.
To solve this issue use one of the following way,
1. Use inside operator like, constraint cn {var1 inside {[1:99]};}
          You can’t use constraint cn {var1 inside {(0:100)};} -> Wrong Syntax
2. Constraint cn {var1 >0; var1 < 100}



What are bi-directional constraints?
Constraints by-default in SystemVerilog is bi-directional. That implies that the constraint solver doesn't follow the sequence in which the constraints are specified. All the variables are looked simultaneously. Even the procedural looking constrains like if ... else ... and -> constrains, both if and else part are tried to solve concurrently.

So, all the variables are randomized simultaneously,
typedef enum {low, mid, high} AddrType;
class MyBus extends Bus;
rand bit [15:0] addr;
rand AddrType atype;
constraint addr_range {
(atype == low ) -> addr inside { [0 : 15] };
(atype == mid ) -> addr inside { [16 : 127]};
(atype == high) -> addr inside {[128 : 255]};
}
endclass
So as shown in above example, the value chosen for addr depends on atype and how it is constrained, and the value chosen for atype depends on addr and how it is constrained.



What is circular dependency?
Too many explicit variable ordering in randomization may lead to circular dependency. The LRM says that "Circular dependencies created by the implicit variable ordering shall result in an error." and "circular dependency is not allowed". But it does not put restriction on what to do if an explicit circular dependency exists.
...
int x, y, z;
constraint XYZ  {
 solve x before y;
 solve y before z;
 solve z before x;
 ....
}



What is solve...before constraint? 
By default Constraint solver tried to randomize all variables simultaneously.
In the case where the user want to specify the order in which the constraints solver shall solve the constraints, the user can specify the order via solve before construct.



What is the difference between randomize() and std::randomize() method?
Variables in an object are randomized using the randomize() class method. Every class has a built-in randomize() virtual method. However, std::randomize() is a mechanism to randomize data that do not belong to a class.
Ultimately, randomize() is a virtual method of class, so class’s variables are randomized using randomize() method while std::randomize() is used when you want to randomize variable without writing class and creating its object.



Are pre_randomize/post_randomize methods virtual?
No. The pre_randomize() and post_randomize() methods are not virtual. However, because they are automatically called by the randomize() method, which is virtual, so they appear to behave as virtual methods.
Below example demonstrates that these functions are not virtual but simulation results show that, it executed extended class definition functions. Extended class object is created and assigned to base class object. Calls to pre_randomize and post_randomize calls in object B, executed the extended class definitions.
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
By removing the virtual keyword for the post_randomize() function, calling the randomize() function by parent and child class, both will execute functions of child class only. This is virtual function behaviour.
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------



Does post_randomize() is called if randomize() method is failed?
No.



How to randomize string variable?
String variable cannot be randomized directly using randomize method.
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------



How to randomize real variable?
Using randomize() method we can randomize only integral data-type variables only (bit, logic, reg, wire, integer, enum, packed struct)
Here I show two ways to randomize variable of type real.
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------




Without using randomize method or rand, generate an array of unique values?
...

int UniqVal[10];

foreach(UniqVal[i]) begin

  UniqVal[i] = i;

end

UniqVal.shuffle();




Can I use randc variable inside solve...before constraint? 
The following restrictions apply to solve...before:

1) Only random variables are allowed, that is, they shall be rand.
2) randc variables are not allowed. randc variables are always solved before any other.



Write code to generate one-hot random variable having length of 5 bit.
-------------------------------------------------------------

-------------------------------------------------------------



Write code to generate two-hot random variable having length of  5 bit.
-------------------------------------------------------------

-------------------------------------------------------------




How to randomize dynamic unpacked array?
  1. Randomize size of dynamic array, it’s contents will be automatically randomized and assigned.
  2. Randomize size of dynamic array, then randomize content of dynamic array in post_randomize method.
    class ABC;
    // Dynamic array
    rand bit [7:0] data [];


    // Constraints
    constraint cc {
     // constraining size
     data.size inside {[1:10]};


     // constraining individual entry
     data[0] > 5;


     // All elements
     foreach(data[i])
     if(i > 0)
       data[i] > data[i-1];
    }
    endclass : ABC



What will be the output of following Code?
-------------------------------------------------------------
-------------------------------------------------------------

How to resolve it?
Need to turn off constraint of Base class.

How to turn off that constraint?
-------------------------------------------------------------

-------------------------------------------------------------

No comments:

Post a Comment