这是一个小 tb,带有一些示例代码,我尝试以两种方式定义函数,然后在其定义下面调用该 fn 。
module tryfact;
localparam FUNCTION_IS_STATIC = 1;
generate :mygen
if(FUNCTION_IS_STATIC)
// static fn
function integer factorial (input [31:0] operand);
factorial = 1;
endfunction: factorial
else
// automatic fn
function automatic integer factorial (input [31:0] operand);
factorial = 1;
endfunction: factorial
endgenerate :mygen
// test the function
begin
integer result;
initial begin
result = mygen.factorial(1);
$display("%0d factorial=%0d", 1, result);
end
end
endmodule
如果我命名该块,以便可以使用“mygen.factorial1”调用它,则会产生模拟错误:
generate :mygen
|
xmvlog: *E,UMGENE (testbench.sv,5|11): An 'endgenerate' is expected [12.1.3(IEEE 2001)].
generate :mygen
|
xmvlog: *E,EXPENM (testbench.sv,5|11): expecting the keyword 'endmodule' [12.1(IEEE)].
endmodule
如果我删除块标签,则生成没问题,但现在我没有分层引用。
xmelab: *E,CUVUNF (./testbench.sv,25|23): Hierarchical name component lookup failed for 'factorial' at 'tryfact'.
生成应该如何命名,使用单个名称,以便在调用时可以通过该名称分层引用?
这可以在 Cadence 和 Synopsys 模拟器上为我进行编译:
module tryfact;
localparam FUNCTION_IS_STATIC = 1;
if (FUNCTION_IS_STATIC) begin : mygen
function integer factorial (input [31:0] operand);
factorial = 1;
endfunction: factorial
end else begin : mygen
function automatic integer factorial (input [31:0] operand);
factorial = 1;
endfunction: factorial
end
// test the function
begin
integer result;
initial begin
result = mygen.factorial(1);
$display("%0d factorial=%0d", 1, result);
end
end
endmodule