Timing.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "Timing.h"
  2. #ifdef CORTEXM
  3. #define SYSTICK_INITIAL_VALUE 0xFFFFFF
  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 ARMv7A
  54. /* TODO */
  55. #else
  56. #define NOTIMING
  57. #endif
  58. #endif /* CORTEXM*/
  59. #if defined(CORTEXA) || defined(CORTEXR)
  60. #if !defined(__GNUC_PYTHON__)
  61. #include "cmsis_cp15.h"
  62. #else
  63. #if defined(__aarch64__)
  64. #include "timing_aarch64.h"
  65. #define AARCH64_TIMING
  66. #endif
  67. #endif
  68. #if defined(CORTEXA) && defined(AARCH64_TIMING)
  69. unsigned long long startCycles;
  70. #else
  71. unsigned int startCycles;
  72. #endif
  73. #define DO_RESET 1
  74. #define ENABLE_DIVIDER 0
  75. #endif
  76. #if defined(EXTBENCH) || defined(CACHEANALYSIS)
  77. unsigned long sectionCounter=0;
  78. #endif
  79. void initCycleMeasurement()
  80. {
  81. #if !defined(NOTIMING)
  82. #ifdef CORTEXM
  83. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  84. SysTick->VAL = 0;
  85. SysTick->CTRL = 0;
  86. #endif
  87. #if defined(CORTEXA) || defined(CORTEXR)
  88. #if !defined(AARCH64_TIMING)
  89. // in general enable all counters (including cycle counter)
  90. int32_t value = 1;
  91. // peform reset:
  92. if (DO_RESET)
  93. {
  94. value |= 2; // reset all counters to zero.
  95. value |= 4; // reset cycle counter to zero.
  96. }
  97. if (ENABLE_DIVIDER)
  98. value |= 8; // enable "by 64" divider for CCNT.
  99. //value |= 16;
  100. // program the performance-counter control-register:
  101. __set_CP(15, 0, value, 9, 12, 0);
  102. // enable all counters:
  103. __set_CP(15, 0, 0x8000000f, 9, 12, 1);
  104. // clear overflows:
  105. __set_CP(15, 0, 0x8000000f, 9, 12, 3);
  106. #if defined(ARMCR52)
  107. __get_CP(15, 0, value, 14, 15, 7);
  108. value = value | (0x8000 << 12);
  109. __set_CP(15, 0, value, 14, 15, 7);
  110. #endif
  111. #else
  112. enable_timing();
  113. #endif
  114. #endif
  115. #endif
  116. }
  117. void cycleMeasurementStart()
  118. {
  119. #if !defined(NOTIMING)
  120. #ifndef EXTBENCH
  121. #ifdef CORTEXM
  122. SysTick->CTRL = 0;
  123. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  124. SysTick->VAL = 0;
  125. SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
  126. while(SysTick->VAL == 0);
  127. startCycles = SysTick->VAL;
  128. #endif
  129. #if (defined(CORTEXA) || defined(CORTEXR))
  130. #if !defined(AARCH64_TIMING)
  131. unsigned int value;
  132. // Read CCNT Register
  133. __get_CP(15, 0, value, 9, 13, 0);
  134. startCycles = value;
  135. #else
  136. startCycles = readCCNT();
  137. #endif
  138. #endif
  139. #endif
  140. #endif
  141. }
  142. void cycleMeasurementStop()
  143. {
  144. #if !defined(NOTIMING)
  145. #ifndef EXTBENCH
  146. #ifdef CORTEXM
  147. SysTick->CTRL = 0;
  148. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  149. #endif
  150. #endif
  151. #endif
  152. }
  153. Testing::cycles_t getCycles()
  154. {
  155. #if defined(NOTIMING)
  156. return(0);
  157. #else
  158. #ifdef CORTEXM
  159. #if defined(NORMALFVP)
  160. return(0);
  161. #else
  162. uint32_t v = SysTick->VAL;
  163. Testing::cycles_t result;
  164. if (v < startCycles)
  165. {
  166. result = startCycles - v;
  167. }
  168. else
  169. {
  170. result = SYSTICK_INITIAL_VALUE - (v - startCycles);
  171. }
  172. /* SysTick tested and tuned on IPSS.
  173. On other FVP, the value is forced to 0
  174. because measurement is wrong.
  175. */
  176. return(result);
  177. #endif
  178. #endif
  179. #if (defined(CORTEXA) || defined(CORTEXR))
  180. #if !defined(AARCH64_TIMING)
  181. unsigned int value;
  182. // Read CCNT Register
  183. __get_CP(15, 0, value, 9, 13, 0);
  184. return(value - startCycles);
  185. #else
  186. unsigned long long value;
  187. value = readCCNT();
  188. return((Testing::cycles_t)(value - startCycles));
  189. #endif
  190. #endif
  191. #endif
  192. }