I have a doubt on pre_randomize() and post_randomize() method.
Every class has built in randomize() method, which automatically calls both pre_randomize()(built in method) and post_randomize() (built in method) method.
For what purpose we are using pre_randomize() and post_randomize() methods?
Shall i give constraints in both pre_randomize() and post_randomize() methods?
shall i give constrains for “rand/randc” or “non_random” variables in pre_randomize() and post_randomize() methods?
May i know actual purpose of post_randomize() and pre_randomize() method because what are can be implemented before randomize and after it.
Hello ,
You can introduce user-defined functions inside these pre and post_randomize . some times all variable needed not be randomized and these variable may depend on previous cycle transaction variables like data inversions , ecc checks etc or you may want to change the randomized values.(pre randomize will be useful in inherited classes)
function void prerandomize()
function int add(input a, input b);
add = a +b;
end
end
endclass:A
class B extends class A;
rand int c;
function void prerandomize()
function int mul(input a, input b);
mul = a * b;
end
end
endclass:B
Initial begin
A a1 =new();
B b1 = new();
b1.randomize();
whether Parent class add() method which is in prerandomize() method will be available to child class prerandomize() method also?
How can we call prerandomize methods of add() and mul() methods by using a1 and b1 handles?
can you please briefly explain “some times all variable needed not be randomized and these variable may depend on previous cycle transaction variables like data inversions , ecc checks” statement.
“endclass” is missing and function inside a function is not allowed.
function void prerandomize()
function int add(input a, input b);
add = a +b;
end
end
you are using wrong functions to override pre/post randomize methods.The should be using pre_randomize/post_randomize methods.
you should call super.pre_randomize or super.post_randomize functions when you override them. randomize() function is virtual. When you call randomize(ie: obj.randomize()) it will first call pre_randomize method which is not virtual and at the end calls post_randomize.(in between these two calls actual randomization will happen)
Bydefault when you didn’t override a method then randomize() call will invoke base class pre_randomize() method because of inheritance.
You need not to call pre_randomize/post_randomize methods explicitly. They will be called automatically by randomize() method.
For example in a networking chip we append ECC value just to make sure transferred data at the destination didn’t have errors. In this case we can make use of post_randomize() method to calculate ECC value(this will be efficient way). If we do this via a constraint then it will inefficient.