10000 Allow selecting cpu to disassemble for · pidou46/micropython-esp32-ulp@b83b73d · GitHub
[go: up one dir, main page]

Skip to content

Commit b83b73d

Browse files
committed
Allow selecting cpu to disassemble for
Currently only the esp32 is implemented (esp32s2 soon to follow). This commit adds the -c command line option to the disassembler, as well as updates the integration tests to supply the cpu parameter, and test fixtures are renamed to include the cpu type in their name, so that we can have separate fixture files for other cpus.
1 parent 2e69c12 commit b83b73d

File tree

6 files changed

+53
-21
lines changed

6 files changed

+53
-21
lines changed

tests/03_disassembler_tests.sh

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
set -e
44

55
test_disassembling_a_file() {
6+
local cpu=$1
67
local verbose
7-
if [ "$1" == verbose ]; then
8+
if [ "$2" == verbose ]; then
89
verbose=-v
910
echo -e "Testing disassembling a file in VERBOSE mode"
1011
else
@@ -13,16 +14,16 @@ test_disassembling_a_file() {
1314

1415
testname=all_opcodes
1516
fixture=fixtures/${testname}.S
16-
echo -e "\tBuilding $fixture using micropython-esp32-ulp"
17+
echo -e "\tBuilding $fixture using micropython-esp32-ulp ($cpu)"
1718

1819
log_file="${testname}.log"
1920
ulp_file="fixtures/${testname}.ulp"
20-
micropython -m esp32_ulp $fixture 1>$log_file # generates $ulp_file
21+
micropython -m esp32_ulp -c $cpu $fixture 1>$log_file # generates $ulp_file
2122

22-
lst_file="${testname}.lst"
23-
lst_file_fixture=fixtures/${testname}${verbose}.lst
24-
echo -e "\tDisassembling $ulp_file using micropython-esp32-ulp disassembler"
25-
micropython -m tools.disassemble $verbose $ulp_file > $lst_file
23+
lst_file="${testname}.$cpu.lst"
24+
lst_file_fixture=fixtures/${testname}${verbose}.$cpu.lst
25+
echo -e "\tDisassembling $ulp_file using micropython-esp32-ulp disassembler ($cpu)"
26+
micropython -m tools.disassemble -c $cpu $verbose $ulp_file > $lst_file
2627

2728
if ! diff $lst_file_fixture $lst_file 1>/dev/null; then
2829
echo -e "\tDisassembled output differs from expected output!"
@@ -36,8 +37,9 @@ test_disassembling_a_file() {
3637
}
3738

3839
test_disassembling_a_manual_sequence() {
40+
local cpu=$1
3941
local verbose
40-
if [ "$1" == verbose ]; then
42+
if [ "$2" == verbose ]; then
4143
verbose=-v
4244
echo -e "Testing disassembling a manual byte sequence in VERBOSE mode"
4345
else
@@ -46,10 +48,10 @@ test_disassembling_a_manual_sequence() {
4648

4749
sequence="e1af 8c72 0100 0068 2705 cc19 0005 681d 0000 00a0 0000 0074"
4850

49-
lst_file="manual_bytes.lst"
50-
lst_file_fixture=fixtures/manual_bytes${verbose}.lst
51-
echo -e "\tDisassembling manual byte sequence using micropython-esp32-ulp disassembler"
52-
micropython -m tools.disassemble $verbose -m $sequence > $lst_file
51+
lst_file="manual_bytes.$cpu.lst"
52+
lst_file_fixture=fixtures/manual_bytes${verbose}.$cpu.lst
53+
echo -e "\tDisassembling manual byte sequence using micropython-esp32-ulp disassembler ($cpu)"
54+
micropython -m tools.disassemble -c $cpu $verbose -m $sequence> $lst_file
5355

5456
if ! diff $lst_file_fixture $lst_file 1>/dev/null; then
5557
echo -e "\tDisassembled output differs from expected output!"
@@ -60,8 +62,10 @@ test_disassembling_a_manual_sequence() {
6062
fi
6163
}
6264

63-
test_disassembling_a_file
64-
test_disassembling_a_file verbose
65+
# esp32
66+
echo "Testing for CPU: esp32"
67+
test_disassembling_a_file esp32
68+
test_disassembling_a_file esp32 verbose
6569

66-
test_disassembling_a_manual_sequence
67-
test_disassembling_a_manual_sequence verbose
70+
test_disassembling_a_manual_sequence esp32
71+
test_disassembling_a_manual_sequence esp32 verbose
File renamed without changes.

tools/disassemble.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
from uctypes import struct, addressof, LITTLE_ENDIAN, UINT16, UINT32
2-
from .decode import decode_instruction, get_instruction_fields
32
import ubinascii
43
import 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+
725
def 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

124147
def 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

168196
if sys.argv: # if run from cmdline

0 commit comments

Comments
 (0)
0