在不使用 $countones 的情况下随机化 UVM 中数组中 1 的数量?

randomizing number of 1's in an array in UVM without using $countones?

在 UVM 中,我想约束一个数组,以便我可以将数组中的个数固定为 3,我使用使用 $countones 的约束编写了以下代码,但是如何在不使用 $ 的情况下执行此操作计数??

class class_1;
rand bit[31:0] array;

constraint three_ones {
$countones(array) == 3;
}
endclass

你写的相当于

constraint three_ones {
int'(array[0]) + int'(array[1]) + int'(array[2]) + ... + int'(array[31]) )
   == 3;
}
endclass

更新

要在没有任何函数的情况下以编程方式执行此操作,您可以创建另一个数组,其中包含应设置为一个的索引列表。数组的大小就是你要设置的个数。

bit [0:255] array;
int countones = 5;
rand int unsigned bitset[]; 
constraint c_bits { bitset.size == countones;
                    foreach(bitset[i]) bitset[i] inside {[0;$bits(array)-1];
                    unique {bitset};
                  }
function void post_randomize();
   array = 0;
   foreach(bitset[i]) array[bitset[i]] = 1'b1;
endfunction

现在有一种方法可以在没有唯一约束且没有 post_randomize 的情况下执行此操作,但这对我来说太麻烦了。