Constraint for a Palindrome No.
In order to solve the above constraints, we first need to understand what a palindrome number is. A palindrome number is a number that reads the same forwards and backwards. For example, 121 and 1331 are palindrome numbers because they remain the same when their digits are reversed.
As it is not possible or very hard to write a constraint to generate a palindrome number directly, we are going to use an array where we force the first element to be equal to the last element, the second element to be equal to the last element minus one, and so on. We then concatenate the array values in the post-randomize method.
for(int i=0; i<5; i++) begin
data = {data,array[i]};
//data = ((data<<4) | array[i]); end
Let's understand this with an example using an array containing 1, 2, 3, 4, and 5:
First Iteration (i=0):
Second Iteration (i=1):
Fifth Iteration (i=4):
After the loop completes, data will contain the final concatenated value of all elements in the array represented in binary.
We can achieve the same behavior using the following method, which will produce the same output:
It's important to note that we will see the palindrome behavior only if we print that value in hexadecimal.
Check out the Below Code and the output.
领英推荐
class palindrome;
rand bit [ 3:0] array[4:0] ;
bit [ 19:0] data;
constraint array_c {
foreach(array[i]) {
array[i] inside {[0:9]};
if(i<3) array[i] == array[4-i];
}
}
function void post_randomize();
for(int i=0; i<5; i++) begin
data = {data,array[i]};
//data = ((data<<4) | array[i]);
end
endfunction
endclass
module top;
palindrome num=new();
initial begin
$display("###### OUTPUT ######");
for(int i=0 ; i< 5; i++) begin
assert(num.randomize());
$write("Array Values are {");
foreach(num.array[i]) $write(" %h ",num.array[i]);
$display(" }\n data = %h",num.data);
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:
How Constraints Achieve Palindrome
Concatenation Using Left Shifting
Overall Functionality
Verification Engineer @ Scaledge | UCIe, PCIe, SV, UVM and AMBA Protocols | M.Tech
8 个月Save each digit in one array after that Constraint ??{ foreach(arr[i]) { If (i < arr.size()/2) arr[i] == arr[arr.size() - (i+1)]; } } Btw you can use constraint for storing digit inside array
Design Verification Engineer
8 个月jagadeesh kumar