|
|
@@ -4,6 +4,8 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
output logic [N-1:0] output_z;
|
|
|
|
|
|
reg [N-2-M:0] a_m, b_m, z_m; // mantissa
|
|
|
+ reg [N-2-M:0] a_m1, b_m1;
|
|
|
+
|
|
|
reg [M-1:0] a_e, b_e, z_e; // exponent
|
|
|
reg a_s, b_s, z_s; // sign
|
|
|
|
|
|
@@ -12,19 +14,24 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
logic [N-2-M:0] res; // For the addition result
|
|
|
|
|
|
|
|
|
+ always_comb
|
|
|
+ begin
|
|
|
+ // Unpacking the inputs
|
|
|
+ a_m = input_a[N-M-2:0];
|
|
|
+ a_e = input_a[N-2:N-M-1];
|
|
|
+ a_s = input_a[N-1];
|
|
|
+
|
|
|
+ b_m = input_b[N-M-2:0];
|
|
|
+ b_e = input_b[N-2:N-M-1];
|
|
|
+ b_s = input_b[N-1];
|
|
|
+ end
|
|
|
+
|
|
|
always_ff @(posedge clk)
|
|
|
begin
|
|
|
if(~reset)
|
|
|
begin
|
|
|
- // Unpacking the inputs
|
|
|
- a_m <= input_a[N-M-2:0];
|
|
|
- a_e <= input_a[N-2:N-M-1];
|
|
|
- a_s <= input_a[N-1];
|
|
|
-
|
|
|
- b_m <= input_b[N-M-2:0];
|
|
|
- b_e <= input_b[N-2:N-M-1];
|
|
|
- b_s <= input_b[N-1];
|
|
|
-
|
|
|
+ a_m1 <= a_m;
|
|
|
+ b_m1 <= b_m;
|
|
|
// If input_a has the bigger exponent then flag it with greater and find the absolute difference
|
|
|
if (a_e > b_e)
|
|
|
begin
|
|
|
@@ -33,6 +40,7 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
z_s <= a_s;
|
|
|
z_e <= a_e;
|
|
|
end
|
|
|
+
|
|
|
// If input_a has the bigger exponent then flag it with greater and find the absolute difference
|
|
|
else if (b_e > a_e)
|
|
|
begin
|
|
|
@@ -41,6 +49,7 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
z_s <= b_s;
|
|
|
z_e <= b_e;
|
|
|
end
|
|
|
+
|
|
|
// If the inputs have equal exponent
|
|
|
else
|
|
|
begin
|
|
|
@@ -61,26 +70,27 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
z_s <= 0;
|
|
|
end
|
|
|
end
|
|
|
+
|
|
|
// Condition for overflow is that it sets the output to the larger input
|
|
|
if (abs > N-1-M) // Shifting by N-1-M would give 0
|
|
|
begin
|
|
|
if (greater == 2'b01)
|
|
|
begin
|
|
|
- z_m <= a_m; // Input a is larger and is translated to the output
|
|
|
+ z_m <= a_m1; // Input a is larger and is translated to the output
|
|
|
end
|
|
|
else if (greater == 2'b10)
|
|
|
begin
|
|
|
- z_m <= b_m; // Input b is larger and is translated to the output
|
|
|
+ z_m <= b_m1; // Input b is larger and is translated to the output
|
|
|
end
|
|
|
else // Shouldn't happen as abs should be 0 for this to occur
|
|
|
begin
|
|
|
- if (a_m >= b_m)
|
|
|
+ if (a_m1 >= b_m1)
|
|
|
begin
|
|
|
- z_m <= a_m; // Equal exponents but a has the larger mantissa
|
|
|
+ z_m <= a_m1; // Equal exponents but a has the larger mantissa
|
|
|
end
|
|
|
- else if (b_m > a_m)
|
|
|
+ else if (b_m1 > a_m1)
|
|
|
begin
|
|
|
- z_m <= b_m; // Equal exponents but b has the larger mantissa
|
|
|
+ z_m <= b_m1; // Equal exponents but b has the larger mantissa
|
|
|
end
|
|
|
end
|
|
|
end
|
|
|
@@ -93,12 +103,12 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
// If the signs are the same then add
|
|
|
if (a_s == b_s)
|
|
|
begin
|
|
|
- res <= a_m + (b_m >> (abs-1));
|
|
|
+ res <= a_m1 + (b_m1 >> (abs-1));
|
|
|
end
|
|
|
// If they are different then subtract
|
|
|
else
|
|
|
begin
|
|
|
- res <= a_m - (b_m >> (abs-1));
|
|
|
+ res <= a_m1 - (b_m1 >> (abs-1));
|
|
|
end
|
|
|
end
|
|
|
// If b has the bigger exponent
|
|
|
@@ -107,12 +117,12 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
// If the signs are the same then add
|
|
|
if (a_s == b_s)
|
|
|
begin
|
|
|
- res <= b_m + (a_m >> (abs-1));
|
|
|
+ res <= b_m1 + (a_m1 >> (abs-1));
|
|
|
end
|
|
|
// If they are different then subtract
|
|
|
else
|
|
|
begin
|
|
|
- res <= b_m - (a_m >> (abs-1));
|
|
|
+ res <= b_m1 - (a_m1 >> (abs-1));
|
|
|
end
|
|
|
end
|
|
|
// If the exponents are equal
|
|
|
@@ -121,19 +131,19 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
// If the signs are the same then add
|
|
|
if (a_s == b_s)
|
|
|
begin
|
|
|
- res <= a_m + b_m;
|
|
|
+ res <= a_m1 + b_m1;
|
|
|
end
|
|
|
// If the signs are different then subtract
|
|
|
else
|
|
|
begin
|
|
|
// First checking which has the bigger mantissa
|
|
|
- if (a_m > b_m)
|
|
|
+ if (a_m1 > b_m1)
|
|
|
begin
|
|
|
- res <= a_m - b_m;
|
|
|
+ res <= a_m1 - b_m1;
|
|
|
end
|
|
|
- else if (b_m > a_m)
|
|
|
+ else if (b_m1 > a_m1)
|
|
|
begin
|
|
|
- res <= b_m - a_m;
|
|
|
+ res <= b_m1 - a_m1;
|
|
|
end
|
|
|
// If the mantissa are the same as well then the result should be 0
|
|
|
else
|
|
|
@@ -153,13 +163,8 @@ module fp_adder #(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
|
|
|
|
|
|
else
|
|
|
begin
|
|
|
- a_s <= 0;
|
|
|
- a_e <= 0;
|
|
|
- a_m <= 0;
|
|
|
-
|
|
|
- b_s <= 0;
|
|
|
- b_e <= 0;
|
|
|
- b_m <= 0;
|
|
|
+ a_m1 <= 0;
|
|
|
+ b_m1 <= 0;
|
|
|
|
|
|
z_s <= 0;
|
|
|
z_e <= 0;
|