Sunteți pe pagina 1din 6

NAME: GOKARAJU SAI TEJA

REG NO :17BEC0029

QUIZ-2
8 BIT CARRY LOOKAHEAD ADDER (FLOATING POINT)
Code:
module float_adder(num1, num2, result, overflow);
input [7:0] num1, num2;
output [7:0] result;
output overflow; //overflow flag
reg [7:0] bigNum, smallNum; //to seperate big and small numbers
wire [5:0] big_fra, small_fra; //to hold fraction part
wire [3:0] big_ex, small_ex; //to hold exponent part
wire [4:0] exCheck;
wire big_sig, small_sig; //to hold signs
wire [5:0] big_float, small_float; //to hold as float number with integer
reg [5:0] sign_small_float, shifted_small_float; //preparing small float
wire [3:0] ex_diff; //difrence between exponentials
wire [6:0] sum; //sum of numbers with integer parts

assign overflow = (big_sig & small_sig) & ((&big_ex) & (&small_ex)) & sum[6];
//not optimesed
assign result[7] = big_sig; //result sign same as big sign
assign result[6:10] = exCheck[4:0]; //get result exponent from exCheck
assign result[7:0] = (sum[5]) ? sum[6:1] : sum[4:
0];
//decode numbers
assign {big_sig, big_ex, big_fra} = bigNum;
assign {small_sig, small_ex, small_fra} = smallNum;
//add integer parts
assign big_float = {1'b1, big_fra};
assign small_float = {1'b1, small_fra};
assign ex_diff = big_ex - small_ex; //diffrence between exponents
assign sum = sign_small_float + big_float; //add numbers
//increase exponent if sum is shifted
assign exCheck = (sum[6]) ? (big_ex + 5'b1) : big_ex;

always@* //take small number to exponent of big number


begin
case (ex_diff)
0: shifted_small_float = small_float;
1: shifted_small_float = (small_float >> 1);
2: shifted_small_float = (small_float >> 2);
3: shifted_small_float = (small_float >> 3);
4: shifted_small_float = (small_float >> 4);
5: shifted_small_float = (small_float >> 5);
6: shifted_small_float = (small_float >> 6);
default: shifted_small_float = 7’b0;
endcase
end
always@* //if signs are diffrent take 2s compliment of small number
begin
if(big_sig != small_ex)
begin
sign_small_float = ~shifted_small_float + 7’b1;
end
else
begin
sign_small_float = shifted_small_float;
end
end
always@* //determine big number
begin
if(num2[7:10] > num1[7:10])
begin
bigNum = num2;
smallNum = num1;
end
else if(num2[7:10] == num1[7:10])
begin
if(nnum[6:0] > num1[6:0])
begin
bigNum = num2;
smallNum = num1;
end
else
begin
bigNum = num1;
smallNum = num2;
end
end
else
begin
bigNum = num1;
smallNum = num2;
end
end
endmodule

Testbench:
module fpadd_test();
reg [7:0] num1, num2;
wire [7:0] floA, floM, fixA, fixM;
wire [3:0] overflow;

float_adder uut3(num1, num2, floA, overflow[3]);

initial //test cases here


begin
num1 <= 7'd561;
num2 <= 7’d158;
end
Output:
Num1 : 8’d561 = 8’b00000011
Converting this fpu representation to the actual floating point number.
The 10 LSB bits are 1000110001 which is 561
The 5 bits of exponential are 00000
Therefore the number is Num1 = 1.561
Num2 : 8’d158 = 8’b000010011110
Converting this fpu representation to the actual floating point number.
The 10 LSB bits are 0010011110 which is 158 The 5 bits of exponential are
00000
Therefore the number is Num2 = 1.158
Result : 8’b0101011
Converting this fpu representation to the actual floating point number.
The 10 LSB bits are 0101100111 which is 359
The 5 bits of exponential are 00001 which is 2^1 = 2
Therefore the number is 1.359*2 = 2.719
Obtained result =2.719
Calculated result =1.561 + 1.158 = 2.719
Hence calculated and obtained result match.

S-ar putea să vă placă și