|
此程序有错误 ,所以就不做功能仿真了,只把程序和仿真模块贴出来
module verilog_prj(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 |
|