⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2.txt

📁 一个利用task和电平敏感的always块设计比较后重组信号的组合逻辑的实例。可以看到
💻 TXT
字号:
模块源代码:
module tryfunct(clk,n,result,reset);

  output[31:0] result;
  input[3:0]  n;
  input reset,clk;
  reg[31:0] result;

  always @(posedge clk)    //clk的上沿触发同步运算。
begin
 		 if(!reset)            //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

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

module tryfuctTop;

reg[3:0] n,i;
reg reset,clk;

wire[31:0] 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

    上例中函数factorial(n)实际上就是阶乘运算。必须提醒大家注意的是,在实际的设计中,我们不希望设计中的运算过于复杂,以免在综合后带来不可预测的后果。经常的情况是,我们把复杂的运算分成几个步骤,分别在不同的时钟周期完成。
        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -