www.pudn.com > x86.rar > addSub.v, change:2007-12-05,size:8631b


module add_sub(Sum, input1, input2,addSub);

input [79:0] input1,input2;
input addSub;
output[79:0] Sum;

reg [79:0] Sum;
reg [79:0] temp;

reg [64:0] A;
reg [64:0] B;
reg [65:0] tempSum;
reg [78:64] tempExp;
reg flag;

reg [63:0] shiftIn;
wire[63:0] shiftOut;
reg [63:0] count;

reg [63:0] scanInRev;                   // 64-bit input number
wire [5:0] indexOutRev;         // Position from left where first 1 starts
reg [63:0] leftShiftValue;
reg [63:0] leftShiftCount;
reg direction;
wire [63:0] resultOfLeftShift;
wire [63:0] twoscompout1,twoscompout2,twoscompout3;
wire [15:0] twoscompout4,twoscompout6;
wire [7:0] twoscompout5;
reg [63:0] twoscompin1,twoscompin2,twoscompin3;
reg [15:0] twoscompin4,twoscompin6;
reg [7:0] twoscompin5;
wire [64:0] cla65Out;
wire carryOut;
reg [64:0] cla65in1,cla65in2;
reg [15:0] cla16in1,cla16in2;
reg [15:0] cla162in1,cla162in2;
reg [7:0] cla8in1,cla8in2;
wire [15:0] cla16Out;
wire [15:0] cla162Out;
wire [7:0] cla8Out;
wire [16:1] carry16Out;
wire [16:1] carry162Out;
wire [8:1] carry8Out;
reg neg;
reg [63:0] tcount;
reg [15:0] i;
reg [15:0] compIn1,compIn2;
wire [2:0] compOut;

shift_64_Logical shift(shiftOut, shiftIn, count,1'b1);          //this is to shift right 64 bits given in count value
bitScanRev64 scan(indexOutRev, scanInRev);              //this is to find left most 1 digit
shift_64_Logical leftShift(resultOfLeftShift, leftShiftValue,leftShiftCount,direction);//to shift the bits left for give                                                                        n count value

twosComp64 twoscomp1(twoscompout1,twoscompin1);
twosComp64 twoscomp2(twoscompout2,twoscompin2);
twosComp64 twoscomp3(twoscompout3,twoscompin3);
twosComp16 twoscomp4(twoscompout4,twoscompin4);
twosComp8  twoscomp5(twoscompout5,twoscompin5);
twosComp16  twoscomp6(twoscompout6,twoscompin6);
cla65 cla651(cla65Out,carryOut,cla65in1,cla65in2);
cla16Bit cla161(cla16Out,carry16Out,cla16in1,cla16in2,1'b0);
cla16Bit cla162(cla162Out,carry162Out,cla162in1,cla162in2,1'b0);
cla8Bit cla81(cla8Out,carry8Out,cla8in1,cla8in2,1'b0);
compare16 comp161(compOut, compIn1, compIn2);

always @(input1
      or input2
      or shiftOut
      or resultOfLeftShift
      or indexOutRev
      or addSub
      or twoscompout1
      or twoscompout2
      or twoscompout3
      or twoscompout4
      or twoscompout5
      or twoscompout5
      or compOut
      or cla16Out
      or carry16Out
      or cla8Out
      or carry8Out
      or cla65Out
      or cla162Out
      or carryOut
      )
begin
 Sum = 80'h0;
 flag=1'b0;
 direction=1'b0;
 A={1'b0,input1[63:0]};
 B={1'b0,input2[63:0]};
 if(input1[78:0] == 79'b0)               // If input1 is 0
        Sum = input2;
 else if(input2[78:0] == 79'b0)          // If input2 is 0
        Sum = input1;
 else                                    // If both are not 0's
 begin

        compIn1={1'b0,input1[78:64]};
        compIn2={1'b0,input2[78:64]};
        if(compOut[1])  // the exponents of both inputs are equal
        begin
                tempExp[78:64]=input1[78:64];   //give the exponent value to result exponent
        end
        else            // if the exponents are not equal
        begin

                if(compOut[2])  // if the input1's exponent is greater
                begin


                        tempExp=input1[78:64];          //assign exponent of larger value to result exponent
                        shiftIn=input2[63:0];//store the value of sgnificand to be shifted in shiftIn
                        twoscompin4={1'b0,input2[78:64]};
                        cla16in1={1'b0,input1[78:64]};
                        cla16in2=twoscompout4;
                        tcount=cla16Out; //store the no of positions to be shifted in count
                        neg=1'b1;
                        for(i=0;i<64;i=i+1)
                        begin
                                if(neg)
                                begin
                                        if(tcount[i])
                                                neg=1'b0;
                                        count[i] = ~tcount[i];
                                end
                                else
                                        count[i] = tcount[i];
                        end


                        B[63:0]=shiftOut[63:0];// result of shifted value into B



                end
                else
                begin


                        tempExp = input2[78:64];
                        shiftIn = input1[63:0];
                        twoscompin4 = {1'b0,input1[78:64]};
                        cla16in1={1'b0,input2[78:64]};
                        cla16in2=twoscompout4;
                        tcount=cla16Out; //store the no of positions to be shifted in count
                        neg=1'b1;
                        for(i=0;i<64;i=i+1)
                        begin
                                if(neg)
                                begin
                                        if(tcount[i])
                                                neg=1'b0;
                                        count[i] = ~tcount[i];
                                end
                                else
                                        count[i] = tcount[i];
                        end

                        A[63:0]=shiftOut[63:0];


                end
        end

        twoscompin1=A[63:0];
        twoscompin2=B[63:0];


// following block is to determine sign bit of result

        if((input1[79] ^ (input2[79]^addSub))==0)               //if both signs are same
        begin



                cla65in1=A;
                cla65in2=B;

                tempSum={carryOut,cla65Out};


                Sum[79]=input1[79];             //assign the sign to result

                tempSum={1'b0,tempSum[65:1]};   //shift significand right
                neg=1'b1;


                for(i=64;i<79;i=i+1)
                begin

                        if(neg)
                        begin

                                if(!tempExp[i])
                                        neg=1'b0;

                                tempExp[i] = ~tempExp[i];
                        end
                        else
                                tempExp[i] = tempExp[i];
                end//increment exponent


        end
        else
        begin


                if(input1[79])
                        A={twoscompout1[63],twoscompout1};

                if(input2[79]^addSub)
                        B={twoscompout2[63],twoscompout2};


                cla65in1=A;
                cla65in2=B;

                tempSum={carryOut,cla65Out};

                if(tempSum[65]==1'b1)          //check for overflow digit
                begin

                        Sum[79] = 1'b0;

                end
                else
                if(tempSum[64]==1'b1)
                begin

                        Sum[79] = 1'b1;
                        twoscompin3 = tempSum[63:0];
                        tempSum[63:0] = twoscompout3;

                end
                else
                        Sum[79]=1'b0;
        end


        Sum[78:64]=tempExp;             //store the resultant exponent into Sum
        Sum[63:0]=tempSum[63:0];        //store the result Sum value into Sum

// this is for normalization

        scanInRev=Sum[63:0];    //store the sum value into scan value to check where is the first left most 1
        twoscompin5={2'b0,indexOutRev};
        cla8in1=8'd63;
        cla8in2=twoscompout5;
        leftShiftCount=cla8Out;//returns the postion ; count is caluclated by using this

        leftShiftValue=Sum[63:0];//sum is stored in shift value to shift left by the calculated count value

        Sum[63:0]=resultOfLeftShift;//store the result of shifted value back into Sum
        twoscompin6=leftShiftCount[15:0];
        cla162in1=twoscompout6;
        cla162in2={1'b0,Sum[78:64]};
        Sum[78:64]=cla162Out;   //decrement exponent by the given count value
        if(Sum[63:0]==64'b0)    //if sum is 0
        Sum[78:0]=79'b0;        // make exponent also 0


 end
end

endmodule
//end of the top module


module cla65(cla65Out,carry65Out,cla65in1,cla65in2);
 input [64:0]   cla65in1;
 input [64:0]   cla65in2;
 output [64:0]  cla65Out;
 output         carry65Out;
 wire [63:0]    carryOut;
 cla64Bit       cla64BitInst(cla65Out[63:0],carryOut[63:0],cla65in1[63:0],cla65in2[63:0],1'b0);
 assign         cla65Out[64] = cla65in1[64] ^ cla65in2[64] ^ carryOut[63];
 assign         carry65Out   = ((cla65in1[64] & cla65in2[64]) | (cla65in2[64] & carryOut[63]) | (cla65in1[64] & carryOut[63]));
endmodule