csi_instr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file csi_instr.h
  18. * @brief CSI Header File for instruct.
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #ifndef _CSI_INSTR_H_
  23. #define _CSI_INSTR_H_
  24. #define __CSI_GCC_OUT_REG(r) "=r" (r)
  25. #define __CSI_GCC_USE_REG(r) "r" (r)
  26. /**
  27. \brief No Operation
  28. \details No Operation does nothing. This instruction can be used for code alignment purposes.
  29. */
  30. __ALWAYS_INLINE void __NOP(void)
  31. {
  32. __ASM volatile("nop");
  33. }
  34. /**
  35. \brief Wait For Interrupt
  36. \details Wait For Interrupt is a hint instruction that suspends execution until one of a number of events occurs.
  37. */
  38. __ALWAYS_INLINE void __WFI(void)
  39. {
  40. __ASM volatile("wait");
  41. }
  42. /**
  43. \brief Wait For Interrupt
  44. \details Wait For Interrupt is a hint instruction that suspends execution until one interrupt occurs.
  45. */
  46. __ALWAYS_INLINE void __WAIT(void)
  47. {
  48. __ASM volatile("wait");
  49. }
  50. /**
  51. \brief Doze For Interrupt
  52. \details Doze For Interrupt is a hint instruction that suspends execution until one interrupt occurs.
  53. */
  54. __ALWAYS_INLINE void __DOZE(void)
  55. {
  56. __ASM volatile("doze");
  57. }
  58. /**
  59. \brief Stop For Interrupt
  60. \details Stop For Interrupt is a hint instruction that suspends execution until one interrupt occurs.
  61. */
  62. __ALWAYS_INLINE void __STOP(void)
  63. {
  64. __ASM volatile("stop");
  65. }
  66. /**
  67. \brief Instruction Synchronization Barrier
  68. \details Instruction Synchronization Barrier flushes the pipeline in the processor,
  69. so that all instructions following the ISB are fetched from cache or memory,
  70. after the instruction has been completed.
  71. */
  72. __ALWAYS_INLINE void __ISB(void)
  73. {
  74. __ASM volatile("sync"::: "memory");
  75. }
  76. /**
  77. \brief Data Synchronization Barrier
  78. \details Acts as a special kind of Data Memory Barrier.
  79. It completes when all explicit memory accesses before this instruction complete.
  80. */
  81. __ALWAYS_INLINE void __DSB(void)
  82. {
  83. __ASM volatile("sync"::: "memory");
  84. }
  85. /**
  86. \brief Data Memory Barrier
  87. \details Ensures the apparent order of the explicit memory operations before
  88. and after the instruction, without ensuring their completion.
  89. */
  90. __ALWAYS_INLINE void __DMB(void)
  91. {
  92. __ASM volatile("sync"::: "memory");
  93. }
  94. /**
  95. \brief Reverse byte order (32 bit)
  96. \details Reverses the byte order in integer value.
  97. \param [in] value Value to reverse
  98. \return Reversed value
  99. */
  100. __ALWAYS_INLINE uint32_t __REV(uint32_t value)
  101. {
  102. return __builtin_bswap32(value);
  103. }
  104. /**
  105. \brief Reverse byte order (16 bit)
  106. \details Reverses the byte order in two unsigned short values.
  107. \param [in] value Value to reverse
  108. \return Reversed value
  109. */
  110. __ALWAYS_INLINE uint32_t __REV16(uint32_t value)
  111. {
  112. uint32_t result;
  113. #if (__CK80X >= 2)
  114. __ASM volatile("revh %0, %1" : __CSI_GCC_OUT_REG(result) : __CSI_GCC_USE_REG(value));
  115. #else
  116. result = ((value & 0xFF000000) >> 8) | ((value & 0x00FF0000) << 8) |
  117. ((value & 0x0000FF00) >> 8) | ((value & 0x000000FF) << 8);
  118. #endif
  119. return (result);
  120. }
  121. /**
  122. \brief Reverse byte order in signed short value
  123. \details Reverses the byte order in a signed short value with sign extension to integer.
  124. \param [in] value Value to reverse
  125. \return Reversed value
  126. */
  127. __ALWAYS_INLINE int32_t __REVSH(int32_t value)
  128. {
  129. return (short)(((value & 0xFF00) >> 8) | ((value & 0x00FF) << 8));
  130. }
  131. /**
  132. \brief Rotate Right in unsigned value (32 bit)
  133. \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  134. \param [in] op1 Value to rotate
  135. \param [in] op2 Number of Bits to rotate
  136. \return Rotated value
  137. */
  138. __ALWAYS_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  139. {
  140. return (op1 >> op2) | (op1 << (32U - op2));
  141. }
  142. /**
  143. \brief Breakpoint
  144. \details Causes the processor to enter Debug state
  145. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  146. */
  147. __ALWAYS_INLINE void __BKPT()
  148. {
  149. __ASM volatile("bkpt");
  150. }
  151. /**
  152. \brief Reverse bit order of value
  153. \details Reverses the bit order of the given value.
  154. \param [in] value Value to reverse
  155. \return Reversed value
  156. */
  157. __ALWAYS_INLINE uint32_t __RBIT(uint32_t value)
  158. {
  159. uint32_t result;
  160. #if (__CK80X >= 0x03U)
  161. __ASM volatile("brev %0, %1" : "=r"(result) : "r"(value));
  162. #else
  163. int32_t s = 4 /*sizeof(v)*/ * 8 - 1; /* extra shift needed at end */
  164. result = value; /* r will be reversed bits of v; first get LSB of v */
  165. for (value >>= 1U; value; value >>= 1U)
  166. {
  167. result <<= 1U;
  168. result |= value & 1U;
  169. s--;
  170. }
  171. result <<= s; /* shift when v's highest bits are zero */
  172. #endif
  173. return (result);
  174. }
  175. /**
  176. \brief Count leading zeros
  177. \details Counts the number of leading zeros of a data value.
  178. \param [in] value Value to count the leading zeros
  179. \return number of leading zeros in value
  180. */
  181. #define __CLZ __builtin_clz
  182. /**
  183. \details This function saturates a signed value.
  184. \param [in] x Value to be saturated
  185. \param [in] y Bit position to saturate to [1..32]
  186. \return Saturated value.
  187. */
  188. __ALWAYS_INLINE int32_t __SSAT(int32_t x, uint32_t y)
  189. {
  190. int32_t posMax, negMin;
  191. uint32_t i;
  192. posMax = 1;
  193. for (i = 0; i < (y - 1); i++)
  194. {
  195. posMax = posMax * 2;
  196. }
  197. if (x > 0)
  198. {
  199. posMax = (posMax - 1);
  200. if (x > posMax)
  201. {
  202. x = posMax;
  203. }
  204. // x &= (posMax * 2 + 1);
  205. }
  206. else
  207. {
  208. negMin = -posMax;
  209. if (x < negMin)
  210. {
  211. x = negMin;
  212. }
  213. // x &= (posMax * 2 - 1);
  214. }
  215. return (x);
  216. }
  217. /**
  218. \brief Unsigned Saturate
  219. \details Saturates an unsigned value.
  220. \param [in] value Value to be saturated
  221. \param [in] sat Bit position to saturate to (0..31)
  222. \return Saturated value
  223. */
  224. __ALWAYS_INLINE uint32_t __USAT(uint32_t value, uint32_t sat)
  225. {
  226. uint32_t result;
  227. if ((((0xFFFFFFFF >> sat) << sat) & value) != 0)
  228. {
  229. result = 0xFFFFFFFF >> (32 - sat);
  230. }
  231. else
  232. {
  233. result = value;
  234. }
  235. return (result);
  236. }
  237. /**
  238. \brief Unsigned Saturate for internal use
  239. \details Saturates an unsigned value, should not call directly.
  240. \param [in] value Value to be saturated
  241. \param [in] sat Bit position to saturate to (0..31)
  242. \return Saturated value
  243. */
  244. __ALWAYS_INLINE uint32_t __IUSAT(uint32_t value, uint32_t sat)
  245. {
  246. uint32_t result;
  247. if (value & 0x80000000) /* only overflow set bit-31 */
  248. {
  249. result = 0;
  250. }
  251. else if ((((0xFFFFFFFF >> sat) << sat) & value) != 0)
  252. {
  253. result = 0xFFFFFFFF >> (32 - sat);
  254. }
  255. else
  256. {
  257. result = value;
  258. }
  259. return (result);
  260. }
  261. /**
  262. \brief Rotate Right with Extend
  263. \details This function moves each bit of a bitstring right by one bit.
  264. The carry input is shifted in at the left end of the bitstring.
  265. \note carry input will always 0.
  266. \param [in] op1 Value to rotate
  267. \return Rotated value
  268. */
  269. __ALWAYS_INLINE uint32_t __RRX(uint32_t op1)
  270. {
  271. #if (__CK80X >= 2)
  272. uint32_t res = 0;
  273. __ASM volatile("bgeni t0, 31\n\t"
  274. "lsri %0, 1\n\t"
  275. "movt %1, t0\n\t"
  276. "or %1, %1, %0\n\t"
  277. : "=r"(op1), "=r"(res): "0"(op1), "1"(res): "t0");
  278. return res;
  279. #else
  280. uint32_t res = 0;
  281. __ASM volatile("movi r7, 0\n\t"
  282. "bseti r7, 31\n\t"
  283. "lsri %0, 1\n\t"
  284. "bf 1f\n\t"
  285. "mov %1, r7\n\t"
  286. "1:\n\t"
  287. "or %1, %1, %0\n\t"
  288. : "=r"(op1), "=r"(res): "0"(op1), "1"(res): "r7");
  289. return res;
  290. #endif
  291. }
  292. /**
  293. \brief LDRT Unprivileged (8 bit)
  294. \details Executes a Unprivileged LDRT instruction for 8 bit value.
  295. \param [in] addr Pointer to location
  296. \return value of type uint8_t at (*ptr)
  297. */
  298. __ALWAYS_INLINE uint8_t __LDRBT(volatile uint8_t *addr)
  299. {
  300. uint32_t result;
  301. //#warning "__LDRBT"
  302. __ASM volatile("ldb %0, (%1, 0)" : "=r"(result) : "r"(addr));
  303. return ((uint8_t) result); /* Add explicit type cast here */
  304. }
  305. /**
  306. \brief LDRT Unprivileged (16 bit)
  307. \details Executes a Unprivileged LDRT instruction for 16 bit values.
  308. \param [in] addr Pointer to location
  309. \return value of type uint16_t at (*ptr)
  310. */
  311. __ALWAYS_INLINE uint16_t __LDRHT(volatile uint16_t *addr)
  312. {
  313. uint32_t result;
  314. //#warning "__LDRHT"
  315. __ASM volatile("ldh %0, (%1, 0)" : "=r"(result) : "r"(addr));
  316. return ((uint16_t) result); /* Add explicit type cast here */
  317. }
  318. /**
  319. \brief LDRT Unprivileged (32 bit)
  320. \details Executes a Unprivileged LDRT instruction for 32 bit values.
  321. \param [in] addr Pointer to location
  322. \return value of type uint32_t at (*ptr)
  323. */
  324. __ALWAYS_INLINE uint32_t __LDRT(volatile uint32_t *addr)
  325. {
  326. uint32_t result;
  327. //#warning "__LDRT"
  328. __ASM volatile("ldw %0, (%1, 0)" : "=r"(result) : "r"(addr));
  329. return (result);
  330. }
  331. /**
  332. \brief STRT Unprivileged (8 bit)
  333. \details Executes a Unprivileged STRT instruction for 8 bit values.
  334. \param [in] value Value to store
  335. \param [in] addr Pointer to location
  336. */
  337. __ALWAYS_INLINE void __STRBT(uint8_t value, volatile uint8_t *addr)
  338. {
  339. //#warning "__STRBT"
  340. __ASM volatile("stb %1, (%0, 0)" :: "r"(addr), "r"((uint32_t)value) : "memory");
  341. }
  342. /**
  343. \brief STRT Unprivileged (16 bit)
  344. \details Executes a Unprivileged STRT instruction for 16 bit values.
  345. \param [in] value Value to store
  346. \param [in] addr Pointer to location
  347. */
  348. __ALWAYS_INLINE void __STRHT(uint16_t value, volatile uint16_t *addr)
  349. {
  350. //#warning "__STRHT"
  351. __ASM volatile("sth %1, (%0, 0)" :: "r"(addr), "r"((uint32_t)value) : "memory");
  352. }
  353. /**
  354. \brief STRT Unprivileged (32 bit)
  355. \details Executes a Unprivileged STRT instruction for 32 bit values.
  356. \param [in] value Value to store
  357. \param [in] addr Pointer to location
  358. */
  359. __ALWAYS_INLINE void __STRT(uint32_t value, volatile uint32_t *addr)
  360. {
  361. //#warning "__STRT"
  362. __ASM volatile("stw %1, (%0, 0)" :: "r"(addr), "r"(value) : "memory");
  363. }
  364. /*@}*/ /* end of group CSI_Core_InstructionInterface */
  365. /* ########################## FPU functions #################################### */
  366. /**
  367. \brief get FPU type
  368. \details returns the FPU type, always 0.
  369. \returns
  370. - \b 0: No FPU
  371. - \b 1: Single precision FPU
  372. - \b 2: Double + Single precision FPU
  373. */
  374. __ALWAYS_INLINE uint32_t __get_FPUType(void)
  375. {
  376. uint32_t result;
  377. __ASM volatile("mfcr %0, cr<13, 0>" : "=r"(result));
  378. return 0;
  379. }
  380. #endif /* _CSI_INSTR_H_ */