注意:task和function 都是可以综合的,但是有诸多的要求和限制,所以要谨慎使用
不同点
1function 不能包含时序控制语句,只能在一个时间单位执行,而task就可以包含时序控制语句
2 function 不能调用task,而task 可以调用function
3 function至少要有一个input参数,不能有output 和 inout 类型参数,而task既可以没有参数,也可以有各种类型参数
4 function 返回一个值,而task不返回值
5 function 只能对输入值返回一个结果值,而task 可以支持各种用途
task
// Verilog-1995 Task Declaration
task [automatic] task_name;
port_declaration port_name, port_name, ... ;
port_declaration port_name, port_name, ... ;
local variable declarations;
procedural_statement or statement_group
endtask
// Verilog-2001 Task Declaration (ANSI-C Style)
task [automatic] task_name (
port_declaration port_name, port_name, ... ,
port_declaration port_name, port_name, ... );
local variable declarations;
procedural_statement or statement_group
endtask
// 可以在task内声明各种类型的变量( reg [signed] 、 integer 、 time 、 real 、 realtime )
// 没有使用automatic 的task是静态的, 共享参数和变量
// 使用automatic的task 是可重入的, 动态分配参数和变量
task参数传递
1 如果没有参数,那么就不需要传递参数,空表达式不能作为task参数
2 表达式列表中计算顺序是不定的
3 如果参数是input,那么表达式可以是任意表达式
4 如果是output 或 inout 。那么需满足LHS 的规则
5 所以参数是按值传递,不是按引用传递
// Verilog-2001 task definition with five arguments
task my_task (input a, b, input c, output d, e);
begin
c = fool1;
d = fool2;
e = fool3;
end
endtask
// task enabling statement
my_task (v, w, x, y, z);
对于static task 和static function, 只在仿真开始时分配一次,然后一直使用它,这就导致这些参数和变量一直保持最后一次使用时的值。但是综合工具每次调用的时候都会重新初始化这些参数和变量。这就导致仿真和综合结果不一致。所以应该使用automatic
function的用途时返回一个值,并把它用在表达式中
// Verilog-1995 Function Declaration
function [automatic] [function_range_or_type] function_name;
port_declaration port_name, port_name, ... ;
port_declaration port_name, port_name, ... ;
local variable declarations;
procedural_statement or statement_group
endfunction
// Verilog-2001 Function Declaration (ANSI-C Style)
function [automatic] [function_range_or_type] function_name(
port_declaration port_name, port_name, ... ,
port_declaration port_name, port_name, ... );
local variable declarations;
procedural_statement or statement_group
endfunction
// 可以在task内声明各种类型的变量( reg [signed] 、 integer 、 time 、 real 、 realtime )
// 没有使用automatic 的task是静态的, 共享参数和变量
// 使用automatic的task 是可重入的, 动态分配参数和变量
// function_range_or_type 用于声明返回值的类型,是可选的。
// 至少有一个input参数
// Verilog-2001 syntax
function [7:0] getbyte (input [15:0] address);
begin
getbyte = result_expression;
end
endfunction
//在定义function时,同时在function内部隐含的声明了一个与function相同名字的变量。
getbyte = result_expression;
// 函数调用被当作表达式中的操作数使用
word = control ? {getbyte(msbyte), getbyte(1sbyte)} :0;
function 总是综合出组合逻辑,但是如果在设计组合逻辑时出了错误,创建了表现为Latch的仿真代码,那么就会出现问题、仿真时function表现为Latch的行为,而综合工具却按组合逻辑综合,而且不会报告warning。所以对要综合的组合逻辑使用function时危险的。