在systemverilog的书上,在程序中修改对象的代码 求解
trans tr;
tr=new();
repeat(n)
begin
tr.addr=$random();
$display("tr.addr=%h",tr.addr);
transmit(tr);
end
endtask
书上的解释说打印的addr是不一样的,但是transmit发送出去的addr都是一样的,怎么理解?
作者没有表达清楚,应该是这个意思:tr里面永远是最后一个随机出来的地址。
为啥呢
?
句柄永远指向对象本身,现在翻译的书,不要太在意思文字了,往往会被误导。
这是原版的书上写的
What are the symptoms of this mistake? The code above creates only one
Transaction, so every time through the loop, generator_bad changes the object
at the same time it is being transmitted. When you run this, the $display shows
many addr values, but all transmitted Transaction objects have the same value
of addr.
试了一下,把new放在循环的外面,发出去的也不会是同一个包啊
这是原版的
What are the symptoms of this mistake? The code above creates only one
Transaction, so every time through the loop, generator_bad changes the object
at the same time it is being transmitted. When you run this, the $display shows
many addr values, but all transmitted Transaction objects have the same value
of addr.
原版
What are the symptoms of this mistake? The code above creates only one
Transaction, so every time through the loop, generator_bad changes the object
at the same time it is being transmitted. When you run this, the $display shows
many addr values, but all transmitted Transaction objects have the same value
of addr.
直接一点,你可以做实验去验证。如果transmit(tr)这个里面就实现了接口上的driver的话,那么每一次都是不是一样的。如果transmit(tr)只是传输一个句柄到另一个队列里面,那么最后每一次driver出来的信号都是一样的。