@@ -108,10 +108,78 @@ def assert_raises(exception, func, *args):
108108 assert raised
109109
110110
111+ def test_reg_direct_ulp_addressing ():
112+ """
113+ Test direct ULP addressing of peripheral registers
114+ input must be <= 0x3ff (10 bits)
115+ periph_sel == high 2 bits from input
116+ addr == low 8 bits from input
117+ """
118+
119+ ins = make_ins ("""
120+ addr : 8 # Address within either RTC_CNTL, RTC_IO, or SARADC
121+ periph_sel : 2 # Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2)
122+ unused : 8 # Unused
123+ low : 5 # Low bit
124+ high : 5 # High bit
125+ opcode : 4 # Opcode (OPCODE_RD_REG)
126+ """ )
127+
128+ ins .all = opcodes .i_reg_rd ("0x0" , "0" , "0" )
129+ assert ins .periph_sel == 0
130+ assert ins .addr == 0x0
131+
132+ ins .all = opcodes .i_reg_rd ("0x012" , "0" , "0" )
133+ assert ins .periph_sel == 0
134+ assert ins .addr == 0x12
135+
136+ ins .all = opcodes .i_reg_rd ("0x123" , "0" , "0" )
137+ assert ins .periph_sel == 1
138+ assert ins .addr == 0x23
139+
140+ ins .all = opcodes .i_reg_rd ("0x2ee" , "0" , "0" )
141+ assert ins .periph_sel == 2
142+ assert ins .addr == 0xee
143+
144+ ins .all = opcodes .i_reg_rd ("0x3ff" , "0" , "0" )
145+ assert ins .periph_sel == 3
146+ assert ins .addr == 0xff
147+
148+ # anything bigger than 0x3ff must be a valid full address
149+ assert_raises (ValueError , opcodes .i_reg_rd , "0x400" , "0" , "0" )
150+
151+
152+ def test_reg_address_translations ():
153+ """
154+ Test addressing of peripheral registers using full DPORT bus addresses
155+ """
156+
157+ ins = make_ins ("""
158+ addr : 8 # Address within either RTC_CNTL, RTC_IO, or SARADC
159+ periph_sel : 2 # Select peripheral: RTC_CNTL (0), RTC_IO(1), SARADC(2)
160+ unused : 8 # Unused
161+ low : 5 # Low bit
162+ high : 5 # High bit
163+ opcode : 4 # Opcode (OPCODE_RD_REG)
164+ """ )
165+
166+ # direct ULP address is derived from full address as follows:
167+ # full:0x3ff484a8 == ulp:(0x3ff484a8-DR_REG_RTCCNTL_BASE) / 4
168+ # full:0x3ff484a8 == ulp:(0x3ff484a8-0x3ff48000) / 4
169+ # full:0x3ff484a8 == ulp:0x4a8 / 4
170+ # full:0x3ff484a8 == ulp:0x12a
171+ # see: https://github.com/espressif/binutils-esp32ulp/blob/249ec34/gas/config/tc-esp32ulp_esp32.c#L149
172+ ins .all = opcodes .i_reg_rd ("0x3ff484a8" , "0" , "0" )
173+ assert ins .periph_sel == 1 # high 2 bits of 0x12a
174+ assert ins .addr == 0x2a # low 8 bits of 0x12a
175+
176+
111177test_make_ins_struct_def ()
112178test_make_ins ()
113179test_arg_qualify ()
114180test_get_reg ()
115181test_get_imm ()
116182test_get_cond ()
117- test_eval_arg ()
183+ test_eval_arg ()
184+ test_reg_direct_ulp_addressing ()
185+ test_reg_address_translations ()
0 commit comments