对于乘法 P=A*B 有:
实现:乘法转化成了个N(A的位宽)个加法运算;第n次加法的加数是A的第n位(An)与B左移n位(B<)的乘积;<>
权衡: 速度和面积。速度不够,吞吐不够,有流水线方式;面积太大(消耗资源多),用串行方式(时分复用)。
1、 串行方式,需要若干周期才能完成一次乘法计算
module top(clk,rst_n,A,B,C);
input clk;
input rst_n;
input [7:0] A;
input [7:0] B;
output reg [15:0] C;
reg [7:0] a;
reg [15:0] b;
reg [15:0] c;
reg [3:0] count;
reg [2:0] i;
always@(posedge clk or negedge rst_n)
if(!rst_n)
begin
i<=3'd0;
count<=4'd0;
a<=8'd0;
b<=16'd0;
c<=16'd0;
C<=16'd0;
end
else case(i)
3'd0: begin
a<=A;
b[7:0]<=B;
c<=16'd0;
i<=3'd1;
end
3'd1: if(count==4'd8)
begin
C<=c;
i<=3'd0;
end
else begin
if(a[0]==1'b1)
begin
c<=c+b;
b<=b<<1;
a<=a>>1;
count<=count+4'd1;
i<=3'd1;
end
end
default: i<=3'd0;
endcase
endmodule
资源使用
速率
2、流水线
module top(clk,rst_n,A,B,C);
input clk;
input rst_n;
input [7:0] A;
input [7:0] B;
output reg [15:0] C;
reg [7:0] a;
reg [7:0] b;
always@(posedge clk or negedge rst_n)
if(!rst_n)
begin
a<=8'd0;
b<=8'd0;
end
else begin
a<=A;
b<=B;
end
// 移位(相乘)
reg [7:0] x0;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x0<=8'd0;
else if(a[0])
x0<=b;
else *0<=8'd0;
reg [8:0] x1;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x1<=9'd0;
else if(a[1])
x1<={b[7],b};
else *1<=9'd0;
reg [9:0] x2;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x2<=10'd0;
else if(a[2])
x2<={b[7],b[7],b};
else *2<=10'd0;
reg [10:0] x3;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x3<=11'd0;
else if(a[3])
x3<={b[7],b[7],b[7],b};
else *3<=11'd0;
reg [11:0] x4;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x4<=12'd0;
else if(a[4])
x4<={b[7],b[7],b[7],b[7],b};
else *4<=12'd0;
reg [12:0] x5;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x5<=13'd0;
else if(a[5])
x5<={b[7],b[7],b[7],b[7],b[7],b};
else *5<=13'd0;
reg [13:0] x6;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x6<=14'd0;
else if(a[6])
x6<={b[7],b[7],b[7],b[7],b[7],b[7],b};
else *6<=14'd0;
reg [14:0] x7;
always@(posedge clk or negedge rst_n)
if(!rst_n)
x7<=15'd0;
else if(a[6])
x7<={b[7],b[7],b[7],b[7],b[7],b[7],b[7],b};
else *7<=15'd0;
// 第一流水
reg [9:0] y1;
always@(posedge clk or negedge rst_n)
if(!rst_n)
y1<=10'd0;
else
y1<=x0+x1;
reg [11:0] y2;
always@(posedge clk or negedge rst_n)
if(!rst_n)
y2<=12'd0;
else
y2<=x2+x3;
reg [13:0] y3;
always@(posedge clk or negedge rst_n)
if(!rst_n)
y3<=14'd0;
else
y3<=x4+x5;
reg [15:0] y4;
always@(posedge clk or negedge rst_n)
if(!rst_n)
y4<=16'd0;
else
y4<=x6+x7;
// 第二流水
reg [12:0] z1;
always@(posedge clk or negedge rst_n)
if(!rst_n)
z1<=13'd0;
else
z1<=y1+y2;
reg [15:0] z2;
always@(posedge clk or negedge rst_n)
if(!rst_n)
z2<=16'd0;
else
z2<=y3+y4;
// 第三流水
always@(posedge clk or negedge rst_n)
if(!rst_n)
C<=16'd0;
else
C<=z1+z2;
endmodule
RTL 视图
资源使用
速率
这里仅仅是一个例子,乘法器还有很多其他的实现算法,具体应用什么样的还要看实际情况。“解放思想”的同时还需要“实事求是”,哈哈
用户1648762 2014-5-5 17:40
644398774_263592779 2013-5-27 16:37
644398774_263592779 2013-5-27 16:35
用户1710126 2013-5-27 13:58
用户1573081 2013-5-26 18:23
用户377235 2013-5-25 00:31
644398774_263592779 2013-5-23 23:25
用户1696769 2013-5-23 09:37