ulps2_instruction_set.rst 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. ESP32-S2 ULP coprocessor instruction set
  2. ========================================
  3. This document provides details about the instructions used by ESP32-S2 ULP coprocessor assembler.
  4. ULP coprocessor has 4 16-bit general purpose registers, labeled R0, R1, R2, R3. It also has an 8-bit counter register (stage_cnt) which can be used to implement loops. Stage count regiter is accessed using special instructions.
  5. ULP coprocessor can access 8k bytes of RTC_SLOW_MEM memory region. Memory is addressed in 32-bit word units. It can also access peripheral registers in RTC_CNTL, RTC_IO, and SENS peripherals.
  6. All instructions are 32-bit. Jump instructions, ALU instructions, peripheral register and memory access instructions are executed in 1 cycle. Instructions which work with peripherals (TSENS, ADC, I2C) take variable number of cycles, depending on peripheral operation.
  7. The instruction syntax is case insensitive. Upper and lower case letters can be used and intermixed arbitrarily. This is true both for register names and instruction names.
  8. Note about addressing
  9. ---------------------
  10. ESP32-S2 ULP coprocessor's JUMP, ST, LD instructions which take register as an argument (jump address, store/load base address) expect the argument to be expressed in 32-bit words.
  11. Consider the following example program::
  12. entry:
  13. NOP
  14. NOP
  15. NOP
  16. NOP
  17. loop:
  18. MOVE R1, loop
  19. JUMP R1
  20. When this program is assembled and linked, address of label ``loop`` will be equal to 16 (expressed in bytes). However `JUMP` instruction expects the address stored in register to be expressed in 32-bit words. To account for this common use case, assembler will convert the address of label `loop` from bytes to words, when generating ``MOVE`` instruction, so the code generated code will be equivalent to::
  21. 0000 NOP
  22. 0004 NOP
  23. 0008 NOP
  24. 000c NOP
  25. 0010 MOVE R1, 4
  26. 0014 JUMP R1
  27. The other case is when the argument of ``MOVE`` instruction is not a label but a constant. In this case assembler will use the value as is, without any conversion::
  28. .set val, 0x10
  29. MOVE R1, val
  30. In this case, value loaded into R1 will be ``0x10``.
  31. Similar considerations apply to ``LD`` and ``ST`` instructions. Consider the following code::
  32. .global array
  33. array: .long 0
  34. .long 0
  35. .long 0
  36. .long 0
  37. MOVE R1, array
  38. MOVE R2, 0x1234
  39. ST R2, R1, 0 // write value of R2 into the first array element,
  40. // i.e. array[0]
  41. ST R2, R1, 4 // write value of R2 into the second array element
  42. // (4 byte offset), i.e. array[1]
  43. ADD R1, R1, 2 // this increments address by 2 words (8 bytes)
  44. ST R2, R1, 0 // write value of R2 into the third array element,
  45. // i.e. array[2]
  46. Note about instruction execution time
  47. -------------------------------------
  48. ULP coprocessor is clocked from RTC_FAST_CLK, which is normally derived from the internal 8MHz oscillator. Applications which need to know exact ULP clock frequency can calibrate it against the main XTAL clock::
  49. #include "soc/rtc.h"
  50. // calibrate 8M/256 clock against XTAL, get 8M/256 clock period
  51. uint32_t rtc_8md256_period = rtc_clk_cal(RTC_CAL_8MD256, 100);
  52. uint32_t rtc_fast_freq_hz = 1000000ULL * (1 << RTC_CLK_CAL_FRACT) * 256 / rtc_8md256_period;
  53. ULP coprocessor needs certain number of clock cycles to fetch each instruction, plus certain number of cycles to execute it, depending on the instruction. See description of each instruction below for details on the execution time.
  54. Instruction fetch time is:
  55. - 2 clock cycles — for instructions following ALU and branch instructions.
  56. - 4 clock cycles — in other cases.
  57. Note that when accessing RTC memories and RTC registers, ULP coprocessor has lower priority than the main CPUs. This means that ULP coprocessor execution may be suspended while the main CPUs access same memory region as the ULP.
  58. Difference between ESP32 ULP and ESP32-S2 ULP Instruction sets
  59. --------------------------------------------------------------
  60. Compare to the ESP32 ULP coprocessor, the ESP-S2 ULP coprocessor has extended instruction set. The ESP32-S2 ULP is not binary compatible with ESP32 ULP,
  61. but the assembled program that was written for the ESP32 ULP will also work on the ESP32-S2 ULP after rebuild.
  62. The list of the new instructions that was added to the ESP32-S2 ULP is: LDL, LDH, STO, ST32, STI32.
  63. The detailed description of these commands please see below.
  64. **NOP** - no operation
  65. ----------------------
  66. **Syntax**
  67. **NOP**
  68. **Operands**
  69. None
  70. **Cycles**
  71. 2 cycle to execute, 4 cycles to fetch next instruction
  72. **Description**
  73. No operation is performed. Only the PC is incremented.
  74. **Example**::
  75. 1: NOP
  76. **ADD** - Add to register
  77. -------------------------
  78. **Syntax**
  79. **ADD** *Rdst, Rsrc1, Rsrc2*
  80. **ADD** *Rdst, Rsrc1, imm*
  81. **Operands**
  82. - *Rdst* - Register R[0..3]
  83. - *Rsrc1* - Register R[0..3]
  84. - *Rsrc2* - Register R[0..3]
  85. - *Imm* - 16-bit signed value
  86. **Cycles**
  87. 2 cycles to execute, 4 cycles to fetch next instruction
  88. **Description**
  89. The instruction adds source register to another source register or to a 16-bit signed value and stores result to the destination register.
  90. **Examples**::
  91. 1: ADD R1, R2, R3 //R1 = R2 + R3
  92. 2: Add R1, R2, 0x1234 //R1 = R2 + 0x1234
  93. 3: .set value1, 0x03 //constant value1=0x03
  94. Add R1, R2, value1 //R1 = R2 + value1
  95. 4: .global label //declaration of variable label
  96. Add R1, R2, label //R1 = R2 + label
  97. ...
  98. label: nop //definition of variable label
  99. **SUB** - Subtract from register
  100. --------------------------------
  101. **Syntax**
  102. **SUB** *Rdst, Rsrc1, Rsrc2*
  103. **SUB** *Rdst, Rsrc1, imm*
  104. **Operands**
  105. - *Rdst* - Register R[0..3]
  106. - *Rsrc1* - Register R[0..3]
  107. - *Rsrc2* - Register R[0..3]
  108. - *Imm* - 16-bit signed value
  109. **Cycles**
  110. 2 cycles to execute, 4 cycles to fetch next instruction
  111. **Description**
  112. The instruction subtracts the source register from another source register or subtracts 16-bit signed value from a source register, and stores result to the destination register.
  113. **Examples**::
  114. 1: SUB R1, R2, R3 //R1 = R2 - R3
  115. 2: sub R1, R2, 0x1234 //R1 = R2 - 0x1234
  116. 3: .set value1, 0x03 //constant value1=0x03
  117. SUB R1, R2, value1 //R1 = R2 - value1
  118. 4: .global label //declaration of variable label
  119. SUB R1, R2, label //R1 = R2 - label
  120. ....
  121. label: nop //definition of variable label
  122. **AND** - Logical AND of two operands
  123. -------------------------------------
  124. **Syntax**
  125. **AND** *Rdst, Rsrc1, Rsrc2*
  126. **AND** *Rdst, Rsrc1, imm*
  127. **Operands**
  128. - *Rdst* - Register R[0..3]
  129. - *Rsrc1* - Register R[0..3]
  130. - *Rsrc2* - Register R[0..3]
  131. - *Imm* - 16-bit signed value
  132. **Cycles**
  133. 2 cycles to execute, 4 cycles to fetch next instruction
  134. **Description**
  135. The instruction does logical AND of a source register and another source register or 16-bit signed value and stores result to the destination register.
  136. **Examples**::
  137. 1: AND R1, R2, R3 //R1 = R2 & R3
  138. 2: AND R1, R2, 0x1234 //R1 = R2 & 0x1234
  139. 3: .set value1, 0x03 //constant value1=0x03
  140. AND R1, R2, value1 //R1 = R2 & value1
  141. 4: .global label //declaration of variable label
  142. AND R1, R2, label //R1 = R2 & label
  143. ...
  144. label: nop //definition of variable label
  145. **OR** - Logical OR of two operands
  146. -----------------------------------
  147. **Syntax**
  148. **OR** *Rdst, Rsrc1, Rsrc2*
  149. **OR** *Rdst, Rsrc1, imm*
  150. **Operands**
  151. - *Rdst* - Register R[0..3]
  152. - *Rsrc1* - Register R[0..3]
  153. - *Rsrc2* - Register R[0..3]
  154. - *Imm* - 16-bit signed value
  155. **Cycles**
  156. 2 cycles to execute, 4 cycles to fetch next instruction
  157. **Description**
  158. The instruction does logical OR of a source register and another source register or 16-bit signed value and stores result to the destination register.
  159. **Examples**::
  160. 1: OR R1, R2, R3 //R1 = R2 \| R3
  161. 2: OR R1, R2, 0x1234 //R1 = R2 \| 0x1234
  162. 3: .set value1, 0x03 //constant value1=0x03
  163. OR R1, R2, value1 //R1 = R2 \| value1
  164. 4: .global label //declaration of variable label
  165. OR R1, R2, label //R1 = R2 \|label
  166. ...
  167. label: nop //definition of variable label
  168. **LSH** - Logical Shift Left
  169. ----------------------------
  170. **Syntax**
  171. **LSH** *Rdst, Rsrc1, Rsrc2*
  172. **LSH** *Rdst, Rsrc1, imm*
  173. **Operands**
  174. - *Rdst* - Register R[0..3]
  175. - *Rsrc1* - Register R[0..3]
  176. - *Rsrc2* - Register R[0..3]
  177. - *Imm* - 16-bit signed value
  178. **Cycles**
  179. 2 cycles to execute, 4 cycles to fetch next instruction
  180. **Description**
  181. The instruction does logical shift to left of source register to number of bits from another source register or 16-bit signed value and store result to the destination register.
  182. **Examples**::
  183. 1: LSH R1, R2, R3 //R1 = R2 << R3
  184. 2: LSH R1, R2, 0x03 //R1 = R2 << 0x03
  185. 3: .set value1, 0x03 //constant value1=0x03
  186. LSH R1, R2, value1 //R1 = R2 << value1
  187. 4: .global label //declaration of variable label
  188. LSH R1, R2, label //R1 = R2 << label
  189. ...
  190. label: nop //definition of variable label
  191. **RSH** - Logical Shift Right
  192. -----------------------------
  193. **Syntax**
  194. **RSH** *Rdst, Rsrc1, Rsrc2*
  195. **RSH** *Rdst, Rsrc1, imm*
  196. **Operands**
  197. *Rdst* - Register R[0..3]
  198. *Rsrc1* - Register R[0..3]
  199. *Rsrc2* - Register R[0..3]
  200. *Imm* - 16-bit signed value
  201. **Cycles**
  202. 2 cycles to execute, 4 cycles to fetch next instruction
  203. **Description**
  204. The instruction does logical shift to right of source register to number of bits from another source register or 16-bit signed value and store result to the destination register.
  205. **Examples**::
  206. 1: RSH R1, R2, R3 //R1 = R2 >> R3
  207. 2: RSH R1, R2, 0x03 //R1 = R2 >> 0x03
  208. 3: .set value1, 0x03 //constant value1=0x03
  209. RSH R1, R2, value1 //R1 = R2 >> value1
  210. 4: .global label //declaration of variable label
  211. RSH R1, R2, label //R1 = R2 >> label
  212. label: nop //definition of variable label
  213. **MOVE** – Move to register
  214. ---------------------------
  215. **Syntax**
  216. **MOVE** *Rdst, Rsrc*
  217. **MOVE** *Rdst, imm*
  218. **Operands**
  219. - *Rdst* – Register R[0..3]
  220. - *Rsrc* – Register R[0..3]
  221. - *Imm* – 16-bit signed value
  222. **Cycles**
  223. 2 cycles to execute, 4 cycles to fetch next instruction
  224. **Description**
  225. The instruction move to destination register value from source register or 16-bit signed value.
  226. Note that when a label is used as an immediate, the address of the label will be converted from bytes to words. This is because LD, ST, and JUMP instructions expect the address register value to be expressed in words rather than bytes. To avoid using an extra instruction
  227. **Examples**::
  228. 1: MOVE R1, R2 //R1 = R2
  229. 2: MOVE R1, 0x03 //R1 = 0x03
  230. 3: .set value1, 0x03 //constant value1=0x03
  231. MOVE R1, value1 //R1 = value1
  232. 4: .global label //declaration of label
  233. MOVE R1, label //R1 = address_of(label) / 4
  234. ...
  235. label: nop //definition of label
  236. **STL**/**ST** – Store data to the low 16 bits of 32-bits memory
  237. ----------------------------------------------------------------
  238. **Syntax**
  239. **ST** *Rsrc, Rdst, offset, Label*
  240. **STL** *Rsrc, Rdst, offset, Label*
  241. **Operands**
  242. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  243. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  244. - *Offset* – 11-bit signed value, offset in bytes
  245. - *Label* – 2-bit user defined unsigned value
  246. **Cycles**
  247. 4 cycles to execute, 4 cycles to fetch next instruction
  248. **Description**
  249. The instruction stores the 16-bit value of Rsrc to the lower half-word of memory with address Rdst+offset::
  250. Mem[Rdst + offset / 4]{15:0} = {Rsrc[15:0]}
  251. Mem[Rdst + offset / 4]{15:0} = {Label[1:0],Rsrc[13:0]}
  252. The ST command introduced to make compatibility with previous versions of UPL core.
  253. The application can use higher 16 bits to determine which instruction in the ULP program has written any particular word into memory.
  254. **Examples**::
  255. 1: STL R1, R2, 0x12 //MEM[R2+0x12] = R1
  256. 2: .data //Data section definition
  257. Addr1: .word 123 // Define label Addr1 16 bit
  258. .set offs, 0x00 // Define constant offs
  259. .text //Text section definition
  260. MOVE R1, 1 // R1 = 1
  261. MOVE R2, Addr1 // R2 = Addr1
  262. STL R1, R2, offs // MEM[R2 + 0] = R1
  263. // MEM[Addr1 + 0] will be 32'hxxxx0001
  264. 3:
  265. MOVE R1, 1 // R1 = 1
  266. STL R1, R2, 0x12,1 // MEM[R2+0x12] 0xxxxx4001
  267. **STH** – Store data to the high 16 bits of 32-bits memory
  268. ----------------------------------------------------------
  269. **Syntax**
  270. **STH** *Rsrc, Rdst, offset, Label*
  271. **Operands**
  272. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  273. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  274. - *Offset* – 11-bit signed value, offset in bytes
  275. - *Label* – 2-bit user defined unsigned value
  276. **Cycles**
  277. 4 cycles to execute, 4 cycles to fetch next instruction
  278. **Description**
  279. The instruction stores the 16-bit value of Rsrc to the high half-word of memory with address Rdst+offset::
  280. Mem[Rdst + offset / 4]{31:16} = {Rsrc[15:0]}
  281. Mem[Rdst + offset / 4]{31:16} = {Label[1:0],Rsrc[13:0]}
  282. **Examples**::
  283. 1: STH R1, R2, 0x12 //MEM[R2+0x12][31:16] = R1
  284. 2: .data //Data section definition
  285. Addr1: .word 123 // Define label Addr1 16 bit
  286. .set offs, 0x00 // Define constant offs
  287. .text //Text section definition
  288. MOVE R1, 1 // R1 = 1
  289. MOVE R2, Addr1 // R2 = Addr1
  290. STH R1, R2, offs // MEM[R2 + 0] = R1
  291. // MEM[Addr1 + 0] will be 32'h0001xxxx
  292. 3:
  293. MOVE R1, 1 // R1 = 1
  294. STH R1, R2, 0x12, 1 //MEM[R2+0x12] 0x4001xxxx
  295. **STO** – Set offset value for auto increment operation
  296. -------------------------------------------------------
  297. **Syntax**
  298. **STO** *offset*
  299. **Operands**
  300. - *Offset* – 11-bit signed value, offset in bytes
  301. **Cycles**
  302. 4 cycles to execute, 4 cycles to fetch next instruction
  303. **Description**
  304. The instruction set 16-bit value to the offset register::
  305. offset = value/ 4
  306. **Examples**::
  307. 1: STO 0x12 // Offset = 0x12/4
  308. 2: .data //Data section definition
  309. Addr1: .word 123 // Define label Addr1 16 bit
  310. .set offs, 0x00 // Define constant offs
  311. .text //Text section definition
  312. STO offs // Offset = 0x00
  313. **STI** – Store data to the 32-bits memory with auto increment of predefined offset address
  314. -------------------------------------------------------------------------------------------
  315. **Syntax**
  316. **STI** *Rsrc, Rdst, Label*
  317. **Operands**
  318. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  319. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  320. - *Label* – 2-bit user defined unsigned value
  321. **Cycles**
  322. 4 cycles to execute, 4 cycles to fetch next instruction
  323. **Description**
  324. The instruction stores the 16-bit value of Rsrc to the low and high half-word of memory with address Rdst+offset with
  325. auto increment of offset::
  326. Mem[Rdst + offset / 4]{15:0/31:16} = {Rsrc[15:0]}
  327. Mem[Rdst + offset / 4]{15:0/31:16} = {Label[1:0],Rsrc[13:0]}
  328. **Examples**::
  329. 1: STO 0 // Set offset to 0
  330. STI R1, R2, 0x12 //MEM[R2+0x12][15:0] = R1
  331. STI R1, R2, 0x12 //MEM[R2+0x12][31:16] = R1
  332. 2: .data //Data section definition
  333. Addr1: .word 123 // Define label Addr1 16 bit
  334. .set offs, 0x00 // Define constant offs
  335. .text //Text section definition
  336. STO 0 // Set offset to 0
  337. MOVE R1, 1 // R1 = 1
  338. MOVE R2, Addr1 // R2 = Addr1
  339. STI R1, R2 // MEM[R2 + 0] = R1
  340. // MEM[Addr1 + 0] will be 32'hxxxx0001
  341. STIx R1, R2 // MEM[R2 + 0] = R1
  342. // MEM[Addr1 + 0] will be 32'h00010001
  343. 3:
  344. STO 0 // Set offset to 0
  345. MOVE R1, 1 // R1 = 1
  346. STI R1, R2, 1 //MEM[R2+0x12] 0xxxxx4001
  347. STI R1, R2, 1 //MEM[R2+0x12] 0x40014001
  348. **ST32** – Store 32-bits data to the 32-bits memory
  349. ---------------------------------------------------
  350. **Syntax**
  351. **ST32** *Rsrc, Rdst, offset, Label*
  352. **Operands**
  353. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  354. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  355. - *Offset* – 11-bit signed value, offset in bytes
  356. - *Label* – 2-bit user defined unsigned value
  357. **Cycles**
  358. 4 cycles to execute, 4 cycles to fetch next instruction
  359. **Description**
  360. The instruction stores 11 bits of the PC value, label value and the 16-bit value of Rsrc to the 32-bits memory with address Rdst+offset::
  361. Mem[Rdst + offset / 4]{31:0} = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  362. **Examples**::
  363. 1: ST32 R1, R2, 0x12, 0 //MEM[R2+0x12][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  364. 2: .data //Data section definition
  365. Addr1: .word 123 // Define label Addr1 16 bit
  366. .set offs, 0x00 // Define constant offs
  367. .text //Text section definition
  368. MOVE R1, 1 // R1 = 1
  369. MOVE R2, Addr1 // R2 = Addr1
  370. ST32 R1, R2, offs,1// MEM[R2 + 0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  371. // MEM[Addr1 + 0] will be 32'h00010001
  372. **STI32** – Store 32-bits data to the 32-bits memory with auto increment of adress offset
  373. -----------------------------------------------------------------------------------------
  374. **Syntax**
  375. **STI32** *Rsrc, Rdst, Label*
  376. **Operands**
  377. - *Rsrc* – Register R[0..3], holds the 16-bit value to store
  378. - *Rdst* – Register R[0..3], address of the destination, in 32-bit words
  379. - *Label* – 2-bit user defined unsigned value
  380. **Cycles**
  381. 4 cycles to execute, 4 cycles to fetch next instruction
  382. **Description**
  383. The instruction stores 11 bits of the PC value, label value and the 16-bit value of Rsrc to the 32-bits memory with address Rdst+offset::
  384. Mem[Rdst + offset / 4]{31:0} = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  385. Where offset value set by STO instruction
  386. **Examples**::
  387. 1: STO 0x12
  388. STI32 R1, R2, 0 //MEM[R2+0x12][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  389. STI32 R1, R2, 0 //MEM[R2+0x13][31:0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  390. 2: .data //Data section definition
  391. Addr1: .word 123 // Define label Addr1 16 bit
  392. .set offs, 0x00 // Define constant offs
  393. .text //Text section definition
  394. MOVE R1, 1 // R1 = 1
  395. MOVE R2, Addr1 // R2 = Addr1
  396. STO offs
  397. STI32 R1, R2, 1// MEM[R2 + 0] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  398. // MEM[Addr1 + 0] will be 32'h00010001
  399. ST32 R1, R2, 1// MEM[R2 + 1] = {PC[10:0],0[2:0],Label[1:0],Rsrc[15:0]}
  400. // MEM[Addr1 + 1] will be 32'h00010001
  401. **LDL**/**LD** – Load data from low part of the 32-bits memory
  402. --------------------------------------------------------------
  403. **Syntax**
  404. **LD** *Rdst, Rsrc, offset*
  405. **LDL** *Rdst, Rsrc, offset*
  406. **Operands**
  407. *Rdst* – Register R[0..3], destination
  408. *Rsrc* – Register R[0..3], holds address of destination, in 32-bit words
  409. *Offset* – 13-bit signed value, offset in bytes
  410. **Cycles**
  411. 4 cycles to execute, 4 cycles to fetch next instruction
  412. **Description**
  413. The instruction loads lower 16-bit half-word from memory with address Rsrc+offset into the destination register Rdst::
  414. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  415. The LD command do the same as LDL, and included for compatibility with previous versions of ULP core.
  416. **Examples**::
  417. 1: LDL R1, R2, 0x12 //R1 = MEM[R2+0x12]
  418. 2: .data //Data section definition
  419. Addr1: .word 123 // Define label Addr1 16 bit
  420. .set offs, 0x00 // Define constant offs
  421. .text //Text section definition
  422. MOVE R1, 1 // R1 = 1
  423. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  424. LDL R1, R2, offs // R1 = MEM[R2 + 0]
  425. // R1 will be 123
  426. **LDH** – Load data from high part of the 32-bits memory
  427. --------------------------------------------------------
  428. **Syntax**
  429. **LDH** *Rdst, Rsrc, offset*
  430. **Operands**
  431. *Rdst* – Register R[0..3], destination
  432. *Rsrc* – Register R[0..3], holds address of destination, in 32-bit words
  433. *Offset* – 13-bit signed value, offset in bytes
  434. **Cycles**
  435. 4 cycles to execute, 4 cycles to fetch next instruction
  436. **Description**
  437. The instruction loads higher 16-bit half-word from memory with address Rsrc+offset into the destination register Rdst::
  438. Rdst[15:0] = Mem[Rsrc + offset / 4][15:0]
  439. The LD command do the same as LDL, and included for compatibility with previous versions of ULP core.
  440. **Examples**::
  441. 1: LDH R1, R2, 0x12 //R1 = MEM[R2+0x12]
  442. 2: .data //Data section definition
  443. Addr1: .word 0x12345678 // Define label Addr1 16 bit
  444. .set offs, 0x00 // Define constant offs
  445. .text //Text section definition
  446. MOVE R1, 1 // R1 = 1
  447. MOVE R2, Addr1 // R2 = Addr1 / 4 (address of label is converted into words)
  448. LDH R1, R2, offs // R1 = MEM[R2 + 0]
  449. // R1 will be 0x1234
  450. **JUMP** – Jump to an absolute address
  451. --------------------------------------
  452. **Syntax**
  453. **JUMP** *Rdst*
  454. **JUMP** *ImmAddr*
  455. **JUMP** *Rdst, Condition*
  456. **JUMP** *ImmAddr, Condition*
  457. **Operands**
  458. - *Rdst* – Register R[0..3] containing address to jump to (expressed in 32-bit words)
  459. - *ImmAddr* – 13 bits address (expressed in bytes), aligned to 4 bytes
  460. - *Condition*:
  461. - EQ – jump if last ALU operation result was zero
  462. - OV – jump if last ALU has set overflow flag
  463. **Cycles**
  464. 2 cycles to execute, 2 cycles to fetch next instruction
  465. **Description**
  466. The instruction makes jump to the specified address. Jump can be either unconditional or based on an ALU flag.
  467. **Examples**::
  468. 1: JUMP R1 // Jump to address in R1 (address in R1 is in 32-bit words)
  469. 2: JUMP 0x120, EQ // Jump to address 0x120 (in bytes) if ALU result is zero
  470. 3: JUMP label // Jump to label
  471. ...
  472. label: nop // Definition of label
  473. 4: .global label // Declaration of global label
  474. MOVE R1, label // R1 = label (value loaded into R1 is in words)
  475. JUMP R1 // Jump to label
  476. ...
  477. label: nop // Definition of label
  478. **JUMPR** – Jump to a relative offset (condition based on R0)
  479. -------------------------------------------------------------
  480. **Syntax**
  481. **JUMPR** *Step, Threshold, Condition*
  482. **Operands**
  483. - *Step* – relative shift from current position, in bytes
  484. - *Threshold* – threshold value for branch condition
  485. - *Condition*:
  486. - *EQ* (equal) – jump if value in R0 == threshold
  487. - *LT* (less than) – jump if value in R0 < threshold
  488. - *LE* (less or equal) – jump if value in R0 <= threshold
  489. - *GT* (greater than) – jump if value in R0 > threshold
  490. - *GE* (greater or equal) – jump if value in R0 >= threshold
  491. **Cycles**
  492. Conditions *EQ*, *GT* and *LT*: 2 cycles to execute, 2 cycles to fetch next instruction
  493. Conditions *LE* and *GE* are implemented in the assembler using two **JUMPR** instructions::
  494. // JUMPR target, threshold, LE is implemented as:
  495. JUMPR target, threshold, EQ
  496. JUMPR target, threshold, LT
  497. // JUMPR target, threshold, GE is implemented as:
  498. JUMPR target, threshold, EQ
  499. JUMPR target, threshold, GT
  500. Therefore the execution time will depend on the branches taken: either 2 cycles to execute + 2 cycles to fetch, or 4 cycles to execute + 4 cycles to fetch.
  501. **Description**
  502. The instruction makes a jump to a relative address if condition is true. Condition is the result of comparison of R0 register value and the threshold value.
  503. **Examples**::
  504. 1:pos: JUMPR 16, 20, GE // Jump to address (position + 16 bytes) if value in R0 >= 20
  505. 2: // Down counting loop using R0 register
  506. MOVE R0, 16 // load 16 into R0
  507. label: SUB R0, R0, 1 // R0--
  508. NOP // do something
  509. JUMPR label, 1, GE // jump to label if R0 >= 1
  510. **JUMPS** – Jump to a relative address (condition based on stage count)
  511. -----------------------------------------------------------------------
  512. **Syntax**
  513. **JUMPS** *Step, Threshold, Condition*
  514. **Operands**
  515. - *Step* – relative shift from current position, in bytes
  516. - *Threshold* – threshold value for branch condition
  517. - *Condition*:
  518. - *EQ* (equal) – jump if value in stage_cnt == threshold
  519. - *LT* (less than) – jump if value in stage_cnt < threshold
  520. - *LE* (less or equal) - jump if value in stage_cnt <= threshold
  521. - *GT* (greater than) – jump if value in stage_cnt > threshold
  522. - *GE* (greater or equal) — jump if value in stage_cnt >= threshold
  523. **Cycles**
  524. 2 cycles to execute, 2 cycles to fetch next instruction::
  525. // JUMPS target, threshold, EQ is implemented as:
  526. JUMPS next, threshold, LT
  527. JUMPS target, threshold, LE
  528. next:
  529. // JUMPS target, threshold, GT is implemented as:
  530. JUMPS next, threshold, LE
  531. JUMPS target, threshold, GE
  532. next:
  533. Therefore the execution time will depend on the branches taken: either 2 cycles to execute + 2 cycles to fetch, or 4 cycles to execute + 4 cycles to fetch.
  534. **Description**
  535. The instruction makes a jump to a relative address if condition is true. Condition is the result of comparison of count register value and threshold value.
  536. **Examples**::
  537. 1:pos: JUMPS 16, 20, EQ // Jump to (position + 16 bytes) if stage_cnt == 20
  538. 2: // Up counting loop using stage count register
  539. STAGE_RST // set stage_cnt to 0
  540. label: STAGE_INC 1 // stage_cnt++
  541. NOP // do something
  542. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  543. **STAGE_RST** – Reset stage count register
  544. ------------------------------------------
  545. **Syntax**
  546. **STAGE_RST**
  547. **Operands**
  548. No operands
  549. **Description**
  550. The instruction sets the stage count register to 0
  551. **Cycles**
  552. 2 cycles to execute, 4 cycles to fetch next instruction
  553. **Examples**::
  554. 1: STAGE_RST // Reset stage count register
  555. **STAGE_INC** – Increment stage count register
  556. ----------------------------------------------
  557. **Syntax**
  558. **STAGE_INC** *Value*
  559. **Operands**
  560. - *Value* – 8 bits value
  561. **Cycles**
  562. 2 cycles to execute, 4 cycles to fetch next instruction
  563. **Description**
  564. The instruction increments stage count register by given value.
  565. **Examples**::
  566. 1: STAGE_INC 10 // stage_cnt += 10
  567. 2: // Up counting loop example:
  568. STAGE_RST // set stage_cnt to 0
  569. label: STAGE_INC 1 // stage_cnt++
  570. NOP // do something
  571. JUMPS label, 16, LT // jump to label if stage_cnt < 16
  572. **STAGE_DEC** – Decrement stage count register
  573. ----------------------------------------------
  574. **Syntax**
  575. **STAGE_DEC** *Value*
  576. **Operands**
  577. - *Value* – 8 bits value
  578. **Cycles**
  579. 2 cycles to execute, 4 cycles to fetch next instruction
  580. **Description**
  581. The instruction decrements stage count register by given value.
  582. **Examples**::
  583. 1: STAGE_DEC 10 // stage_cnt -= 10;
  584. 2: // Down counting loop exaple
  585. STAGE_RST // set stage_cnt to 0
  586. STAGE_INC 16 // increment stage_cnt to 16
  587. label: STAGE_DEC 1 // stage_cnt--;
  588. NOP // do something
  589. JUMPS label, 0, GT // jump to label if stage_cnt > 0
  590. **HALT** – End the program
  591. --------------------------
  592. **Syntax**
  593. **HALT**
  594. **Operands**
  595. No operands
  596. **Cycles**
  597. 2 cycles to execute
  598. **Description**
  599. The instruction halts the ULP coprocessor and restarts ULP wakeup timer, if it is enabled.
  600. **Examples**::
  601. 1: HALT // Halt the coprocessor
  602. **WAKE** – Wake up the chip
  603. ---------------------------
  604. **Syntax**
  605. **WAKE**
  606. **Operands**
  607. No operands
  608. **Cycles**
  609. 2 cycles to execute, 4 cycles to fetch next instruction
  610. **Description**
  611. The instruction sends an interrupt from ULP to RTC controller.
  612. - If the SoC is in deep sleep mode, and ULP wakeup is enabled, this causes the SoC to wake up.
  613. - If the SoC is not in deep sleep mode, and ULP interrupt bit (RTC_CNTL_ULP_CP_INT_ENA) is set in RTC_CNTL_INT_ENA_REG register, RTC interrupt will be triggered.
  614. Note that before using WAKE instruction, ULP program may needs to wait until RTC controller is ready to wake up the main CPU. This is indicated using RTC_CNTL_RDY_FOR_WAKEUP bit of RTC_CNTL_LOW_POWER_ST_REG register. If WAKE instruction is executed while RTC_CNTL_RDY_FOR_WAKEUP is zero, it has no effect (wake up does not occur).
  615. **Examples**::
  616. 1: is_rdy_for_wakeup: // Read RTC_CNTL_RDY_FOR_WAKEUP bit
  617. READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP)
  618. AND r0, r0, 1
  619. JUMP is_rdy_for_wakeup, eq // Retry until the bit is set
  620. WAKE // Trigger wake up
  621. REG_WR 0x006, 24, 24, 0 // Stop ULP timer (clear RTC_CNTL_ULP_CP_SLP_TIMER_EN)
  622. HALT // Stop the ULP program
  623. // After these instructions, SoC will wake up,
  624. // and ULP will not run again until started by the main program.
  625. **WAIT** – wait some number of cycles
  626. -------------------------------------
  627. **Syntax**
  628. **WAIT** *Cycles*
  629. **Operands**
  630. - *Cycles* – number of cycles for wait
  631. **Cycles**
  632. 2 + *Cycles* cycles to execute, 4 cycles to fetch next instruction
  633. **Description**
  634. The instruction delays for given number of cycles.
  635. **Examples**::
  636. 1: WAIT 10 // Do nothing for 10 cycles
  637. 2: .set wait_cnt, 10 // Set a constant
  638. WAIT wait_cnt // wait for 10 cycles
  639. **TSENS** – do measurement with temperature sensor
  640. --------------------------------------------------
  641. **Syntax**
  642. - **TSENS** *Rdst, Wait_Delay*
  643. **Operands**
  644. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  645. - *Wait_Delay* – number of cycles used to perform the measurement
  646. **Cycles**
  647. 2 + *Wait_Delay* + 3 * TSENS_CLK to execute, 4 cycles to fetch next instruction
  648. **Description**
  649. The instruction performs measurement using TSENS and stores the result into a general purpose register.
  650. **Examples**::
  651. 1: TSENS R1, 1000 // Measure temperature sensor for 1000 cycles,
  652. // and store result to R1
  653. **ADC** – do measurement with ADC
  654. ---------------------------------
  655. **Syntax**
  656. - **ADC** *Rdst, Sar_sel, Mux*
  657. - **ADC** *Rdst, Sar_sel, Mux, 0* — deprecated form
  658. **Operands**
  659. - *Rdst* – Destination Register R[0..3], result will be stored to this register
  660. - *Sar_sel* – Select ADC: 0 = SARADC1, 1 = SARADC2
  661. - *Mux* - selected PAD, SARADC Pad[Mux-1] is enabled. If the user passes Mux value 1, then ADC pad 0 gets used.
  662. **Cycles**
  663. ``23 + max(1, SAR_AMP_WAIT1) + max(1, SAR_AMP_WAIT2) + max(1, SAR_AMP_WAIT3) + SARx_SAMPLE_CYCLE + SARx_SAMPLE_BIT`` cycles to execute, 4 cycles to fetch next instruction
  664. **Description**
  665. The instruction makes measurements from ADC.
  666. **Examples**::
  667. 1: ADC R1, 0, 1 // Measure value using ADC1 pad 2 and store result into R1
  668. **REG_RD** – read from peripheral register
  669. ------------------------------------------
  670. **Syntax**
  671. **REG_RD** *Addr, High, Low*
  672. **Operands**
  673. - *Addr* – Register address, in 32-bit words
  674. - *High* – Register end bit number
  675. - *Low* – Register start bit number
  676. **Cycles**
  677. 4 cycles to execute, 4 cycles to fetch next instruction
  678. **Description**
  679. The instruction reads up to 16 bits from a peripheral register into a general purpose register: ``R0 = REG[Addr][High:Low]``.
  680. This instruction can access registers in RTC_CNTL, RTC_IO, SENS, and RTC_I2C peripherals. Address of the the register, as seen from the ULP, can be calculated from the address of the same register on the PeriBUS1 as follows::
  681. addr_ulp = (addr_peribus1 - DR_REG_RTCCNTL_BASE) / 4
  682. **Examples**::
  683. 1: REG_RD 0x120, 7, 4 // load 4 bits: R0 = {12'b0, REG[0x120][7:4]}
  684. **REG_WR** – write to peripheral register
  685. -----------------------------------------
  686. **Syntax**
  687. **REG_WR** *Addr, High, Low, Data*
  688. **Operands**
  689. - *Addr* – Register address, in 32-bit words.
  690. - *High* – Register end bit number
  691. - *Low* – Register start bit number
  692. - *Data* – Value to write, 8 bits
  693. **Cycles**
  694. 8 cycles to execute, 4 cycles to fetch next instruction
  695. **Description**
  696. The instruction writes up to 8 bits from an immediate data value into a peripheral register: ``REG[Addr][High:Low] = data``.
  697. This instruction can access registers in RTC_CNTL, RTC_IO, SENS, and RTC_I2C peripherals. Address of the the register, as seen from the ULP, can be calculated from the address of the same register on the PeriBUS1 as follows::
  698. addr_ulp = (addr_peribus1 - DR_REG_RTCCNTL_BASE) / 4
  699. **Examples**::
  700. 1: REG_WR 0x120, 7, 0, 0x10 // set 8 bits: REG[0x120][7:0] = 0x10
  701. Convenience macros for peripheral registers access
  702. --------------------------------------------------
  703. ULP source files are passed through C preprocessor before the assembler. This allows certain macros to be used to facilitate access to peripheral registers.
  704. Some existing macros are defined in ``soc/soc_ulp.h`` header file. These macros allow access to the fields of peripheral registers by their names.
  705. Peripheral registers names which can be used with these macros are the ones defined in ``soc/rtc_cntl_reg.h``, ``soc/rtc_io_reg.h``, ``soc/sens_reg.h``, and ``soc/rtc_i2c_reg.h``.
  706. READ_RTC_REG(rtc_reg, low_bit, bit_width)
  707. Read up to 16 bits from rtc_reg[low_bit + bit_width - 1 : low_bit] into R0. For example::
  708. #include "soc/soc_ulp.h"
  709. #include "soc/rtc_cntl_reg.h"
  710. /* Read 16 lower bits of RTC_CNTL_TIME0_REG into R0 */
  711. READ_RTC_REG(RTC_CNTL_TIME0_REG, 0, 16)
  712. READ_RTC_FIELD(rtc_reg, field)
  713. Read from a field in rtc_reg into R0, up to 16 bits. For example::
  714. #include "soc/soc_ulp.h"
  715. #include "soc/sens_reg.h"
  716. /* Read 8-bit SENS_TSENS_OUT field of SENS_SAR_SLAVE_ADDR3_REG into R0 */
  717. READ_RTC_FIELD(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT)
  718. WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value)
  719. Write immediate value into rtc_reg[low_bit + bit_width - 1 : low_bit], bit_width <= 8. For example::
  720. #include "soc/soc_ulp.h"
  721. #include "soc/rtc_io_reg.h"
  722. /* Set BIT(2) of RTC_GPIO_OUT_DATA_W1TS field in RTC_GPIO_OUT_W1TS_REG */
  723. WRITE_RTC_REG(RTC_GPIO_OUT_W1TS_REG, RTC_GPIO_OUT_DATA_W1TS_S + 2, 1, 1)
  724. WRITE_RTC_FIELD(rtc_reg, field, value)
  725. Write immediate value into a field in rtc_reg, up to 8 bits. For example::
  726. #include "soc/soc_ulp.h"
  727. #include "soc/rtc_cntl_reg.h"
  728. /* Set RTC_CNTL_ULP_CP_SLP_TIMER_EN field of RTC_CNTL_STATE0_REG to 0 */
  729. WRITE_RTC_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN, 0)