可以使用蕴含运算符 (->) 和 if-else 构造进一步限制随机化。它们都表示两个变量之间的条件关系。隐含运算符用于声明隐含约束的表达式。例如:
constraint dataC { (data == 'hff) -> (addr == 0); }
相当于:
if (data == 'hff) addr = 0;
看一个例子:
class dataP;
rand bit [7:0] data;
string burst_mode ;
constraint burst1 { (burst_mode == "short") -> (data < 16);}
//implication
constraint burstIf { if (burst_mode == "long") data > 16;
else if (burst_mode == "garbage") data == 0;}
endclass
module top;
initial begin
dataP d1;
d1 = new();
d1.burst_mode = "short";
repeat(4) begin
d1.randomize();
$display("\t Short burst_mode = %s data = %0d",d1.burst_mode,d1.data);
end
$display("\n");
d1.burst_mode = "long";
repeat(4) begin
d1.randomize();
$display("\t Long burst_mode = %s data = %0d",d1.burst_mode,d1.data);
end
$display("\n");
d1.burst_mode = "garbage";
repeat(4) begin
d1.randomize();
$display("\t garbage burst_mode = %s data = %0d",d1.burst_mode,d1.data);
end
end
endmodule
仿真结果:
ompiler version S-2021.09; Runtime version S-2021.09; Aug 20 09:46 2022
Short burst_mode = short data = 13
Short burst_mode = short data = 6
Short burst_mode = short data = 1
Short burst_mode = short data = 2
Long burst_mode = long data = 36
Long burst_mode = long data = 109
Long burst_mode = long data = 28
Long burst_mode = long data = 47
garbage burst_mode = garbage data = 0
garbage burst_mode = garbage data = 0
garbage burst_mode = garbage data = 0
garbage burst_mode = garbage data = 0
V C S S i m u l a t i o n R e p o r t
当burst_mode == “short”时,data应该小于16。在模块“top”中,我们为字符串burst_mode分配值“short”,得到的data值均小于16。类似地,在 if-else 条件下,如果 burst_mode == long,我们看到data值大于 16,如果 burst_mode == “garbage”,data值被限制为0。
标签:short,garbage,burst,constraint,Else,mode,Implication,data,d1 From: https://www.cnblogs.com/fuqiangblog/p/16608720.html