Constraint #49
Constraint for a variable i) on every 2nd randomization the value should be the reverse of the previous value ii) otherwise the value ranges from 500 to 10,000
First, let's clarify the constraint. We want a variable's value to range from 500 to 10,000 on every odd randomization. Additionally, on every odd randomization, the value should be the reverse of the previous randomized value.
To reverse a number mathematically, we use the modulus operator (%) with 10, as a decimal number system is based on 10 elements. Let's illustrate this with an example: if we have the number 3874, its reverse should be 4783.
Here's how the algorithm works:
We can achieve this with a while loop, as the number of iterations depends on the number of digits in the input.
while(prev_d1) begin rev_data = (rev_data*10) + (prev_d1%10); prev_d1 = prev_d1/10; end
To achieve the specified behavior, we can use a constraint that handles odd and even randomizations differently. We'll use a flag to keep track of whether the current randomization is odd or even. Here's how we can do it:
We'll use a single bit variable count to track whether the current randomization is odd or even. We'll initialize it to 1 for odd randomization. Inside the constraint, we'll use an if statement to check the value of count. If it's 0 (even), we'll constrain the variable to be in the range [500:10000]. If it's 1 (odd), we'll constrain the variable to be equal to the reverse of the previous value by assigning the Calculated value on the post_randomize.
After each randomization, we'll toggle the value of count using a post_randomize function to ensure that the next randomization behaves correctly.
Explore the executable constraint below and be sure to check out the EDA link.
class sample;
rand int data;
int prev_d;
int rev_data;
bit count;
constraint array_c {
data inside {[500:10000]};
if(count)
data == rev_data;
}
function void post_randomize();
int prev_d1;
count++;
prev_d = data;
prev_d1 = prev_d;
rev_data = 0;
while(prev_d1) begin
rev_data = (rev_data*10) + (prev_d1%10);
prev_d1 = prev_d1/10;
end
endfunction
endclass
module top;
sample s=new();
initial begin
$display("######## OUTPUT ########");
repeat(3) begin
repeat(2) begin
assert(s.randomize());
$display("data = %0d \t\t",s.data);
end
$display("");
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
Constraint:
Function post_randomize():
In Module Top
This program generates random values for the variable "data" and ensures that if "count" is true, "data" is equal to its reverse value. The output displays the generated values of "data".
Design Verification Engineer
1ySabarish Potnuru It depends upon our requirement If you want to share the same value among all the objects created from that class. Then declare it as static. Otherwise no need. The same applicable for the rand or non rand varibale
Staff Design Verification Engineer at NXP | IIT Hyderabad | SV, UVM, C++, DV, IP, Perl | Ex-Marvell
1ythe non rand variables in class should be declared as static ! so that you can call constructor inside the loop
Verification Engineer at Scaledge
1yconstraint LOGIC {foreach(arr[i]) if(i%2!=0) arr[i]==calculate(arr[i-1]); else arr[i] inside {[50:10000]}; } function int calculate(int num); int rem=0;int rev=0; while(num!=0) begin rem=num%10; rev=rev*10+rem; num=num/10; end return (rev); endfunction
Verification Engineer @ Scaledge | Functional & Formal Verification | UCIe, PCIe | SV, UVM | M.Tech
1yPerhaps this will work - constraint 🤔 { num inside {[500:10000]}; foreach (a[i]) a[i] = num%10**i; foreach (a[i]) num+= a[i] * 10**(a.size()); }
Jr. R&D Engineer @Synopsys | Ex-STMicroelectronics | Physical Verification | PDK Runset | CAD | Sign-Off | VLSI Voyager | AI Maestro | VIT'25 | LinkedIn Limits on connections So Please DM me @Whatsaap (9063958119)
1yThanks for the share sir mohamed irsath I