Skip to content

Commit e3597a7

Browse files
committed
cleanup peripheral register addressing code
There is no need to duplicate the "<= DR_REG_MAG_DIRECT" check in two functions. Now the _soc_reg_to_ulp_periph_sel() function is used only for "full addresses" (as it was some commits ago) and will return a ValueError if used with direct ULP addresses. This commit also masks values correctly when setting the addr and periph_sel fields for "direct ULP addresses", so only the bits we're interested in actually get used (rather than being implicitly trimmed because there aren't more bits available in a field).
1 parent c967c1d commit e3597a7

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

esp32_ulp/opcodes.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,7 @@ def get_cond(arg):
358358

359359
def _soc_reg_to_ulp_periph_sel(reg):
360360
# Map SoC peripheral register to periph_sel field of RD_REG and WR_REG instructions.
361-
if reg <= DR_REG_MAX_DIRECT:
362-
ret = RD_REG_PERIPH_RTC_CNTL
363-
elif reg < DR_REG_RTCCNTL_BASE:
361+
if reg < DR_REG_RTCCNTL_BASE:
364362
raise ValueError("invalid register base")
365363
elif reg < DR_REG_RTCIO_BASE:
366364
ret = RD_REG_PERIPH_RTC_CNTL
@@ -378,8 +376,8 @@ def _soc_reg_to_ulp_periph_sel(reg):
378376
def i_reg_wr(reg, high_bit, low_bit, val):
379377
reg = get_imm(reg)
380378
if reg <= DR_REG_MAX_DIRECT: # see https://github.com/espressif/binutils-esp32ulp/blob/master/gas/config/tc-esp32ulp_esp32.c
381-
_wr_reg.addr = reg
382-
_wr_reg.periph_sel = reg >> 8
379+
_wr_reg.addr = reg & 0xff
380+
_wr_reg.periph_sel = (reg & 0x300) >> 8
383381
else:
384382
_wr_reg.addr = (reg & 0xff) >> 2
385383
_wr_reg.periph_sel = _soc_reg_to_ulp_periph_sel(reg)
@@ -393,8 +391,8 @@ def i_reg_wr(reg, high_bit, low_bit, val):
393391
def i_reg_rd(reg, high_bit, low_bit):
394392
reg = get_imm(reg)
395393
if reg <= DR_REG_MAX_DIRECT: # see https://github.com/espressif/binutils-esp32ulp/blob/master/gas/config/tc-esp32ulp_esp32.c
396-
_rd_reg.addr = reg
397-
_rd_reg.periph_sel = reg >> 8
394+
_rd_reg.addr = reg & 0xff
395+
_rd_reg.periph_sel = (reg & 0x300) >> 8
398396
else:
399397
_rd_reg.addr = (reg & 0xff) >> 2
400398
_rd_reg.periph_sel = _soc_reg_to_ulp_periph_sel(reg)

0 commit comments

Comments
 (0)