hover
Junior Member level 2
booth multiplier
Hi, every one, I have written a booth multiplier, But when I simulate it, I found that some times it can work well, some times it can't. I don't know what's wrong with my design. Please help me. Below is my code.
Hi, every one, I have written a booth multiplier, But when I simulate it, I found that some times it can work well, some times it can't. I don't know what's wrong with my design. Please help me. Below is my code.
Code:
module booth_multiplier(product,ready,word1,word2,start,reset,clk);
parameter L_word=4;
parameter L_BRC=2;
parameter S_idle=0,S_shifting=1,S_adding=2,S_subing=3,S_done=4;
parameter All_ones=4'b1111;
output[2*L_word-1:0] product;
output ready;
input [L_word-1:0] word1,word2;
input start,clk,reset;
reg[2:0] state,next_state;
reg[2*L_word-1:0] multiplicand;
reg[L_word-1:0] multiplier;
reg m_brc;
reg shift,Add,Sub,load_words;
reg flush;
reg[2*L_word-1:0] product;
wire m0=multiplier[0];
wire[L_BRC-1:0] BRC={m0,m_brc};
wire ready=((state==S_idle)&&!reset)||(state==S_done);
wire empty=((word1==0)||(word2==0));
always@(posedge clk or posedge reset) //Datapath configuration
begin
if(reset) begin
multiplicand<=0;
multiplier<=0;
product<=0;
end
else if(flush)
product<=0;
else if(load_words) begin
if(word1[L_word-1]==0) multiplicand<=word1;
else multiplicand<={All_ones,word1[L_word-1:0]};
multiplier<=word2;
product<=0;
end
else if(shift) begin
multiplicand<=multiplicand<<1;
multiplier<=multiplier>>1;
end
else if(Add)
product<=product+multiplicand;
else if(Sub)
product<=product-multiplicand;
end
always@(posedge clk or posedge reset)
if(reset)
m_brc<=0;
else if(load_words)
m_brc<=0;
else
m_brc<=m0;
always@(posedge clk or posedge reset)//state transition
if(reset)
state<=S_idle;
else
state<=next_state;
always@(state or start or BRC or multiplier or empty)//state machine
begin
load_words=0;
shift=0;
Add=0;
Sub=0;
flush=0;
case(state)
S_idle: if(!start) next_state=S_idle;
else if(start&&!empty) begin
load_words=1; next_state=S_shifting;
end
else if(start&&empty) begin
flush=1; next_state=S_done;
end
S_shifting: if(multiplier==1) begin Add=1; next_state=S_done; end
else if(BRC==2'b00||BRC==2'b11) begin shift=1;
next_state=S_shifting; end
else if(BRC==2'b01) begin Add=1; next_state=S_adding; end
else if(BRC==2'b10) begin Sub=1; next_state=S_subing; end
S_adding: begin shift=1; next_state=S_shifting; end
S_subing: begin shift=1; next_state=S_shifting; end
S_done: if(start==0) next_state=S_done;
else if(start&&empty) begin
flush=1; next_state=S_done;
end
else if(start&&!empty) begin
load_words=1; next_state=S_shifting;
end
default: next_state=S_idle;
endcase
end
endmodule