两个4bit的无符号的二进制数相乘,采用的是列竖式相乘的方法来获得的:
首先,在数据进入时,用寄存器变量存起来。 把其中一个乘数拼接成一个与乘积同样bit的数。
其次,在乘数对应的bit是1时,对被乘数向左移位。
最后,关于av信号。缓存一个周期,add一个周期,共两个周期
module multiply_binary(
//Inputs
clk,
rstn,
ina,
inb,
en,
//Outputs
result_av,
result
);
parameter DATA_WD = 4;
parameter MULT_WD = 8;
input clk;
input rstn;
input en;
input [DATA_WD-1:0] ina;
input [DATA_WD-1:0] ina;
output reg [2*DATA_WD-1:0] result;
output result_av;
wire [MULT_WD-1:0] add_value0;
wire [MULT_WD-1:0] add_value1;
wire [MULT_WD-1:0] add_value2;
wire [MULT_WD-1:0] add_value3;
reg [MULT_WD-1:0] mult_r;
reg [DATA_WD-1:0] data_r;
always@(posedge clk or negedge rstn)begin
if(!rstn) begin
data_r <= {DATA_WD{1'b0}};
mult_r <= {MULT_WD{1'b0)};
end
else begin
if(en)begin
data_r <= inb;
mult_r <= {{(MULT_WD - DATA_WD){1'b0}}, ina};
end
end
end
assign add_value0 = data_r[0]? mult_r : MULT_WD'b0;
assign add_value1 = data_r[1]? mult_r<<1 : MULT_WD'b0;
assign add_value2 = data_r[2]? mult_r<<2 : MULT_WD'b0;
assign add_value0 = data_r[3]? mult_r<<3 : MULT_WD'b0;
always@(posedge clk or negedge rstn)begin
if(!rstn) result <= MULT_WD{1'b0)};
else if(add_av) begin
result <= add_value0 + add_value1 + add_value2 + add_value3; // dd_value need one cl
end
end
lways@(posedge clk or negedge rstn)begin
if(!rstn)begin
add_av <= 1'b0;
add_av1 <= 1'b0;
end
else begin
add_av <= en;
add_av1 <= add_av;
end
end
assign result_av = add_av1; // the computation need two clk
endmodule