-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloatadd
101 lines (95 loc) · 2.91 KB
/
floatadd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
`timescale 100 ns / 10 ps
module floatAdd (floatA,floatB,sum);
input [15:0] floatA, floatB;
output reg [15:0] sum;
reg sign;
reg signed [5:0] exponent; //fifth bit is sign
reg [9:0] mantissa;
reg [4:0] exponentA, exponentB;
reg [10:0] fractionA, fractionB, fraction; //fraction = {1,mantissa}
reg [7:0] shiftAmount;
reg cout;
always @ (floatA or floatB) begin
exponentA = floatA[14:10];
exponentB = floatB[14:10];
fractionA = {1'b1,floatA[9:0]};
fractionB = {1'b1,floatB[9:0]};
exponent = exponentA;
if (floatA == 0) begin //special case (floatA = 0)
sum = floatB;
end else if (floatB == 0) begin //special case (floatB = 0)
sum = floatA;
end else if (floatA[14:0] == floatB[14:0] && floatA[15]^floatB[15]==1'b1) begin
sum=0;
end else begin
if (exponentB > exponentA) begin
shiftAmount = exponentB - exponentA;
fractionA = fractionA >> (shiftAmount);
exponent = exponentB;
end else if (exponentA > exponentB) begin
shiftAmount = exponentA - exponentB;
fractionB = fractionB >> (shiftAmount);
exponent = exponentA;
end
if (floatA[15] == floatB[15]) begin //same sign
{cout,fraction} = fractionA + fractionB;
if (cout == 1'b1) begin
{cout,fraction} = {cout,fraction} >> 1'b1;
exponent = exponent + 1'b1;
end
sign = floatA[15];
end else begin //different signs
if (floatA[15] == 1'b1) begin
{cout,fraction} = fractionB - fractionA;
end else begin
{cout,fraction} = fractionA - fractionB;
end
sign = cout;
if (cout == 1'b1) begin
fraction = -fraction;
end else begin
end
if (fraction [10] == 0) begin
if (fraction[9] == 1'b1) begin
fraction = fraction << 1'b1;
exponent = exponent - 1'b1;
end else if (fraction[8] == 1'b1) begin
fraction = fraction << 2'b10;
exponent = exponent - 2'b10;
end else if (fraction[7] == 1'b1) begin
fraction = fraction << 2'b11;
exponent = exponent - 2'b11;
end else if (fraction[6] == 1'b1) begin
fraction = fraction << 3'b100;
exponent = exponent - 3'b100;
end else if (fraction[5] == 1'b1) begin
fraction = fraction << 3'b101;
exponent = exponent - 3'b101;
end else if (fraction[4] == 1'b1) begin
fraction = fraction << 3'b110;
exponent = exponent - 3'b110;
end else if (fraction[3] == 1'b1) begin
fraction = fraction << 3'b111;
exponent = exponent - 3'b111;
end else if (fraction[2] == 1'b1) begin
fraction = fraction << 4'b1000;
exponent = exponent - 4'b1000;
end else if (fraction[1] == 1'b1) begin
fraction = fraction << 4'b1001;
exponent = exponent - 4'b1001;
end else if (fraction[0] == 1'b1) begin
fraction = fraction << 4'b1010;
exponent = exponent - 4'b1010;
end
end
end
mantissa = fraction[9:0];
if(exponent[5]==1'b1) begin //exponent is negative
sum = 16'b0000000000000000;
end
else begin
sum = {sign,exponent[4:0],mantissa};
end
end
end
endmodule