如何随机化 100 个变量中的 1 个

How to randomize 1 of 100 variables

假设我们有一个 class 和一堆随机变量(大约 100 rand 个变量)。现在,我只想随机化 class 中的一个变量,其余变量应该相同。我该怎么做?

class rand_var extends vmm_data;
    rand bit abc;
    rand bit [3:0] cde;
    rand bit en;
    ...
endclass

现在从其中一项任务中,我只想随机化 abc,而不是 rand_var 的任何其他变量。

task custom_task();
    rand_var rand1;
    rand1 = new config

    if (config.randomize () with {

    }) 
    else $display("randomization failed");

一种方法是调用 randomize 并仅传递您希望随机的变量。请参阅 IEEE Std 1800-2017,第 18.11 节 在线随机变量控制 。这是一个完整的例子:

class rand_var;
    rand bit abc;
    rand bit [3:0] cde;
    rand bit en;
endclass

module tb;

task custom_task();
    rand_var rand1;
    rand1 = new();
    if (!rand1.randomize(abc)) $display("randomization failed");
    $display("%b %d %b", rand1.abc, rand1.cde, rand1.en);
endtask

initial repeat (10) custom_task();

endmodule

输出:

1  0 0
1  0 0
1  0 0
0  0 0
1  0 0
1  0 0
0  0 0
1  0 0
1  0 0
0  0 0

每个任务调用只有 abc 是随机的。


另见

随机化单个变量的最简单方法是使用 std::randomize 而不是 class 的 randomize 方法。

task custom_task();
    rand_var config;
    config = new();

    if (std::randomize(config.abc) with {
    /* constraints on config.abc */
    }) 
    else $display("randomization failed");

您还可以在调用 class randomize 方法时使用所谓的 内联随机化控制 ,如

if (!config.randomize(abc)) $display("randomization failed");

但这样做时必须小心,因为 config 对象中的所有活动 class 约束仍然必须满足,即使变量未被随机化。