题解 | #流水线乘法器#
流水线乘法器
https://www.nowcoder.com/practice/be97f63817c543fe9260d46d971a7283
`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
);
wire [size*2-1:0] temp [size-1:0];
assign temp[0] = mul_b[0] ? {4'b0,mul_a}:8'b0;
assign temp[1] = mul_b[1] ? {3'b0,mul_a,1'b0}:8'b0;
assign temp[2] = mul_b[2] ? {2'b0,mul_a,2'b0}:8'b0;
assign temp[3] = mul_b[3] ? {1'b0,mul_a,3'b0}:8'b0;
reg [size*2-1:0] sum1 [0:1];
always @(posedge clk or negedge rst_n)begin
if(!rst_n)begin
sum1[0] <= 8'b0;
sum1[1] <= 8'b0;
end
else begin
sum1[0] <= temp[0] + temp[1];
sum1[1] <= temp[2] + temp[3];
end
end
always @(posedge clk or negedge rst_n)begin
if(!rst_n)begin
mul_out <= 'b0;
end
else begin
mul_out <= sum1[0] + sum1[1];
end
end
endmodule

