eric2013 发表于 2013-1-30 13:49:34

Verilog设计练习进阶(6)------- 在Verilog HDL 中使用函数

此程序有错误 ,所以就不做功能仿真了,只把程序和仿真模块贴出来
module verilog_prj(clk,n,result,reset);
output result;
inputn;
input reset,clk;
reg result;
always @(posedge clk)    //clk的上沿触发同步运算。
    begin
      if(!reset)            //reset为低时复位。
         result<=0;
      else
         begin
            result <= n * factorial(n)/((n*2)+1);
         end
   end
function factorial;      //函数定义。
    inputoperand;
    reg    index;
    begin
      factorial = operand ? 1 : 0;
      for(index = 2; index <= operand; index = index + 1)
      factorial = index * factorial;
    end
endfunction
endmodule
测试模块
`include "./step6.v"
`timescale 1ns/100ps
`define clk_cycle 50

module tryfuctTop;

reg n,i;
reg reset,clk;

wire result;

initial
begin
    n=0;
    reset=1;
    clk=0;
    #100 reset=0;
    #100 reset=1;
    for(i=0;i<=15;i=i+1)
      begin
      #200 n=i;
      end
    #100 $stop;
end
always #`clk_cycle clk=~clk;
tryfunct tryfunct(.clk(clk),.n(n),.result(result),.reset(reset) );
endmodule

eric2013 发表于 2013-1-30 14:17:50

特别注意
      上例中函数factorial(n)实际上就是阶乘运算。必须提醒大家注意的是,在实际的设计
中,我们不希望设计中的运算过于复杂,以免在综合后带来不可预测的后果。经常的情况是,
我们把复杂的运算分成几个步骤,分别在不同的时钟周期完成。
页: [1]
查看完整版本: Verilog设计练习进阶(6)------- 在Verilog HDL 中使用函数