testgen.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdint.h>
  5. #include <time.h>
  6. #include <arpa/inet.h>
  7. int decimal_to_binary(char* p, int32_t n)
  8. {
  9. int32_t c, d, t;
  10. t = 0;
  11. if (p == NULL)
  12. exit(EXIT_FAILURE);
  13. for (c = 31 ; c >= 0 ; c--)
  14. {
  15. d = n >> c;
  16. if (d & 1)
  17. *(p+t) = 1 + '0';
  18. else
  19. *(p+t) = 0 + '0';
  20. t++;
  21. }
  22. *(p+t) = '\n';
  23. *(p+t+1) = '\0';
  24. return 0;
  25. }
  26. int unsigned_decimal_to_binary(char* p, uint32_t n)
  27. {
  28. int c;
  29. uint32_t d, t;
  30. t = 0;
  31. if (p == NULL)
  32. exit(EXIT_FAILURE);
  33. for (c = 31 ; c >= 0 ; c--)
  34. {
  35. d = n >> c;
  36. if (d & 1)
  37. *(p+t) = 1 + '0';
  38. else
  39. *(p+t) = 0 + '0';
  40. t++;
  41. }
  42. *(p+t) = '\n';
  43. *(p+t+1) = '\0';
  44. return 0;
  45. }
  46. int short_decimal_to_binary(char* p, uint16_t n)
  47. {
  48. int c;
  49. uint16_t d, t;
  50. t = 0;
  51. if (p == NULL)
  52. exit(EXIT_FAILURE);
  53. for (c = 15 ; c >= 0 ; c--)
  54. {
  55. d = n >> c;
  56. if (d & 1)
  57. *(p+t) = 1 + '0';
  58. else
  59. *(p+t) = 0 + '0';
  60. t++;
  61. }
  62. *(p+t) = '\n';
  63. *(p+t+1) = '\0';
  64. return 0;
  65. }
  66. int main (int argc, char ** argv) {
  67. int i = 0;
  68. int32_t r;
  69. uint16_t s, old_s;
  70. FILE* output;
  71. FILE* input;
  72. char str[40];
  73. output = fopen("./testdata/output.txt", "w");
  74. input = fopen("./testdata/input.txt", "w");
  75. srand(time(NULL));
  76. sprintf(str, "%d\n", i);
  77. fputs(str, input);
  78. fputs(str, output);
  79. fputs("0\n0\n", input);
  80. short_decimal_to_binary(str, (uint16_t) i << 2);
  81. fputs(str, output);
  82. i = 1;
  83. unsigned_decimal_to_binary(str, htonl(i));
  84. fputs(str, output);
  85. fputs("0\n", input); //pc_src
  86. short_decimal_to_binary(str, 0);
  87. fputs(str, input); //pc_in
  88. fputs("\n", output);
  89. fputs("\n", input);
  90. for (i = 1; i < 50; i++) { //random inputs without branch
  91. sprintf(str, "%d\n", i);
  92. fputs(str, input);
  93. fputs(str, output);
  94. r = (int32_t) rand() % 5;
  95. if (r == 1) {
  96. fputs("1\n0\n", input); // stall case
  97. short_decimal_to_binary(str, (uint16_t) i << 2);
  98. fputs(str, output);
  99. unsigned_decimal_to_binary(str, htonl((int32_t)i));
  100. fputs(str, output);
  101. i--;
  102. }
  103. else if (r == 2) {
  104. fputs("0\n1\n", input); // flush case
  105. short_decimal_to_binary(str, (uint16_t) i << 2);
  106. fputs(str, output);
  107. unsigned_decimal_to_binary(str, 0x00000013);
  108. fputs(str, output);
  109. }
  110. else {
  111. fputs("0\n0\n", input);
  112. short_decimal_to_binary(str, (uint16_t) i << 2);
  113. fputs(str, output);
  114. unsigned_decimal_to_binary(str, htonl((int32_t)i));
  115. fputs(str, output);
  116. }
  117. fputs("0\n", input); //pc_src
  118. short_decimal_to_binary(str, 0);
  119. fputs(str, input); //pc_in
  120. fputs("\n", output);
  121. fputs("\n", input);
  122. }
  123. sprintf(str, "%d\n", i);
  124. fputs(str, input);
  125. fputs(str, output);
  126. fputs("0\n0\n", input);
  127. short_decimal_to_binary(str, (uint16_t) i << 2);
  128. fputs(str, output);
  129. unsigned_decimal_to_binary(str, htonl((int32_t)i));
  130. fputs(str, output);
  131. s = ((uint16_t) rand() % 10) << 2;
  132. fputs("1\n", input); //pc_src
  133. short_decimal_to_binary(str, s);
  134. fputs(str, input); //pc_in
  135. fputs("\n", output);
  136. fputs("\n", input);
  137. for (i = 0; i < 50; i++) {
  138. r = (int32_t) rand() % 5;
  139. sprintf(str, "%d\n", i);
  140. fputs(str, input);
  141. fputs(str, output);
  142. if (r == 1) {
  143. old_s = s;
  144. fputs("1\n0\n", input); // stall case
  145. short_decimal_to_binary(str, (uint16_t) s);
  146. fputs(str, output);
  147. unsigned_decimal_to_binary(str, htonl((int32_t)s >> 2));
  148. fputs(str, output);
  149. }
  150. else if (r == 2) {
  151. old_s = s;
  152. fputs("0\n1\n", input); // flush case
  153. short_decimal_to_binary(str, (uint16_t) s);
  154. fputs(str, output);
  155. unsigned_decimal_to_binary(str, 0x00000013);
  156. fputs(str, output);
  157. }
  158. else {
  159. fputs("0\n0\n", input);
  160. short_decimal_to_binary(str, (uint16_t) s);
  161. fputs(str, output);
  162. unsigned_decimal_to_binary(str, htonl((int32_t)s >> 2));
  163. fputs(str, output);
  164. }
  165. s = ((uint16_t) rand() % 10) << 2;
  166. fputs("1\n", input); //pc_src
  167. short_decimal_to_binary(str, s);
  168. fputs(str, input); //pc_in
  169. if (r == 1) s = old_s;
  170. if (i != 49) {
  171. fputs("\n", output);
  172. fputs("\n", input);
  173. }
  174. }
  175. fclose(output);
  176. fclose(input);
  177. }