`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// 4位流水线乘法器,二叉树结构,需要log2(N)级实现
//////////////////////////////////////////////////////////////////////////////////
module mul_addtree(mul_a,mul_b,mul_out,clk,rst_n);
parameter MUL_WIDTH=4;
parameter MUL_RESULT=8;
input [MUL_WIDTH-1:0] mul_a,mul_b;
input clk;
input rst_n;
output [MUL_RESULT-1:0] mul_out;
reg[MUL_RESULT-1:0] mul_out;
reg[MUL_RESULT-1:0] stored0,stored1,stored2,stored3;
reg[MUL_RESULT-1:0] add01,add23;
always @(posedge clk or negedge rst_n) begin
if(!rst_n) begin
mul_out<=8'b0;
stored0<=8'b0;
stored1<=8'b0;
stored2<=8'b0;
stored3<=8'b0;
add01<=8'b0;
add23<=8'b0;
end
else begin //模拟人工笔算过程
stored3<=mul_b[3]?{1'b0,mul_a,3'b0}:8'b0;
stored2<=mul_b[2]?{2'b0,mul_a,2'b0}:8'b0;
stored1<=mul_b[1]?{3'b0,mul_a,1'b0}:8'b0;
stored0<=mul_b[0]?{4'b0,mul_a}:8'b0;
add01<=stored1+stored0; //第一级
add23<=stored3+stored2;
mul_out<=add01+add23; //第二级
end
end
endmodule