加减乘除计算器(32位)verilog实现

前言

关于相关原理可以百度相关介绍或者参照我之前的文章。
加减法采用纯组合逻辑,没有做busy判定(后续可以加上)。
目前没有做符号位判定。
除法低32位为余数,33-64位为商。

加减乘除计算器(32位)verilog实现

加减法

module add_sub (
    input  [31:0]   Operand_X,
    input  [31:0]   Operand_Y,
    input           Opcode   ,
    output [31:0]   Result   ,
    output          Cout
);
    
reg [31:0] B;

// Opcode is 1 means Operand_Y is negetive number

assign {Cout, Result} = Operand_X + B + Opcode;

always @(Operand_Y or Opcode) begin
    if (Opcode) begin
        B = ~Operand_Y;
    end else begin
        B = Operand_Y;
    end
end

endmodule

32位乘法

module mul  (
    input                   rst         ,
    input                   clk         ,
    input       [ 2:0]      Opcode      ,
    // multiplication Opcode is 110
    // add code is 100
    // sub code is 101
    input       [31:0]      Operand_X   ,
    input       [31:0]      Operand_Y   ,
    output reg              Busy        ,
    output reg              Cout        ,
    output reg  [64:0]      Result
);
// Complement one digit multiplication, the default input is already a complement!
reg     [31:0]      Add_A, Add_B, Add_Result     ;
reg                 Add_Cin                      ;
reg     [ 4:0]      Mul_Counter                  ;
reg     [31:0]      Multiplicand, Operand_Y_inner;
// add 00; sub 01; mul 10;
reg     [ 1:0]      Opcode_inner                 ;
// reg     [64:0]      Product                      ;

// add or sub
always @(Add_A or Add_B or Add_Cin) begin
    {Cout, Add_Result} = Add_A + Add_B + Add_Cin;
end

// Mul_Counter register
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Mul_Counter <= 5'b00000;
    end else if (Opcode == 3'b110) begin
        Mul_Counter <= 5'b11111;
    end else if (Busy) begin
        Mul_Counter <= Mul_Counter - 1;
    end
end

// Busy
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Busy <= 1'b0;
    end else if (Opcode == 3'b110 && ~Busy) begin
        // multiplication Opcode is 110
        // add            Opcode is 100
        // sub            Opcode is 101
        Busy <= 1'b1;
    end else if (Busy == 1'b1 & Mul_Counter == 5'b00000) begin
        // complete Multiplication operation
        Busy <= 1'b0;
    end
end
// Multiplicand register in_Y
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Multiplicand <= 32'b0;
    end else if (Opcode == 3'b110) begin
        // if mul, multiplicand is Operand_Y
        Multiplicand <= Operand_Y;
    end
end

// Result register
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Result <= 65'b0;
    end else if (Opcode == 3'b110) begin
        // initial Result
        Result <= {32'b0, Operand_X, 1'b0};
    end else begin
        // shifter with symbol
        Result <= {Add_Result[31], Add_Result, Result[32:1]};
    end
end

// adder inA is Add_A
always @(Result or Operand_X or Operand_Y) begin
    if (Busy) begin
        Add_A <= Result[64:33];
    end else begin
        Add_A <= Operand_X    ;
    end
end

// Operand_Y_inner
always @(Busy or Multiplicand or Operand_Y) begin
    if (Busy) begin
        // if mul, inner is multiplicand
        Operand_Y_inner <= Multiplicand;
    end else begin
        Operand_Y_inner <= Operand_Y   ;
    end
end

// Add_B
always @(Opcode_inner or Operand_Y_inner) begin
    // add 10; sub 01;
    if (Opcode_inner == 2'b10) begin
        Add_B <= Operand_Y_inner;
    end else if (Opcode_inner == 2'b01) begin
        Add_B <= ~Operand_Y_inner;
    end else begin
        Add_B <= 32'b0;
    end
end

always @(Opcode_inner) begin
    begin
        if (Opcode_inner == 2'b01) begin
            Add_Cin <= 1'b1;
        end else begin
            Add_Cin <= 1'b0;
        end
    end
end

always @(Busy or Result or Opcode) begin
    if (Busy) begin
        if (Result[1:0] == 2'b00 || Result[1:0] == 2'b11) begin
            Opcode_inner <= 2'b00;
        end else if (Result[1:0] == 2'b10) begin
            Opcode_inner <= 2'b01;
        end else if (Result[1:0] == 2'b01) begin
            Opcode_inner <= 2'b10;
        end
    end else begin
        if (Opcode == 3'b100) begin
            Opcode_inner <= 2'b10;
        end else if (Opcode == 3'b101) begin
            Opcode_inner <= 2'b11;
        end else begin
            Opcode_inner <= 2'b00;
        end
    end
end

endmodule

32位除法

module div  (
    input                   rst         ,
    input                   clk         ,
    input       [ 2:0]      Opcode      ,
    // multiplication Opcode is 110
    // add code is 100
    // sub code is 101
    // div code is 110 
    input       [31:0]      Operand_X   ,
    input       [31:0]      Operand_Y   ,
    output reg              Busy        ,
    output reg              Cout        ,
    output reg  [63:0]      Result
);

// Complement one digit div
// the default input is already a complement!!!!
reg     [31:0]      Add_A                        ;
reg     [31:0]      Add_Result = 32'b0           ;
reg     [ 4:0]      Mul_Counter                  ;
reg     [31:0]      Dividend, Operand_Y_inner    ;

// just sub
always @(Add_A) begin  
    {Cout, Add_Result} <= Add_A + ~Operand_Y_inner + 1'b1;
end

// Mul_Counter register
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Mul_Counter <= 5'b00000;
    end else if (Opcode == 3'b111) begin
        Mul_Counter <= 5'b11111;
    end else if (Busy) begin
        Mul_Counter <= Mul_Counter - 1;
    end
end

// Busy
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Busy <= 1'b0;
    end else if (Opcode == 3'b111 && ~Busy) begin
        Busy <= 1'b1;
    end else if (Busy == 1'b1 & Mul_Counter == 5'b00000) begin
        Busy <= 1'b0;
    end
end

// divisor register in_Y
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Dividend <= 32'b0;
    end else if (Opcode == 3'b111) begin
        Dividend <= Operand_Y;
    end
end

// Result register
always @(posedge rst or posedge clk) begin
    if (rst) begin
        Result <= 64'b0;
    end else if (Opcode == 3'b111) begin
        // initial Result
        Result <= {31'b0, Operand_X, 1'b0};
    end else if (~Add_Result[31]) begin
        // if 0, quotient is 1
        Result <= {Add_Result[30:0], Result[31:0], 1'b1};
    end else begin
        // if 1, just shift, quotient is 0
        Result <= {Result[62:0], 1'b0};
    end
end

// adder inA is Add_A
always @(Result or Operand_X or Operand_Y) begin
    if (Busy) begin
        Add_A <= Result[63:32];
    end else begin
        Add_A <= Operand_X    ;
    end
end

// Operand_Y_inner
always @(Busy or Dividend or Operand_Y) begin
    if (Busy) begin
        Operand_Y_inner <= Dividend;
    end else begin
        Operand_Y_inner <= Operand_Y   ;
    end
end

endmodule

顶层模块

module alu (
    input                   rst         ,
    input                   clk         ,
    input       [ 2:0]      Opcode      ,
    // multiplication Opcode is 110
    // add            Opcode is 100
    // sub            Opcode is 101
    // div            Opcode is 110 
    input       [31:0]      Operand_X   ,
    input       [31:0]      Operand_Y   ,
    output reg              Busy        ,
    output reg              Cout        ,
    output reg  [64:0]      Result
);

wire         [31:0]          add_sub_result;
wire         [64:0]          mul_result    ;
wire         [63:0]          div_result    ;

wire                         Busy_mul      ;
wire                         Busy_div      ;
wire                         Cout_mul      ;
wire                         Cout_div      ;
wire                         Cout_add_sub  ;

wire          [2:0]          inst_opcode   ;

// always @(posedge clk or posedge rst) begin
//     if (rst) begin
//         Opcode <= 3'b000;
//     end else if (Busy) begin
//         Opcode <= 3'b000;
//     end
// end

assign inst_opcode = Opcode!=3'b0 ? Opcode : inst_opcode;

add_sub  u_add_sub (
    // .rst                     ( rst               ),
    // .clk                     ( clk               ),
    .Opcode                  ( Opcode     [   0] ),
    .Operand_X               ( Operand_X  [31:0] ),
    .Operand_Y               ( Operand_Y  [31:0] ),

    // .Busy                    ( Busy              ),
    .Cout                    ( Cout_add_sub      ),
    .Result                  ( add_sub_result    )
);

mul  u_mul (
    .rst                     ( rst               ),
    .clk                     ( clk               ),
    .Opcode                  ( Opcode     [ 2:0] ),
    .Operand_X               ( Operand_X  [31:0] ),
    .Operand_Y               ( Operand_Y  [31:0] ),

    .Busy                    ( Busy_mul          ),
    .Cout                    ( Cout_mul          ),
    .Result                  ( mul_result        )
);

div  u_div (
    .rst                     ( rst               ),
    .clk                     ( clk               ),
    .Opcode                  ( Opcode     [ 2:0] ),
    .Operand_X               ( Operand_X  [31:0] ),
    .Operand_Y               ( Operand_Y  [31:0] ),

    .Busy                    ( Busy_div          ),
    .Cout                    ( Cout_div          ),
    .Result                  ( div_result        )
);

always @(*) begin
    if (rst) begin
        Result <= 65'b0;
    end else if (inst_opcode == 3'b100 || inst_opcode == 3'b101) begin
        Result <= {33'b0, add_sub_result};
        Cout   <= Cout_add_sub;
    end else if (inst_opcode == 3'b110) begin
        Result <= mul_result;
        Busy   <= Busy_mul;
        Cout   <= Cout_mul;
    end else if (inst_opcode == 3'b111) begin
        Result <= {1'b0, div_result[31:0], div_result[63:32]};
        Busy   <= Busy_div;
        Cout   <= Cout_div;
    end
end

endmodule

仿真

TestBench

`timescale  1ns / 1ps

module tb_alu();

// alu Parameters
parameter PERIOD  = 20;


// alu Inputs
reg   rst                                  = 0 ;
reg   clk                                  = 0 ;
reg   [ 2:0]  Opcode                       = 0 ;
reg   [31:0]  Operand_X                    = 0 ;
reg   [31:0]  Operand_Y                    = 0 ;

// alu Outputs
wire  Busy                                 ;
wire  Cout                                 ;
wire  [64:0]  Result                       ;


initial begin
    forever #(PERIOD/2)  clk=~clk;
end

initial begin
    rst = 1'b1;
    #25  rst = 1'b0;
    Opcode = 3'b100;
    Operand_X = 32'h0006;
    Operand_Y = 32'h0002;
     
    #20  Opcode = 3'b101;
 
    #20  Opcode = 3'b110;
    #10  Opcode = 3'b000;

    #650 Opcode = 3'b111;
    #10  Opcode = 3'b000;

    #700 $finish        ;

end


alu  u_alu (
    .rst                     ( rst               ),
    .clk                     ( clk               ),
    .Opcode                  ( Opcode     [ 2:0] ),
    .Operand_X               ( Operand_X  [31:0] ),
    .Operand_Y               ( Operand_Y  [31:0] ),

    .Busy                    ( Busy              ),
    .Cout                    ( Cout              ),
    .Result                  ( Result     [64:0] )
);


endmodule

仿真结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 32位无符号除法器是一种用于计算器、数字信号处理器等电子工程中的数字芯片。它可以执行32位数的无符号除法运算,无符号表示运算中不考虑正负号。在Verilog实现32位无符号除法器需要以下步骤: 第一步是输入寄存器,并设置初始值。在该寄存器中输入需要进行除法运算的两个32位无符号整数B和A,其中被除数A为32位,除数B为32位。 第二步是进行除法操作,可以使用“非规范化除法法”或“高精度除法法”等算法。这里我们以“非规范化除法法”为例进行说明。首先将余数寄存器初始化为被除数A的值,然后除以除数B,如果余数寄存器的值小于除数B,则继续左移一位,将除数B左移一位,然后再次减去除数B。一直重复这个过程,直到余数寄存器的值大于等于除数B为止,此时将商寄存器的值左移一位,最后将商寄存器加上1。 第三步是输出商寄存器的结果。商寄存器存储的即为除法运算的结果,是32位无符号整数。 综上所述,我们可以在Verilog实现32位无符号除法器,它能够进行32位无符号整数A/B的除法操作,并输出商寄存器的结果。这种除法器可以用于数字计算器、数字信号处理器等电子工程中的数字芯片中。 ### 回答2: 32位无符号除法器是一种数字电路设计,可以将32位无符号整数除以任意不为零的32位无符号数。在Verilog语言中,可以使用模块化方法实现32位无符号除法器。 首先,需要定义输入输出端口,包括32位除数dividend、32位除数divisor和32位商quotient。接着,使用Verilog代码实现除法运算的算法,例如时间复杂度为O(n)的16位位移除法算法。在该算法中,先将除数左移直至其最高位小于等于被除数的最高位,然后将被除数减去左移后的除数,直到被除数小于除数,商的每一位由减法的次数确定。 最后,需要注意除数不能为零的情况,可以使用if语句判断并将商赋值为全零。此外,在实现中需要注意数据类型的选择,整数需要用无符号类型表示,如"reg [31:0] dividend"。 ### 回答3: 32位无符号除法器是一种能够将两个32位无符号整数相除的硬件电路。相对于软件实现,在硬件中实现除法运算可以获得更高的速度和性能,适用于需要快速且高效处理除法运算的场合。 在verilog语言中实现32位无符号除法器,需要写出divisor、dividend、quotient和remainder四个信号的代码,并通过实例化模块实现调用和传输数据。在计算机底层结构中,实现方式主要是通过作差减法和移位右移的方式将除数和被除数逐渐逼近相等,并通过商和余数的累计得出最终结果。 具体实现过程中,可以使用类似于Booth算法或者类似于龙皮递归算法的递归结构来进行除法计算。此外,在代码实现中还需要注意一些细节问题,如除数为0的特殊情况、被除数小于除数的情况等,需要考虑如何解决或者报错提示。 总的来说,32位无符号除法器是一种实现除法运算的高效且可靠的方法。在硬件实现中可以使用verilog语言进行代码编写,并通过递归算法实现高效的除法运算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值