bench.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Benchmarking tool
  2. #define word char
  3. #include "lib/math.c"
  4. #include "lib/stdlib.c"
  5. #include "lib/sys.c"
  6. #define N 256 // Decimals of pi to compute.
  7. #define LEN 854 // (10*N) / 3 + 1
  8. #define TMPMEM_LOCATION 0x440000
  9. word frameCount = 0;
  10. //word a[LEN];
  11. word *a = (char*) TMPMEM_LOCATION;
  12. void spigotPiBench()
  13. {
  14. frameCount = 0;
  15. while (frameCount == 0); // wait until next frame to start
  16. frameCount = 0;
  17. word j = 0;
  18. word predigit = 0;
  19. word nines = 0;
  20. word x = 0;
  21. word q = 0;
  22. word k = 0;
  23. word len = 0;
  24. word i = 0;
  25. word y = 0;
  26. for(j=N; j; )
  27. {
  28. q = 0;
  29. k = LEN+LEN-1;
  30. for(i=LEN; i; --i)
  31. {
  32. if (j == N)
  33. {
  34. x = 20 + q*i;
  35. }
  36. else
  37. {
  38. x = (10*a[i-1]) + q*i;
  39. }
  40. q = MATH_div(x, k);
  41. a[i-1] = (x-q*k);
  42. k -= 2;
  43. }
  44. k = MATH_mod(x, 10);
  45. if (k==9)
  46. {
  47. ++nines;
  48. }
  49. else
  50. {
  51. if (j)
  52. {
  53. --j;
  54. y = predigit+MATH_div(x,10);
  55. bdos_printdec(y);
  56. }
  57. for(; nines; --nines)
  58. {
  59. if (j)
  60. {
  61. --j;
  62. if (x >= 10)
  63. {
  64. bdos_printc('0');
  65. }
  66. else
  67. {
  68. bdos_printc('9');
  69. }
  70. }
  71. }
  72. predigit = k;
  73. }
  74. }
  75. bdos_print("\nPiBench256 took ");
  76. bdos_printdec(frameCount);
  77. bdos_print(" frames\n");
  78. }
  79. // LoopBench: a simple increase and loop bench for a set amount of time.
  80. // reads framecount from memory in the loop.
  81. // no pipeline clears within the loop.
  82. int loopBench()
  83. {
  84. word retval = 0;
  85. asm(
  86. "push r1\npush r2\npush r3\npush r4\n"
  87. "addr2reg frameCount r2\n"
  88. "write 0 r2 r0 ; reset frameCount\n"
  89. "load 0 r4 ; score\n"
  90. "Label_ASM_Loop:\n"
  91. "read 0 r2 r3 ; read frameCount\n"
  92. "slt r3 300 r3 ; TESTDURATION here in frames\n"
  93. "beq r3 r0 3 ; check if done \n"
  94. "add r4 1 r4 ; increase score and loop\n"
  95. "jump Label_ASM_Loop\n"
  96. "jump Label_ASM_Done\n"
  97. "Label_ASM_Done:\n"
  98. "or r4 r0 r2 ; set return value\n"
  99. "write -4 r14 r2 ; write to stack to return\n"
  100. "pop r4\npop r3\npop r2\npop r1\n"
  101. );
  102. return retval;
  103. }
  104. // CountMillionBench: how many frames it takes to do a C for loop to a million
  105. int countMillionBench()
  106. {
  107. frameCount = 0;
  108. while (frameCount == 0); // wait until next frame to start
  109. frameCount = 0;
  110. int i;
  111. for (i = 0; i < 1000000; i++);
  112. return frameCount;
  113. }
  114. int main()
  115. {
  116. bdos_println("---------------FPGCbench---------------\n");
  117. bdos_print("LoopBench: ");
  118. frameCount = 0;
  119. while (frameCount == 0); // wait until next frame to start
  120. bdos_printdec(loopBench());
  121. bdos_printc('\n');
  122. bdos_print("\nCountMillionBench: ");
  123. bdos_printdec(countMillionBench());
  124. bdos_print(" frames\n");
  125. bdos_print("\nPiBench256:\n");
  126. spigotPiBench();
  127. return 'q';
  128. }
  129. void interrupt()
  130. {
  131. // Handle all interrupts
  132. word i = get_int_id();
  133. switch(i)
  134. {
  135. case INTID_TIMER1:
  136. timer1Value = 1; // Notify ending of timer1
  137. break;
  138. case INTID_GPU:
  139. frameCount++;
  140. break;
  141. }
  142. }