练习:设计一个带控制端的逻辑运算电路,分别完成正整数的平方、立方和阶乘的运算。//--------------myfunction----------modulemyfunction(clk,n,result,reset,sl);output[6:0]result;input[2:0]n;inputreset,clk;input[1:0]sl;reg[6:0]result;//defineinputandoutputalways@(posedgeclk)beginif(!reset)result=0;elsebegincase(sl)2'd0:result=square(n);2'd1:result=cubic(n);2'd2:result=factorial(n);endcaseendendfunction[6:0]square;input[2:0]operand;beginsquare=operand*operand;endendfunctionfunction[6:0]cubic;input[2:0]operand;begincubic=operand*operand*operand;endendfunctionfunction[6:0]factorial;input[2:0]operand;reg[2:0]index;beginfactorial=1;for(index=2;index=operand;index=index+1)factorial=index*factorial;endendfunctionendmodule//--------------testmyfunc----------`include./myfunction.v`timescale1ns/100ps`defineclk_cycle50moduletestmyfunc;reg[2:0]n;regreset,clk;reg[1:0]sl;wire[6:0]result;parametertimes=20;initialbeginn=0;reset=1;clk=0;sl=0;#100reset=0;#100reset=1;repeat(times)begin#50sl={$random}%3;#50n={$random}%6;end#1000$stop;endalways#`clk_cycleclk=~clk;myfunctionmyfunct(.clk(clk),.n(n),.result(result),.reset(reset),.sl(sl));endmodule