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