SystemVerilog 中一组变量的循环随机化
cyclic randomization for a group of variables in SystemVerilog
我试图在系统 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}
}
有了上面的约束,所有的3个变量(2x6x3)总共有36种组合。
但是如果我们 运行 一个 36 的循环,就像这样:
repeat(36) begin
this.randomize(a,b,c);
$display("%d %d %d", a,b,c);
end
我们不会命中所有可能的组合,因为某些组合可能会重复。因此,我正在寻找一种方法,通过 运行 正好循环 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 'a' to one of the 2 values.
if(k<9)
a = 1;
else
a = 2;
// randomizing variable 'b' to one of the 6 values.
case(k)
1,2,3,19,20,21 : b = 1;
4,5,6,22,23,24 : b = 2;
7,8,9,25,26,27 : b = 3;
//
// finishing the sequence
//
endcase
case(k)
// similar case statement for the final variable
endcase
$display("%d, %d, %d", a,b,c);
end
上面的方法很好用,但对我来说这似乎有点麻烦(也不能应用于大型组合),因此想知道是否有更优雅的方法来实现这一点。
谢谢你的帮助。
你可以做的是将你的变量连接成一个压缩结构,并使它成为一个 randc 变量。
module top;
class A;
typedef struct packed {
bit [1:0] a;
bit [2:0] b;
bit [1:0] c;
} abc_t;
randc abc_t k;
constraint c_a{
k.a inside {1,2};
}
constraint c_b{
k.b inside {1,2,3,4,5,6};
}
constraint c_c{
k.c inside {1,2,3};
}
endclass
A h = new;
initial
repeat(40) begin
h.randomize();
$display("%0p",h.k);
end
endmodule
请注意,randc
变量允许的总位数可能会受到模拟器的限制
我试图在系统 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}
}
有了上面的约束,所有的3个变量(2x6x3)总共有36种组合。
但是如果我们 运行 一个 36 的循环,就像这样:
repeat(36) begin
this.randomize(a,b,c);
$display("%d %d %d", a,b,c);
end
我们不会命中所有可能的组合,因为某些组合可能会重复。因此,我正在寻找一种方法,通过 运行 正好循环 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 'a' to one of the 2 values.
if(k<9)
a = 1;
else
a = 2;
// randomizing variable 'b' to one of the 6 values.
case(k)
1,2,3,19,20,21 : b = 1;
4,5,6,22,23,24 : b = 2;
7,8,9,25,26,27 : b = 3;
//
// finishing the sequence
//
endcase
case(k)
// similar case statement for the final variable
endcase
$display("%d, %d, %d", a,b,c);
end
上面的方法很好用,但对我来说这似乎有点麻烦(也不能应用于大型组合),因此想知道是否有更优雅的方法来实现这一点。
谢谢你的帮助。
你可以做的是将你的变量连接成一个压缩结构,并使它成为一个 randc 变量。
module top;
class A;
typedef struct packed {
bit [1:0] a;
bit [2:0] b;
bit [1:0] c;
} abc_t;
randc abc_t k;
constraint c_a{
k.a inside {1,2};
}
constraint c_b{
k.b inside {1,2,3,4,5,6};
}
constraint c_c{
k.c inside {1,2,3};
}
endclass
A h = new;
initial
repeat(40) begin
h.randomize();
$display("%0p",h.k);
end
endmodule
请注意,randc
变量允许的总位数可能会受到模拟器的限制