ulp.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stddef.h>
  9. #include <stdlib.h>
  10. #include "esp_err.h"
  11. #include "ulp_common.h"
  12. #include "ulp_fsm_common.h"
  13. #include "soc/reg_base.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /**
  18. * @defgroup ulp_registers ULP coprocessor registers
  19. * @{
  20. */
  21. #define R0 0 /*!< general purpose register 0 */
  22. #define R1 1 /*!< general purpose register 1 */
  23. #define R2 2 /*!< general purpose register 2 */
  24. #define R3 3 /*!< general purpose register 3 */
  25. /**@}*/
  26. /** @defgroup ulp_opcodes ULP coprocessor opcodes, sub opcodes, and various modifiers/flags
  27. *
  28. * These definitions are not intended to be used directly.
  29. * They are used in definitions of instructions later on.
  30. *
  31. * @{
  32. */
  33. #define OPCODE_WR_REG 1 /*!< Instruction: write peripheral register (RTC_CNTL/RTC_IO/SARADC) (not implemented yet) */
  34. #define OPCODE_RD_REG 2 /*!< Instruction: read peripheral register (RTC_CNTL/RTC_IO/SARADC) (not implemented yet) */
  35. #define RD_REG_PERIPH_RTC_CNTL 0 /*!< Identifier of RTC_CNTL peripheral for RD_REG and WR_REG instructions */
  36. #define RD_REG_PERIPH_RTC_IO 1 /*!< Identifier of RTC_IO peripheral for RD_REG and WR_REG instructions */
  37. #define RD_REG_PERIPH_SENS 2 /*!< Identifier of SARADC peripheral for RD_REG and WR_REG instructions */
  38. #define RD_REG_PERIPH_RTC_I2C 3 /*!< Identifier of RTC_I2C peripheral for RD_REG and WR_REG instructions */
  39. #define OPCODE_I2C 3 /*!< Instruction: read/write I2C (not implemented yet) */
  40. #define OPCODE_DELAY 4 /*!< Instruction: delay (nop) for a given number of cycles */
  41. #define OPCODE_ADC 5 /*!< Instruction: SAR ADC measurement (not implemented yet) */
  42. #define OPCODE_ST 6 /*!< Instruction: store indirect to RTC memory */
  43. #define SUB_OPCODE_ST 4 /*!< Store 32 bits, 16 MSBs contain PC, 16 LSBs contain value from source register */
  44. #define OPCODE_ALU 7 /*!< Arithmetic instructions */
  45. #define SUB_OPCODE_ALU_REG 0 /*!< Arithmetic instruction, both source values are in register */
  46. #define SUB_OPCODE_ALU_IMM 1 /*!< Arithmetic instruction, one source value is an immediate */
  47. #define SUB_OPCODE_ALU_CNT 2 /*!< Arithmetic instruction between counter register and an immediate (not implemented yet)*/
  48. #define ALU_SEL_ADD 0 /*!< Addition */
  49. #define ALU_SEL_SUB 1 /*!< Subtraction */
  50. #define ALU_SEL_AND 2 /*!< Logical AND */
  51. #define ALU_SEL_OR 3 /*!< Logical OR */
  52. #define ALU_SEL_MOV 4 /*!< Copy value (immediate to destination register or source register to destination register */
  53. #define ALU_SEL_LSH 5 /*!< Shift left by given number of bits */
  54. #define ALU_SEL_RSH 6 /*!< Shift right by given number of bits */
  55. #define OPCODE_BRANCH 8 /*!< Branch instructions */
  56. #define SUB_OPCODE_BX 0 /*!< Branch to absolute PC (immediate or in register) */
  57. #define BX_JUMP_TYPE_DIRECT 0 /*!< Unconditional jump */
  58. #define BX_JUMP_TYPE_ZERO 1 /*!< Branch if last ALU result is zero */
  59. #define BX_JUMP_TYPE_OVF 2 /*!< Branch if last ALU operation caused and overflow */
  60. #define SUB_OPCODE_B 1 /*!< Branch to a relative offset */
  61. #define B_CMP_L 0 /*!< Branch if R0 is less than an immediate */
  62. #define B_CMP_GE 1 /*!< Branch if R0 is greater than or equal to an immediate */
  63. #define OPCODE_END 9 /*!< Stop executing the program */
  64. #define SUB_OPCODE_END 0 /*!< Stop executing the program and optionally wake up the chip */
  65. #define SUB_OPCODE_SLEEP 1 /*!< Stop executing the program and run it again after selected interval */
  66. #define OPCODE_TSENS 10 /*!< Instruction: temperature sensor measurement (not implemented yet) */
  67. #define OPCODE_HALT 11 /*!< Halt the coprocessor */
  68. #define OPCODE_LD 13 /*!< Indirect load lower 16 bits from RTC memory */
  69. #define OPCODE_MACRO 15 /*!< Not a real opcode. Used to identify labels and branches in the program */
  70. #define SUB_OPCODE_MACRO_LABEL 0 /*!< Label macro */
  71. #define SUB_OPCODE_MACRO_BRANCH 1 /*!< Branch macro */
  72. /**@}*/
  73. /**
  74. * @brief Instruction format structure
  75. *
  76. * All ULP instructions are 32 bit long.
  77. * This union contains field layouts used by all of the supported instructions.
  78. * This union also includes a special "macro" instruction layout.
  79. * This is not a real instruction which can be executed by the CPU. It acts
  80. * as a token which is removed from the program by the
  81. * ulp_process_macros_and_load function.
  82. *
  83. * These structures are not intended to be used directly.
  84. * Preprocessor definitions provided below fill the fields of these structure with
  85. * the right arguments.
  86. */
  87. union ulp_insn {
  88. struct {
  89. uint32_t cycles : 16; /*!< Number of cycles to sleep */
  90. uint32_t unused : 12; /*!< Unused */
  91. uint32_t opcode : 4; /*!< Opcode (OPCODE_DELAY) */
  92. } delay; /*!< Format of DELAY instruction */
  93. struct {
  94. uint32_t dreg : 2; /*!< Register which contains data to store */
  95. uint32_t sreg : 2; /*!< Register which contains address in RTC memory (expressed in words) */
  96. uint32_t unused1 : 6; /*!< Unused */
  97. uint32_t offset : 11; /*!< Offset to add to sreg */
  98. uint32_t unused2 : 4; /*!< Unused */
  99. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ST) */
  100. uint32_t opcode : 4; /*!< Opcode (OPCODE_ST) */
  101. } st; /*!< Format of ST instruction */
  102. struct {
  103. uint32_t dreg : 2; /*!< Register where the data should be loaded to */
  104. uint32_t sreg : 2; /*!< Register which contains address in RTC memory (expressed in words) */
  105. uint32_t unused1 : 6; /*!< Unused */
  106. uint32_t offset : 11; /*!< Offset to add to sreg */
  107. uint32_t unused2 : 7; /*!< Unused */
  108. uint32_t opcode : 4; /*!< Opcode (OPCODE_LD) */
  109. } ld; /*!< Format of LD instruction */
  110. struct {
  111. uint32_t unused : 28; /*!< Unused */
  112. uint32_t opcode : 4; /*!< Opcode (OPCODE_HALT) */
  113. } halt; /*!< Format of HALT instruction */
  114. struct {
  115. uint32_t dreg : 2; /*!< Register which contains target PC, expressed in words (used if .reg == 1) */
  116. uint32_t addr : 11; /*!< Target PC, expressed in words (used if .reg == 0) */
  117. uint32_t unused : 8; /*!< Unused */
  118. uint32_t reg : 1; /*!< Target PC in register (1) or immediate (0) */
  119. uint32_t type : 3; /*!< Jump condition (BX_JUMP_TYPE_xxx) */
  120. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_BX) */
  121. uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */
  122. } bx; /*!< Format of BRANCH instruction (absolute address) */
  123. struct {
  124. uint32_t imm : 16; /*!< Immediate value to compare against */
  125. uint32_t cmp : 1; /*!< Comparison to perform: B_CMP_L or B_CMP_GE */
  126. uint32_t offset : 7; /*!< Absolute value of target PC offset w.r.t. current PC, expressed in words */
  127. uint32_t sign : 1; /*!< Sign of target PC offset: 0: positive, 1: negative */
  128. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_B) */
  129. uint32_t opcode : 4; /*!< Opcode (OPCODE_BRANCH) */
  130. } b; /*!< Format of BRANCH instruction (relative address) */
  131. struct {
  132. uint32_t dreg : 2; /*!< Destination register */
  133. uint32_t sreg : 2; /*!< Register with operand A */
  134. uint32_t treg : 2; /*!< Register with operand B */
  135. uint32_t unused : 15; /*!< Unused */
  136. uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_xxx */
  137. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_REG) */
  138. uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */
  139. } alu_reg; /*!< Format of ALU instruction (both sources are registers) */
  140. struct {
  141. uint32_t dreg : 2; /*!< Destination register */
  142. uint32_t sreg : 2; /*!< Register with operand A */
  143. uint32_t imm : 16; /*!< Immediate value of operand B */
  144. uint32_t unused : 1; /*!< Unused */
  145. uint32_t sel : 4; /*!< Operation to perform, one of ALU_SEL_xxx */
  146. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_ALU_IMM) */
  147. uint32_t opcode : 4; /*!< Opcode (OPCODE_ALU) */
  148. } alu_imm; /*!< Format of ALU instruction (one source is an immediate) */
  149. struct {
  150. uint32_t addr : 8; /*!< Address within either RTC_CNTL, RTC_IO, or SARADC */
  151. uint32_t periph_sel : 2; /*!< Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2) */
  152. uint32_t data : 8; /*!< 8 bits of data to write */
  153. uint32_t low : 5; /*!< Low bit */
  154. uint32_t high : 5; /*!< High bit */
  155. uint32_t opcode : 4; /*!< Opcode (OPCODE_WR_REG) */
  156. } wr_reg; /*!< Format of WR_REG instruction */
  157. struct {
  158. uint32_t addr : 8; /*!< Address within either RTC_CNTL, RTC_IO, or SARADC */
  159. uint32_t periph_sel : 2; /*!< Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2) */
  160. uint32_t unused : 8; /*!< Unused */
  161. uint32_t low : 5; /*!< Low bit */
  162. uint32_t high : 5; /*!< High bit */
  163. uint32_t opcode : 4; /*!< Opcode (OPCODE_RD_REG) */
  164. } rd_reg; /*!< Format of RD_REG instruction */
  165. struct {
  166. uint32_t dreg : 2; /*!< Register where to store ADC result */
  167. uint32_t mux : 4; /*!< Select SARADC pad (mux + 1) */
  168. uint32_t sar_sel : 1; /*!< Select SARADC0 (0) or SARADC1 (1) */
  169. uint32_t unused1 : 1; /*!< Unused */
  170. uint32_t cycles : 16; /*!< TBD, cycles used for measurement */
  171. uint32_t unused2 : 4; /*!< Unused */
  172. uint32_t opcode: 4; /*!< Opcode (OPCODE_ADC) */
  173. } adc; /*!< Format of ADC instruction */
  174. struct {
  175. uint32_t dreg : 2; /*!< Register where to store temperature measurement result */
  176. uint32_t wait_delay: 14; /*!< Cycles to wait after measurement is done */
  177. uint32_t reserved: 12; /*!< Reserved, set to 0 */
  178. uint32_t opcode: 4; /*!< Opcode (OPCODE_TSENS) */
  179. } tsens; /*!< Format of TSENS instruction */
  180. struct {
  181. uint32_t i2c_addr : 8; /*!< I2C slave address */
  182. uint32_t data : 8; /*!< Data to read or write */
  183. uint32_t low_bits : 3; /*!< TBD */
  184. uint32_t high_bits : 3; /*!< TBD */
  185. uint32_t i2c_sel : 4; /*!< TBD, select reg_i2c_slave_address[7:0] */
  186. uint32_t unused : 1; /*!< Unused */
  187. uint32_t rw : 1; /*!< Write (1) or read (0) */
  188. uint32_t opcode : 4; /*!< Opcode (OPCODE_I2C) */
  189. } i2c; /*!< Format of I2C instruction */
  190. struct {
  191. uint32_t wakeup : 1; /*!< Set to 1 to wake up chip */
  192. uint32_t unused : 24; /*!< Unused */
  193. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_WAKEUP) */
  194. uint32_t opcode : 4; /*!< Opcode (OPCODE_END) */
  195. } end; /*!< Format of END instruction with wakeup */
  196. struct {
  197. uint32_t cycle_sel : 4; /*!< Select which one of SARADC_ULP_CP_SLEEP_CYCx_REG to get the sleep duration from */
  198. uint32_t unused : 21; /*!< Unused */
  199. uint32_t sub_opcode : 3; /*!< Sub opcode (SUB_OPCODE_SLEEP) */
  200. uint32_t opcode : 4; /*!< Opcode (OPCODE_END) */
  201. } sleep; /*!< Format of END instruction with sleep */
  202. struct {
  203. uint32_t label : 16; /*!< Label number */
  204. uint32_t unused : 8; /*!< Unused */
  205. uint32_t sub_opcode : 4; /*!< SUB_OPCODE_MACRO_LABEL or SUB_OPCODE_MACRO_BRANCH */
  206. uint32_t opcode: 4; /*!< Opcode (OPCODE_MACRO) */
  207. } macro; /*!< Format of tokens used by LABEL and BRANCH macros */
  208. };
  209. typedef union ulp_insn ulp_insn_t;
  210. _Static_assert(sizeof(ulp_insn_t) == 4, "ULP coprocessor instruction size should be 4 bytes");
  211. /**
  212. * Delay (nop) for a given number of cycles
  213. */
  214. #define I_DELAY(cycles_) { .delay = {\
  215. .cycles = cycles_, \
  216. .unused = 0, \
  217. .opcode = OPCODE_DELAY } }
  218. /**
  219. * Halt the coprocessor.
  220. *
  221. * This instruction halts the coprocessor, but keeps ULP timer active.
  222. * As such, ULP program will be restarted again by timer.
  223. * To stop the program and prevent the timer from restarting the program,
  224. * use I_END(0) instruction.
  225. */
  226. #define I_HALT() { .halt = {\
  227. .unused = 0, \
  228. .opcode = OPCODE_HALT } }
  229. /**
  230. * Map SoC peripheral register to periph_sel field of RD_REG and WR_REG
  231. * instructions.
  232. *
  233. * @param reg peripheral register in RTC_CNTL_, RTC_IO_, SENS_, RTC_I2C peripherals.
  234. * @return periph_sel value for the peripheral to which this register belongs.
  235. */
  236. static inline uint32_t SOC_REG_TO_ULP_PERIPH_SEL(uint32_t reg) {
  237. uint32_t ret = 3;
  238. if (reg < DR_REG_RTCCNTL_BASE) {
  239. assert(0 && "invalid register base");
  240. } else if (reg < DR_REG_RTCIO_BASE) {
  241. ret = RD_REG_PERIPH_RTC_CNTL;
  242. } else if (reg < DR_REG_SENS_BASE) {
  243. ret = RD_REG_PERIPH_RTC_IO;
  244. } else if (reg < DR_REG_RTC_I2C_BASE){
  245. ret = RD_REG_PERIPH_SENS;
  246. } else if (reg < DR_REG_IO_MUX_BASE){
  247. ret = RD_REG_PERIPH_RTC_I2C;
  248. } else {
  249. assert(0 && "invalid register base");
  250. }
  251. return ret;
  252. }
  253. /**
  254. * Write literal value to a peripheral register
  255. *
  256. * reg[high_bit : low_bit] = val
  257. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  258. */
  259. #define I_WR_REG(reg, low_bit, high_bit, val) {.wr_reg = {\
  260. .addr = (reg & 0xff) / sizeof(uint32_t), \
  261. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  262. .data = val, \
  263. .low = low_bit, \
  264. .high = high_bit, \
  265. .opcode = OPCODE_WR_REG } }
  266. /**
  267. * Read from peripheral register into R0
  268. *
  269. * R0 = reg[high_bit : low_bit]
  270. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  271. */
  272. #define I_RD_REG(reg, low_bit, high_bit) {.rd_reg = {\
  273. .addr = (reg & 0xff) / sizeof(uint32_t), \
  274. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  275. .unused = 0, \
  276. .low = low_bit, \
  277. .high = high_bit, \
  278. .opcode = OPCODE_RD_REG } }
  279. /**
  280. * Set or clear a bit in the peripheral register.
  281. *
  282. * Sets bit (1 << shift) of register reg to value val.
  283. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  284. */
  285. #define I_WR_REG_BIT(reg, shift, val) I_WR_REG(reg, shift, shift, val)
  286. /**
  287. * Wake the SoC from deep sleep.
  288. *
  289. * This instruction initiates wake up from deep sleep.
  290. * Use esp_deep_sleep_enable_ulp_wakeup to enable deep sleep wakeup
  291. * triggered by the ULP before going into deep sleep.
  292. * Note that ULP program will still keep running until the I_HALT
  293. * instruction, and it will still be restarted by timer at regular
  294. * intervals, even when the SoC is woken up.
  295. *
  296. * To stop the ULP program, use I_HALT instruction.
  297. *
  298. * To disable the timer which start ULP program, use I_END()
  299. * instruction. I_END instruction clears the
  300. * RTC_CNTL_ULP_CP_SLP_TIMER_EN_S bit of RTC_CNTL_STATE0_REG
  301. * register, which controls the ULP timer.
  302. */
  303. #define I_WAKE() { .end = { \
  304. .wakeup = 1, \
  305. .unused = 0, \
  306. .sub_opcode = SUB_OPCODE_END, \
  307. .opcode = OPCODE_END } }
  308. /**
  309. * Stop ULP program timer.
  310. *
  311. * This is a convenience macro which disables the ULP program timer.
  312. * Once this instruction is used, ULP program will not be restarted
  313. * anymore until ulp_run function is called.
  314. *
  315. * ULP program will continue running after this instruction. To stop
  316. * the currently running program, use I_HALT().
  317. */
  318. #define I_END() \
  319. I_WR_REG_BIT(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN_S, 0)
  320. /**
  321. * Select the time interval used to run ULP program.
  322. *
  323. * This instructions selects which of the SENS_SLEEP_CYCLES_Sx
  324. * registers' value is used by the ULP program timer.
  325. * When the ULP program stops at I_HALT instruction, ULP program
  326. * timer start counting. When the counter reaches the value of
  327. * the selected SENS_SLEEP_CYCLES_Sx register, ULP program
  328. * start running again from the start address (passed to the ulp_run
  329. * function).
  330. * There are 5 SENS_SLEEP_CYCLES_Sx registers, so 0 <= timer_idx < 5.
  331. *
  332. * By default, SENS_SLEEP_CYCLES_S0 register is used by the ULP
  333. * program timer.
  334. */
  335. #define I_SLEEP_CYCLE_SEL(timer_idx) { .sleep = { \
  336. .cycle_sel = timer_idx, \
  337. .unused = 0, \
  338. .sub_opcode = SUB_OPCODE_SLEEP, \
  339. .opcode = OPCODE_END } }
  340. /**
  341. * Perform temperature sensor measurement and store it into reg_dest.
  342. *
  343. * Delay can be set between 1 and ((1 << 14) - 1). Higher values give
  344. * higher measurement resolution.
  345. */
  346. #define I_TSENS(reg_dest, delay) { .tsens = { \
  347. .dreg = reg_dest, \
  348. .wait_delay = delay, \
  349. .reserved = 0, \
  350. .opcode = OPCODE_TSENS } }
  351. /**
  352. * Perform ADC measurement and store result in reg_dest.
  353. *
  354. * adc_idx selects ADC (0 or 1).
  355. * pad_idx selects ADC pad (0 - 7).
  356. */
  357. #define I_ADC(reg_dest, adc_idx, pad_idx) { .adc = {\
  358. .dreg = reg_dest, \
  359. .mux = pad_idx + 1, \
  360. .sar_sel = adc_idx, \
  361. .unused1 = 0, \
  362. .cycles = 0, \
  363. .unused2 = 0, \
  364. .opcode = OPCODE_ADC } }
  365. /**
  366. * Store value from register reg_val into RTC memory.
  367. *
  368. * The value is written to an offset calculated by adding value of
  369. * reg_addr register and offset_ field (this offset is expressed in 32-bit words).
  370. * 32 bits written to RTC memory are built as follows:
  371. * - bits [31:21] hold the PC of current instruction, expressed in 32-bit words
  372. * - bits [20:18] = 3'b0
  373. * - bits [17:16] reg_addr (0..3)
  374. * - bits [15:0] are assigned the contents of reg_val
  375. *
  376. * RTC_SLOW_MEM[addr + offset_] = { insn_PC[10:0], 3'b0, reg_addr, reg_val[15:0] }
  377. */
  378. #define I_ST(reg_val, reg_addr, offset_) { .st = { \
  379. .dreg = reg_val, \
  380. .sreg = reg_addr, \
  381. .unused1 = 0, \
  382. .offset = offset_, \
  383. .unused2 = 0, \
  384. .sub_opcode = SUB_OPCODE_ST, \
  385. .opcode = OPCODE_ST } }
  386. /**
  387. * Load value from RTC memory into reg_dest register.
  388. *
  389. * Loads 16 LSBs from RTC memory word given by the sum of value in reg_addr and
  390. * value of offset_.
  391. */
  392. #define I_LD(reg_dest, reg_addr, offset_) { .ld = { \
  393. .dreg = reg_dest, \
  394. .sreg = reg_addr, \
  395. .unused1 = 0, \
  396. .offset = offset_, \
  397. .unused2 = 0, \
  398. .opcode = OPCODE_LD } }
  399. /**
  400. * Branch relative if R0 less than immediate value.
  401. *
  402. * pc_offset is expressed in words, and can be from -127 to 127
  403. * imm_value is a 16-bit value to compare R0 against
  404. */
  405. #define I_BL(pc_offset, imm_value) { .b = { \
  406. .imm = imm_value, \
  407. .cmp = B_CMP_L, \
  408. .offset = abs(pc_offset), \
  409. .sign = (pc_offset >= 0) ? 0 : 1, \
  410. .sub_opcode = SUB_OPCODE_B, \
  411. .opcode = OPCODE_BRANCH } }
  412. /**
  413. * Branch relative if R0 greater or equal than immediate value.
  414. *
  415. * pc_offset is expressed in words, and can be from -127 to 127
  416. * imm_value is a 16-bit value to compare R0 against
  417. */
  418. #define I_BGE(pc_offset, imm_value) { .b = { \
  419. .imm = imm_value, \
  420. .cmp = B_CMP_GE, \
  421. .offset = abs(pc_offset), \
  422. .sign = (pc_offset >= 0) ? 0 : 1, \
  423. .sub_opcode = SUB_OPCODE_B, \
  424. .opcode = OPCODE_BRANCH } }
  425. /**
  426. * Unconditional branch to absolute PC, address in register.
  427. *
  428. * reg_pc is the register which contains address to jump to.
  429. * Address is expressed in 32-bit words.
  430. */
  431. #define I_BXR(reg_pc) { .bx = { \
  432. .dreg = reg_pc, \
  433. .addr = 0, \
  434. .unused = 0, \
  435. .reg = 1, \
  436. .type = BX_JUMP_TYPE_DIRECT, \
  437. .sub_opcode = SUB_OPCODE_BX, \
  438. .opcode = OPCODE_BRANCH } }
  439. /**
  440. * Unconditional branch to absolute PC, immediate address.
  441. *
  442. * Address imm_pc is expressed in 32-bit words.
  443. */
  444. #define I_BXI(imm_pc) { .bx = { \
  445. .dreg = 0, \
  446. .addr = imm_pc, \
  447. .unused = 0, \
  448. .reg = 0, \
  449. .type = BX_JUMP_TYPE_DIRECT, \
  450. .sub_opcode = SUB_OPCODE_BX, \
  451. .opcode = OPCODE_BRANCH } }
  452. /**
  453. * Branch to absolute PC if ALU result is zero, address in register.
  454. *
  455. * reg_pc is the register which contains address to jump to.
  456. * Address is expressed in 32-bit words.
  457. */
  458. #define I_BXZR(reg_pc) { .bx = { \
  459. .dreg = reg_pc, \
  460. .addr = 0, \
  461. .unused = 0, \
  462. .reg = 1, \
  463. .type = BX_JUMP_TYPE_ZERO, \
  464. .sub_opcode = SUB_OPCODE_BX, \
  465. .opcode = OPCODE_BRANCH } }
  466. /**
  467. * Branch to absolute PC if ALU result is zero, immediate address.
  468. *
  469. * Address imm_pc is expressed in 32-bit words.
  470. */
  471. #define I_BXZI(imm_pc) { .bx = { \
  472. .dreg = 0, \
  473. .addr = imm_pc, \
  474. .unused = 0, \
  475. .reg = 0, \
  476. .type = BX_JUMP_TYPE_ZERO, \
  477. .sub_opcode = SUB_OPCODE_BX, \
  478. .opcode = OPCODE_BRANCH } }
  479. /**
  480. * Branch to absolute PC if ALU overflow, address in register
  481. *
  482. * reg_pc is the register which contains address to jump to.
  483. * Address is expressed in 32-bit words.
  484. */
  485. #define I_BXFR(reg_pc) { .bx = { \
  486. .dreg = reg_pc, \
  487. .addr = 0, \
  488. .unused = 0, \
  489. .reg = 1, \
  490. .type = BX_JUMP_TYPE_OVF, \
  491. .sub_opcode = SUB_OPCODE_BX, \
  492. .opcode = OPCODE_BRANCH } }
  493. /**
  494. * Branch to absolute PC if ALU overflow, immediate address
  495. *
  496. * Address imm_pc is expressed in 32-bit words.
  497. */
  498. #define I_BXFI(imm_pc) { .bx = { \
  499. .dreg = 0, \
  500. .addr = imm_pc, \
  501. .unused = 0, \
  502. .reg = 0, \
  503. .type = BX_JUMP_TYPE_OVF, \
  504. .sub_opcode = SUB_OPCODE_BX, \
  505. .opcode = OPCODE_BRANCH } }
  506. /**
  507. * Addition: dest = src1 + src2
  508. */
  509. #define I_ADDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  510. .dreg = reg_dest, \
  511. .sreg = reg_src1, \
  512. .treg = reg_src2, \
  513. .unused = 0, \
  514. .sel = ALU_SEL_ADD, \
  515. .sub_opcode = SUB_OPCODE_ALU_REG, \
  516. .opcode = OPCODE_ALU } }
  517. /**
  518. * Subtraction: dest = src1 - src2
  519. */
  520. #define I_SUBR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  521. .dreg = reg_dest, \
  522. .sreg = reg_src1, \
  523. .treg = reg_src2, \
  524. .unused = 0, \
  525. .sel = ALU_SEL_SUB, \
  526. .sub_opcode = SUB_OPCODE_ALU_REG, \
  527. .opcode = OPCODE_ALU } }
  528. /**
  529. * Logical AND: dest = src1 & src2
  530. */
  531. #define I_ANDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  532. .dreg = reg_dest, \
  533. .sreg = reg_src1, \
  534. .treg = reg_src2, \
  535. .unused = 0, \
  536. .sel = ALU_SEL_AND, \
  537. .sub_opcode = SUB_OPCODE_ALU_REG, \
  538. .opcode = OPCODE_ALU } }
  539. /**
  540. * Logical OR: dest = src1 | src2
  541. */
  542. #define I_ORR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  543. .dreg = reg_dest, \
  544. .sreg = reg_src1, \
  545. .treg = reg_src2, \
  546. .unused = 0, \
  547. .sel = ALU_SEL_OR, \
  548. .sub_opcode = SUB_OPCODE_ALU_REG, \
  549. .opcode = OPCODE_ALU } }
  550. /**
  551. * Copy: dest = src
  552. */
  553. #define I_MOVR(reg_dest, reg_src) { .alu_reg = { \
  554. .dreg = reg_dest, \
  555. .sreg = reg_src, \
  556. .treg = 0, \
  557. .unused = 0, \
  558. .sel = ALU_SEL_MOV, \
  559. .sub_opcode = SUB_OPCODE_ALU_REG, \
  560. .opcode = OPCODE_ALU } }
  561. /**
  562. * Logical shift left: dest = src << shift
  563. */
  564. #define I_LSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  565. .dreg = reg_dest, \
  566. .sreg = reg_src, \
  567. .treg = reg_shift, \
  568. .unused = 0, \
  569. .sel = ALU_SEL_LSH, \
  570. .sub_opcode = SUB_OPCODE_ALU_REG, \
  571. .opcode = OPCODE_ALU } }
  572. /**
  573. * Logical shift right: dest = src >> shift
  574. */
  575. #define I_RSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  576. .dreg = reg_dest, \
  577. .sreg = reg_src, \
  578. .treg = reg_shift, \
  579. .unused = 0, \
  580. .sel = ALU_SEL_RSH, \
  581. .sub_opcode = SUB_OPCODE_ALU_REG, \
  582. .opcode = OPCODE_ALU } }
  583. /**
  584. * Add register and an immediate value: dest = src1 + imm
  585. */
  586. #define I_ADDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  587. .dreg = reg_dest, \
  588. .sreg = reg_src, \
  589. .imm = imm_, \
  590. .unused = 0, \
  591. .sel = ALU_SEL_ADD, \
  592. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  593. .opcode = OPCODE_ALU } }
  594. /**
  595. * Subtract register and an immediate value: dest = src - imm
  596. */
  597. #define I_SUBI(reg_dest, reg_src, imm_) { .alu_imm = { \
  598. .dreg = reg_dest, \
  599. .sreg = reg_src, \
  600. .imm = imm_, \
  601. .unused = 0, \
  602. .sel = ALU_SEL_SUB, \
  603. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  604. .opcode = OPCODE_ALU } }
  605. /**
  606. * Logical AND register and an immediate value: dest = src & imm
  607. */
  608. #define I_ANDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  609. .dreg = reg_dest, \
  610. .sreg = reg_src, \
  611. .imm = imm_, \
  612. .unused = 0, \
  613. .sel = ALU_SEL_AND, \
  614. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  615. .opcode = OPCODE_ALU } }
  616. /**
  617. * Logical OR register and an immediate value: dest = src | imm
  618. */
  619. #define I_ORI(reg_dest, reg_src, imm_) { .alu_imm = { \
  620. .dreg = reg_dest, \
  621. .sreg = reg_src, \
  622. .imm = imm_, \
  623. .unused = 0, \
  624. .sel = ALU_SEL_OR, \
  625. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  626. .opcode = OPCODE_ALU } }
  627. /**
  628. * Copy an immediate value into register: dest = imm
  629. */
  630. #define I_MOVI(reg_dest, imm_) { .alu_imm = { \
  631. .dreg = reg_dest, \
  632. .sreg = 0, \
  633. .imm = imm_, \
  634. .unused = 0, \
  635. .sel = ALU_SEL_MOV, \
  636. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  637. .opcode = OPCODE_ALU } }
  638. /**
  639. * Logical shift left register value by an immediate: dest = src << imm
  640. */
  641. #define I_LSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  642. .dreg = reg_dest, \
  643. .sreg = reg_src, \
  644. .imm = imm_, \
  645. .unused = 0, \
  646. .sel = ALU_SEL_LSH, \
  647. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  648. .opcode = OPCODE_ALU } }
  649. /**
  650. * Logical shift right register value by an immediate: dest = val >> imm
  651. */
  652. #define I_RSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  653. .dreg = reg_dest, \
  654. .sreg = reg_src, \
  655. .imm = imm_, \
  656. .unused = 0, \
  657. .sel = ALU_SEL_RSH, \
  658. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  659. .opcode = OPCODE_ALU } }
  660. /**
  661. * Define a label with number label_num.
  662. *
  663. * This is a macro which doesn't generate a real instruction.
  664. * The token generated by this macro is removed by ulp_process_macros_and_load
  665. * function. Label defined using this macro can be used in branch macros defined
  666. * below.
  667. */
  668. #define M_LABEL(label_num) { .macro = { \
  669. .label = label_num, \
  670. .unused = 0, \
  671. .sub_opcode = SUB_OPCODE_MACRO_LABEL, \
  672. .opcode = OPCODE_MACRO } }
  673. /**
  674. * Token macro used by M_B and M_BX macros. Not to be used directly.
  675. */
  676. #define M_BRANCH(label_num) { .macro = { \
  677. .label = label_num, \
  678. .unused = 0, \
  679. .sub_opcode = SUB_OPCODE_MACRO_BRANCH, \
  680. .opcode = OPCODE_MACRO } }
  681. /**
  682. * Macro: branch to label label_num if R0 is less than immediate value.
  683. *
  684. * This macro generates two ulp_insn_t values separated by a comma, and should
  685. * be used when defining contents of ulp_insn_t arrays. First value is not a
  686. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  687. * function.
  688. */
  689. #define M_BL(label_num, imm_value) \
  690. M_BRANCH(label_num), \
  691. I_BL(0, imm_value)
  692. /**
  693. * Macro: branch to label label_num if R0 is greater or equal than immediate value
  694. *
  695. * This macro generates two ulp_insn_t values separated by a comma, and should
  696. * be used when defining contents of ulp_insn_t arrays. First value is not a
  697. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  698. * function.
  699. */
  700. #define M_BGE(label_num, imm_value) \
  701. M_BRANCH(label_num), \
  702. I_BGE(0, imm_value)
  703. /**
  704. * Macro: unconditional branch to label
  705. *
  706. * This macro generates two ulp_insn_t values separated by a comma, and should
  707. * be used when defining contents of ulp_insn_t arrays. First value is not a
  708. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  709. * function.
  710. */
  711. #define M_BX(label_num) \
  712. M_BRANCH(label_num), \
  713. I_BXI(0)
  714. /**
  715. * Macro: branch to label if ALU result is zero
  716. *
  717. * This macro generates two ulp_insn_t values separated by a comma, and should
  718. * be used when defining contents of ulp_insn_t arrays. First value is not a
  719. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  720. * function.
  721. */
  722. #define M_BXZ(label_num) \
  723. M_BRANCH(label_num), \
  724. I_BXZI(0)
  725. /**
  726. * Macro: branch to label if ALU overflow
  727. *
  728. * This macro generates two ulp_insn_t values separated by a comma, and should
  729. * be used when defining contents of ulp_insn_t arrays. First value is not a
  730. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  731. * function.
  732. */
  733. #define M_BXF(label_num) \
  734. M_BRANCH(label_num), \
  735. I_BXFI(0)
  736. #ifdef __cplusplus
  737. }
  738. #endif