-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathinteract.mu4
190 lines (142 loc) · 5.7 KB
/
interact.mu4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
| This file is part of muforth: https://muforth.dev/
|
| Copyright 2002-2025 David Frech. (Read the LICENSE for details.)
loading MSP430 interaction
variable chatting
variable chat-vector
: chat-cmd ( index - index+1) dup cells constant 1+
does> @ chat-vector @ =if + @execute ^ then 2drop
error" Not connected to a chat-capable target" ;
: chat-fail error" Chat command not implemented" ;
0 ( initial command index)
chat-cmd t.Hello
chat-cmd t.Status ( - dp)
chat-cmd t.Run ( pc dp)
chat-cmd t.ReadChunk ( buf a u)
chat-cmd t.WriteChunk ( buf a u)
chat-cmd t.ErasePage ( a - fail?)
chat-cmd t.FlashChunk ( buf a u - fail?)
drop ( last command index)
: >image ['] read-host-image ( read from memory image) du-target ;
: >chat ['] t.ReadChunk ( read from connected target) du-target ;
: copy-chunk ( 'target len - 'target+len)
| cr ." copy-chunk " 2dup swap u. u.
2dup + push over image+ -rot t.WriteChunk pop ;
: copy-chunked ( a u)
| cr ." copy-chunked " 2dup swap u. u.
256 /mod ( r q) swap push for 256 copy-chunk next
pop =if ( rem) copy-chunk drop ^ then 2drop ;
variable ram-copied ( pointer to first un-copied byte)
: copy-ram
h preserve ram
ram-copied @ dup 0= if drop region drop then
\m here over - copy-chunked
\m here ram-copied ! ;
variable in-ram? | true if chat running in RAM
defer verify-quietly
: hi chatting on >chat t.Hello
dup in-ram? ! if ." (running in RAM)" then
ram-copied off copy-ram
.ifndef noverify
h preserve flash region nip if ( non-empty) verify-quietly if ( differs)
cr warn" Target's flash image differs from host's."
then then
.then
now __chatting is __meta __meta ;
: chat-via pop chat-vector ! hi ;
"cafe constant stack-sentinel
@ram #ram +
| If at least 2 Ki of ram, leave room for ram vector table.
#ram 2 Ki u< .not .if #vectors 16 aligned-by - .then
constant rp0 | R stack starts below vectors, if any
rp0 24 \m cells - constant dp0 | D stack starts 24 cells below R stack
: stack-write t.WriteChunk ;
: stack-read t.ReadChunk ;
ld target/common/stacks.mu4
: run ( pc) copy-ram dp@ t.Run ; ( don't wait for target)
: runwait ( pc) run t.Status dp! ; ( wait for target)
( when the code you're calling doesn't speak the same protocol as running code)
: go \eq Vreset image-@ run ;
| : reset go 0 100,000,000 nanosleep ( 100ms) get-regs .regs ;
| Useful for later code.
: 4# # # # # ;
| Set |@ and |c@ to _some_ form of target fetch. Prefer to get bytes from
| target if we're connected. This word is useful so we can set an initial
| state for target's du and dis so that inspect won't crash when it
| runs |@ to get a default ea.
: >target chatting @ if >chat ^ then >image ;
| Define our own key bindings for memory dumping and disassembly. We'll
| default to host bindings if there isn't one in our array. This way we
| only have to define the "delta" between host and target behaviours.
128 array msp-seekeys
( Default key action is to run host key code)
host-seekeys msp-seekeys 128 cells cmove
( Dump one line of memory from target program memory.)
: 1dump ( a)
hex-bytes
>image dup .addr dup .hex-cells
chatting @ if
-valid
>chat dup _addr dup .hex-cells
-valid
then
drop ;
( So we can easily look at the signature embedded into the image.)
: 1dump-chars ( a)
hex-bytes
>target
dup _addr dup .chars
dup .addr dup .hex-bytes
dup _addr dup .hex-cells
drop ;
( Support for interactive decompiling.)
: dec+ ( a - a' 0) 2 advance 0 ;
: dec- ( a - a' 0) -2 advance 0 ;
defer 1smart-dec
defer 1smart-dis
msp-seekeys -2 ( XXX are these stack comments right???)
2dup du-mode dumping >target skip+ skip- 1dump ( a - a')
2dup du-mode dumping-chars >target skip+ skip- 1dump-chars ( a - a')
2dup du-mode decompiling >target dec+ dec- 1smart-dec
du-mode disasming >target dis+ dis- 1smart-dis ( a - a')
meta
variable target-do-colon
variable target-do-const
variable target-do-var
forth
: .target-code ( a)
p ! cell* ( code field)
0 disasming drop
dup p @ = if drop ." code " ^ then
0 decompiling drop ( start decompiling a colon definition)
dup \m target-do-colon @ = if drop ." : " ^ then
dup \m target-do-const @ = if drop ." constant " ^ then
dup \m target-do-var @ = if drop ." variable " ^ then
.hcell_ ." (unknown code field) " ( print value and mark as "unknown") ;
: 1dec ( a)
dup .addr .nesting space
p ! cell* dup .hcell_
dup -2 and ea ! ( default ea: fetch cell and round down)
dup .target-runtime. find-constant-chained if
.target space drop ^ then
equate? if .equate space then ;
: ?switch ( a - a | <nothing> )
dup p ! cell* ea ! ( default ea: fetch cell)
dup .target-runtime. find-constant-chained if push ( 'link)
dup .addr .nesting space
.target-code pop .target space shunt ^ then ;
| Generic decompiler. Switches modes any time a target word points to
| current address.
-: ( a) ?switch 1dis ; is 1smart-dis
-: ( a) ?switch 1dec ; is 1smart-dec
msp-seekeys 'seekeys ! ( switch over to our bindings)
key: d ( a - a 0) dumping 0 ;
key: C ( a - a 0) dumping-chars 0 ; ( small c is "call")
key: i ( a - a 0) disasming 0 ;
host-seekeys 'seekeys ! ( back to host bindings)
( Interactive)
( Host du available as \f du)
meta
: du ( a - a') dumping inspect ;
: dis ( a - a') disasming inspect ;
forth