shmilyeal 发表于 2012-8-5 16:02:44

求助,我写一段verilog程序总是报错,请高手帮忙

module tryfunct(clk,reset,n,result);
input reset,clk;
input n;
output result;
reg result;
always@(posedge clk)
      begin
                  if(!reset)
                           result<=0;
                       else
                           begin
                                  result<=n*factorial(n)/((n*2)+1);
                                end
                end
                               
function factorial;
   input operand;
          reg index;
          begin
          factorial=operand?1:0;
          for(index=2;index<=operand;index=index+1)
              factorial=index*factorial;
          end
endfunction
endmodule


报错是:
Error (10119): Verilog HDL Loop Statement error at tryfunct.v(21): loop with non-constant loop condition must terminate within 250 iterations
Error (10903): Verilog HDL error at tryfunct.v(12): failed to elaborate task or function "factorial"
Error: Can't elaborate top-level user hierarchy

zxq6 发表于 2012-8-5 16:45:34

module light(clk,reset,n,result);
        input reset,clk;
        input n;
        output result;
        reg result;
        always@(posedge clk)
        begin
                if(!reset)
                        result<=0;
                else
                        result<=n*factorial(n)/((n*2)+1);
        end


        function factorial;
                input operand;
                reg index;         
                begin
                        factorial=operand?1:0;
                        for(index=2;index<=7;index=index+1)
                        begin
                                factorial=index*factorial;
                                if(index==operand)
                                        $finish;
                        end
                end
        endfunction
endmodule


代码编译过了,不知道能不能反应实际逻辑。

STM_FPGA 发表于 2012-8-5 19:33:28

C语言的思路吧,即使能够编译成功,形成的实际逻辑也会很臃肿的,楼主换种思路吧.{:smile:}.

shmilyeal 发表于 2012-8-7 07:19:24

STM_FPGA 发表于 2012-8-5 19:33 static/image/common/back.gif
C语言的思路吧,即使能够编译成功,形成的实际逻辑也会很臃肿的,楼主换种思路吧.. ...

恩,我会的,刚刚开始学verilog,这个也是做课后习题时弄出来的问题,谢谢你

shmilyeal 发表于 2012-8-7 07:23:23

zxq6 发表于 2012-8-5 16:45 static/image/common/back.gif
module light(clk,reset,n,result);
        input reset,clk;
        input n;


谢谢你啊,我是新手
我还想问一下
for(index=2;index<=7;index=index+1)这里的index<=7只能写明确的数字么?或者是不能超过一定范围的表达吗?

zxq6 发表于 2012-8-7 08:42:05

shmilyeal 发表于 2012-8-7 07:23 static/image/common/back.gif
谢谢你啊,我是新手
我还想问一下
for(index=2;index

其实我也是新手。只是不知道你所需要的逻辑,只是从你的代码来进行的修改。

dbx12358 发表于 2012-8-7 10:30:50

确实是C语言的思路,最好还是采用并行的思想,这才是FPGA的精髓。

zxq6 发表于 2012-8-9 20:48:49

dbx12358 发表于 2012-8-7 10:30 static/image/common/back.gif
确实是C语言的思路,最好还是采用并行的思想,这才是FPGA的精髓。

楼上能不能点拨一下?

korgo 发表于 2012-8-9 21:47:05

由于verilog和C的语法很相近,容易用C的思维去写hdl
建议初学者从VHDL开始,语法不太一样就等于从新开始,理解了后再看verilog就很容易了

dbx12358 发表于 2012-8-11 12:08:17

zxq6 发表于 2012-8-9 20:48 static/image/common/back.gif
楼上能不能点拨一下?

这样的代码可能也可以综合出电路,但会是工具展开后的电路,并不是一个循环。

shmilyeal 发表于 2012-8-13 09:52:10

dbx12358 发表于 2012-8-11 12:08 static/image/common/back.gif
这样的代码可能也可以综合出电路,但会是工具展开后的电路,并不是一个循环。 ...

工具展开电路??
是用USB_BLASTER么?
是一个课后习题,当时没想那么多,怎么样才算是一个循环呢?进行反馈?还是用状态机呢?

岭上开花 发表于 2012-8-13 10:31:51

被FPGA一搞,最近写ARM程序也开始用状态机了。

honeybear 发表于 2012-8-20 17:57:05

尽量不要使用for之类的循环,FPGA很难实现的!

chanly1 发表于 2012-8-21 13:19:28

function factorial;
   input operand;
          reg index;
          begin
          factorial=operand?1:0;
          for(index=2;index<=operand;index=index+1)
            factorial=index*factorial;
          end
endfunction
递归函数这样写行吗?

shmilyeal 发表于 2012-8-23 08:53:31

chanly1 发表于 2012-8-21 13:19 static/image/common/back.gif
function factorial;
   input operand;
          reg index;


不就是我写的那个么?
不行,软件检查语法就没通过
页: [1]
查看完整版本: 求助,我写一段verilog程序总是报错,请高手帮忙