当前位置:网站首页>Differences between task parameter types inout and ref
Differences between task parameter types inout and ref
2022-06-28 09:22:00 【Ethan_ WC】
task parameter types inout and ref The difference between
Link to the original text https://stackoverflow.com/q/31017629/13685812
Generally speaking ,task perhaps function The formal parameter of is declared as input when ,input The formal parameter of type just gets a copy Copy ;
When task perhaps function The formal parameter of is declared as output when ,output The formal parameters of the type will be in return Of this parameter Copy copy to the receiving object .
and inout Type parameters , Will get a copy when called Copy , And in return Will be the formal parameter Copy Copy to the receiving object .
==》 The above is all about taking the formal parameters and copying them .
about ref Type parameters , What is passed in is quote , and Not a copy . therefore ,ref Type parameters , have More stringent data type requirements . Because the incoming is quote , When task perhaps function When modifying this parameter , The outside is visible .
Particular attention :
For a need Elapsed time Of task Speaking of ,inout and ref The parameter of , There is a big difference . stay task Operation period ,ref Parameters of type , The outside is always visible , And external modification of the value of the memory space pointed to by this reference ,task It's also visible . And for inout Type parameters ,task Is just a share of what you get Copy , therefore , stay task Operation period , Although the external value corresponding to this parameter changes ,task Is also invisible . Of course ,task When this value is modified in , The outside is also invisible . When task At the end , Will pass a copy of this parameter to the outside , At this time , This value can only be obtained externally .
module ref_inout;
logic [3:0] a,b;
task automatic mytask(inout logic [3:0] arg1, ref logic [3:0] arg2);
#0 $display("%m %t arg1 %h arg2 %h", $time, arg1, arg2);
#5 $display("%m %t arg1 %h arg2 %h", $time, arg1, arg2);
#0 arg1 = 1; arg2 = 2; // task I changed it halfway ,inout Parameters are invisible . however ref Parameters can be seen in real time .
#5 $display("%m %t arg1 %h arg2 %h", $time, arg1, arg2);
endtask
initial begin
#0 mytask(a,b);
end
initial begin
a = 'z; b = 'z;
#2
a = 0; b = 0;
#5 $display("%m %t arg1 %h arg2 %h", $time, a, b);
#5 $display("%m %t arg1 %h arg2 %h", $time, a, b);
end
endmodule
/* Print //====================================================== inout ref # ref_inout.mytask 0 arg1 z arg2 z # ref_inout.mytask 5 arg1 z arg2 0 # ref_inout 7 arg1 0 arg2 2 --> 7ns External printing cannot be seen inout The parameters vary , But you can see it in real time ref The parameters passed in change . # ref_inout.mytask 10 arg1 1 arg2 2 --> inout Only when task It's over , Then we can see the changes . # ref_inout 12 arg1 1 arg2 2 //====================================================== */
The above example clearly shows inout and ref The difference between :
1, stay 0ns, Start two initial block ; initialization a b by z;
2, stay 0ns, call my_task;line4 Print arg1 =z arg2 =z;
3, stay 1ns, Yes a b Assign the values to 0;
4, stay 5ns, Print arg1 =z arg2 =0;(b The value of is now 0, Accurately speaking , stay 2ns when ,arg2 Already visible b Value 0)
5, stay 6ns, Print a = 0,b=1;(arg2 by 1, Visible to the outside , so B by 1. Accurately speaking , stay 6ns when , send b Value obtained 1)
6, stay 10ns, Print arg1 =1 arg2 =1;(task sign out ,arg1 A copy of was passed to a)
7, stay 12ns, Print a = 1,b=1;(arg1 by 1, Pass to a)
==》 You can see , Even if task Parameters of I changed it halfway ,inout Parameters can only see the values before and after entering . however ref Parameters can be seen in real time . When task When it takes time , The difference is especially obvious .
//=================================
There's another one that needs attention :
One thing that needs special attention is , If the formal parameter is a class Type of , So what comes in at this time is Handle . in other words ,task This is right class When making modifications , In fact, you are modifying the object that this handle points to , Then the outside is also visible , This may not be what you expect , It's easy with ref confusion ,( I clearly did not state ref type , Why did I change it task Inside , But it will still affect the outside ?), Special care should be taken . At the same time, it needs to be explained , If you want to task perhaps function in , Antitype Handle to modify ( Such as new A new memory space ), that , This parameter should be declared as ref.
class reg_trans;
xxxx;
endclass
task reg_write(input reg_trans t); // What's coming in here is input, But below t.data = xxx; It can be spread out !!! ==> Because the formal parameter passed in is a handle !!! Be careful , Is handle , You can modify the value in the object directly .
@(posedge intf.clk iff intf.rstn);
case(t.cmd)
`READ: begin
intf.drv_ck.cmd_addr <= t.addr;
intf.drv_ck.cmd <= t.cmd;
repeat(2) @(negedge intf.clk); // The first falling edge is in the current shot , The first 2 The first falling edge is in the middle of the next data
t.data = intf.cmd_data_s2m; // This is a direct change t The value in the object that the handle points to .
end
`IDLE: begin
this.reg_idle();
end
default: $error("command %b is illegal", t.cmd);
endcase
$display("%0t reg driver [%s] sent addr %2x, cmd %2b, data %8x", $time, name, t.addr, t.cmd, t.data);
endtask
//=================================
Last , If you need to pass a particularly large parameter to task perhaps function, Use ref Type is a good choice , Can save memory resources .
Why? ?
Because only ref It's direct reference , Others are used after copying .
边栏推荐
- 1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定
- DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation
- 华泰证券网上开户安全吗 办理流程是什么
- 理解IO模型
- 分而治之之经典Hanoi
- The private attribute of this class can be used directly? New() in use!!!
- Application of current limiting protector in preventing electrical fire in shopping malls
- RMAN backup message ora-19809 ora-19804
- 股票 停牌
- Container adapter - stack: stack queue: queue priority_ Queue: priority queue
猜你喜欢
随机推荐
Is it safe to open an account for mobile phone stock speculation?
Using transform:scale causes the page mouse hover event to disappear
Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes
虛擬機14安裝win7(圖教程)
虚拟机14安装win7(图教程)
1182: group photo effect
买卖股票费用计算
Why does select * lead to low query efficiency?
Discussion on the improvement and application of the prepayment system in the management of electricity charge and price
1182:合影效果
在本类私有属性直接使用?new()在使用!!!
Import and export of a single collection in postman
Common test method used by testers --- orthogonal method
Zhejiang energy online monitoring and management system
DEJA_VU3D - Cesium功能集 之 052-模拟卫星轨道(高空)效果
我想网上注册股票开户,如何操作?在线开户安全么?
SQL 优化经历:从 30248秒到 0.001秒的经历
PMP needs to master its own learning methods
Is it safe to open an account for online stock speculation?
Prototype chain JS