Write a constraint for polindrome (say 32 bit)?

constraint

In reply to surya narayana Gutha:

There can be 2 ways of doing it , one is having the logic in post_randomize function . And other one is having logic in constraint .Find below the example
Code :

rand bit[31:0] dummy; //Logic in post randomize
rand bit[31:0] dummy1; // Logic in constraint
function void post_randomize();
for(int i =0;i<16;i++)
begin
dummy[31-i] = dummy[i];
end
endfunction

constraint range {
foreach(dummy[j])
dummy1[j] == dummy1[31-j];}

Output :
1st iteration :
dummy = 10110010000101111110100001001101
dummy1 = 10110111101001111110010111101101
2nd iteration :
dummy= 11010000000111100111100000001011
dummy1 = 00111000101010011001010100011100

1 Like

In reply to nikhilverif:

Now that you’ve given the interview/homework problem away, here’s the simple version:

constraint palindrome { dummy2 == {<<{dummy2}}; }

Ref: 11.4.14.2 Re-ordering of the generic stream

1 Like

In reply to nikhilverif:

Thanks for the solution.I want to see in decimal mode like this ‘58985’.

Hi Dave,

I see below error if I use streaming operators in constraints.

Error-[IUSO] Incorrect use of streaming operators
testbench.sv, 7
$unit, “{ << 1 {this.data}}”
Streaming operators cannot be used in this context

Hi @dave_59 I am also getting same error as @VarunVlsi mentioned.
CODE :
class A;
rand bit[31:0] dummy; //Logic in post randomize
rand bit[31:0] dummy1; // Logic in constraint
rand bit[31:0] dummy2; // Logic in constraint

function void post_randomize();

for(int i =0;i<16;i++)
begin
dummy[31-i] = dummy[i];
end
endfunction

constraint range {
foreach(dummy1[j])
dummy1[j] == dummy1[31-j];}

constraint palindrome { dummy2 == {<<{dummy2}}; }
endclass

module AA;
A a;
initial begin
a = new();
a.randomize();
$display(" dummy = %b", a.dummy);
$display(" dummy = %b", a.dummy1);
end

endmodule

Simulation output result:

Error-[IUSO] Incorrect use of streaming operators
testbench.sv, 21
$unit, “{ << 1 {this.dummy2}}”
Streaming operators cannot be used in this context

Error-[ETRFS] Explicit typecast required for streams
testbench.sv, 21
$unit, “(this.dummy2 == { << 1 {this.dummy2}})”
Stream operands require explicit typecast to be used with other operators

And one more thing that @surya_narayana_Guptha said he want palindrome in the form of decimal.
Eg : 59395.

Please suggest us or help us.

Thanks in Adavance.

In reply to VK_18:

A decimal palindrome requires a completely different strategy, probably using binary-coded decimal(BCD)

In reply to dave_59:

Hi, I was trying to make code for decimal base palindrome here. I am able to reverse number but I don’t know

1.) how to reduce length of a to 16 bits, so that total produce result is of 32 bit after concatenation.

Is there any better approach you can suggest? Thanks.

[i]In reply to [url=https://verificationacademy.com/forums/systemverilog/write-

 class b;
  rand int unsigned a;   
  rand shortint x,y;
   constraint a_c {reverse_num(x)==y;}
   constraint xy { a[15:0] == x; a[31:16] == y; }
    function shortint unsigned reverse_num(shortint a);
    while(a>0) begin
      shortint pop_1;
      pop_1= a%10;
      reverse_num=reverse_num*10 + pop_1;
      a =a /10;
    end
  endfunction
  endclass

Hi Dave,

I tried the above code, but not able to see the actual output.

module p1;
class b;
  rand int unsigned a;   
  rand shortint unsigned x,y;
   constraint a_c {reverse_num(x)==y;}
   constraint xy { a[15:0] == x; a[31:16] == y; }
    function shortint unsigned reverse_num(shortint a);
    while(a>0) begin
      shortint pop_1;
      pop_1= a%10;
      reverse_num=reverse_num*10 + pop_1;
      a =a /10;
    end
  endfunction
  function void post_randomize();
    $display("elements %0d %0d %0d", x,y, a);
    endfunction
  endclass
  b obj;
  initial begin
    obj = new;
    obj.randomize();
  end
endmodule

==================
output of the code

elements 53412 0 53412

if the input is 53412, output must be 21435.
May I know where do I see the output?

Thank you,

 class A;
    
    rand bit[31:0] val;
    rand bit[3:0] arr[8];
    
    constraint PALINDROME {   foreach(arr[i] )
                             {
                                if( i < (8/2) )
                              { arr[i] == arr[(8-1)-i]; }

                                arr[i] inside {[0:9]};   // For BCD. Can comment it for Hex values
                             }        
                          }
      
    constraint VALUE     {   foreach(arr[i] )
                             {                               
                                val[4*i+:4] == arr[i];
                             }        
                          }                
  endclass  
      
  A a1;
                           
  initial begin
    
    a1 = new();
    
    repeat(6) begin    
      if( a1.randomize() )         
        $display("Success with %0h_%0h_%0h_%0h_%0h_%0h_%0h_%0h",
                 a1.val[31:28],a1.val[27:24],a1.val[23:20],a1.val[19:16],a1.val[15:12],a1.val[11:8],a1.val[7:4],a1.val[3:0]);
      
    end  
    
  end
2 Likes

Thank You, it was helpful.

Dave,
I tried your approach with following changes within the subroutine

class b;
  rand int unsigned val;   
  rand shortint unsigned x,y;
   
   constraint a_c  { reverse_num(x) == y; }   
   constraint  xy  { val[15:0] == x; val[31:16] == y; }

   function bit[15:0] reverse_num(bit[15:0] a);
     reverse_num = { <<4{a} }  ;
   endfunction   
 endclass

  b  a1;
                           
  initial begin   
    a1 = new();   
    repeat(6) begin     
      if( a1.randomize() )          
        $display("Success with %0h_%0h_%0h_%0h_%0h_%0h_%0h_%0h",
           a1.val[31:28],a1.val[27:24],a1.val[23:20],a1.val[19:16],a1.val[15:12],a1.val[11:8],a1.val[7:4],a1.val[3:0]);      
    end  
  end

However when testing with random seeds I observe that one of the tools ( VCS ) throws constraint failure.
I am not clear on the reason behind the failure , would like to hear your thoughts on the same

Works for me. I cannot see why it would fail.

Hi Dave,
I tried using streaming operator directly without calling the function in constraint

class b;       
   rand int unsigned val;    
   constraint  palindrome  { val[31:16] ==  { << 4 { val[15:0] } } ; }                            
endclass

  b a1;                           

  initial begin   
    a1 = new();   
    repeat(6) begin     
      if( a1.randomize() )  
         $display("Success with %0h_%0h_%0h_%0h_%0h_%0h_%0h_%0h",
                 a1.val[31:28],a1.val[27:24],a1.val[23:20],a1.val[19:16],a1.val[15:12],a1.val[11:8],a1.val[7:4],a1.val[3:0]);    
    end  
  end

Unfortunately the code doesn’t compile.
Wouldn’t the return type of { << 4 { val[15:0] } } be a 16-bit packed value ?
Could I add any cast to make the code work ?

Hi,
I have made few changes, now code is working fine.

class b;
  rand int unsigned val;   
  rand shortint unsigned x;
   
  constraint  xy  { val == reverse_num(x); }

  function bit[31:0] reverse_num(bit[15:0] x);   
    reverse_num = {{ <<4{x} },x}  ;
   endfunction   
 endclass

module tb;
  b  a1;
                           
  initial begin   
    a1 = new();   
    repeat(6) begin     
      if( a1.randomize() )          
        $display("Success with %0h_%0h_%0h_%0h_%0h_%0h_%0h_%0h",
           a1.val[31:28],a1.val[27:24],a1.val[23:20],a1.val[19:16],a1.val[15:12],a1.val[11:8],a1.val[7:4],a1.val[3:0]);      
    end  
  end
endmodule

Success with d_2_1_d_d_1_2_d
Success with 6_e_6_5_5_6_e_6
Success with 1_8_0_6_6_0_8_1
Success with 2_a_1_3_3_1_a_2
Success with 4_2_0_6_6_0_2_4
Success with 6_b_8_c_c_8_b_6