Skip to main content

Floating point addition 32 bit

// 32 bit  floating point  addition//

Author : Kishore Papisetty
HDl lagugage; verilog hdl

module fp_add_seq(sum,ready,a_original,b_original,start,clk);
   input [31:0] a_original, b_original;
   input        start, clk;
   output [31:0] sum;
   output        ready;

   reg           sumneg;
   reg [7:0]    sumexp;
   reg [22:0]    sumsig;
   assign  sum [31]    = sumneg;
   assign        sum [30:23] = sumexp;
   assign        sum [22:0]  = sumsig;

   reg [32:0]    a, b;
   reg [22:0]    asig, bsig;
   reg [7:0]    aexp, bexp;
   reg           aneg, bneg;
   reg [7:0]    diff;

   parameter     st_idle  = 0;
   parameter     st_cyc_1 = 1;
   parameter     st_cyc_2 = 2;
   parameter     st_cyc_3 = 3;

   reg [1:0]     state;

 

   assign ready = state == st_idle;

   always @( posedge clk )
     case( state )
       st_idle:
         if ( start ) begin
if ( a_original[30:23] < b_original[30:23] ) begin

               a = b_original;  b = a_original;

            end else begin

               a = a_original;  b = b_original;

            end

            state = st_cyc_1;

         end

       st_cyc_1:
         begin
aneg = a[31];     bneg = b[31];
            aexp = a[30:23];  bexp = b[30:23];
         
            asig = { 2'b0, aexp ? 1'b1 : 1'b0, a[22:0] };
            bsig = { 2'b0, bexp ? 1'b1 : 1'b0, b[22:0] };

            diff = aexp - bexp;
            bsig = bsig >> diff;
         
            state = st_cyc_2;

         end

       st_cyc_2:
         begin

            if ( aneg ) asig = -asig;
            if ( bneg ) bsig = -bsig;

            sumsig = asig + bsig;

            state = st_cyc_3;

         end

       st_cyc_3:
         begin
            sumneg = sumsig[22];
            if ( sumneg ) sumsig = -sumsig;

            if ( sumsig[21] )
begin
             

               sumexp = aexp + 1;
               sumsig = sumsig >> 1;

            end else if ( sumsig ) begin:A
             
               integer pos, adj, i;

             
               pos = 0;
               for (i = 22; i >= 0; i = i - 1 )
                 if ( !pos && sumsig[i] ) pos = i;

             
               adj = 23 - pos;
               if ( aexp < adj ) begin
                  sumexp = 0;
                  sumsig = 0;
                  sumneg = 0;

               end else begin
               

                  sumexp = aexp - adj;
                  sumsig = sumsig << adj;

               end

            end else begin
             
               sumexp = 0;
               sumsig = 0;

            end
end


           default: state = st_idle;

              endcase

endmodule

//test bench //


module faddition_tb;

// Inputs
reg [31:0] a_original;
reg [31:0] b_original;
reg start;
reg clk;

// Outputs
wire [31:0] sum;
wire ready;

// Instantiate the Unit Under Test (UUT)
fp_add_seq uut (
.sum(sum),
.ready(ready),
.a_original(a_original),
.b_original(b_original),
.start(start),
.clk(clk)
);

initial begin
// Initialize Inputs
a_original = 0;
b_original = 0;
start = 0;
clk = 0;

// Wait 100 ns for global reset to finish
#100;
        a_original = 32'b10010100101000000010001000100011;
b_original = 32'b00010010100000000001000000000000;
start = 1'b1;
// Add stimulus here

end
always #100 clk = !clk;

     
endmodule

Comments

Popular posts from this blog

Gray code counter in VHDL

Author : Kishore Papisetty code : VHDL Gray code is the code with only bit transition between adjacent words. The direct description of Gray counter is based on the equation extraction from the truth table. Such solution of n bit counter demands 2^(n-2). product terms. Implementation may be difficult for greater width of counter. The alternative is using of auxiliary bit. The design was based on the Auxiliary bit generation. vhdl code for the gray counter library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity gray_counter is     Port ( clk : in  STD_LOGIC;            rst : in  STD_LOGIC;            gray_code : out  STD_LOGIC_VECTOR (3 DOWNTO 0)); end gray_counter; architecture Behavioral of gray_counter is signal count:STD_LOGIC_VECTOR(3 DOWNTO 0):="0000"; begin process(clk) begin if(rst='1') then count <= "0000"...

VHDL code for a 3-bit binary to thermometer converter

Author : Kishore Papisetty Platform : Xilinx code : VHDL //bit binary to thermometer converter// library IEEE; use IEEE.STD_LOGIC_1164.ALL; use ieee.numeric_std.all; entity bin2therm2bit is                 port (                                 binary_input : in std_logic_vector (1 downto 0);                                 therm_output : out std_logic_vector (6 downto 0)                 ); end bin2therm6bit; architecture Behavioral of bin2therm6bit is begin                 process (binary_input)                 begin                                 label1 : case binary_inpu...