我试图在系统 verilog 中随机化 3 个不同的变量,但以循环方式。我的意思是,我有以下 3 个变量
rand int a;
rand int b;
rand int c;
constraint c_a{
a inside {1,2};
}
constraint c_b{
b inside {1,2,3,4,5,6};
}
constraint c_c{
c inside {1,2,3}
}
Run Code Online (Sandbox Code Playgroud)
有了上述约束,所有 3 个变量 (2x6x3) 总共有 36 种组合。
但如果我们运行 36 次循环,如下所示:
repeat(36) begin
this.randomize(a,b,c);
$display("%d %d %d", a,b,c);
end
Run Code Online (Sandbox Code Playgroud)
我们不会击中所有可能的组合,因为某些组合可能会重复。因此,我希望找到一种方法,通过精确运行循环 36 次来实现所有这些组合。
我编写了一种强力方法来实现此目的,方法是声明另一个 rand 变量来表示每个组合并在其上使用 randc,如下所示:
int a;
int b;
int c;
randc int k;
constraint c_k{
k inside {[1:36]};
}
repeat(36) begin
this.randomize(k);
// randomizing variable …
Run Code Online (Sandbox Code Playgroud) 我遇到了一个表现奇怪的约束(如下图所示):
class A;
rand bit[3:0] a,b,c,d;
constraint c_abcd{
2<a<b<c<d<20;
}
endclass : A
Run Code Online (Sandbox Code Playgroud)
我知道这不是指定该约束的正确方法,我们需要将其拆分为多个约束.但我预计它会给出一个错误.相反,它运行没有任何错误,我可以看到生成a,b,c,d的不同值.
这些数字最初似乎是随机的,但我想系统verilog求解器以可预测的方式表现出来解决这些约束.
那么你能解释一下这是如何解决的吗?当我使用上述约束随机化时,我得到以下输出:
a = 1101;
b = 0101;
c = 0111;
d = 1100;
Run Code Online (Sandbox Code Playgroud)
谢谢