我需要编写一个约束来随机化,对于 16 位变量,连续 2 位设置为 1,其他位设置为 0(即 16'b0000_0000_0011、16'b0000_1100_0000 等)。
我尝试使用$countones
。它适用于两位,但不适用于连续的 2 位。其显示为:
# Value is 10000000000100
# Value is 10000000010
最佳答案
一种简单的方法是使用限制在 0 到 14 之间的随机索引变量,然后使用另一个约束仅设置数据的 2 位。
module tb;
class c;
rand bit [ 3:0] idx;
rand bit [15:0] data;
constraint c0 {
idx < 15;
data == (3 << idx);
}
endclass
initial begin
c c;
c = new();
repeat (20) begin
c.randomize();
$displayb(c.data);
end
end
endmodule
示例输出:
0000110000000000
0000011000000000
0000110000000000
0110000000000000
0000110000000000
0000000000110000
0000000110000000
0000110000000000
0000110000000000
0000001100000000
0000000000000011
0000001100000000
0000110000000000
0001100000000000
0000110000000000
0000000110000000
0011000000000000
0000000000110000
0000000000110000
0000001100000000
关于constraints - 约束随机化,对于 16 位变量,连续 2 位设置为 1,其他位设置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70538692/