+-

这是交易类的代码。
class transaction;
//declaring the transaction items
randc bit [3:0] a;
randc bit [3:0] b;
bit [6:0] c;
function void display(string name);
$display("-------------------------");
$display("- %s ",name);
$display("-------------------------");
$display("- a = %0d, b = %0d",a,b);
$display("- c = %0d",c);
$display("-------------------------");
endfunction
endclass
而这是生成器类的代码。
class generator;
rand transaction trans;
int repeat_count;
mailbox gen2driv;
event ended;
function new(mailbox gen2driv);
this.gen2driv = gen2driv;
endfunction
task main();
repeat(repeat_count) begin
trans = new();
if( !trans.randomize() ) $fatal("Gen:: trans randomization failed");
trans.display("[ Generator ]");
gen2driv.put(trans);
end
-> ended;
endtask
endclass
价值 repeat_count 我传递的是10,这是输出。
- a = 2, b = 0
- c = 0
- a = 1, b = 9
- c = 0
- a = 9, b = 9
- c = 0
- a = 7, b = 15
- c = 0
- a = 10, b = 15
- c = 0
- a = 3, b = 1
- c = 0
- a = 13, b = 12
- c = 0
- a = 1, b = 9
- c = 0
- a = 7, b = 5
- c = 0
- a = 3, b = 15
- c = 0
但是,随机化过程中的值并没有出现循环重复的情况 在变量的所有可能的值出现之前,它就在重复着 a 和 b.
1
投票
投票
将事务构造函数从 repeat 循环。 改为:
repeat(repeat_count) begin
trans = new();
改为:
trans = new();
repeat(repeat_count) begin
看来... new 每次调用时都会重置范围值的初始随机排列。
这里有一个独立的、可运行的例子,它演示了这个问题(和修复方法)。
class transaction;
randc bit [3:0] a; // 16 values: 0-15
function void display;
$display("a=%0d", a);
endfunction
endclass
class generator;
rand transaction trans;
task main;
//trans = new(); // Un-comment this line for fix
repeat (16) begin
trans = new(); // Comment out this line for fix
if (!trans.randomize()) $fatal(1, "trans randomization failed");
trans.display();
end
endtask
endclass
module tb;
generator gen = new();
initial gen.main();
endmodule
1
投票
投票
是的,正如@toolic所说... 在重复循环中使用new操作符初始化,会在每次循环运行时创建一个新的空间,因此新创建的对象(在你的例子中是事务)没有之前被执行的值的痕迹。因此会给出一个随机数,使得randc没有任何用处。虽然它与'rand'关键字正常工作。