搜索
bottom↓
回复: 14

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

[复制链接]

出0入0汤圆

发表于 2012-8-5 16:02:44 | 显示全部楼层 |阅读模式
module tryfunct(clk,reset,n,result);
  input reset,clk;
  input [3:0] n;
  output [31:0] result;
  reg [31:0] result;
  always@(posedge clk)
      begin
                    if(!reset)
                           result<=0;
                         else
                           begin
                                  result<=n*factorial(n)/((n*2)+1);
                                end
                end
                               
  function [31:0] factorial;
     input [3:0] operand;
          reg [3:0] 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

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入22汤圆

发表于 2012-8-5 16:45:34 | 显示全部楼层
module light(clk,reset,n,result);
        input reset,clk;
        input [3:0] n;
        output [31:0] result;
        reg [31:0] result;
        always@(posedge clk)
        begin
                if(!reset)
                        result<=0;
                else
                        result<=n*factorial(n)/((n*2)+1);
        end


        function [31:0] factorial;
                input [3:0] operand;
                reg [3:0] 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


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

出0入0汤圆

发表于 2012-8-5 19:33:28 | 显示全部楼层
C语言的思路吧,即使能够编译成功,形成的实际逻辑也会很臃肿的,楼主换种思路吧..

出0入0汤圆

 楼主| 发表于 2012-8-7 07:19:24 | 显示全部楼层
STM_FPGA 发表于 2012-8-5 19:33
C语言的思路吧,即使能够编译成功,形成的实际逻辑也会很臃肿的,楼主换种思路吧.. ...

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

出0入0汤圆

 楼主| 发表于 2012-8-7 07:23:23 | 显示全部楼层
zxq6 发表于 2012-8-5 16:45
module light(clk,reset,n,result);
        input reset,clk;
        input [3:0] n;

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

出0入22汤圆

发表于 2012-8-7 08:42:05 | 显示全部楼层
shmilyeal 发表于 2012-8-7 07:23
谢谢你啊,我是新手
我还想问一下
for(index=2;index

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

出0入0汤圆

发表于 2012-8-7 10:30:50 | 显示全部楼层
确实是C语言的思路,最好还是采用并行的思想,这才是FPGA的精髓。

出0入22汤圆

发表于 2012-8-9 20:48:49 | 显示全部楼层
dbx12358 发表于 2012-8-7 10:30
确实是C语言的思路,最好还是采用并行的思想,这才是FPGA的精髓。

楼上能不能点拨一下?

出0入0汤圆

发表于 2012-8-9 21:47:05 | 显示全部楼层
由于verilog和C的语法很相近,容易用C的思维去写hdl
建议初学者从VHDL开始,语法不太一样就等于从新开始,理解了后再看verilog就很容易了

出0入0汤圆

发表于 2012-8-11 12:08:17 | 显示全部楼层
zxq6 发表于 2012-8-9 20:48
楼上能不能点拨一下?

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

出0入0汤圆

 楼主| 发表于 2012-8-13 09:52:10 | 显示全部楼层
dbx12358 发表于 2012-8-11 12:08
这样的代码可能也可以综合出电路,但会是工具展开后的电路,并不是一个循环。 ...

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

出0入0汤圆

发表于 2012-8-13 10:31:51 | 显示全部楼层
被FPGA一搞,最近写ARM程序也开始用状态机了。

出0入0汤圆

发表于 2012-8-20 17:57:05 | 显示全部楼层
尽量不要使用for之类的循环,FPGA很难实现的!

出0入0汤圆

发表于 2012-8-21 13:19:28 | 显示全部楼层
function [31:0] factorial;
     input [3:0] operand;
          reg [3:0] index;
          begin
          factorial=operand?1:0;
          for(index=2;index<=operand;index=index+1)
              factorial=index*factorial;
          end
  endfunction
递归函数这样写行吗?

出0入0汤圆

 楼主| 发表于 2012-8-23 08:53:31 | 显示全部楼层
chanly1 发表于 2012-8-21 13:19
function [31:0] factorial;
     input [3:0] operand;
          reg [3:0] index;

不就是我写的那个么?
不行,软件检查语法就没通过
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 09:20

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表