设计一个带控制端的逻辑运算电路,分别完成正整数的平方、立方和最大为5的阶乘的运算

//********************tryfunct.v*********************


module tryfunct(clk,n,result,sl,reset);
output[31:0] result;
input[3:0] n;
input reset,clk;
input[1:0] sl;
reg[31:0] result;

always @(posedge clk)
    begin
        if(!reset) result<=0;
        else begin
                case(sl)
                    2'b00: result <= square(n);
                    2'b01: result <= cubic(n);
                    2'b10: result <= factorial(n);
                    default: result <= 32'bx;
                endcase
            end
    end
function[31:0] square;
input[3:0] operand;
begin
    square = operand * operand;    
end
endfunction

function[31:0] cubic;
input[3:0] operand;
begin
    cubic = operand * operand * operand;    
end
endfunction

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
 

//********************tryfunct_TB.v*********************

`timescale 1ns / 100ps
`define clk_cycle 50

module tryfunct_TB;
reg[3:0] n,i;
reg reset,clk;
reg[1:0] sl;
wire[31:0] result;
initial
    begin
        clk=0;
        n=0;
        sl=0;
        reset=1;
        #100 reset=0;
        #100 reset=1;
        for(i=0;i<=5;i=i+1)
        begin
            #200 n=i;
        end
        #100 $stop;
    end
always # (`clk_cycle*2) sl=sl+1;
always # `clk_cycle clk=~clk;
tryfunct m(clk, n, result, sl, reset);
endmodule
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值