System verilog SV随机化给出意外值
我有以下示例代码System verilog SV随机化给出意外值,system-verilog,System Verilog,我有以下示例代码 class c; rand int a; constraint a_c { soft a == 10; } function void post_randomize(); $display("From c the random value is %0d\n", a); endfunction endclass class b extends c; constraint a_c_1 { a inside {[10:100]};
class c;
rand int a;
constraint a_c {
soft a == 10;
}
function void post_randomize();
$display("From c the random value is %0d\n", a);
endfunction
endclass
class b extends c;
constraint a_c_1 {
a inside {[10:100]};
}
function void post_randomize();
$display("From b the random value is %0d\n", a);
endfunction
endclass
module m;
c c_obj;
b b_obj;
initial begin
c_obj = new();
b_obj = new();
repeat (100) begin
c_obj.randomize() with {a inside {[10:100]};};
b_obj.randomize();
end
end
endmodule
//两种随机化的结果都是10
因此,如果基类的软约束为fixed 10,除非我们禁用约束或使用相同的名称重写,否则我们永远不能放松约束值。我本来希望它放松约束,因为孩子正在放松约束。扩展的
类c
不会覆盖类b
中的约束;它将添加到约束集。由于添加的约束与软约束不冲突,因此软约束保持不变。在删除软约束之前,必须没有可能的解决方案
如果要释放约束,必须覆盖或禁用它。如果尝试执行,例如,
c_obj.randomize()和{a inside{[11:100]};}代码>,c类内部的软约束将被内联约束抑制,并将a
随机化为11到100之间的值。例如,如果您将代码更改为:
constraint a_c_1 {
a inside {[11:100]};
}
在这种情况下,b类中的约束将抑制c类中的软约束,因此b_obj
将a
再次随机化为11到100的值
否则,如果由于不存在冲突,所有这些约束保持不变,则在变量中总是会得到10的值