总是提示: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
提交
取消