题解主体
4bit流水线乘法器的设计采用乘法竖式运算的思想,本质是将乘法运算转换为加法运算。具体实现思路如下图:
最后的temp0、temp1、temp2、temp3的相加结果就是相乘结果。
Verilog代码描述如下:
reg [7:0] addr01; reg [7:0] addr23; wire [7:0] temp0 ; wire [7:0] temp1 ; wire [7:0] temp2 ; wire [7:0] temp3 ; assign temp0 = mul_b[0]? {4'b0, mul_a} : 'd0; assign temp1 = mul_b[1]? {3'b0, mul_a, 1'b0} : 'd0; assign temp2 = mul_b[2]? {2'b0, mul_a, 2'b0} : 'd0; assign temp3 = mul_b[3]? {1'b0, mul_a, 3'b0} : 'd0; always @(posedge clk or negedge rst_n) begin if(~rst_n) begin addr01 <= 'd0; addr23 <= 'd0; mul_out <= 'd0; end else begin addr01 <= temp0 + temp1; addr23 <= temp2 + temp3; mul_out <= addr01 + addr23; end end
参考答案
`timescale 1ns/1ns module multi_pipe#( parameter size = 4 )( input clk , input rst_n , input [size-1:0] mul_a , input [size-1:0] mul_b , output reg [size*2-1:0] mul_out ); /********************************************************************/ reg [7:0] addr01; reg [7:0] addr23; wire [7:0] temp0 ; wire [7:0] temp1 ; wire [7:0] temp2 ; wire [7:0] temp3 ; assign temp0 = mul_b[0]? {4'b0, mul_a} : 'd0; assign temp1 = mul_b[1]? {3'b0, mul_a, 1'b0} : 'd0; assign temp2 = mul_b[2]? {2'b0, mul_a, 2'b0} : 'd0; assign temp3 = mul_b[3]? {1'b0, mul_a, 3'b0} : 'd0; always @(posedge clk or negedge rst_n) begin if(~rst_n) begin addr01 <= 'd0; addr23 <= 'd0; mul_out <= 'd0; end else begin addr01 <= temp0 + temp1; addr23 <= temp2 + temp3; mul_out <= addr01 + addr23; end end endmodule