11from uctypes import struct , addressof , LITTLE_ENDIAN , UINT16 , UINT32
2- from .decode import decode_instruction , get_instruction_fields
32import ubinascii
43import sys
54
65
6+ # placeholders:
7+ # these functions will be dynamically loaded later based on the chosen cpu
8+ decode_instruction , get_instruction_fields = None , None
9+
10+
11+ def load_decoder (cpu ):
12+ if cpu == 'esp32' :
13+ mod = 'decode'
14+ else :
15+ raise ValueError ('Invalid cpu' )
16+
17+ relative_import = 1 if '/' in __file__ else 0
18+ decode = __import__ (mod , globals (), locals (), [], relative_import )
19+
20+ global decode_instruction , get_instruction_fields
21+ decode_instruction = decode .decode_instruction
22+ get_instruction_fields = decode .get_instruction_fields
23+
24+
725def chunk_into_words (code , bytes_per_word , byteorder ):
826 chunks = [
927 ubinascii .hexlify (code [i :i + bytes_per_word ])
@@ -65,7 +83,9 @@ def print_data_section(data_offset, code):
6583 print_code_line (data_offset + (idx << 2 ), i , asm )
6684
6785
68- def disassemble_manually (byte_sequence_string , verbose = False ):
86+ def disassemble_manually (byte_sequence_string , cpu , verbose = False ):
87+ load_decoder (cpu )
88+
6989 sequence = byte_sequence_string .strip ().replace (' ' ,'' )
7090 chars_per_instruction = 8
7191 list = [
@@ -79,7 +99,9 @@ def disassemble_manually(byte_sequence_string, verbose=False):
7999 decode_instruction_and_print (idx << 2 , i , verbose )
80100
81101
82- def disassemble_file (filename , verbose = False ):
102+ def disassemble_file (filename , cpu , verbose = False ):
103+ load_decoder (cpu )
104+
83105 with open (filename , 'rb' ) as f :
84106 data = f .read ()
85107
@@ -114,6 +136,7 @@ def print_help():
114136 print ('Usage: disassemble.py [<options>] [-m <byte_sequence> | <filename>]' )
115137 print ('' )
116138 print ('Options:' )
139+ print (' -c Choose ULP variant: only esp32 supported for now' )
117140 print (' -h Show this help text' )
118141 print (' -m <byte_sequence> Sequence of hex bytes (8 per instruction)' )
119142 print (' -v Verbose mode. Show ULP header and fields of each instruction' )
@@ -122,6 +145,7 @@ def print_help():
122145
123146
124147def handle_cmdline (params ):
148+ cpu = 'esp32'
125149 verbose = False
126150 filename = None
127151 byte_sequence = None
@@ -130,6 +154,9 @@ def handle_cmdline(params):
130154 if params [0 ] == '-h' :
131155 print_help ()
132156 sys .exit (0 )
157+ elif params [0 ] == '-c' :
158+ cpu = params [1 ]
159+ params = params [1 :] # remove first param from list
133160 elif params [0 ] == '-m' :
134161 if len (params ) == 1 :
135162 print_help ()
@@ -159,10 +186,11 @@ def handle_cmdline(params):
159186
160187 params = params [1 :] # remove first param from list
161188
189+
162190 if byte_sequence :
163- disassemble_manually (byte_sequence , verbose )
191+ disassemble_manually (byte_sequence , cpu , verbose )
164192 elif filename :
165- disassemble_file (filename , verbose )
193+ disassemble_file (filename , cpu , verbose )
166194
167195
168196if sys .argv : # if run from cmdline
0 commit comments