首先使用关键字 soft 声明软约束。
硬约束之间如果存在矛盾,那么求解器将失败,仿真也会失败。当没有同时满足所有有效硬约束和定义为软约束的解决方案时,求解器将放弃软约束并找到满足剩余硬约束的解决方案,如果还找不到那就出错。软约束仅表示对一种解决方案的偏好,当软约束与其他硬约束相矛盾时,软约束将被放弃。
看一个约束冲突的例子:
class sft;
rand bit [3:0] data;
constraint data_range { data > 'hf; }
constraint data_range1 { data <= 'ha; } //conflicting constraint
endclass
module soft_constr;
initial begin
sft sf;
sf = new();
repeat(4) begin
sf.randomize();
$display("\data = %0h",sf.data);
end
end
endmodule
在上边的例子中,这两个约束是冲突的。一个是要求data为>'hf,而另一个是限制data为<='ha。在这种情况下,将收到以下运行时错误 (Synopsys – VCS):
仿真结果:
Compiler version S-2021.09; Runtime version S-2021.09; Aug 21 03:22 2022
=======================================================
Solver failed when solving following set of constraints
rand bit[3:0] data; // rand_mode = ON
constraint data_range // (from this) (constraint_mode = ON) (testbench.sv:5)
{
(data > 4'hf);
}
=======================================================
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 13
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
data = 0
V C S S i m u l a t i o n R e p o r t
添加软约束之后:
class sft;
rand bit [3:0] data;
constraint data_range { soft data > 'hf; }
constraint data_range1 { data <= 'ha; } //conflicting constraint
endclass
module soft_constr;
initial begin
sft sf;
sf = new();
repeat(4) begin
sf.randomize();
$display("\data = %0h",sf.data);
end
end
endmodule
仿真结果:
Compiler version S-2021.09; Runtime version S-2021.09; Aug 21 03:32 2022
data = 6
data = 3
data = 0
data = 1
V C S S i m u l a t i o n R e p o r t
再来看一下相同的示例,类约束和内联(inline)约束(with constraint)之间存在冲突。这是软约束最有用的地方。将通用约束声明为软约束,允许内联约束(with 约束)覆盖类约束:
看一个例子:
class sft;
rand bit [3:0] data;
//class constraint converted to 'soft' constraint
constraint data_range { soft data > 'hf; }
endclass
module soft_constr;
initial begin
sft sf;
sf = new( );
repeat(4) begin
//inline constraint - hard
sf.randomize( ) with { data <= 'ha;};
$display("\data = %0h",sf.data);
end
end
endmodule
仿真结果:
Compiler version S-2021.09; Runtime version S-2021.09; Aug 21 03:47 2022
data = 6
data = 3
data = 0
data = 1
V C S S i m u l a t i o n R e p o r t
最后,我们可以禁用软约束:
constraint data_range { disable soft data; }
标签:constraint,soft,2021.09,约束,version,data From: https://www.cnblogs.com/fuqiangblog/p/16610051.html