script async='async' crossorigin='anonymous' src='https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6016566166623052'/> Verilog coding: Verilog Code for 8-bit Booth’s Multiplier

Thursday, 14 December 2017

Verilog Code for 8-bit Booth’s Multiplier

This is a Multiplication algorithm which multiplies two binary numbers's in 2's Compliment.

Following is the 8-bits Booth's Multiplier verilog code:
fpga verilog code example

VHDL of 8-bit Booth’s Multiplier

module 8_bit_booth_mult(prd, busy, mc, mp, clk, start);
output [15:0] prd;
output busy;
input [7:0] mc, mp;
input clk, start;
reg [7:0] A, Q, B;
reg Q_1;
reg [3:0] count;
wire [7:0] sum, difference;
always @(posedge clk)
begin
if (start) begin
A <= 8'b0;
B <= mc;
Q <= mp;
Q_1 <= 1'b0;
count <= 4'b0;
end
else begin
case ({Q[0], Q_1})
2'b0_1 : {A, Q, Q_1} <= {sum[7], sum, Q};
2'b1_0 : {A, Q, Q_1} <= {difference[7], difference, Q};
default: {A, Q, Q_1} <= {A[7], A, Q};
endcase
count <= count + 1'b1;
end
end
alu adder (sum, A, B, 1'b0);
alu subtracter (difference, A, ~B, 1'b1);
assign prd = {A, Q};
assign busy = (count < 8);
endmodule
//For alu.
//It is an adder, but capable of subtraction:
//Recall that subtraction means adding the two's complement
//a - b = a + (-b) = a + (inverted b + 1)
//The 1 will be coming in as Cin (carry-in)
module alu(out, a, b, cin);
output [7:0] out;
input [7:0] a;
input [7:0] b;
input cin;
assign out = a + b + cin;
endmodule



Testbench for Booth’s Multiplier


module testbench;
reg clk, start;
reg [7:0] a, b;
wire [15:0] ab;
wire busy;
booth_multiplier multiplier1(ab, busy, a, b, clk, start);
initial begin
clk = 0;
$display("first example: a = 3 b = 17");
a = 3; b = 17; start = 1; #50 start = 0;
#80 $display("first example done");
$display("second example: a = 7 b = 7");
a = 7; b = 7; start = 1; #50 start = 0;
#80 $display("second example done");
$finish;
end
always #5 clk = !clk;
always @(posedge clk) $strobe("ab: %d busy: %d at time=%t", ab, busy, $stime);
endmodule






4 comments:

  1. It generates a delay of 5 units of given time. In this example, after every 5 units of time, the clock value is reversed. I hope you understood. Cheers!

    ReplyDelete
  2. Sorry i have to write a testbench code until tomorrow but it have to fit to my Booth’s FSM code. Howevet, i cant do it. Could you help me i can share my code with you ?

    module fsm(input clk, input reset, input start,input Q0,input Q1,output reg Load_M,output reg Load_Q,output reg Shift_Q,output reg LoadA,output reg ShiftA,output reg ShiftQ_1,output reg add,output reg sub,output reg [3:0]state_out);
    reg [3:0]N=4'b0100;
    reg state,nextstate;
    parameter S0=3'b000;
    parameter S1=3'b001;
    parameter S2=3'b010;
    parameter S3=3'b011;
    parameter S4=3'b100;
    parameter S5=3'b101;
    parameter S6=3'b110;
    parameter S7=3'b111;
    always @(posedge clk or posedge reset) begin
    if (reset)
    state <= S0;
    else
    state <= nextstate;
    end
    always @(state or nextstate or N or Q0 or Q1) begin
    case(state)
    S0: if(start) // wait state
    nextstate<=S1;
    else
    nextstate<=S0;
    S1: // load state
    nextstate<=S2;
    S2: if(Q0==Q1) // qo q1 checking state
    nextstate <= S5;
    else if(Q0==0 && Q1==1)
    nextstate<=S3;
    else if(Q0==1 && Q1==0)
    nextstate<=S4;
    S3: // add state
    nextstate<=S5;
    S4: // sub state
    nextstate<=S5;
    S5: // shift state
    nextstate<=S6;
    S6: if(N) // n checking state
    // N<=N-4'b0001; */
    nextstate<=S1;
    else
    nextstate<=S7;
    S7: // finish state
    nextstate<=S7;
    endcase
    end
    always @(state) begin
    case (state)
    S0 : begin
    sub = 1'b0;
    add= 1'b0;
    LoadA= 1'b0;
    Load_M= 1'b0;
    Load_Q= 1'b0;
    ShiftA= 1'b0;
    Shift_Q = 1'b0;
    ShiftQ_1= 1'b0;
    state_out=4'b0000;
    end
    S1 : begin
    sub = 1'b0;
    add= 1'b0;
    LoadA= 1'b1;
    Load_M= 1'b1;
    Load_Q= 1'b1;
    ShiftA= 1'b0;
    Shift_Q = 1'b0;
    ShiftQ_1= 1'b0;
    state_out=4'b0001;
    end
    S2 : begin
    sub = 1'b0;
    add= 1'b0;
    LoadA= 1'b0;
    Load_M= 1'b0;
    Load_Q= 1'b0;
    ShiftA= 1'b0;
    Shift_Q = 1'b0;
    ShiftQ_1= 1'b0;
    state_out=4'b0010;
    end
    S3 : begin
    sub = 1'b0;
    add= 1'b1;
    LoadA= 1'b0;
    Load_M= 1'b0;
    Load_Q= 1'b0;
    ShiftA= 1'b0;
    Shift_Q = 1'b0;
    ShiftQ_1= 1'b0;
    state_out=4'b0011;
    end
    S4 : begin
    sub = 1'b1;
    add= 1'b0;
    LoadA= 1'b0;
    Load_M= 1'b0;
    Load_Q= 1'b0;
    ShiftA= 1'b0;
    Shift_Q = 1'b0;
    ShiftQ_1= 1'b0;
    state_out=4'b0100;
    end
    S5 : begin
    sub = 1'b0;
    add= 1'b0;
    LoadA= 1'b0;
    Load_M= 1'b0;
    Load_Q= 1'b0;
    ShiftA= 1'b1;
    Shift_Q = 1'b1;
    ShiftQ_1= 1'b1;
    state_out=4'b0101;
    end
    S6 : begin
    sub = 1'b0;
    add= 1'b0;
    LoadA= 1'b0;
    Load_M= 1'b0;
    Load_Q= 1'b0;
    ShiftA= 1'b0;
    Shift_Q = 1'b0;
    ShiftQ_1= 1'b0;
    state_out=4'b0110;
    end
    S7 : begin
    sub = 1'b0;
    add= 1'b0;
    LoadA= 1'b0;
    Load_M= 1'b0;
    Load_Q= 1'b0;
    ShiftA= 1'b0;
    Shift_Q = 1'b0;
    ShiftQ_1= 1'b0;
    state_out=4'b0111;
    end
    endcase
    end
    endmodule

    ReplyDelete
  3. Is a game developer ready to go? - Shootercasino
    “The game developer has agreed to make a long-term deal with 바카라 the 제왕카지노 company that will allow the brand deccasino to expand its market in a number of different ways”.

    ReplyDelete