FPA_module_test.sv 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. module floating_add #(parameter N=16, M=4)(input_1, input_2, sum, diff, clk, reset);
  2. input logic [N-1:0] input_1, input_2;
  3. input logic clk, reset;
  4. output logic [N-1:0] sum;
  5. output logic [M:0] diff;
  6. // logic flag_a;
  7. // logic flag_b;
  8. // logic [M:0] abs;
  9. // logic [N-3-M:0] res;
  10. logic [N-1:0] D0 [7:0];
  11. logic [N-1:0] Q0 [7:0];
  12. logic [N-1:0] Q1 [7:0];
  13. logic [N-1:0] Q2 [7:0];
  14. // sign_x = x[N-1]
  15. // exponent_x = x[N-2:N-2-M]
  16. // mantissa_x = x[N-3-M:0]
  17. // Pipeline stage 0
  18. always_comb
  19. begin
  20. D0[0] = input_1;
  21. D0[1] = input_2;
  22. D0[2] = 0 // sum
  23. D0[3] = 0 // diff
  24. D0[4] = 0 // flag_a
  25. Do[5] = 0 // flag_b
  26. D0[6] = 0 // abs
  27. D0[7] = 0 // res
  28. end
  29. pipe pipe_0(.clk(clk), .reset(reset), .D(D0), .Q(Q0));
  30. always_comb
  31. begin
  32. if (Q0[0][N-2:N-2-M] > Q0[1][N-2:N-2-M]) // If input 1 has the bigger exponent
  33. begin
  34. // Flags input a as larger and calculates the absolute difference
  35. Q0]4] = 1;
  36. Q0[5] = 0;
  37. Q0[6] = Q0[0][N-2:N-2-M] - Q0[1][N-2:N-2-M];
  38. // ASsigning overall sign of the output
  39. Q0[2][N-1] = Q0[0][N-1];
  40. // Sets output to have the same exponent
  41. Q0[2][N-2:N-2-M] = Q0[0][N-2:N-2-M];
  42. end
  43. else if (Q0[1][N-2:N-2-M] > Q0[0][N-2:N-2-M]) // If input 2 has the bigger exponent
  44. begin
  45. // Similarly flags input b as larger and calculates the absolute difference
  46. Q0[4] = 0;
  47. Q0[5] = 1;
  48. Q0[6] = Q0[1][N-2:N-2-M] - Q0[0][N-2:N-2-M];
  49. // ASsigning overall sign of the output
  50. Q0[2][N-1] = Q0[1][N-1];
  51. // Sets ouput to have the same exponent
  52. Q0[2][N-2:N-2-M] = Q0[1][N-2:N-2-M];
  53. end
  54. else
  55. begin
  56. // THe condition that both inputs have the same exponent
  57. Q0[4] = 1;
  58. Q0[5] = 1;
  59. Q0[6] = 0;
  60. // ASsigning overall sign of the output based on size of the mantissa
  61. if (Q0[0][N-3-M:0] >= Q0[1][N-3-M:0]) sum[N-1] = Q0[0][N-1];
  62. else Q0[2][N-1] = Q0[1][N-1];
  63. Q0[2][N-2:N-2-M] = Q0[0][N-2:N-2-M];
  64. end
  65. Q0[3] = Q0[6];
  66. end
  67. //Pipeline stage 1
  68. pipe pipe_1(.clk(clk), .reset(reset), .D(Q0), .Q(Q1));
  69. always_comb
  70. begin
  71. // Condition for overflow is that it sets the output to the larger input
  72. if (Q1[6] > N-M-2) // Because size of mantissa is 10 bits and shifting by 10 would give 0
  73. begin
  74. if (Q1[4] & ~Q1[5]) Q1[2] = Q1[0]; // input 1 is larger and is translated to output
  75. else if (~Q1[4] & Q1[5]) Q1[2] = Q1[1]; // input 2 is larger and is translated to output
  76. else // exponents are the same
  77. begin
  78. if (Q1[0][N-3-M:0] >= Q1[1][N-3-M:0]) Q1[2] = Q1[0];// input 1 has the bigger mantissa
  79. else Q1[2] = Q1[1]; // input 2 has the bigger mantissa
  80. end
  81. end
  82. else
  83. begin
  84. // Shifts the smaller input's mantissa to the right based on abs
  85. if (Q1[4] & ~Q1[5])// If input 1 has the larger exponent
  86. begin
  87. // If the signs of both inputs are the same you add, otherwise you subtract
  88. if (Q1[0][N-1] == Q1[1][N-1])
  89. begin
  90. Q1[7] = Q1[0][N-3-M:0] + (Q1[1][N-3-M:0] >> Q1[6]-1); // Sum the mantissa
  91. Q1[2][N-3-M:0] = Q1[7];
  92. end
  93. else
  94. begin
  95. Q1[7] = Q1[0][N-3-M:0] - (Q1[1][N-3-M:0] >> Q1[6]-1); // Subtract the mantissas
  96. Q1[2][N-3-M:0] = Q1[7];
  97. end
  98. end
  99. else if (~Q1[4] & Q1[5])
  100. begin
  101. // If the signs of both inputs are the same you add, otherwise you subtract
  102. if (Q1[0][N-1] == Q1[1][N-1])
  103. begin
  104. Q1[7] = (Q1[0][N-3-M:0] >> Q1[6]-1) + Q1[1][N-3-M:0]; // Sum the mantissa
  105. Q1[2][N-3-M:0] = Q1[7];
  106. end
  107. else
  108. begin
  109. Q1[7] = Q1[1][N-3-M:0] - (Q1[0][N-3-M:0] >> Q1[6]-1); // Subtract the mantissas
  110. Q1[2][N-3-M:0] = Q1[7];
  111. end
  112. end
  113. else
  114. begin
  115. if (Q1[0][N-1] == Q1[1][N-1]) // If exponents and signs equal
  116. begin
  117. Q1[7] = Q1[0][N-3-M:0] + Q1[1][N-3-M:0]; // Sum the mantissa
  118. Q1[2][N-3-M:0] = Q1[7];
  119. end
  120. else // In this case it will be a subtraction
  121. begin
  122. if (Q1[0][N-3-M:0] > Q1[1][N-3-M:0]) // Which has the larger mantissa
  123. begin
  124. Q1[7] = Q1[0][N-3-M:0] - Q1[1][N-3-M:0]; // Subtract the mantissa
  125. Q1[2][N-3-M:0] = Q1[7];
  126. end
  127. else if (Q1[0][N-3-M:0] < Q1[1][N-3-M:0])
  128. begin
  129. Q1[7] = Q1[1][N-3-M:0] - Q1[0][N-3-M:0]; // Subtract the mantissa
  130. Q1[2][N-3-M:0] = Q1[7];
  131. end
  132. else Q1[7] = 0; // Both the exponent and the mantissa are equal so subtraction leads to 0
  133. Q1[2][N-3-M:0] = Q1[7];
  134. end
  135. end
  136. end
  137. end
  138. //Final pipeline stage 2
  139. pipe pipe2(.clk(clk), .reset(reset), .D(Q1), .Q(Q2));
  140. assign sum = Q2[2];
  141. assign diff = Q2[3];
  142. endmodule : floating_add
  143. module floating_product #(parameter N=16, M=4)(input_1, input_2, product, clk, reset);
  144. input logic [N-1:0] input_1, input_2;
  145. input logic clk, reset;
  146. output logic [N-1:0] product;
  147. // sign_x = x[N-1]
  148. // exponent_x = x[N-2:N-2-M]
  149. // mantissa_x = x[N-3-M:0]
  150. // logic [N-2:N-2-M] sum;
  151. // logic [2*(N-3-M):0] mult;
  152. logic [2*(N-3-M):0] D0 [4:0];
  153. logic [2*(N-3-M):0] Q0 [4:0];
  154. logic [2*(N-3-M):0] Q1 [4:0];
  155. logic [2*(N-3-M):0] Q2 [4:0];
  156. // First pipeline stage 0
  157. assign D0[0] = input_1;
  158. assign D0[1] = input_2;
  159. assign D0[2] = 0; // product
  160. assign D0[3] = 0; // sum
  161. assign D0[4] = 0; // mult
  162. pipe pipe0 #(N = 32, K = 4)(.clk(clk), .reset(reset), .D(D0), .Q(Q0));
  163. // We have assigned an {M+1} bit exponent so we must have a 2^{M} offset
  164. assign Q0[3] = Q0[0][N-2:N-2-M] + Q0[1][N-2:N-2-M];
  165. assign Q0[2][N-2:N-2-M] = Q0[3] - (1'b1 << M) + 2;
  166. // Second pipeline stage 1
  167. pipe pipe1 #(N = 32, K = 4)(.clk(clk), .reset(reset), .D(Q0), .Q(Q1));
  168. always_comb
  169. begin
  170. // Setting the mantissa of the output
  171. Q1[4] = Q1[0][N-3-M:0] * Q1[1][N-3-M:0];
  172. if (Q1[4][N-3-M]) Q1[2][N-3-M:0] = Q1[4][2*(N-3-M):2*(N-3-M)-9];
  173. else Q1[2][N-3-M:0] = Q1[4][2*(N-3-M):2*(N-3-M)-9] << 1;
  174. Q1[2][N-1] = Q1[0][N-1] ^ Q1[1][N-1];
  175. end
  176. // Final Pipeline Stage 2
  177. pipe pipe2 #(N = 32, K = 4)(.clk(clk), .reset(reset), .D(Q1), .Q(Q2));
  178. assign product = Q2[2][N-1:0];
  179. endmodule : floating_product
  180. module pipe #(parameter N = 16, K = 7)pipe(clk, reset, D, Q);
  181. input logic clk, reset;
  182. input logic [N-1:0] D [K:0];
  183. output reg [N-1:0] Q [K:0];
  184. reg [N-1:0] in_pipe;
  185. always @(posedge clk or negedge reset)
  186. begin
  187. if(reset) in_pipe = 0;
  188. else in_pipe = D;
  189. end
  190. always @(posedge clk or negedge reset)
  191. begin
  192. if(reset) Q = 0;
  193. else Q = in_pipe;
  194. end
  195. endmodule : pipe
  196. module floating_tb;
  197. reg reset, clk;
  198. logic [15:0] input_a, input_b, result_add, result_mult;
  199. logic [4:0] diff;
  200. floating_add adder1(.input_1(input_a), .input_2(input_b), .sum(result_add), .diff(diff));
  201. floating_product multiplier1(.input_1(input_a), .input_2(input_b), .product(result_mult));
  202. reg [15:0] test_mem [29:0][3:0];
  203. initial $readmemh("../../scripts/fp16_test.hex", test_mem);
  204. initial begin
  205. static int num_err = 0;
  206. static int num_tests = $size(test_mem) * 2;
  207. for (int i=0; i < $size(test_mem); i++) begin
  208. input_a = test_mem[i][0];
  209. input_b = test_mem[i][1];
  210. #10;
  211. if(result_add != test_mem[i][2]) begin
  212. if(num_err < 20)
  213. $display("FAIL ADD: %H + %H = %H, expected %H", input_a, input_b, result_add, test_mem[i][2]);
  214. num_err = num_err + 1;
  215. end
  216. if(result_mult != test_mem[i][3]) begin
  217. if(num_err < 20)
  218. $display("FAIL MULTIPLY: %H + %H = %H, expected %H", input_a, input_b, result_mult, test_mem[i][3]);
  219. num_err = num_err + 1;
  220. end
  221. end
  222. $display("Passed %d of %d tests", num_tests-num_err, num_tests);
  223. $finish();
  224. end
  225. endmodule : floating_tb