1
1
from uctypes import struct , addressof , LITTLE_ENDIAN , UINT16 , UINT32
2
- from .decode import decode_instruction , get_instruction_fields
3
2
import ubinascii
4
3
import sys
5
4
6
5
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
+
7
25
def chunk_into_words (code , bytes_per_word , byteorder ):
8
26
chunks = [
9
27
ubinascii .hexlify (code [i :i + bytes_per_word ])
@@ -65,7 +83,9 @@ def print_data_section(data_offset, code):
65
83
print_code_line (data_offset + (idx << 2 ), i , asm )
66
84
67
85
68
- def disassemble_manually (byte_sequence_string , verbose = False ):
86
+ def disassemble_manually (byte_sequence_string , cpu , verbose = False ):
87
+ load_decoder (cpu )
88
+
69
89
sequence = byte_sequence_string .strip ().replace (' ' ,'' )
70
90
chars_per_instruction = 8
71
91
list = [
@@ -79,7 +99,9 @@ def disassemble_manually(byte_sequence_string, verbose=False):
79
99
decode_instruction_and_print (idx << 2 , i , verbose )
80
100
81
101
82
- def disassemble_file (filename , verbose = False ):
102
+ def disassemble_file (filename , cpu , verbose = False ):
103
+ load_decoder (cpu )
104
+
83
105
with open (filename , 'rb' ) as f :
84
106
data = f .read ()
85
107
@@ -114,6 +136,7 @@ def print_help():
114
136
print ('Usage: disassemble.py [<options>] [-m <byte_sequence> | <filename>]' )
115
137
print ('' )
116
138
print ('Options:' )
139
+ print (' -c Choose ULP variant: only esp32 supported for now' )
117
140
print (' -h Show this help text' )
118
141
print (' -m <byte_sequence> Sequence of hex bytes (8 per instruction)' )
119
142
print (' -v Verbose mode. Show ULP header and fields of each instruction' )
@@ -122,6 +145,7 @@ def print_help():
122
145
123
146
124
147
def handle_cmdline (params ):
148
+ cpu = 'esp32'
125
149
verbose = False
126
150
filename = None
127
151
byte_sequence = None
@@ -130,6 +154,9 @@ def handle_cmdline(params):
130
154
if params [0 ] == '-h' :
131
155
print_help ()
132
156
sys .exit (0 )
157
+ elif params [0 ] == '-c' :
158
+ cpu = params [1 ]
159
+ params = params [1 :] # remove first param from list
133
160
elif params [0 ] == '-m' :
134
161
if len (params ) == 1 :
135
162
print_help ()
@@ -159,10 +186,11 @@ def handle_cmdline(params):
159
186
160
187
params = params [1 :] # remove first param from list
161
188
189
+
162
190
if byte_sequence :
163
- disassemble_manually (byte_sequence , verbose )
191
+ disassemble_manually (byte_sequence , cpu , verbose )
164
192
elif filename :
165
- disassemble_file (filename , verbose )
193
+ disassemble_file (filename , cpu , verbose )
166
194
167
195
168
196
if sys .argv : # if run from cmdline
0 commit comments