Constraint for a Palindrome No.
Constraint #53

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):

  • data before: 0000 (decimal: 0)
  • array[0]: 0001 (decimal: 1)
  • data after: 0001 (decimal: 1) (hexadecimal: 0x1)

Second Iteration (i=1):

  • data before: 0001 (decimal: 1) (hexadecimal: 0x1)
  • array[1]: 0010 (decimal: 2)
  • data after: 00010010 (decimal: 18) (hexadecimal: 0x12)

Fifth Iteration (i=4):

  • data before: 0001001000110100 (decimal: 4660) (hexadecimal: 0x1234)
  • array[4]: 0101 (decimal: 5)
  • data after: 00010010001101000101 (decimal: 74565) (hexadecimal: 0x12345)

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:

  • data = ((data<<4) | array[i]);

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:

  1. Class palindrome:Defines a class palindrome with a random array array of size 5, where each element is a 4-bit value.It also contains a 20-bit data variable.
  2. Constraint array_c:Constrains the elements of array to form a palindrome.Uses a foreach loop to iterate over each element of array.The constraint ensures that the first element (array[0]) is equal to the last element (array[4]), the second element (array[1]) is equal to the fourth element (array[3]), and so on, creating a palindrome structure.
  3. Function post_randomize:Defines a function post_randomize that concatenates the elements of array to data.Uses a for loop to iterate over each element of array and concatenate it to data.
  4. Module top: Instantiates an object num of a class palindrome. In the initial block, it calls randomize() for num to randomize the values of an array according to the array_c constraint. Displays the randomized array values and the concatenated data value.

How Constraints Achieve Palindrome

  • The constraint array_c ensures that the elements of the array form a palindrome by enforcing that the first element must be equal to the last element, the second element must be equal to the second-to-last element, and so on.

Concatenation Using Left Shifting

  • The post_randomize function uses the concatenation ({} operator) to combine the elements of an array into data.
  • data = {data, array[i]}; concatenates the current value of data with the value of array[i], effectively adding array[i] to the end of data.

Overall Functionality

  • The code generates random values for array that form a palindrome.
  • It then concatenates these values into data, creating a number that represents the palindrome.


Amir Sharfuddin

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

mohamed irsath I

Design Verification Engineer

8 个月
回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了