Queue constraints

Hi,

I am trying to write a constraint where if i have 6 numbers lets say in top_q = 10,11,12,13,14,15 then bot_q should not have the same element in the same index. Also same numbers cannot be repeated again. i tried with xor of (item^item==1), but that dint help. can somebody tell mewhat is wrong?

Examples:
top_q = 10,11,12,13,14,15
bot_q = 15,14,13,12,11,10

Below case is not allowed:

top_q = 10,11,12,13,14,15
bot_q = 15,11,12,14,13,10



class A;
  
  rand int unsigned top_q[$];  
  rand int unsigned bot_q[$];
  
  constraint top_q_c_size {top_q.size == 6; bot_q.size ==6;}
  
  constraint top_q_c_value {foreach (top_q[i]) 
                           
    top_q[i] inside {[10:15]};                   
                           }
  
  constraint bot_q_c_value {foreach (bot_q[i])
  
    bot_q[i] inside {[10:15]};}
                           
  function new();
    bot_q = top_q.find(item) with ((item^item)==1);
  endfunction
  
  
endclass

module tb;
  A a1;
  
  initial begin
    a1 = new();
    
    if (!a1.randomize) $display ("Error");
    
    $display ("%p",a1.top_q);
    $display ("%p",a1.bot_q);
    
  end
  
  
endmodule

Solution for my code :
'{'hb, 'he, 'ha, 'ha, 'hf, 'ha} 
'{'hd, 'hd, 'ha, 'ha, 'ha, 'ha} 


In reply to rag123:



class A;
 
  rand int unsigned top_q[$];  
  rand int unsigned bot_q[$];
 
  constraint top_q_c_size {top_q.size == 6; bot_q.size ==6;}
 
  constraint top_q_c_value {foreach (top_q[i]) 
                              top_q[i] inside {[10:15]};
                            foreach (top_q[i])
                              foreach (top_q[j])
                                if(i!=j)
                                  top_q[i]!= top_q[j];}
                           
 
  constraint bot_q_c_value {foreach (bot_q[i])
                              bot_q[i] inside {top_q};
                            foreach (bot_q[i])
                              foreach (bot_q[j])
                                if(i!=j)
                                  bot_q[i]!= bot_q[j];          }

  
  constraint valid_value {foreach (top_q[i]) 
                              top_q[i]!= bot_q[i];}   
 
endclass
 
module tb;
  A a1;
 
  initial begin
    a1 = new();
 
    if (!a1.randomize) $display ("Error");
 
    $display ("%p",a1.top_q);
    $display ("%p",a1.bot_q);
 
  end
endmodule


In reply to rag123:

Hope below code helps:

class queue;
   rand int unsigned top_q[$];  
   rand int unsigned bot_q[$];

   constraint top_q_C {
       top_q.size() == 6;
       foreach(top_q[i])
           top_q[i] inside {[10:15]};
       unique{top_q};
   }
  constraint bot_q_C {
      bot_q.size() == top_q.size();
      foreach(bot_q[i]) {
	bot_q[i] inside {top_q};
	bot_q[i] != top_q[i];
      }
      unique{bot_q};
  }


endclass


module main;
   initial begin
     queue i_q;
     i_q = new();
     i_q.randomize();

     $display("top_q = %p",i_q.top_q);
     $display("bot_q = %p",i_q.bot_q);
   end
endmodule

Thanks Desam and Shanti ! Any leads to do this with array operators? instead of foreach?

In reply to rag123:

foreach(top_q[i]) {
  top_q[i] inside {[10:15]};
  bot_q[i] != top_q[i];}

could be replaced with

top_q.and() with (item inside {[10:15]} && item != bot_q[item.index]);

But which do you think is easer to understand?

class packet;
  bit[7:0] topQ[6];
  rand bit[7:0] btmQ[6];
  //Helper array to randomize the idx values. 
  rand bit[2:0] idx[6];
  
  //bottomQ needs to have the same values but at different indices
  constraint c1 {
    unique {idx};
    foreach(idx[i]) {
      idx[i] inside {[0:5]};
      idx[i] != i; //Main constraint that ensures the indices in top & btm are not the same.
    }
    foreach(btmQ[i]) {
      btmQ[i] == topQ[idx[i]];
    }
  }
  
  
  function new();
    topQ = {1,2,3,4,5,6};
  endfunction
   
endclass
            
module top;
  initial begin 
    packet p = new();
    $display("%p",p.topQ);
    repeat(5) begin 
      p.randomize();
      $display("%p",p.btmQ);
    end 
  end 
endmodule