task:
1)task无法通过return返回结果,因此只能通过output、inout或者ref的参数来返回。
2)task内可以置入耗时语句,而function不能,常见的耗时语句有:@event、wait event、#delay等。
task mytask1 (output logic [31:0] x, input logic y);
...
endtast
ps: 1)在非耗时方法定义时使用function,
2)在内置耗时语句时使用task
3)function和task均可调用function,task只能用task调用。(因为task可能内置耗时语句)
习题1
typedef struct{
bit [1:0] cmd;
bit [7:0] addr;
bit [31:0] data;
} trans;
function automatic void op_copy(trans t , trans s);//trans t , trans s默认都是输入
t = s;
endfunction
initial begin
trans s;
trans t;
s.cmd = 'h1;
s.addr = 'h10;
s.data = 'h100;
op_copy(t,s);
t.cmd = 'h2;
end
习题1中,变量t中的三个成员 cmd,addr,data最后数值为多少?
A 'h1,'h10,'h100 B 'h2,'h0,'h0 C'h2,'h10,'h100 D 'h0,'h0,'h0
答案:B
解析:因为trans t , trans s都是输入,虽然在function中赋值了,但退出function时,变量t仍为0,然后通过t.cmd = 'h2,给变量t的cmd赋值为'h2,addr、data没有赋值,所以最后为 'h2,'h0,'h0。
若前面定义变量t为输出,则在function中会被赋值为'h1,'h10,'h100,最后变成'h2,'h10,'h100。