ulp.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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_RD_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. uint32_t ret = 3;
  247. if (reg < DR_REG_RTCCNTL_BASE) {
  248. assert(0 && "invalid register base");
  249. } else if (reg < DR_REG_RTCIO_BASE) {
  250. ret = RD_REG_PERIPH_RTC_CNTL;
  251. } else if (reg < DR_REG_SENS_BASE) {
  252. ret = RD_REG_PERIPH_RTC_IO;
  253. } else if (reg < DR_REG_RTC_I2C_BASE){
  254. ret = RD_REG_PERIPH_SENS;
  255. } else if (reg < DR_REG_IO_MUX_BASE){
  256. ret = RD_REG_PERIPH_RTC_I2C;
  257. } else {
  258. assert(0 && "invalid register base");
  259. }
  260. return ret;
  261. }
  262. /**
  263. * Write literal value to a peripheral register
  264. *
  265. * reg[high_bit : low_bit] = val
  266. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  267. */
  268. #define I_WR_REG(reg, low_bit, high_bit, val) {.wr_reg = {\
  269. .addr = (reg & 0xff) / sizeof(uint32_t), \
  270. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  271. .data = val, \
  272. .low = low_bit, \
  273. .high = high_bit, \
  274. .opcode = OPCODE_WR_REG } }
  275. /**
  276. * Read from peripheral register into R0
  277. *
  278. * R0 = reg[high_bit : low_bit]
  279. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  280. */
  281. #define I_RD_REG(reg, low_bit, high_bit) {.rd_reg = {\
  282. .addr = (reg & 0xff) / sizeof(uint32_t), \
  283. .periph_sel = SOC_REG_TO_ULP_PERIPH_SEL(reg), \
  284. .unused = 0, \
  285. .low = low_bit, \
  286. .high = high_bit, \
  287. .opcode = OPCODE_RD_REG } }
  288. /**
  289. * Set or clear a bit in the peripheral register.
  290. *
  291. * Sets bit (1 << shift) of register reg to value val.
  292. * This instruction can access RTC_CNTL_, RTC_IO_, SENS_, and RTC_I2C peripheral registers.
  293. */
  294. #define I_WR_REG_BIT(reg, shift, val) I_WR_REG(reg, shift, shift, val)
  295. /**
  296. * Wake the SoC from deep sleep.
  297. *
  298. * This instruction initiates wake up from deep sleep.
  299. * Use esp_deep_sleep_enable_ulp_wakeup to enable deep sleep wakeup
  300. * triggered by the ULP before going into deep sleep.
  301. * Note that ULP program will still keep running until the I_HALT
  302. * instruction, and it will still be restarted by timer at regular
  303. * intervals, even when the SoC is woken up.
  304. *
  305. * To stop the ULP program, use I_HALT instruction.
  306. *
  307. * To disable the timer which start ULP program, use I_END()
  308. * instruction. I_END instruction clears the
  309. * RTC_CNTL_ULP_CP_SLP_TIMER_EN_S bit of RTC_CNTL_STATE0_REG
  310. * register, which controls the ULP timer.
  311. */
  312. #define I_WAKE() { .end = { \
  313. .wakeup = 1, \
  314. .unused = 0, \
  315. .sub_opcode = SUB_OPCODE_END, \
  316. .opcode = OPCODE_END } }
  317. /**
  318. * Stop ULP program timer.
  319. *
  320. * This is a convenience macro which disables the ULP program timer.
  321. * Once this instruction is used, ULP program will not be restarted
  322. * anymore until ulp_run function is called.
  323. *
  324. * ULP program will continue running after this instruction. To stop
  325. * the currently running program, use I_HALT().
  326. */
  327. #define I_END() \
  328. I_WR_REG_BIT(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN_S, 0)
  329. /**
  330. * Select the time interval used to run ULP program.
  331. *
  332. * This instructions selects which of the SENS_SLEEP_CYCLES_Sx
  333. * registers' value is used by the ULP program timer.
  334. * When the ULP program stops at I_HALT instruction, ULP program
  335. * timer start counting. When the counter reaches the value of
  336. * the selected SENS_SLEEP_CYCLES_Sx register, ULP program
  337. * start running again from the start address (passed to the ulp_run
  338. * function).
  339. * There are 5 SENS_SLEEP_CYCLES_Sx registers, so 0 <= timer_idx < 5.
  340. *
  341. * By default, SENS_SLEEP_CYCLES_S0 register is used by the ULP
  342. * program timer.
  343. */
  344. #define I_SLEEP_CYCLE_SEL(timer_idx) { .sleep = { \
  345. .cycle_sel = timer_idx, \
  346. .unused = 0, \
  347. .sub_opcode = SUB_OPCODE_SLEEP, \
  348. .opcode = OPCODE_END } }
  349. /**
  350. * Perform temperature sensor measurement and store it into reg_dest.
  351. *
  352. * Delay can be set between 1 and ((1 << 14) - 1). Higher values give
  353. * higher measurement resolution.
  354. */
  355. #define I_TSENS(reg_dest, delay) { .tsens = { \
  356. .dreg = reg_dest, \
  357. .wait_delay = delay, \
  358. .reserved = 0, \
  359. .opcode = OPCODE_TSENS } }
  360. /**
  361. * Perform ADC measurement and store result in reg_dest.
  362. *
  363. * adc_idx selects ADC (0 or 1).
  364. * pad_idx selects ADC pad (0 - 7).
  365. */
  366. #define I_ADC(reg_dest, adc_idx, pad_idx) { .adc = {\
  367. .dreg = reg_dest, \
  368. .mux = pad_idx + 1, \
  369. .sar_sel = adc_idx, \
  370. .unused1 = 0, \
  371. .cycles = 0, \
  372. .unused2 = 0, \
  373. .opcode = OPCODE_ADC } }
  374. /**
  375. * Store value from register reg_val into RTC memory.
  376. *
  377. * The value is written to an offset calculated by adding value of
  378. * reg_addr register and offset_ field (this offset is expressed in 32-bit words).
  379. * 32 bits written to RTC memory are built as follows:
  380. * - bits [31:21] hold the PC of current instruction, expressed in 32-bit words
  381. * - bits [20:16] = 5'b1
  382. * - bits [15:0] are assigned the contents of reg_val
  383. *
  384. * RTC_SLOW_MEM[addr + offset_] = { 5'b0, insn_PC[10:0], val[15:0] }
  385. */
  386. #define I_ST(reg_val, reg_addr, offset_) { .st = { \
  387. .dreg = reg_val, \
  388. .sreg = reg_addr, \
  389. .unused1 = 0, \
  390. .offset = offset_, \
  391. .unused2 = 0, \
  392. .sub_opcode = SUB_OPCODE_ST, \
  393. .opcode = OPCODE_ST } }
  394. /**
  395. * Load value from RTC memory into reg_dest register.
  396. *
  397. * Loads 16 LSBs from RTC memory word given by the sum of value in reg_addr and
  398. * value of offset_.
  399. */
  400. #define I_LD(reg_dest, reg_addr, offset_) { .ld = { \
  401. .dreg = reg_dest, \
  402. .sreg = reg_addr, \
  403. .unused1 = 0, \
  404. .offset = offset_, \
  405. .unused2 = 0, \
  406. .opcode = OPCODE_LD } }
  407. /**
  408. * Branch relative if R0 less than immediate value.
  409. *
  410. * pc_offset is expressed in words, and can be from -127 to 127
  411. * imm_value is a 16-bit value to compare R0 against
  412. */
  413. #define I_BL(pc_offset, imm_value) { .b = { \
  414. .imm = imm_value, \
  415. .cmp = B_CMP_L, \
  416. .offset = abs(pc_offset), \
  417. .sign = (pc_offset >= 0) ? 0 : 1, \
  418. .sub_opcode = SUB_OPCODE_B, \
  419. .opcode = OPCODE_BRANCH } }
  420. /**
  421. * Branch relative if R0 greater or equal than immediate value.
  422. *
  423. * pc_offset is expressed in words, and can be from -127 to 127
  424. * imm_value is a 16-bit value to compare R0 against
  425. */
  426. #define I_BGE(pc_offset, imm_value) { .b = { \
  427. .imm = imm_value, \
  428. .cmp = B_CMP_GE, \
  429. .offset = abs(pc_offset), \
  430. .sign = (pc_offset >= 0) ? 0 : 1, \
  431. .sub_opcode = SUB_OPCODE_B, \
  432. .opcode = OPCODE_BRANCH } }
  433. /**
  434. * Unconditional branch to absolute PC, address in register.
  435. *
  436. * reg_pc is the register which contains address to jump to.
  437. * Address is expressed in 32-bit words.
  438. */
  439. #define I_BXR(reg_pc) { .bx = { \
  440. .dreg = reg_pc, \
  441. .addr = 0, \
  442. .unused = 0, \
  443. .reg = 1, \
  444. .type = BX_JUMP_TYPE_DIRECT, \
  445. .sub_opcode = SUB_OPCODE_BX, \
  446. .opcode = OPCODE_BRANCH } }
  447. /**
  448. * Unconditional branch to absolute PC, immediate address.
  449. *
  450. * Address imm_pc is expressed in 32-bit words.
  451. */
  452. #define I_BXI(imm_pc) { .bx = { \
  453. .dreg = 0, \
  454. .addr = imm_pc, \
  455. .unused = 0, \
  456. .reg = 0, \
  457. .type = BX_JUMP_TYPE_DIRECT, \
  458. .sub_opcode = SUB_OPCODE_BX, \
  459. .opcode = OPCODE_BRANCH } }
  460. /**
  461. * Branch to absolute PC if ALU result is zero, address in register.
  462. *
  463. * reg_pc is the register which contains address to jump to.
  464. * Address is expressed in 32-bit words.
  465. */
  466. #define I_BXZR(reg_pc) { .bx = { \
  467. .dreg = reg_pc, \
  468. .addr = 0, \
  469. .unused = 0, \
  470. .reg = 1, \
  471. .type = BX_JUMP_TYPE_ZERO, \
  472. .sub_opcode = SUB_OPCODE_BX, \
  473. .opcode = OPCODE_BRANCH } }
  474. /**
  475. * Branch to absolute PC if ALU result is zero, immediate address.
  476. *
  477. * Address imm_pc is expressed in 32-bit words.
  478. */
  479. #define I_BXZI(imm_pc) { .bx = { \
  480. .dreg = 0, \
  481. .addr = imm_pc, \
  482. .unused = 0, \
  483. .reg = 0, \
  484. .type = BX_JUMP_TYPE_ZERO, \
  485. .sub_opcode = SUB_OPCODE_BX, \
  486. .opcode = OPCODE_BRANCH } }
  487. /**
  488. * Branch to absolute PC if ALU overflow, address in register
  489. *
  490. * reg_pc is the register which contains address to jump to.
  491. * Address is expressed in 32-bit words.
  492. */
  493. #define I_BXFR(reg_pc) { .bx = { \
  494. .dreg = reg_pc, \
  495. .addr = 0, \
  496. .unused = 0, \
  497. .reg = 1, \
  498. .type = BX_JUMP_TYPE_OVF, \
  499. .sub_opcode = SUB_OPCODE_BX, \
  500. .opcode = OPCODE_BRANCH } }
  501. /**
  502. * Branch to absolute PC if ALU overflow, immediate address
  503. *
  504. * Address imm_pc is expressed in 32-bit words.
  505. */
  506. #define I_BXFI(imm_pc) { .bx = { \
  507. .dreg = 0, \
  508. .addr = imm_pc, \
  509. .unused = 0, \
  510. .reg = 0, \
  511. .type = BX_JUMP_TYPE_OVF, \
  512. .sub_opcode = SUB_OPCODE_BX, \
  513. .opcode = OPCODE_BRANCH } }
  514. /**
  515. * Addition: dest = src1 + src2
  516. */
  517. #define I_ADDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  518. .dreg = reg_dest, \
  519. .sreg = reg_src1, \
  520. .treg = reg_src2, \
  521. .unused = 0, \
  522. .sel = ALU_SEL_ADD, \
  523. .sub_opcode = SUB_OPCODE_ALU_REG, \
  524. .opcode = OPCODE_ALU } }
  525. /**
  526. * Subtraction: dest = src1 - src2
  527. */
  528. #define I_SUBR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  529. .dreg = reg_dest, \
  530. .sreg = reg_src1, \
  531. .treg = reg_src2, \
  532. .unused = 0, \
  533. .sel = ALU_SEL_SUB, \
  534. .sub_opcode = SUB_OPCODE_ALU_REG, \
  535. .opcode = OPCODE_ALU } }
  536. /**
  537. * Logical AND: dest = src1 & src2
  538. */
  539. #define I_ANDR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  540. .dreg = reg_dest, \
  541. .sreg = reg_src1, \
  542. .treg = reg_src2, \
  543. .unused = 0, \
  544. .sel = ALU_SEL_AND, \
  545. .sub_opcode = SUB_OPCODE_ALU_REG, \
  546. .opcode = OPCODE_ALU } }
  547. /**
  548. * Logical OR: dest = src1 | src2
  549. */
  550. #define I_ORR(reg_dest, reg_src1, reg_src2) { .alu_reg = { \
  551. .dreg = reg_dest, \
  552. .sreg = reg_src1, \
  553. .treg = reg_src2, \
  554. .unused = 0, \
  555. .sel = ALU_SEL_OR, \
  556. .sub_opcode = SUB_OPCODE_ALU_REG, \
  557. .opcode = OPCODE_ALU } }
  558. /**
  559. * Copy: dest = src
  560. */
  561. #define I_MOVR(reg_dest, reg_src) { .alu_reg = { \
  562. .dreg = reg_dest, \
  563. .sreg = reg_src, \
  564. .treg = 0, \
  565. .unused = 0, \
  566. .sel = ALU_SEL_MOV, \
  567. .sub_opcode = SUB_OPCODE_ALU_REG, \
  568. .opcode = OPCODE_ALU } }
  569. /**
  570. * Logical shift left: dest = src << shift
  571. */
  572. #define I_LSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  573. .dreg = reg_dest, \
  574. .sreg = reg_src, \
  575. .treg = reg_shift, \
  576. .unused = 0, \
  577. .sel = ALU_SEL_LSH, \
  578. .sub_opcode = SUB_OPCODE_ALU_REG, \
  579. .opcode = OPCODE_ALU } }
  580. /**
  581. * Logical shift right: dest = src >> shift
  582. */
  583. #define I_RSHR(reg_dest, reg_src, reg_shift) { .alu_reg = { \
  584. .dreg = reg_dest, \
  585. .sreg = reg_src, \
  586. .treg = reg_shift, \
  587. .unused = 0, \
  588. .sel = ALU_SEL_RSH, \
  589. .sub_opcode = SUB_OPCODE_ALU_REG, \
  590. .opcode = OPCODE_ALU } }
  591. /**
  592. * Add register and an immediate value: dest = src1 + imm
  593. */
  594. #define I_ADDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  595. .dreg = reg_dest, \
  596. .sreg = reg_src, \
  597. .imm = imm_, \
  598. .unused = 0, \
  599. .sel = ALU_SEL_ADD, \
  600. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  601. .opcode = OPCODE_ALU } }
  602. /**
  603. * Subtract register and an immediate value: dest = src - imm
  604. */
  605. #define I_SUBI(reg_dest, reg_src, imm_) { .alu_imm = { \
  606. .dreg = reg_dest, \
  607. .sreg = reg_src, \
  608. .imm = imm_, \
  609. .unused = 0, \
  610. .sel = ALU_SEL_SUB, \
  611. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  612. .opcode = OPCODE_ALU } }
  613. /**
  614. * Logical AND register and an immediate value: dest = src & imm
  615. */
  616. #define I_ANDI(reg_dest, reg_src, imm_) { .alu_imm = { \
  617. .dreg = reg_dest, \
  618. .sreg = reg_src, \
  619. .imm = imm_, \
  620. .unused = 0, \
  621. .sel = ALU_SEL_AND, \
  622. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  623. .opcode = OPCODE_ALU } }
  624. /**
  625. * Logical OR register and an immediate value: dest = src | imm
  626. */
  627. #define I_ORI(reg_dest, reg_src, imm_) { .alu_imm = { \
  628. .dreg = reg_dest, \
  629. .sreg = reg_src, \
  630. .imm = imm_, \
  631. .unused = 0, \
  632. .sel = ALU_SEL_OR, \
  633. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  634. .opcode = OPCODE_ALU } }
  635. /**
  636. * Copy an immediate value into register: dest = imm
  637. */
  638. #define I_MOVI(reg_dest, imm_) { .alu_imm = { \
  639. .dreg = reg_dest, \
  640. .sreg = 0, \
  641. .imm = imm_, \
  642. .unused = 0, \
  643. .sel = ALU_SEL_MOV, \
  644. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  645. .opcode = OPCODE_ALU } }
  646. /**
  647. * Logical shift left register value by an immediate: dest = src << imm
  648. */
  649. #define I_LSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  650. .dreg = reg_dest, \
  651. .sreg = reg_src, \
  652. .imm = imm_, \
  653. .unused = 0, \
  654. .sel = ALU_SEL_LSH, \
  655. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  656. .opcode = OPCODE_ALU } }
  657. /**
  658. * Logical shift right register value by an immediate: dest = val >> imm
  659. */
  660. #define I_RSHI(reg_dest, reg_src, imm_) { .alu_imm = { \
  661. .dreg = reg_dest, \
  662. .sreg = reg_src, \
  663. .imm = imm_, \
  664. .unused = 0, \
  665. .sel = ALU_SEL_RSH, \
  666. .sub_opcode = SUB_OPCODE_ALU_IMM, \
  667. .opcode = OPCODE_ALU } }
  668. /**
  669. * Define a label with number label_num.
  670. *
  671. * This is a macro which doesn't generate a real instruction.
  672. * The token generated by this macro is removed by ulp_process_macros_and_load
  673. * function. Label defined using this macro can be used in branch macros defined
  674. * below.
  675. */
  676. #define M_LABEL(label_num) { .macro = { \
  677. .label = label_num, \
  678. .unused = 0, \
  679. .sub_opcode = SUB_OPCODE_MACRO_LABEL, \
  680. .opcode = OPCODE_MACRO } }
  681. /**
  682. * Token macro used by M_B and M_BX macros. Not to be used directly.
  683. */
  684. #define M_BRANCH(label_num) { .macro = { \
  685. .label = label_num, \
  686. .unused = 0, \
  687. .sub_opcode = SUB_OPCODE_MACRO_BRANCH, \
  688. .opcode = OPCODE_MACRO } }
  689. /**
  690. * Macro: branch to label label_num if R0 is less than immediate value.
  691. *
  692. * This macro generates two ulp_insn_t values separated by a comma, and should
  693. * be used when defining contents of ulp_insn_t arrays. First value is not a
  694. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  695. * function.
  696. */
  697. #define M_BL(label_num, imm_value) \
  698. M_BRANCH(label_num), \
  699. I_BL(0, imm_value)
  700. /**
  701. * Macro: branch to label label_num if R0 is greater or equal than immediate value
  702. *
  703. * This macro generates two ulp_insn_t values separated by a comma, and should
  704. * be used when defining contents of ulp_insn_t arrays. First value is not a
  705. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  706. * function.
  707. */
  708. #define M_BGE(label_num, imm_value) \
  709. M_BRANCH(label_num), \
  710. I_BGE(0, imm_value)
  711. /**
  712. * Macro: unconditional branch to label
  713. *
  714. * This macro generates two ulp_insn_t values separated by a comma, and should
  715. * be used when defining contents of ulp_insn_t arrays. First value is not a
  716. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  717. * function.
  718. */
  719. #define M_BX(label_num) \
  720. M_BRANCH(label_num), \
  721. I_BXI(0)
  722. /**
  723. * Macro: branch to label if ALU result is zero
  724. *
  725. * This macro generates two ulp_insn_t values separated by a comma, and should
  726. * be used when defining contents of ulp_insn_t arrays. First value is not a
  727. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  728. * function.
  729. */
  730. #define M_BXZ(label_num) \
  731. M_BRANCH(label_num), \
  732. I_BXZI(0)
  733. /**
  734. * Macro: branch to label if ALU overflow
  735. *
  736. * This macro generates two ulp_insn_t values separated by a comma, and should
  737. * be used when defining contents of ulp_insn_t arrays. First value is not a
  738. * real instruction; it is a token which is removed by ulp_process_macros_and_load
  739. * function.
  740. */
  741. #define M_BXF(label_num) \
  742. M_BRANCH(label_num), \
  743. I_BXFI(0)
  744. #define RTC_SLOW_MEM ((uint32_t*) 0x50000000) /*!< RTC slow memory, 8k size */
  745. #ifdef __cplusplus
  746. }
  747. #endif