Timing.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "Timing.h"
  2. #ifdef CORTEXM
  3. #define SYSTICK_INITIAL_VALUE 0x0FFFFFF
  4. static uint32_t startCycles=0;
  5. #if defined ARMCM0
  6. #include "ARMCM0.h"
  7. #elif defined ARMCM0P
  8. #include "ARMCM0plus.h"
  9. #elif defined ARMCM0P_MPU
  10. #include "ARMCM0plus_MPU.h"
  11. #elif defined ARMCM3
  12. #include "ARMCM3.h"
  13. #elif defined ARMCM4
  14. #include "ARMCM4.h"
  15. #elif defined ARMCM4_FP
  16. #include "ARMCM4_FP.h"
  17. #elif defined ARMCM7
  18. #include "ARMCM7.h"
  19. #elif defined ARMCM7_SP
  20. #include "ARMCM7_SP.h"
  21. #elif defined ARMCM7_DP
  22. #include "ARMCM7_DP.h"
  23. #elif defined (ARMCM33)
  24. #include "ARMCM33.h"
  25. #elif defined (ARMCM33_DSP_FP)
  26. #include "ARMCM33_DSP_FP.h"
  27. #elif defined (ARMCM33_DSP_FP_TZ)
  28. #include "ARMCM33_DSP_FP_TZ.h"
  29. #elif defined ARMSC000
  30. #include "ARMSC000.h"
  31. #elif defined ARMSC300
  32. #include "ARMSC300.h"
  33. #elif defined ARMv8MBL
  34. #include "ARMv8MBL.h"
  35. #elif defined ARMv8MML
  36. #include "ARMv8MML.h"
  37. #elif defined ARMv8MML_DSP
  38. #include "ARMv8MML_DSP.h"
  39. #elif defined ARMv8MML_SP
  40. #include "ARMv8MML_SP.h"
  41. #elif defined ARMv8MML_DSP_SP
  42. #include "ARMv8MML_DSP_SP.h"
  43. #elif defined ARMv8MML_DP
  44. #include "ARMv8MML_DP.h"
  45. #elif defined ARMv8MML_DSP_DP
  46. #include "ARMv8MML_DSP_DP.h"
  47. #elif defined ARMv81MML_DSP_DP_MVE_FP
  48. #include "ARMv81MML_DSP_DP_MVE_FP.h"
  49. #elif defined ARMCM55
  50. #include "ARMCM55.h"
  51. #elif defined ARMCM85
  52. #include "ARMCM85.h"
  53. #elif defined SSE300MPS3
  54. #include "SSE300MPS3.h"
  55. #elif defined ARMv7A
  56. /* TODO */
  57. #else
  58. #define NOTIMING
  59. #endif
  60. #endif /* CORTEXM*/
  61. #if defined(CORTEXA) || defined(CORTEXR)
  62. #if !defined(__GNUC_PYTHON__)
  63. #include "cmsis_cp15.h"
  64. #else
  65. #if defined(__aarch64__)
  66. #include "timing_aarch64.h"
  67. #define AARCH64_TIMING
  68. #endif
  69. #endif
  70. #if defined(CORTEXA) && defined(AARCH64_TIMING)
  71. unsigned long long startCycles;
  72. #else
  73. unsigned int startCycles;
  74. #endif
  75. #define DO_RESET 1
  76. #define ENABLE_DIVIDER 0
  77. #endif
  78. #if defined(EXTBENCH) || defined(CACHEANALYSIS)
  79. unsigned long sectionCounter=0;
  80. #endif
  81. void initCycleMeasurement()
  82. {
  83. #if !defined(NOTIMING)
  84. #ifdef CORTEXM
  85. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  86. SysTick->VAL = 0;
  87. SysTick->CTRL = 0;
  88. #endif
  89. #if defined(CORTEXA) || defined(CORTEXR)
  90. #if !defined(AARCH64_TIMING)
  91. // in general enable all counters (including cycle counter)
  92. int32_t value = 1;
  93. // peform reset:
  94. if (DO_RESET)
  95. {
  96. value |= 2; // reset all counters to zero.
  97. value |= 4; // reset cycle counter to zero.
  98. }
  99. if (ENABLE_DIVIDER)
  100. value |= 8; // enable "by 64" divider for CCNT.
  101. //value |= 16;
  102. // program the performance-counter control-register:
  103. __set_CP(15, 0, value, 9, 12, 0);
  104. // enable all counters:
  105. __set_CP(15, 0, 0x8000000f, 9, 12, 1);
  106. // clear overflows:
  107. __set_CP(15, 0, 0x8000000f, 9, 12, 3);
  108. #if defined(ARMCR52)
  109. __get_CP(15, 0, value, 14, 15, 7);
  110. value = value | (0x8000 << 12);
  111. __set_CP(15, 0, value, 14, 15, 7);
  112. #endif
  113. #else
  114. enable_timing();
  115. #endif
  116. #endif
  117. #endif
  118. }
  119. void cycleMeasurementStart()
  120. {
  121. #if !defined(NOTIMING)
  122. #ifndef EXTBENCH
  123. #ifdef CORTEXM
  124. SysTick->CTRL = 0;
  125. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  126. SysTick->VAL = 0;
  127. SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
  128. startCycles = SysTick->VAL;
  129. #endif
  130. #if (defined(CORTEXA) || defined(CORTEXR))
  131. #if !defined(AARCH64_TIMING)
  132. unsigned int value;
  133. // Read CCNT Register
  134. __get_CP(15, 0, value, 9, 13, 0);
  135. startCycles = value;
  136. #else
  137. startCycles = readCCNT();
  138. #endif
  139. #endif
  140. #endif
  141. #endif
  142. }
  143. void cycleMeasurementStop()
  144. {
  145. #if !defined(NOTIMING)
  146. #ifndef EXTBENCH
  147. #ifdef CORTEXM
  148. SysTick->CTRL = 0;
  149. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  150. #endif
  151. #endif
  152. #endif
  153. }
  154. Testing::cycles_t getCycles()
  155. {
  156. #if defined(NOTIMING)
  157. return(0);
  158. #else
  159. #ifdef CORTEXM
  160. #if defined(NORMALFVP)
  161. return(0);
  162. #else
  163. uint32_t v = SysTick->VAL;
  164. int32_t result;
  165. result = (int32_t)startCycles - (int32_t)v;
  166. if (result < 0)
  167. {
  168. result += SYSTICK_INITIAL_VALUE;
  169. }
  170. /* SysTick tested and tuned on IPSS.
  171. On other FVP, the value is forced to 0
  172. because measurement is wrong.
  173. */
  174. return(result);
  175. #endif
  176. #endif
  177. #if (defined(CORTEXA) || defined(CORTEXR))
  178. #if !defined(AARCH64_TIMING)
  179. unsigned int value;
  180. // Read CCNT Register
  181. __get_CP(15, 0, value, 9, 13, 0);
  182. return(value - startCycles);
  183. #else
  184. unsigned long long value;
  185. value = readCCNT();
  186. return((Testing::cycles_t)(value - startCycles));
  187. #endif
  188. #endif
  189. #endif
  190. }