core_util.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Author : Shay Gal-On, EEMBC
  3. This file is part of EEMBC(R) and CoreMark(TM), which are Copyright (C) 2009
  4. All rights reserved.
  5. EEMBC CoreMark Software is a product of EEMBC and is provided under the terms of the
  6. CoreMark License that is distributed with the official EEMBC COREMARK Software release.
  7. If you received this EEMBC CoreMark Software without the accompanying CoreMark License,
  8. you must discontinue use and download the official release from www.coremark.org.
  9. Also, if you are publicly displaying scores generated from the EEMBC CoreMark software,
  10. make sure that you are in compliance with Run and Reporting rules specified in the accompanying readme.txt file.
  11. EEMBC
  12. 4354 Town Center Blvd. Suite 114-200
  13. El Dorado Hills, CA, 95762
  14. */
  15. #include "coremark.h"
  16. /* Function: get_seed
  17. Get a values that cannot be determined at compile time.
  18. Since different embedded systems and compilers are used, 3 different methods are provided:
  19. 1 - Using a volatile variable. This method is only valid if the compiler is forced to generate code that
  20. reads the value of a volatile variable from memory at run time.
  21. Please note, if using this method, you would need to modify core_portme.c to generate training profile.
  22. 2 - Command line arguments. This is the preferred method if command line arguments are supported.
  23. 3 - System function. If none of the first 2 methods is available on the platform,
  24. a system function which is not a stub can be used.
  25. e.g. read the value on GPIO pins connected to switches, or invoke special simulator functions.
  26. */
  27. #if (SEED_METHOD==SEED_VOLATILE)
  28. extern volatile ee_s32 seed1_volatile;
  29. extern volatile ee_s32 seed2_volatile;
  30. extern volatile ee_s32 seed3_volatile;
  31. extern volatile ee_s32 seed4_volatile;
  32. extern volatile ee_s32 seed5_volatile;
  33. ee_s32 get_seed_32(int i)
  34. {
  35. ee_s32 retval;
  36. switch (i) {
  37. case 1:
  38. retval = seed1_volatile;
  39. break;
  40. case 2:
  41. retval = seed2_volatile;
  42. break;
  43. case 3:
  44. retval = seed3_volatile;
  45. break;
  46. case 4:
  47. retval = seed4_volatile;
  48. break;
  49. case 5:
  50. retval = seed5_volatile;
  51. break;
  52. default:
  53. retval = 0;
  54. break;
  55. }
  56. return retval;
  57. }
  58. #elif (SEED_METHOD==SEED_ARG)
  59. ee_s32 parseval(char* valstring)
  60. {
  61. ee_s32 retval = 0;
  62. ee_s32 neg = 1;
  63. int hexmode = 0;
  64. if (*valstring == '-') {
  65. neg = -1;
  66. valstring++;
  67. }
  68. if ((valstring[0] == '0') && (valstring[1] == 'x')) {
  69. hexmode = 1;
  70. valstring += 2;
  71. }
  72. /* first look for digits */
  73. if (hexmode) {
  74. while (((*valstring >= '0') && (*valstring <= '9')) || ((*valstring >= 'a') && (*valstring <= 'f'))) {
  75. ee_s32 digit = *valstring - '0';
  76. if (digit > 9) {
  77. digit = 10 + *valstring - 'a';
  78. }
  79. retval *= 16;
  80. retval += digit;
  81. valstring++;
  82. }
  83. } else {
  84. while ((*valstring >= '0') && (*valstring <= '9')) {
  85. ee_s32 digit = *valstring - '0';
  86. retval *= 10;
  87. retval += digit;
  88. valstring++;
  89. }
  90. }
  91. /* now add qualifiers */
  92. if (*valstring == 'K') {
  93. retval *= 1024;
  94. }
  95. if (*valstring == 'M') {
  96. retval *= 1024 * 1024;
  97. }
  98. retval *= neg;
  99. return retval;
  100. }
  101. ee_s32 get_seed_args(int i, int argc, char* argv[])
  102. {
  103. if (argc > i) {
  104. return parseval(argv[i]);
  105. }
  106. return 0;
  107. }
  108. #elif (SEED_METHOD==SEED_FUNC)
  109. /* If using OS based function, you must define and implement the functions below in core_portme.h and core_portme.c ! */
  110. ee_s32 get_seed_32(int i)
  111. {
  112. ee_s32 retval;
  113. switch (i) {
  114. case 1:
  115. retval = portme_sys1();
  116. break;
  117. case 2:
  118. retval = portme_sys2();
  119. break;
  120. case 3:
  121. retval = portme_sys3();
  122. break;
  123. case 4:
  124. retval = portme_sys4();
  125. break;
  126. case 5:
  127. retval = portme_sys5();
  128. break;
  129. default:
  130. retval = 0;
  131. break;
  132. }
  133. return retval;
  134. }
  135. #endif
  136. /* Function: crc*
  137. Service functions to calculate 16b CRC code.
  138. */
  139. ee_u16 crcu8(ee_u8 data, ee_u16 crc)
  140. {
  141. ee_u8 i = 0, x16 = 0, carry = 0;
  142. for (i = 0; i < 8; i++) {
  143. x16 = (ee_u8)((data & 1) ^ ((ee_u8)crc & 1));
  144. data >>= 1;
  145. if (x16 == 1) {
  146. crc ^= 0x4002;
  147. carry = 1;
  148. } else {
  149. carry = 0;
  150. }
  151. crc >>= 1;
  152. if (carry) {
  153. crc |= 0x8000;
  154. } else {
  155. crc &= 0x7fff;
  156. }
  157. }
  158. return crc;
  159. }
  160. #if (defined(__riscv_zicond) && !defined(__clang__))
  161. #pragma GCC push_options
  162. #pragma GCC optimize("-fcode-hoisting")
  163. #pragma GCC optimize("-ftree-vrp")
  164. #endif
  165. ee_u16 crcu16(ee_u16 newval, ee_u16 crc)
  166. {
  167. crc = crcu8((ee_u8)(newval), crc);
  168. crc = crcu8((ee_u8)((newval) >> 8), crc);
  169. return crc;
  170. }
  171. #if (defined(__riscv_zicond) && !defined(__clang__))
  172. #pragma GCC pop_options
  173. #endif
  174. ee_u16 crcu32(ee_u32 newval, ee_u16 crc)
  175. {
  176. crc = crc16((ee_s16) newval, crc);
  177. crc = crc16((ee_s16)(newval >> 16), crc);
  178. return crc;
  179. }
  180. #if (defined(__riscv_zicond) && !defined(__clang__))
  181. #pragma GCC push_options
  182. #pragma GCC optimize("-fcode-hoisting")
  183. #pragma GCC optimize("-ftree-vrp")
  184. #endif
  185. ee_u16 crc16(ee_s16 newval, ee_u16 crc)
  186. {
  187. return crcu16((ee_u16)newval, crc);
  188. }
  189. #if (defined(__riscv_zicond) && !defined(__clang__))
  190. #pragma GCC pop_options
  191. #endif
  192. ee_u8 check_data_types()
  193. {
  194. ee_u8 retval = 0;
  195. if (sizeof(ee_u8) != 1) {
  196. ee_printf("ERROR: ee_u8 is not an 8b datatype!\n");
  197. retval++;
  198. }
  199. if (sizeof(ee_u16) != 2) {
  200. ee_printf("ERROR: ee_u16 is not a 16b datatype!\n");
  201. retval++;
  202. }
  203. if (sizeof(ee_s16) != 2) {
  204. ee_printf("ERROR: ee_s16 is not a 16b datatype!\n");
  205. retval++;
  206. }
  207. if (sizeof(ee_s32) != 4) {
  208. ee_printf("ERROR: ee_s32 is not a 32b datatype!\n");
  209. retval++;
  210. }
  211. if (sizeof(ee_u32) != 4) {
  212. ee_printf("ERROR: ee_u32 is not a 32b datatype!\n");
  213. retval++;
  214. }
  215. if (sizeof(ee_ptr_int) != sizeof(int*)) {
  216. ee_printf("ERROR: ee_ptr_int is not a datatype that holds an int pointer!\n");
  217. retval++;
  218. }
  219. if (retval > 0) {
  220. ee_printf("ERROR: Please modify the datatypes in core_portme.h!\n");
  221. }
  222. return retval;
  223. }