fp_adder.sv 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. typedef enum logic [1:0]{
  2. greater_a,
  3. greater_b,
  4. equal_ab
  5. } grater_state;
  6. module fp_adder#(parameter N=16, M=5)(input_a, input_b, output_z, clk, reset);
  7. localparam K=N-M-1; // Size of mantissa
  8. input reg [N-1:0] input_a, input_b;
  9. input logic clk, reset;
  10. output reg [N-1:0] output_z;
  11. reg [K-1:0] a_m0, b_m0; // mantissa
  12. reg [K-1:0] a_m1, b_m1, z_m2, z_m3;
  13. reg [K*2-1:0] z_m1a, z_m1b, z_m1z; // Double mantissa
  14. reg z_m1s, z_m2s;
  15. reg [M-1:0] a_e0, b_e0; // exponent
  16. reg [M-1:0] z_e1, z_e2, z_e3;
  17. reg a_s0, b_s0; // sign
  18. reg a_s1, b_s1, z_s1, z_s2, z_s3;
  19. grater_state greater; // 01 for a, 10 for b, 11 for both and 00 for neither
  20. reg [M:0] abs; // For the absolute difference between exponents
  21. always_comb begin
  22. output_z = {z_s3, z_e3, z_m3};
  23. z_m1a = {a_m1, {K{1'b0}}};
  24. z_m1b = {b_m1, {K{1'b0}}};
  25. case (greater)
  26. greater_a: begin
  27. if (a_s1 == b_s1) {z_m1s, z_m1z} = z_m1a + (z_m1b >> (abs - 1));
  28. else {z_m1s, z_m1z} = z_m1a - (z_m1b >> (abs - 1));
  29. end
  30. greater_b: begin
  31. if (a_s1 == b_s1) {z_m1s, z_m1z} = z_m1b + (z_m1a >> (abs - 1));
  32. else {z_m1s, z_m1z} = z_m1b - (z_m1a >> (abs - 1));
  33. end
  34. equal_ab: begin
  35. // If the signs are the same then add
  36. if (a_s1 == b_s1) {z_m1s, z_m1z} = (z_m1a + z_m1b) >> 1;
  37. // If the signs are different then subtract
  38. else begin
  39. // First checking which has the bigger mantissa
  40. if (a_m1 > b_m1) {z_m1s, z_m1z} = z_m1a - z_m1b;
  41. // If the signs are different then subtract
  42. else if (b_m1 > a_m1) {z_m1s, z_m1z} = z_m1b - z_m1a;
  43. // If the mantissa are the same as well then the result should be 0
  44. else {z_m1s, z_m1z} = 0;
  45. end
  46. end
  47. endcase
  48. end
  49. always_ff @(posedge clk)
  50. begin
  51. if (~reset)
  52. begin
  53. // Unpacking the inputs
  54. a_m0 <= input_a[K-1:0];
  55. a_e0 <= input_a[N-2:K];
  56. a_s0 <= input_a[N-1];
  57. b_m0 <= input_b[K-1:0];
  58. b_e0 <= input_b[N-2:K];
  59. b_s0 <= input_b[N-1];
  60. // Second stage
  61. a_m1 <= a_m0;
  62. a_s1 <= a_s0;
  63. b_m1 <= b_m0;
  64. b_s1 <= b_s0;
  65. z_e2 <= z_e1;
  66. z_s2 <= z_s1;
  67. // If input_a has the bigger exponent then flag it with greater and find the absolute difference
  68. if (a_e0 > b_e0)
  69. begin
  70. greater <= greater_a;
  71. abs <= a_e0 - b_e0;
  72. z_s1 <= a_s0;
  73. z_e1 <= a_e0 + 1;
  74. end
  75. // If input_a has the bigger exponent then flag it with greater and find the absolute difference
  76. else if (b_e0 > a_e0)
  77. begin
  78. greater <= greater_b;
  79. abs <= b_e0 - a_e0;
  80. z_s1 <= b_s0;
  81. z_e1 <= b_e0 + 1;
  82. end
  83. // If the inputs have equal exponent
  84. else
  85. begin
  86. greater <= equal_ab;
  87. abs <= 0;
  88. z_e1 <= a_e0 + 1;
  89. // Assigning the overall sign based on the difference between the mantissa
  90. if (a_m0 > b_m0) z_s1 <= a_s0;
  91. else if (b_m0 > a_m0) z_s1 <= b_s0;
  92. else z_s1 <= 0;
  93. end
  94. // Condition for overflow is that it sets the output to the larger input
  95. if (abs > K) // Shifting by N-1-M would give 0
  96. begin
  97. case (greater)
  98. greater_a: z_m2 <= a_m1;
  99. greater_b: z_m2 <= b_m1;
  100. endcase
  101. end
  102. else
  103. begin
  104. z_m2 <= z_m1z[K*2-1:K];
  105. z_m2s <= z_m1s;
  106. end
  107. if(z_m2s) begin
  108. z_e3 <= z_e2 + 1;
  109. end else begin
  110. z_e3 <= z_e2;
  111. end
  112. z_m3 <= z_m2;
  113. z_s3 <= z_s2;
  114. end // end ~reset
  115. else
  116. begin
  117. a_m0 <= 0;
  118. a_e0 <= 0;
  119. a_s0 <= 0;
  120. b_m0 <= 0;
  121. b_e0 <= 0;
  122. b_s0 <= 0;
  123. a_m1 <= 0;
  124. b_m1 <= 0;
  125. z_s1 <= 0;
  126. z_e1 <= 0;
  127. z_e2 <= 0;
  128. z_s2 <= 0;
  129. z_m2 <= 0;
  130. z_m2s <= 0;
  131. z_s3 <= 0;
  132. z_e3 <= 0;
  133. z_m3 <= 0;
  134. greater <= equal_ab;
  135. abs <= 0;
  136. end
  137. end
  138. endmodule : fp_adder