Skip to content

Commit 06b277e

Browse files
committed
Add documentation for disassembler
1 parent eff6f96 commit 06b277e

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

Diff for: README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The following features are supported:
3535
* expressions in assembly code and constant definitions
3636
* RTC convenience macros (e.g. ``WRITE_RTC_REG``)
3737
* many ESP32 ULP code examples found on the web will work unmodified
38+
* a simple disassembler is also provided
3839

3940

4041
Quick start

Diff for: docs/disassembler.rst

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
=====================
2+
Disassembler
3+
=====================
4+
5+
micropython-esp32-ulp contains a disassembler for disassembling code for the
6+
ESP32 ULP (Ultra Low-Power) Co-Processor.
7+
8+
The main purpose of this tool is to inspect what instructions our assembler
9+
created, what value each field is set to, and to compare this with the output
10+
created by the assembler from Espressif (part of their `binutils-gdb fork <https://github.com/espressif/binutils-gdb/tree/esp32ulp-elf-2.35>`_),
11+
which we use as our reference implementation.
12+
13+
14+
Usage
15+
------------------------
16+
17+
To disassemble a ULP binary, simply run:
18+
19+
.. code-block:: bash
20+
21+
micropython -m tools.disassemble path/to/binary.ulp
22+
23+
You can also specify additional options to ``disassemble.py`` as follows:
24+
25+
+--------------------------+----------------------------------------------------------------+
26+
| Option | Description |
27+
+==========================+================================================================+
28+
| ``-h`` | Show help text |
29+
+--------------------------+----------------------------------------------------------------+
30+
|| ``-m <bytes sequence>`` || Disassemble a provided sequence of hex bytes |
31+
|| || (in this case any filename specified is ignored) |
32+
+--------------------------+----------------------------------------------------------------+
33+
| ``-v`` | Verbose mode (shows ULP header and fields of each instruction) |
34+
+--------------------------+----------------------------------------------------------------+
35+
36+
37+
Disassembling a file
38+
------------------------
39+
40+
The simplest and default mode of the disassembler is to disassemble the
41+
specified file.
42+
43+
Note that the ULP header is validates and files with unknown magic bytes will be
44+
rejected. The correct 4 magic bytes at the start of a ULP binary are ``ulp\x00``.
45+
46+
Example:
47+
48+
.. code-block:: shell
49+
50+
$ micropython -m tools.disassemble path/to/binary.ulp
51+
.text
52+
0000 040000d0 LD r0, r1, 0
53+
0004 0e0400d0 LD r2, r3, 1
54+
0008 84010068 ST r0, r1, 0
55+
000c 8b090068 ST r3, r2, 2
56+
.data
57+
0000 00000000 <empty>
58+
59+
60+
Disassembling a byte sequence
61+
-----------------------------
62+
63+
The ``-m`` option allows disassembling a sequences hex letters representing
64+
ULP instructions.
65+
66+
This option expects the actual instructions directly, without any ULP header.
67+
68+
The sequence must contain a number of hex letters exactly divisible by 8, i.e.
69+
8, 16, 24, etc, because each 32-bit word is made up of 8 hex letters. Spaces
70+
can be included in the sequence and they are ignored.
71+
72+
The typical use case for this feature is to copy/paste some instructions from
73+
a hexdump (e.g. xxd output) for analysis.
74+
75+
Example:
76+
77+
.. code-block:: shell
78+
79+
# hexdump binary.ulp
80+
$ xxd path/to/binary.ulp
81+
00000000: 756c 7000 0c00 2400 0400 0000 9300 8074 ulp...$........t
82+
00000010: 2a80 0488 2004 8074 1c00 0084 0000 0040 *... ..t.......@
83+
(...)
84+
85+
# analyse the last 2 instructions
86+
$ micropython -m tools.disassemble -m "1c00 0084 0000 0040"
87+
0000 1c000084 JUMPS 0, 28, LT
88+
0004 00000040 NOP
89+
90+
91+
Verbose mode
92+
------------------------
93+
94+
In verbose mode the following extra outputs are enabled:
95+
96+
* ULP header (except when using ``-m``)
97+
* The fields of each instruction and their values
98+
99+
For example:
100+
101+
.. code-block::
102+
103+
header
104+
ULP magic : b'ulp\x00' (0x00706c75)
105+
.text offset : 12 (0x0c)
106+
.text size : 36 (0x24)
107+
.data offset : 48 (0x30)
108+
.data size : 4 (0x04)
109+
.bss size : 0 (0x00)
110+
----------------------------------------
111+
.text
112+
0000 93008072 MOVE r3, 9
113+
dreg = 3
114+
imm = 9
115+
opcode = 7
116+
sel = 4 (MOV)
117+
sreg = 0
118+
sub_opcode = 1
119+
unused = 0
120+
(...detail truncated...)
121+
0020 000000b0 HALT
122+
opcode = 11 (0x0b)
123+
unused = 0
124+
----------------------------------------
125+
.data
126+
0000 00000000 <empty>
127+
128+
129+
Disassembling on device
130+
-----------------------------
131+
132+
The disassembler also works when used on an ESP32.
133+
134+
To use the disassembler on a real device:
135+
136+
* ensure ``micropython-esp32-ulp`` is installed on the device (see `docs/index.rst </docs/index.rst>`_).
137+
* upload ``tools/disassemble.py`` to the device (any directory will do)
138+
* run the following:
139+
140+
.. code-block:: python
141+
142+
from disassemble import disassemble_file
143+
# then either:
144+
disassemble_file('path/to/file.ulp') # normal mode
145+
# or:
146+
disassemble_file('path/to/file.ulp', True) # verbose mode

Diff for: docs/index.rst

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ found as part of Arduino/ESP-IDF projects.
136136
The preprocessor and how to use it is documented here: `Preprocessor support </docs/preprocess.rst>`_.
137137

138138

139+
Disassembler
140+
------------
141+
There is a disassembler for disassembling ULP binary code. This is mainly used to
142+
inspect what instructions our assembler created, however it can be used to analyse
143+
any ULP binaries.
144+
145+
The disassembler and how to use it is documented here: `Disassembler </docs/disassembler.rst>`_.
146+
147+
139148
Limitations
140149
-----------
141150

0 commit comments

Comments
 (0)