ulp.h 29 KB

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