總是提示:loop with non-constant loop condition must terminate within 250 iterations.代碼如下:module funtest(clk,rst,n,q);input clk,rst;input [3:0]n;output reg[31:0]q;always @(posedge clk)beginif(rst)q<=0;elsebeginq<=factorial(n);endendfunction [31:0] factorial;input [3:0]pram_n;reg [3:0]i=1;beginfactorial=pram_n?1:0;factorial=1;for(i=2;i<=pram_n;i=i+1)factorial=i*factorial;endendfunctionendmodule
2 回答

肥皂起泡泡
TA貢獻1829條經驗 獲得超6個贊
錯在第18行:reg [3:0]i=1; 應該分著寫:reg[31:0] i;begin之后寫i = 1;
function [31:0] factorial;
input[3:0] pram_n;
reg[3:0] i;//分開寫
begin
i = 1;
factorial=pram_n?1:0;
factorial=1;
for(i=2;i<=pram_n;i=i+1)
factorial=i*factorial;
end
endfunction
原因是:always @(posedge clk)表示時序邏輯,其中引用了function factorial;那么初始定義reg i跟給它賦值的工作。在時序上,只能經兩個clk才能完成,一個clk用來初始化reg,另一個clk用于賦值。
另外,always的rst是組合邏輯,推薦規范寫法:
always @(posedge clk or negedge rst)
begin
if(!rst)
q<=0;
else
begin
q<=factorial(n);
end
end
添加回答
舉報
0/150
提交
取消