mips_asm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-9-7 Urey the first version
  9. */
  10. #ifndef _MIPS_ASM_H_
  11. #define _MIPS_ASM_H_
  12. /* ********************************************************************* */
  13. /* Interface macro & data definition */
  14. #ifdef __ASSEMBLY__
  15. /******** ASSEMBLER SPECIFIC DEFINITIONS ********/
  16. #ifdef __ghs__
  17. #define ALIGN(x) .##align (1 << (x))
  18. #else
  19. #define ALIGN(x) .##align (x)
  20. #endif
  21. #ifdef __ghs__
  22. #define SET_MIPS3()
  23. #define SET_MIPS0()
  24. #define SET_PUSH()
  25. #define SET_POP()
  26. #else
  27. #define SET_MIPS3() .##set mips3
  28. #define SET_MIPS0() .##set mips0
  29. #define SET_PUSH() .##set push
  30. #define SET_POP() .##set pop
  31. #endif
  32. /* Different assemblers have different requirements for how to
  33. * indicate that the next section is bss :
  34. *
  35. * Some use : .bss
  36. * Others use : .section bss
  37. *
  38. * We select which to use based on _BSS_OLD_, which may be defined
  39. * in makefile.
  40. */
  41. #ifdef _BSS_OLD_
  42. #define BSS .##section bss
  43. #else
  44. #define BSS .##bss
  45. #endif
  46. #define LEAF(name)\
  47. .##text;\
  48. .##globl name;\
  49. .##ent name;\
  50. name:
  51. #define SLEAF(name)\
  52. .##text;\
  53. .##ent name;\
  54. name:
  55. #ifdef __ghs__
  56. #define END(name)\
  57. .##end name
  58. #else
  59. #define END(name)\
  60. .##size name,.-name;\
  61. .##end name
  62. #endif
  63. #define EXTERN(name)
  64. #else
  65. #define U64 unsigned long long
  66. #define U32 unsigned int
  67. #define U16 unsigned short
  68. #define U8 unsigned char
  69. #define S64 signed long long
  70. #define S32 int
  71. #define S16 short int
  72. #define S8 signed char
  73. //#define bool U8
  74. #ifndef _SIZE_T_
  75. #define _SIZE_T_
  76. #ifdef __ghs__
  77. typedef unsigned int size_t;
  78. #else
  79. typedef unsigned long size_t;
  80. #endif
  81. #endif
  82. /* Sets the result on bPort */
  83. #define BIT_SET(bPort,bBitMask) (bPort |= bBitMask)
  84. #define BIT_CLR(bPort,bBitMask) (bPort &= ~bBitMask)
  85. /* Returns the result */
  86. #define GET_BIT_SET(bPort,bBitMask) (bPort | bBitMask)
  87. #define GET_BIT_CLR(bPort,bBitMask) (bPort & ~bBitMask)
  88. /* Returns 0 if the condition is False & a non-zero value if it is True */
  89. #define TEST_BIT_SET(bPort,bBitMask) (bPort & bBitMask)
  90. #define TEST_BIT_CLR(bPort,bBitMask) ((~bPort) & bBitMask)
  91. /* Split union definitions */
  92. typedef union tunSU16
  93. {
  94. U16 hwHW;
  95. struct tst2U8
  96. {
  97. U8 bB0;
  98. U8 bB1;
  99. }st2U8;
  100. }tunSU16;
  101. typedef union tunSU32
  102. {
  103. U32 wW;
  104. struct tst2U16
  105. {
  106. U16 hwHW0;
  107. U16 hwHW1;
  108. }st2U16;
  109. struct tst4U8
  110. {
  111. U8 bB0;
  112. U8 bB1;
  113. U8 bB2;
  114. U8 bB3;
  115. }st4U8;
  116. }tunSU32;
  117. #endif /* #ifdef __ASSEMBLY__ */
  118. /******** DEFINITIONS FOR BOTH ASSEMBLER AND C ********/
  119. #define NO_ERR 0x00000000 /* operation completed successfully */
  120. #define ERR 0xffffffff /* operation completed not successfully */
  121. #define False 0
  122. #define True !False
  123. #ifndef NULL
  124. #define NULL ((void *)0)
  125. #endif//NULL
  126. #ifndef MIN
  127. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  128. #endif//MIN
  129. #ifndef MAX
  130. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  131. #endif//MAX
  132. #define MAXUINT(w) (\
  133. ((w) == sizeof(U8)) ? 0xFFU :\
  134. ((w) == sizeof(U16)) ? 0xFFFFU :\
  135. ((w) == sizeof(U32)) ? 0xFFFFFFFFU : 0\
  136. )
  137. #define MAXINT(w) (\
  138. ((w) == sizeof(S8)) ? 0x7F :\
  139. ((w) == sizeof(S16)) ? 0x7FFF :\
  140. ((w) == sizeof(S32)) ? 0x7FFFFFFF : 0\
  141. )
  142. #define MSK(n) ((1 << (n)) - 1)
  143. #define KUSEG_MSK 0x80000000
  144. #define KSEG_MSK 0xE0000000
  145. #define KUSEGBASE 0x00000000
  146. #define KSEG0BASE 0x80000000
  147. #define KSEG1BASE 0xA0000000
  148. #define KSSEGBASE 0xC0000000
  149. #define KSEG3BASE 0xE0000000
  150. /* Below macros perform the following functions :
  151. *
  152. * KSEG0 : Converts KSEG0/1 or physical addr (below 0.5GB) to KSEG0.
  153. * KSEG1 : Converts KSEG0/1 or physical addr (below 0.5GB) to KSEG1.
  154. * PHYS : Converts KSEG0/1 or physical addr (below 0.5GB) to physical address.
  155. * KSSEG : Not relevant for converting, but used for determining range.
  156. * KSEG3 : Not relevant for converting, but used for determining range.
  157. * KUSEG : Not relevant for converting, but used for determining range.
  158. * KSEG0A : Same as KSEG0 but operates on register rather than constant.
  159. * KSEG1A : Same as KSEG1 but operates on register rather than constant.
  160. * PHYSA : Same as PHYS but operates on register rather than constant.
  161. * CACHED : Alias for KSEG0 macro .
  162. * (Note that KSEG0 cache attribute is determined by K0
  163. * field of Config register, but this is typically cached).
  164. * UNCACHED : Alias for KSEG1 macro .
  165. */
  166. #ifdef __ASSEMBLY__
  167. #define KSEG0(addr) (((addr) & ~KSEG_MSK) | KSEG0BASE)
  168. #define KSEG1(addr) (((addr) & ~KSEG_MSK) | KSEG1BASE)
  169. #define KSSEG(addr) (((addr) & ~KSEG_MSK) | KSSEGBASE)
  170. #define KSEG3(addr) (((addr) & ~KSEG_MSK) | KSEG3BASE)
  171. #define KUSEG(addr) (((addr) & ~KUSEG_MSK) | KUSEGBASE)
  172. #define PHYS(addr) ( (addr) & ~KSEG_MSK)
  173. #define KSEG0A(reg) and reg, ~KSEG_MSK; or reg, KSEG0BASE
  174. #define KSEG1A(reg) and reg, ~KSEG_MSK; or reg, KSEG1BASE
  175. #define PHYSA(reg) and reg, ~KSEG_MSK
  176. #else
  177. #define KSEG0(addr) (((U32)(addr) & ~KSEG_MSK) | KSEG0BASE)
  178. #define KSEG1(addr) (((U32)(addr) & ~KSEG_MSK) | KSEG1BASE)
  179. #define KSSEG(addr) (((U32)(addr) & ~KSEG_MSK) | KSSEGBASE)
  180. #define KSEG3(addr) (((U32)(addr) & ~KSEG_MSK) | KSEG3BASE)
  181. #define KUSEG(addr) (((U32)(addr) & ~KUSEG_MSK) | KUSEGBASE)
  182. #define PHYS(addr) ((U32)(addr) & ~KSEG_MSK)
  183. #endif
  184. #define CACHED(addr) KSEG0(addr)
  185. #define UNCACHED(addr) KSEG1(addr)
  186. #ifdef __ASSEMBLY__
  187. /* Macroes to access variables at constant addresses
  188. * Compensates for signed 16 bit displacement
  189. * Typical use: li a0, HIKSEG1(ATLAS_ASCIIWORD)
  190. * sw v1, LO_OFFS(ATLAS_ASCIIWORD)(a0)
  191. */
  192. #define HIKSEG0(addr) ((KSEG0(addr) + 0x8000) & 0xffff0000)
  193. #define HIKSEG1(addr) ((KSEG1(addr) + 0x8000) & 0xffff0000)
  194. #define HI_PART(addr) (((addr) + 0x8000) & 0xffff0000)
  195. #define LO_OFFS(addr) ((addr) & 0xffff)
  196. #endif
  197. /* Most/Least significant 32 bit from 64 bit double word */
  198. #define HI32(data64) ((U32)(data64 >> 32))
  199. #define LO32(data64) ((U32)(data64 & 0xFFFFFFFF))
  200. #if ((!defined(__ASSEMBLY__)) && (!defined(__LANGUAGE_ASSEMBLY)))
  201. #define REG8( addr ) (*(volatile U8 *) (addr))
  202. #define REG16( addr ) (*(volatile U16 *)(addr))
  203. #define REG32( addr ) (*(volatile U32 *)(addr))
  204. #define REG64( addr ) (*(volatile U64 *)(addr))
  205. #endif
  206. /* Register field mapping */
  207. #define REGFIELD(reg, rfld) (((reg) & rfld##_MSK) >> rfld##_SHF)
  208. /* absolute register address, access */
  209. #define REGA(addr) REG32(addr)
  210. /* physical register address, access: base address + offsett */
  211. #define REGP(base,phys) REG32( (U32)(base) + (phys) )
  212. /* relative register address, access: base address + offsett */
  213. #define REG(base,offs) REG32( (U32)(base) + offs##_##OFS )
  214. /* relative register address, access: base address + offsett */
  215. #define REG_8(base,offs) REG8( (U32)(base) + offs##_##OFS )
  216. /* relative register address, access: base address + offsett */
  217. #define REG_16(base,offs) REG16( (U32)(base) + offs##_##OFS )
  218. /* relative register address, access: base address + offsett */
  219. #define REG_64(base,offs) REG64( (U32)(base) + offs##_##OFS )
  220. /**************************************
  221. * Macroes not used by YAMON any more
  222. * (kept for backwards compatibility)
  223. */
  224. /* register read field */
  225. #define REGARD(addr,fld) ((REGA(addr) & addr##_##fld##_##MSK) \
  226. >> addr##_##fld##_##SHF)
  227. /* register write numeric field value */
  228. #define REGAWRI(addr,fld,intval) ((REGA(addr) & ~(addr##_##fld##_##MSK))\
  229. | ((intval) << addr##_##fld##_##SHF))
  230. /* register write enumerated field value */
  231. #define REGAWRE(addr,fld,enumval) ((REGA(addr) & ~(addr##_##fld##_##MSK))\
  232. | ((addr##_##fld##_##enumval) << addr##_##fld##_##SHF))
  233. /* Examples:
  234. *
  235. * exccode = REGARD(CPU_CAUSE,EXC);
  236. *
  237. * REGA(SDR_CONTROL) = REGAWRI(OSG_CONTROL,TMO,17)
  238. * | REGAWRE(OSG_CONTROL,DTYPE,PC1);
  239. */
  240. /* register read field */
  241. #define REGRD(base,offs,fld) ((REG(base,offs) & offs##_##fld##_##MSK) \
  242. >> offs##_##fld##_##SHF)
  243. /* register write numeric field value */
  244. #define REGWRI(base,offs,fld,intval)((REG(base,offs)& ~(offs##_##fld##_##MSK))\
  245. | (((intval) << offs##_##fld##_##SHF) & offs##_##fld##_##MSK))
  246. /* register write enumerated field value */
  247. #define REGWRE(base,offs,fld,enumval)((REG(base,offs) & ~(offs##_##fld##_##MSK))\
  248. | ((offs##_##fld##_##enumval) << offs##_##fld##_##SHF))
  249. /* physical register read field */
  250. #define REGPRD(base,phys,fld) ((REGP(base,phys) & phys##_##fld##_##MSK) \
  251. >> phys##_##fld##_##SHF)
  252. /* physical register write numeric field value */
  253. #define REGPWRI(base,phys,fld,intval)((REGP(base,phys)& ~(phys##_##fld##_##MSK))\
  254. | ((intval) << phys##_##fld##_##SHF))
  255. /* physical register write enumerated field value */
  256. #define REGPWRE(base,phys,fld,enumval)((REGP(base,phys) & ~(phys##_##fld##_##MSK))\
  257. | ((phys##_##fld##_##enumval) << phys##_##fld##_##SHF))
  258. /*
  259. * End of macroes not used by YAMON any more
  260. *********************************************/
  261. /* Endian related macros */
  262. #define SWAP_BYTEADDR32( addr ) ( (addr) ^ 0x3 )
  263. #define SWAP_U16ADDR32( addr ) ( (addr) ^ 0x2 )
  264. /* Set byte address to little endian format */
  265. #ifdef EL
  266. #define SWAP_BYTEADDR_EL(addr) addr
  267. #else
  268. #define SWAP_BYTEADDR_EL(addr) SWAP_BYTEADDR32( addr )
  269. #endif
  270. /* Set byte address to big endian format */
  271. #ifdef EB
  272. #define SWAP_BYTEADDR_EB(addr) addr
  273. #else
  274. #define SWAP_BYTEADDR_EB(addr) SWAP_BYTEADDR32( addr )
  275. #endif
  276. /* Set U16 address to little endian format */
  277. #ifdef EL
  278. #define SWAP_U16ADDR_EL(addr) addr
  279. #else
  280. #define SWAP_U16ADDR_EL(addr) SWAP_U16ADDR32( addr )
  281. #endif
  282. /* Set U16 address to big endian format */
  283. #ifdef EB
  284. #define SWAP_U16ADDR_EB(addr) addr
  285. #else
  286. #define SWAP_U16ADDR_EB(addr) SWAP_U16ADDR32( addr )
  287. #endif
  288. #ifdef EL
  289. #define REGW32LE(addr, data) REG32(addr) = (data)
  290. #define REGR32LE(addr, data) (data) = REG32(addr)
  291. #else
  292. #define REGW32LE(addr, data) REG32(addr) = SWAPEND32(data)
  293. #define REGR32LE(addr, data) (data) = REG32(addr), (data) = SWAPEND32(data)
  294. #endif
  295. /* Set of 'LE'-macros, convert by BE: */
  296. #ifdef EL
  297. #define CPU_TO_LE32( value ) (value)
  298. #define LE32_TO_CPU( value ) (value)
  299. #define CPU_TO_LE16( value ) (value)
  300. #define LE16_TO_CPU( value ) (value)
  301. #else
  302. #define CPU_TO_LE32( value ) ( ( ((U32)value) << 24) | \
  303. ((0x0000FF00UL & ((U32)value)) << 8) | \
  304. ((0x00FF0000UL & ((U32)value)) >> 8) | \
  305. ( ((U32)value) >> 24) )
  306. #define LE32_TO_CPU( value ) CPU_TO_LE32( value )
  307. #define CPU_TO_LE16( value ) ( ((U16)(((U16)value) << 8)) | \
  308. ((U16)(((U16)value) >> 8)) )
  309. #define LE16_TO_CPU( value ) CPU_TO_LE16( value )
  310. #endif
  311. /* Set of 'BE'-macros, convert by LE: */
  312. #ifdef EB
  313. #define CPU_TO_BE32( value ) (value)
  314. #define BE32_TO_CPU( value ) (value)
  315. #define CPU_TO_BE16( value ) (value)
  316. #define BE16_TO_CPU( value ) (value)
  317. #else
  318. #define CPU_TO_BE32( value ) ( ( ((U32)value) << 24) | \
  319. ((0x0000FF00UL & ((U32)value)) << 8) | \
  320. ((0x00FF0000UL & ((U32)value)) >> 8) | \
  321. ( ((U32)value) >> 24) )
  322. #define BE32_TO_CPU( value ) CPU_TO_BE32( value )
  323. #define CPU_TO_BE16( value ) ( ((U16)(((U16)value) << 8)) | \
  324. ((U16)(((U16)value) >> 8)) )
  325. #define BE16_TO_CPU( value ) CPU_TO_BE16( value )
  326. #endif
  327. /* Control characters */
  328. #define CTRL_A ('A'-0x40)
  329. #define CTRL_B ('B'-0x40)
  330. #define CTRL_C ('C'-0x40)
  331. #define CTRL_D ('D'-0x40)
  332. #define CTRL_E ('E'-0x40)
  333. #define CTRL_F ('F'-0x40)
  334. #define CTRL_H ('H'-0x40)
  335. #define CTRL_K ('K'-0x40)
  336. #define CTRL_N ('N'-0x40)
  337. #define CTRL_P ('P'-0x40)
  338. #define CTRL_U ('U'-0x40)
  339. #define BACKSPACE 0x08
  340. #define DEL 0x7F
  341. #define TAB 0x09
  342. #define CR 0x0D /* Enter Key */
  343. #define LF 0x0A
  344. #define ESC 0x1B
  345. #define SP 0x20
  346. #define CSI 0x9B
  347. /* DEF2STR(x) converts #define symbol to string */
  348. #define DEF2STR1(x) #x
  349. #define DEF2STR(x) DEF2STR1(x)
  350. #endif /* _MIPS_ASM_H_ */