练习六:在Verilog中使用函数

一、实验目的

  1. 了解函数的定义和在模块设计中的使用;
  2. 了解函数的可综合性问题;
  3. 了解许多综合器不能综合复杂的算术运算。

 

二、实验原理

与一般的程序设计语言一样,Veirlog HDL也可使用函数以适应对不同变量采取同一运算的操作。Veirlog HDL函数在综合时被理解成具有独立运算功能的电路,每调用一次函数相当于改变这部分电路的输人以得到相应的计算结果。

       下例是函数调用的一个简单示范。它采用同步时钟触发运算的执行,每个clk时钟周期都会执行一次运算,并且在测试模块中,通过调用系统任务$display及在时钟的下降沿显示每次计算的结果。

模块源代码:
//-------------- tryfunct.v --------------
module tryfunct(clk,n,result,reset);
  output    [31:0]    result;
  input     [3:0]     n;
  input               reset,clk;
  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



测试模块源代码:
//-------------- tryfunctTop.v  --------------
`include  "./tryfunct.v"
`timescale  1ns/100ps
`define clk_cycle 50

module  tryfunctTop;
  reg   [3:0]   n,i;
  reg           reset,clk;
  
  wire  [31:0]  result;
  
  initial
  begin
    clk=0;
    n=0;
    reset=1;
    #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 m(.clk(clk),.n(n),.result(result),.reset(reset));
  
endmodule

 

三、实验要求及实验内容

(1)实验内容:

设计一个带控制端的逻辑运算电路,分别完成正整数的平方、立方和最大数为5的阶乘的运算,要求可综合。编写测试模块,并给出各种层次的仿真波形,比较它们的不同。

(2)实验代码:

模块源代码:
//-------------- logicoperation.v --------------
module logicoperation(clk,n,result,reset,sl);
  output  [6:0]   result;
  input   [2:0]   n;
  input           reset,clk;
  input   [1:0]   sl;
  reg     [6:0]   result;

  always@(posedge clk)
  begin
    if(!reset)
      result<=0;
    else
      begin
        case(sl)
          2'd0: result<=square(n);
          2'd1: result<=cubic(n);
          2'd2: result<=factorial(n);
        endcase
      end
  end

  function  [6:0] square;
    input [2:0] operand;
    begin
      square=operand*operand;
    end
  endfunction

  function  [6:0] cubic;
    input [2:0] operand;
    begin
      cubic=operand*operand*operand;
    end
  endfunction

  function  [6:0] factorial;
    input [2:0] operand;
    reg   [2:0] index;
    begin
      factorial = 1;
      for(index = 2; index <= operand; index =index + 1)
        factorial = index * factorial;
    end
  endfunction

endmodule



测试模块源代码:
//-------------- logicoperationTop.v  --------------
`include    "./logicoperation.v"
`timescale  1ns/100ps
`define     clk_cycle 50

module logicoperationTop;
  reg   [2:0] n;
  reg         reset,clk;
  reg   [1:0] sl;
  wire  [6:0] result;

  parameter times=20;

  initial
  begin
    n=0;
    reset=1;
    clk=0;
    sl=0;
    #100 reset=0;
    #100 reset=1;
    repeat(times)
    begin
      #50 sl={$random}%3;
      #50 n={$random}%6;
    end
    #1000 $stop;
  end

  always  #`clk_cycle  clk=~clk;

  logicoperation m(.clk(clk),.n(n),.result(result),.reset(reset),.sl(sl));

endmodule

 

四、实验结果

 

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星羽空间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值