Constraint #40
We have solved a similar constraint in our #34 article. The approach we used in that article may not be supported in some simulators or versions of the simulator. In such cases, we can use the approach discussed below.
In our case, we need to generate a real number from 3.5 to 5.5. we can generate a whole number from 35 to 55. Then, divide the number by 10 in the post_randomize() function, which will give us the Required real number.
To achieve this, we need to declare two variables: one integer type to generate the whole number (which should be rand) and the actual variable of the real type (which should not be a rand variable as mentioned before):
rand int a_temp; real a;
Constraint to generate the Whole number From 35 to 55
constraint a_c { a_temp inside {[35:55]}; }
then Divide the Whole number by 10 and assign it to the real Variable
function void post_randomize(); a = a_temp/10.0; // always provide .0 decimal value for real division. endfunction
By using the same method, you can generate any number. In the constraint, generate the whole number, which is the value multiplied by 10^n, where n is the number of decimals. In our case, the decimal is 1, so 3.5 10 = 35 to 5.5 10 = 55. Then, divide the generated number by 10^n in the post_randomize method.
Here is an example to generate a real number from 65.58 to 75.29:
In this case, the required decimal point is 2, so n = 2. In the constraint, generate the range 65.58 100 = 6558 to 75.29 100 = 7529, and then divide the generated number by 100 in the post_randomize method.
b_temp inside {[6558:7529]};
function void post_randomize(); b = b_temp/100.0; // always provide . decimal value for real division. endfunction
领英推è
This method allows us to generate real numbers within a specified range even when the simulator does not directly support generating real numbers in that range.
Check out the Executable Code:
class sample;
rand int a_temp;
real a;
constraint a_c {
a_temp inside {[35:55]};
}
function void post_randomize();
a = a_temp/10.0;
endfunction
endclass
module top;
sample s=new();
initial begin
$display("########OUTPUT########");
repeat(5) begin
s.randomize();
$display("The value of a = %0.1f",s.a);
end
$display("########END########");
end
endmodule
I appreciate your understanding of the methodology we've utilized to handle constraints and their real-world applications. Your support, through likes and reposts, will indeed encourage further exploration. I'm eagerly looking forward to engaging in meaningful discussions with you. Thank you! ????
Decoding Of the Above Code:
In Class
- Class Declaration: Declares a class named "sample" containing a random integer variable "a_temp" and a real variable "a".
- Random Variable Declaration:Declares a random integer variable named "a_temp" within the "sample" class. The variable is declared as a random variable (rand), which means it will be randomized during object creation.Declares a real variable named "a" within the "sample" class.
- Constraint: Defines a constraint named "a_c" which imposes the following condition on the "a_temp" variable:The value of "a_temp" must be in the range [35, 55].
- Post-Randomize Function: Defines a post-randomize function named "post_randomize()" which calculates the value of "a" based on the value of "a_temp" after randomization. It divides "a_temp" by 10.0 and assigns the result to "a".
In Module Top
- Instantiation of Class: Instantiates an object of the "sample" class named "s".
- Initial Block: Executes the following actions:Displays a header indicating the beginning of the output.Repeats 5 times:Randomizes the data members of the object "s" according to the constraints specified in the class definition.Displays the value of the randomized variable "a" with one decimal place.Displays a footer indicating the end of the output.
This program generates random values for the variable "a_temp" and calculates the corresponding value of "a" based on the constraint and the post-randomize function. The output displays the calculated value of "a" with one decimal place.