IMPORTANT NOTICE: Please be advised that the Verification Academy Forums will be offline for scheduled maintenance on Sunday, September 14th at 1:00 US/Pacific.
LRM 18.6.1 :: Every class has a built-in randomize() virtual method,
So I need not have a random variable within the class !!
class SimpleSum;
function void pre_randomize();
$display("In pre_randomize()");
function void post_randomize();
$display("In post_randomize()");
endclass
Calling randomize() on object of class Simplesum calls both pre_randomize() as well as post_randomize() Functions .
How is it that post_randomize() is Still called ( in spite of No random Variable OR No Constraint Block )
[II] Regarding Operators “++” and “–” within Constraints
Why is that Operators “++” and “–” are Not allowed within Constraints ?
rand bit [1:0] a , b ;
constraint VALUES { a == ++b ; }
**Shouldn't this be equivalent to a == b + 1 ; ??**
If I understand your first question correctly ; you seem to assume that the “post_randomize” only runs if there are “rand” variables in your class.
This isn’t true. pre_randomize and post_randomize don’t really care if you have or do not have rand variables in your class. The only thing they depend on, is the call to “randomize”. If there is a call to “randomize”, pre_randomize and post_randomize will end up getting called.
Infact, the only way that post_randomize won’t get called if the “randomize” call fails. (You should be escalating randomization failures to errors, to be safe).
I don’t the exact answer to your second question. However, i am glad that increment operators aren’t. supported in constraints. Constraints aren’t solved in any particular order or sequence. All constraints are one big bucket, and the solver tries to satisfy all constraints.
Increment operators say b–, --b, would just add to the confusion and make it logically impossible to solve. Consider the below :
constraint SOLVE {
a == --b;
c == b;
}
What is “c” now ? The decrement operator is going to perform a “b=b-1” after that line is evaluated, but for contraints, it doesn’t work line by line. When will you decrement b ? will you revaluate “a” after b is decremented, and end up in an infinite loop ? What will “c” be ?
Logically, this is a nightmare, if not impossible. Just sticking with “b-1” is easy and solvable.
Ah. Your question is philosophical :)
You are asking, “Is it still considered a pass, if there are no variables to randomize?”
In this case (and most of the coding world) , the answer is a yes. The idea here is that the eventual randomize call ended up in passing “0” variables to the constraint solver, and the solver didn’t have to do a thing. A constraint solver failure (which implies that the randomization call failed) signifies that one or more variables couldn’t be solved for.
It would have been good for the LRM to explicitly state that “in case of 0 rand variables, the output of randomize is a 1/pass.” I tried a couple of simulators, and they seems to agree that randomizing 0 varialbes == PASS.
That’s my 2 cents. Others can feel free to comment and correct me where I am wrong.
I would like to add following points to this thread
(1) Regarding randomize() call without random variables and without ANY Constraints
LRM 18.11.1 :: Normally, calling the randomize() method of a class that has no random variables causes the method to behave as a checker.
In other words, it assigns no random values and only returns a status: 1 if all constraints are satisfied and 0 otherwise.
LRM 18.5.1 :: An empty constraint is one that has no effect on randomization, equivalent to
a constraint block containing the constant expression 1.
Hence both empty Constraints as well as a class without Constraints have no effect on
randomization and randomize() returns 1 ( Hence calls post_randomize() )
(2) Regarding Increment Operators in Constraint Blocks
LRM 11.4.2 :: These increment and decrement assignment operators behave as blocking assignments.
Since Assignments are Illegal in Constraint Blocks , Increment Operators are Illegal
rand int a ;
constraint ILLEGAL_ASSIGNMENT { a = 10 ; } // Illegal since its assignment !!
// Re-write to an expression Using :: a == 10 ; for the Constraint Solver to achieve 1'b1
// for the expression !!