前言
普通约束称为硬约束,因为求解器必须始终满足它们。 如果求解器找不到解决方案,则随机化将失败。
但是,声明为软约束可以使求解器具有一定的灵活性,如果存在其他相互矛盾的约束(硬约束或优先级较高的软约束),则需要满足该约束。
软约束用于为随机变量指定默认值和分布。
一、举例
在下面的示例中显示了一个软约束,该约束告诉求解器为名为data的变量生成4到12之间的值。
class ABC;
rand bit [3:0] data;
// This constraint is defined as "soft"
constraint c_data { soft data >= 4;
data <= 12; }
endclass
module tb;
ABC abc;
initial begin
abc = new;
for (int i = 0; i < 5; i++) begin
abc.randomize();
$display ("abc = 0x%0h", abc.data);
end
end
endmodule
Simulation Log
ncsim> run
abc = 0x4
abc