Booth.v
//整数乘法Booth算法
//start<=posedge时读取因数;M<=被乘数,Qin<=乘数
//start<=0,送若干个时钟信号供计算用
//done<=1时计算完毕;{A,Q}<=乘积
module Booth(A,Q,Qi,M,start,done,clk);
parameter n=6;
parameter width_count=3;
output reg [n-1:0] A;
output reg [n-1:0] Q;
input [n-1:0] Qi;
reg Q1;
input [n-1:0] M;
reg [width_count-1:0] count;
input start;
output reg done;
input clk;
reg clks;
always @(posedge clk,posedge start)
if (start) begin
A<=0;
Q1<=0;
Q<=Qi;
count<=n;
done<=1'b0;
clks<=1'b1;
end
else if (~done) begin
clks<=~clks;
if (clks) begin
if (count>0)
case ({Q[0],Q1})
2'b10: A<=A-M;
2'b01: A<=A+M;
endcase
end
else begin
count<=count-1;
if (count>0)
{A,Q,Q1}<={A[n-1],A,Q};
else
done<=0'b1;
end
end
endmodule
tb_Booth.v
`include "Booth.v"
module tb_Booth;
parameter n=6;
wire [n-1:0] A;
wire [n-1:0] Q;
reg [n-1:0] Qi;
reg [n-1:0] M;
reg start;
wire done;
reg clk;
wire [2*n-1:0] result;
Booth booth(A,Q,Qi,M,start,done,clk);
initial
begin
clk=1'b1;
M=12;
Qi=-17;
#1 start=1;
#4 start=0;
end
always
#1 clk=~clk;
assign result={A,Q};
endmodule
- 1
- 2
前往页