Write a constraint to divide values of one queue into three queues so that all 3 queues have unique values

  class example;
    rand bit[5:0] arr[$];
    rand bit[5:0] arr_unique[$];
    rand bit [5:0] arr1[$],arr2[$],arr3[$];
    
    constraint queue_sz{
      arr.size() == 15;
      //arr_unique[$] == arr.unique();
      arr1.size()+arr2.size()+arr3.size() == arr.unique().size();
      15>arr1.size()>0;
      15>arr2.size()>0;
      15>arr3.size()>0;
      
    }
    
    constraint subdivision{
      foreach(arr1[i]){
        arr1[i] == arr.pop_front();
      }
      foreach(arr2[i]){
        arr2[i] == arr.pop_front();
      }
   	  foreach(arr3[i]){
        arr3[i] == arr.pop_front();
      }
    }
    function void print();
      $display("value of array is %p",arr);
      $display("value of array_un is %p",arr1.size());
      $display("value of array1 is %p",arr1);
      $display("value of array2 is %p",arr2);
      $display("value of array3 is %p",arr3);
    endfunction    
    
    
    
  endclass
  
  
  
  
  initial begin
    example ex;
    ex = new();
    repeat(2) begin
    ex.randomize();
    ex.print();
    end
  	$finish; 
  end
endmodule

Output:

# value of array is '{56, 57, 4, 36, 26, 54, 4, 53, 26, 35, 10, 16, 36, 24, 17}
# value of array_un is 6
# value of array1 is '{4, 4, 4, 4, 4, 4}
# value of array2 is '{57, 57, 57, 57}
# value of array3 is '{56, 56}

i tried above code, but not working, can anyone help ?

 class packet extends uvm_object;
     `uvm_object_utils_begin(packet)
     `uvm_object_utils_end

     rand bit [4:0] data[$];
     rand bit [4:0] data1[$];
     rand bit [4:0] data2[$];
     rand bit [4:0] data3[$];

     constraint different_c {
         data.size() inside {[20:30]};
         unique {data}; 
         data1.size() < data.size(); 
         data2.size() < data.size(); 
         data3.size() < data.size(); 
         (data1.size()+data2.size()+data3.size()) == data.size();
         foreach (data1[i]) { data1[i] == data[i]; }
         foreach (data2[j]) { data2[j] == data[j+data1.size()]; }
         foreach (data3[k]) { data3[k] == data[k+data1.size()+data2.size()]; }
     }

     function new(string name="packet");
         super.new(name);
     endfunction

     virtual function void printInfo();
         `uvm_info(get_type_name, "#################################################################################", UVM_LOW)
         `uvm_info(get_type_name, $sformatf("data.size() = %0d, data = %p",data.size(), data), UVM_LOW)
         `uvm_info(get_type_name, $sformatf("data1.size() = %0d, data1 = %p",data1.size(), data1), UVM_LOW)
         `uvm_info(get_type_name, $sformatf("data2.size() = %0d, data2 = %p",data2.size(), data2), UVM_LOW)
         `uvm_info(get_type_name, $sformatf("data3.size() = %0d, data3 = %p",data3.size(), data3), UVM_LOW)
     endfunction

 endclass

A problem with the constraints you wrote.

15>arr1.size()>0; is not what you should be writing to constrain a value in a range. It is actually ( 15 > arr1.size() ) > 0 which only works if minimum in the range is 0. That’s because the result of ( 15 > arr1.size() ) can only be 1’b1 (true) or 1’b0 (false). You should use the inside operator.

arr1.size() inside {[1:14]};
1 Like
  class simple;
    rand int a[$];
    rand int a1[$];
    rand int a2[$];
    rand int a3[$];
    constraint c_values{
      a.size() == 15;
      a1.size() inside {[3:15]};
      a2.size() inside {[3:15]};
      a3.size() inside {[3:15]};
      a1.size()+a2.size()+a3.size() == a.size();       
      foreach(a[i]){
        a[i] inside{[25:50]};
        foreach(a[j]){
          if(i!=j)
            a[i]!=a[j];
        }         
      }
      foreach(a1[i]){
          a1[i] == a[i];
      }
      foreach(a2[j]){
        a2[j] == a[j+a1.size()];   
          
      }
      foreach(a3[k]){
       a3[k] == a[k+a1.size()+a2.size()];   
          
      }    
    }
endclass

Output::

The queue sizes are a= 15 a1= 4 a2= 5 a3=6
a = '{33, 43, 27, 39, 42, 50, 31, 38, 40, 28, 29, 44, 36, 41, 30}
a1 = '{33, 43, 27, 39}
a2 = '{42, 50, 31, 38, 40}
a3 = '{28, 29, 44, 36, 41, 30}

class pattern;
  rand bit[4:0] arr[$];
  rand bit[4:0] arr1[$],arr2[$],arr3[$];
  
    constraint c1{
      arr.size() == 15;
      arr1.size() inside {[5:10]};
      arr2.size() inside {[5:10]};
      arr3.size() inside {[5:10]};
      arr1.size() + arr2.size() + arr3.size() == arr.size();       
    }
      
    constraint c2{
      unique{arr1};
      unique{arr2};
      unique{arr3};
    }
      
    constraint c3{
      foreach(arr1[i]){
        arr1[i] == arr[i];
      }
      foreach(arr2[j]){
        arr2[j] == arr[j+arr1.size()];   
          
      }
      foreach(arr3[k]){
        arr3[k] == arr[k+arr1.size()+arr2.size()];
      }    
    }
endclass