题解 | #流水线乘法器
流水线乘法器
http://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
);
//储存被乘数移位后的值
reg [7:0] mul_a_save0,mul_a_save1,mul_a_save2,mul_a_save3;
reg [7:0] mul_acc; //累加
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
mul_a_save0 <= 0;
else
mul_a_save0 <= mul_b[0] ? mul_a : 0;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
mul_a_save1 <= 0;
else
mul_a_save1 <= mul_b[1] ? mul_a<<1 : 0;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
mul_a_save2 <= 0;
else
mul_a_save2 <= mul_b[2] ? mul_a<<2 : 0;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
mul_a_save3 <= 0;
else
mul_a_save3 <= mul_b[3] ? mul_a<<3 : 0;
end
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
mul_out <= 0;
else
mul_out <= mul_a_save0+mul_a_save1+mul_a_save2+mul_a_save3;
end
endmodule