如果它具有随机值,如何使verilog中的模拟每次都有不同的结果?
How to make a simualtion in verilog have different results everytime if it has random values?
我想每次 运行 生成相同代码的不同输出,因为它具有分配给某些变量的随机值。有没有办法做到这一点,例如在 C 中使用 time
播种?
其中包含随机化的示例代码:
class ABC;
rand bit [4 : 0] arr []; // dynamic array
constraint arr_size{
arr.size() >= 2;
arr.size() <= 6;
}
endclass
module constraint_array_randomization();
ABC test_class;
initial begin
test_class = new();
test_class.randomize();
$display("The array has the value = %p ", test_class.arr);
end
endmodule
我这可能取决于所使用的工具。例如,cadence 的 xcelium 支持 xrun -seed some_seed
(我认为 Questa 有 -sv_seed some_seed
)。我确信所有工具都支持类似的东西。寻找模拟工具 reference/manual/guide/help 它可能支持每次模拟的随机种子 运行。
不确定这是否可以从模拟内部实现。
如 Questa 的评论中所述,-sv_seed random
应该可以解决问题。
通常,在模拟时不受控制的随机播种会产生可重复性问题。换句话说,如果您不知道种子,将很难调试失败的案例。但如果你坚持,那么请阅读以下内容。
您可以模仿 'c' 随时间随机化的方式。但是,verilog 中没有很好的方法来访问系统时间。因此,没有好的方法可以在程序中进行基于时间的播种。
但是,一如既往,有一个解决方法可用。例如,可以使用 $system
调用来获取系统时间(取决于系统)。然后可以使用 srandom
函数来设置种子。以下(基于 linux 的)示例可能适合您(或者您可以针对您的系统进行调整)。
这里的时间由 date +'%s'
命令提供为 unix 时间。它将它写入一个文件,然后使用 $fopen/$fscan.
从中读取为 'int'
module constraint_array_randomization();
ABC test_class;
int today ;
initial begin
// get system time
$system("date +'%s' > date_file"); // write date into a file
fh = $fopen("date_file", "r");
void'($fscanf(fh, "%d", today)); // cast to void to avoid warnings
$fclose(fh);
$system("rm -f date_file"); // remove the file
$display("time = %d", today);
test_class = new();
test_class.srandom(today); // seed it
test_class.randomize();
$display("The array has the value = %p ", test_class.arr);
end
endmodule
我想每次 运行 生成相同代码的不同输出,因为它具有分配给某些变量的随机值。有没有办法做到这一点,例如在 C 中使用 time
播种?
其中包含随机化的示例代码:
class ABC;
rand bit [4 : 0] arr []; // dynamic array
constraint arr_size{
arr.size() >= 2;
arr.size() <= 6;
}
endclass
module constraint_array_randomization();
ABC test_class;
initial begin
test_class = new();
test_class.randomize();
$display("The array has the value = %p ", test_class.arr);
end
endmodule
我这可能取决于所使用的工具。例如,cadence 的 xcelium 支持 xrun -seed some_seed
(我认为 Questa 有 -sv_seed some_seed
)。我确信所有工具都支持类似的东西。寻找模拟工具 reference/manual/guide/help 它可能支持每次模拟的随机种子 运行。
不确定这是否可以从模拟内部实现。
如 Questa 的评论中所述,-sv_seed random
应该可以解决问题。
通常,在模拟时不受控制的随机播种会产生可重复性问题。换句话说,如果您不知道种子,将很难调试失败的案例。但如果你坚持,那么请阅读以下内容。
您可以模仿 'c' 随时间随机化的方式。但是,verilog 中没有很好的方法来访问系统时间。因此,没有好的方法可以在程序中进行基于时间的播种。
但是,一如既往,有一个解决方法可用。例如,可以使用 $system
调用来获取系统时间(取决于系统)。然后可以使用 srandom
函数来设置种子。以下(基于 linux 的)示例可能适合您(或者您可以针对您的系统进行调整)。
这里的时间由 date +'%s'
命令提供为 unix 时间。它将它写入一个文件,然后使用 $fopen/$fscan.
module constraint_array_randomization();
ABC test_class;
int today ;
initial begin
// get system time
$system("date +'%s' > date_file"); // write date into a file
fh = $fopen("date_file", "r");
void'($fscanf(fh, "%d", today)); // cast to void to avoid warnings
$fclose(fh);
$system("rm -f date_file"); // remove the file
$display("time = %d", today);
test_class = new();
test_class.srandom(today); // seed it
test_class.randomize();
$display("The array has the value = %p ", test_class.arr);
end
endmodule