计组第二次实验 乘法器
乘法器模块设计 输入 input clk, input mult_begin, input [31 :0 ] mult_op1, input [31 :0 ] mult_op2,
输出 output [63 :0 ] product, output mult_end
设计思路
输入两个32位数mult_op1作为被乘数,mult_op2作为乘数 两个数都认为是有符号数所以需要取出符号进行处理 mult_begin作为开始信号,还需设置一个乘法进行中的标志信号,开始信号刚变成1时,对初始数据进行处理将进行信号设为1 当进行信号为1时每次上升沿到来就进行一次相加,结束后被乘数左移,乘数右移
reg mult_valid; assign mult_end = ~(|multiplier); always @(posedge clk) begin if (!mult_begin) begin mult_valid <= 1'b0 ; end else begin mult_valid <= 1'b1 ; end end wire op1_sign; wire op2_sign; wire [31 :0 ] op1_absolute; wire [31 :0 ] op2_absolute; assign op1_sign = mult_op1[31 ]; assign op2_sign = mult_op2[31 ]; assign op1_absolute = op1_sign ? (~mult_op1+1 ) : mult_op1; assign op2_absolute = op2_sign ? (~mult_op2+1 ) : mult_op2; reg [63 :0 ] multiplicand; always @ (posedge clk) begin if (mult_valid) begin multiplicand <= {multiplicand[62 :0 ],1'b0 }; end else if (mult_begin) begin multiplicand <= {32'd0 ,op1_absolute}; end end reg [31 :0 ] multiplier; always @ (posedge clk) begin if (mult_valid) begin multiplier <= {1'b0 ,multiplier[31 :1 ]}; end else if (mult_begin) begin multiplier <= op2_absolute; end end wire [63 :0 ] partial_product; assign partial_product = multiplier[0 ] ? multiplicand:64'd0 ; reg [63 :0 ] product_temp; always @ (posedge clk) begin if (mult_valid) begin product_temp <= product_temp + partial_product; end else if (mult_begin) begin product_temp <= 64'd0 ; end end reg product_sign; always @ (posedge clk) begin if (mult_valid) begin product_sign <= op1_sign ^ op2_sign; end end assign product = product_sign ? (~product_temp+1 ) : product_temp;