FPA_module_test.sv 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. //First pipeline stage
  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. D0[5] = 0; // flag_b
  26. D0[6] = 0; // abs
  27. D0[7] = 0; // res
  28. end
  29. pipe#(.N(N-1), .K(7)) pipe0(.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]) Q0[2][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. //Second pipeline stage 1
  68. pipe#(.N(N-1), .K(7)) pipe1(.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-2-M) // 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[6][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
  139. pipe#(.N(N-1), .K(7)) 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);
  144. input logic [N-1:0] input_1, input_2;
  145. output logic [N-1:0] product;
  146. // sign_x = x[N-1]
  147. // exponent_x = x[N-2:N-2-M]
  148. // mantissa_x = x[N-3-M:0]
  149. // logic [N-2:N-2-M] sum;
  150. // logic [2*(N-3-M):0] mult;
  151. logic [2*(N-3-M):0] D0 [4:0];
  152. logic [2*(N-3-M):0] Q0 [4:0];
  153. logic [2*(N-3-M):0] Q1 [4:0];
  154. logic [2*(N-3-M):0] Q2 [4:0];
  155. //First pipeline stage
  156. always_comb
  157. begin
  158. D0[0] = input_1;
  159. D0[1] = input_2;
  160. D0[2] = 0; // product
  161. D0[3] = 0; // sum
  162. D0[4] = 0; // mult
  163. end
  164. pipe#(.N(2*N-3-M), .K(4)) pipe0(.clk(clk), .reset(reset), .D(D0), .Q(Q0));
  165. // We have assigned an {M+1} bit exponent so we must have a 2^{M} offset
  166. assign Q0[3] = Q0[0][N-2:N-2-M] + Q0[1][N-2:N-2-M];
  167. assign Q0[2][N-2:N-2-M] = Q0[3] - (1'b1 << M) + 2;
  168. //Second Pipeline stage
  169. pipe#(.N(2*N-3-M), .K(4)) pipe1(.clk(clk), .reset(reset), .D(Q0), .Q(Q1));
  170. always_comb
  171. begin
  172. // Setting the mantissa of the output
  173. Q1[4] = Q1[0][N-3-M:0] * Q1[1][N-3-M:0];
  174. if (Q1[4][N-3-M]) Q0[2][N-3-M:0] = Q1[4][2*(N-3-M):2*(N-3-M)-9];
  175. else Q1[2][N-3-M:0] = Q1[4][2*(N-3-M):2*(N-3-M)-9] << 1;
  176. Q1[2][N-1] = Q1[0][N-1] ^ Q1[1][N-1];
  177. end
  178. //Final Pipeline stage
  179. pipe#(.N(2*N-3-M), .K(4)) pipe2(.clk(clk), .reset(reset), .D(Q1), .Q(Q2));
  180. assign product = Q2[4][N-1:0];
  181. endmodule : floating_product
  182. module pipe #(parameter N, K)(clk, reset, Q, D);
  183. input logic clk, reset;
  184. input reg [N:0] D [K:0];
  185. output reg [N:0] Q [K:0];
  186. logic [N:0] in_pipe [K:0];
  187. always_ff @(posedge clk)
  188. begin
  189. if(reset)
  190. begin
  191. in_pipe <= 0;
  192. Q <= 0;
  193. end
  194. else
  195. begin
  196. in_pipe <= D;
  197. Q <= in_pipe;
  198. end
  199. end
  200. endmodule : pipe
  201. module floating_tb;
  202. reg reset, clk;
  203. logic [15:0] input_a, input_b, result_add, result_mult;
  204. logic [4:0] diff;
  205. floating_add adder1(.input_1(input_a), .input_2(input_b), .sum(result_add), .diff(diff), .clk(clk), .reset(reset));
  206. floating_product multiplier1(.input_1(input_a), .input_2(input_b), .product(result_mult), .clk(clk), .reset(reset));
  207. reg [15:0] test_mem [29:0][3:0];
  208. initial $readmemh("../../scripts/fp16_test.hex", test_mem);
  209. initial begin
  210. static int num_err = 0;
  211. static int num_tests = $size(test_mem) * 2;
  212. for (int i=0; i < $size(test_mem); i++) begin
  213. input_a = test_mem[i][0];
  214. input_b = test_mem[i][1];
  215. #10;
  216. if(result_add != test_mem[i][2]) begin
  217. if(num_err < 20)
  218. $display("FAIL ADD: %H + %H = %H, expected %H", input_a, input_b, result_add, test_mem[i][2]);
  219. num_err = num_err + 1;
  220. end
  221. if(result_mult != test_mem[i][3]) begin
  222. if(num_err < 20)
  223. $display("FAIL MULTIPLY: %H + %H = %H, expected %H", input_a, input_b, result_mult, test_mem[i][3]);
  224. num_err = num_err + 1;
  225. end
  226. end
  227. $display("Passed %d of %d tests", num_tests-num_err, num_tests);
  228. $finish();
  229. end
  230. endmodule : floating_tb
  231. module floating32_tb;
  232. reg reset, clk;
  233. logic [31:0] input_a, input_b, result_add, result_mult;
  234. floating_add#(.N(32), .M(8)) add0(
  235. .input_1(input_a), .input_2(input_b), .sum(result_add), .diff()
  236. );
  237. floating_product#(.N(32), .M(8)) mult0(
  238. .input_1(input_a), .input_2(input_b), .product(result_mult)
  239. );
  240. reg [31:0] test_mem [29:0][3:0];
  241. initial $readmemh("scripts/fp32_test.hex", test_mem);
  242. initial begin
  243. static int num_err = 0;
  244. static int num_tests = $size(test_mem) * 2;
  245. for (int i=0; i < $size(test_mem); i++) begin
  246. input_a = test_mem[i][0];
  247. input_b = test_mem[i][1];
  248. #10;
  249. if(result_add != test_mem[i][2]) begin
  250. if(num_err < 20)
  251. $display("FAIL ADD: %H + %H = %H, expected %H", input_a, input_b, result_add, test_mem[i][2]);
  252. num_err = num_err + 1;
  253. end
  254. if(result_mult != test_mem[i][3]) begin
  255. if(num_err < 20)
  256. $display("FAIL MULTIPLY: %H + %H = %H, expected %H", input_a, input_b, result_mult, test_mem[i][3]);
  257. num_err = num_err + 1;
  258. end
  259. end
  260. $display("Passed %d of %d tests", num_tests-num_err, num_tests);
  261. $finish();
  262. end
  263. endmodule : floating32_tb