OpenCores
URL https://opencores.org/ocsvn/openmsp430/openmsp430/trunk

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [tools/] [openmsp430-gdbproxy/] [commands.tcl] - Blame information for rev 109

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 olivier.gi
#!/usr/bin/wish
2
#------------------------------------------------------------------------------
3
# Copyright (C) 2001 Authors
4
#
5
# This source file may be used and distributed without restriction provided
6
# that this copyright statement is not removed from the file and that any
7
# derivative work contains the original copyright notice and the associated
8
# disclaimer.
9
#
10
# This source file is free software; you can redistribute it and/or modify
11
# it under the terms of the GNU Lesser General Public License as published
12
# by the Free Software Foundation; either version 2.1 of the License, or
13
# (at your option) any later version.
14
#
15
# This source is distributed in the hope that it will be useful, but WITHOUT
16
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18
# License for more details.
19
#
20
# You should have received a copy of the GNU Lesser General Public License
21
# along with this source; if not, write to the Free Software Foundation,
22
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23
#
24
#------------------------------------------------------------------------------
25
# 
26
# File Name: commands.tcl
27
# 
28 15 olivier.gi
# Author(s):
29
#             - Olivier Girard,    olgirard@gmail.com
30
#
31 2 olivier.gi
#------------------------------------------------------------------------------
32 15 olivier.gi
# $Rev: 15 $
33
# $LastChangedBy: olivier.girard $
34
# $LastChangedDate: 2009-08-04 22:41:47 +0200 (Tue, 04 Aug 2009) $
35
#------------------------------------------------------------------------------
36 2 olivier.gi
 
37
global mem_breakpoint
38
 
39
###############################################################################
40
#                                                                             #
41
#                              RSP COMMAND PARSER                             #
42
#                                                                             #
43
###############################################################################
44
 
45
proc rspParse {sock rsp_cmd} {
46
 
47
    set rsp_answer ""
48
    set cmd_tail [string range $rsp_cmd 1 [string length $rsp_cmd]]
49
 
50
    switch -exact -- [string index $rsp_cmd 0] {
51
        "!"     {set rsp_answer "OK"}
52
        "?"     {set rsp_answer [rsp_stop_reply $sock "?"]}
53
        "A"     {}
54
        "b"     {}
55
        "c"     {set rsp_answer [rsp_c $sock $cmd_tail]}
56
        "C"     {set rsp_answer [rsp_c $sock $cmd_tail]}
57
        "D"     {}
58
        "F"     {}
59
        "g"     {set rsp_answer [rsp_g]}
60
        "G"     {set rsp_answer [rsp_G $cmd_tail]}
61
        "H"     {set rsp_answer ""}
62
        "i"     {}
63
        "I"     {}
64
        "k"     {set rsp_answer [rsp_k $cmd_tail]}
65
        "m"     {set rsp_answer [rsp_m $cmd_tail]}
66
        "M"     {set rsp_answer [rsp_M $cmd_tail]}
67
        "p"     {}
68
        "P"     {}
69
        "q"     {set rsp_answer [rsp_q $sock $cmd_tail]}
70
        "Q"     {}
71
        "R"     {}
72
        "s"     {set rsp_answer [rsp_s $sock $cmd_tail]}
73
        "S"     {set rsp_answer [rsp_s $sock $cmd_tail]}
74
        "t"     {}
75
        "T"     {}
76
        "v"     {}
77
        "X"     {}
78
        "z"     {set rsp_answer [rsp_z $sock $cmd_tail]}
79
        "Z"     {set rsp_answer [rsp_Z $sock $cmd_tail]}
80
        default {}
81
    }
82
 
83
 
84
    return $rsp_answer
85
}
86
 
87
 
88
###############################################################################
89
#                                                                             #
90
#                                   RSP COMMANDS                              #
91
#                                                                             #
92
###############################################################################
93
 
94
#-----------------------------------------------------------------------------#
95
# Read CPU registers                                                          #
96
#-----------------------------------------------------------------------------#
97
proc rsp_g {} {
98
 
99
    # Read register value
100
    set reg_val [ReadRegAll]
101
 
102
    # Format answer
103
    set rsp_answer ""
104
    for {set i 0} {$i < [llength $reg_val]} {incr i} {
105
 
106
        regexp {0x(..)(..)} [lindex $reg_val $i] match msb lsb
107
        append rsp_answer "$lsb$msb"
108
    }
109
 
110
    return $rsp_answer
111
}
112
 
113
#-----------------------------------------------------------------------------#
114
# Write CPU registers                                                         #
115
#-----------------------------------------------------------------------------#
116
proc rsp_G {cmd} {
117
 
118
    # Format register value
119
    set num_reg [expr [string length $cmd]/4]
120
 
121
    set reg_val ""
122
    for {set i 0} {$i < $num_reg} {incr i} {
123
 
124
        set lsb "[string index $cmd [expr $i*4+0]][string index $cmd [expr $i*4+1]]"
125
        set msb "[string index $cmd [expr $i*4+2]][string index $cmd [expr $i*4+3]]"
126
        lappend reg_val "0x$msb$lsb"
127
    }
128
 
129
    # Write registers
130
    WriteRegAll $reg_val
131
 
132
    return "OK"
133
}
134
 
135
#-----------------------------------------------------------------------------#
136
# Kill request.                                                               #
137
#-----------------------------------------------------------------------------#
138
proc rsp_k {cmd} {
139
 
140
    # Reset & Stop CPU
141
    ExecutePOR_Halt
142
 
143
    return "-1"
144
}
145
 
146
#-----------------------------------------------------------------------------#
147
# Write length bytes of memory.                                               #
148
#-----------------------------------------------------------------------------#
149
proc rsp_M {cmd} {
150
 
151
    global mem_breakpoint
152
 
153
    # Parse command
154
    regexp {(.*),(.*):(.*)} $cmd match addr length data
155
    set addr   [format %04x "0x$addr"]
156
    set length [format %d   "0x$length"]
157
 
158
    # Format data
159
    set mem_val ""
160
    for {set i 0} {$i<$length} {incr i} {
161
        lappend mem_val "0x[string range $data [expr $i*2] [expr $i*2+1]]"
162
    }
163
 
164
    # Write memory
165
    WriteMemQuick8  "0x$addr" $mem_val
166
 
167
    # Eventually re-set the software breakpoints in case they have been overwritten
168
    set addr_start [format %d "0x$addr"]
169
    foreach {brk_addr brk_val} [array get mem_breakpoint] {
170
        set brk_addr_dec    [format %d "0x$brk_addr"]
171
        set brk_addr_offset [expr $brk_addr_dec-$addr_start]
172
        if {(0<=$brk_addr_offset) && ($brk_addr_offset<=$length)} {
173
            set mem_breakpoint($brk_addr) [lindex $mem_val $brk_addr_offset]
174
            WriteMem 0 "0x$brk_addr" 0x4343
175
        }
176
    }
177
 
178
    return "OK"
179
}
180
 
181
 
182
#-----------------------------------------------------------------------------#
183
# Read length bytes from memory.                                              #
184
#-----------------------------------------------------------------------------#
185
proc rsp_m {cmd} {
186
 
187
    global mem_breakpoint
188
 
189
    # Parse command
190
    regexp {(.*),(.*)} $cmd match addr length
191
    set addr   [format %04x "0x$addr"]
192
    set length [format %d   "0x$length"]
193
 
194
    # Read memory
195
    set data [ReadMemQuick8  "0x$addr" $length]
196
 
197
    # Eventually replace read data by the original software breakpoint value
198
    set addr_start [format %d "0x$addr"]
199
    foreach {brk_addr brk_val} [array get mem_breakpoint] {
200
        set brk_addr_dec    [format %d "0x$brk_addr"]
201
        set brk_addr_offset [expr $brk_addr_dec-$addr_start]
202
        if {(0<=$brk_addr_offset) && ($brk_addr_offset<=$length)} {
203
            set data [lreplace $data $brk_addr_offset $brk_addr_offset "0x$mem_breakpoint($brk_addr)"]
204
        }
205
    }
206
 
207
    # Format data
208
    regsub -all {0x} $data {} data
209
    regsub -all { }  $data {} data
210
 
211
    return $data
212
}
213
 
214
 
215
#-----------------------------------------------------------------------------#
216
# Insert breakpoint.                                                          #
217
#-----------------------------------------------------------------------------#
218
proc rsp_Z {sock cmd} {
219
 
220
    global mem_breakpoint
221
 
222
    # Parse command
223
    regexp {(.),(.*),(.*)} $cmd match type addr length
224
    set addr   [format %04x "0x$addr"]
225
 
226
    switch -exact -- $type {
227
        "0"     {# Memory breakpoint
228
                 set mem_breakpoint($addr) [ReadMem 0 "0x$addr"]
229
                 WriteMem 0 "0x$addr" 0x4343
230
                 return "OK"
231
                }
232
 
233
        "1"     {# Hardware breakpoint
234
                 if {[SetHWBreak 1 [format "0x%04x" 0x$addr] 1 0]} {
235
                     return "OK"
236
                 }
237
                 return ""
238
                }
239
 
240
        "2"     {# Write watchpoint
241
                 if {[SetHWBreak 0 [format "0x%04x" 0x$addr] 0 1]} {
242
                     return "OK"
243
                 }
244
                 return ""
245
                }
246
 
247
        "3"     {# Read watchpoint
248
                 if {[SetHWBreak 0 [format "0x%04x" 0x$addr] 1 0]} {
249
                     return "OK"
250
                 }
251
                 return ""
252
                }
253
 
254
        "4"     {# Access watchpoint
255
                 if {[SetHWBreak 0 [format "0x%04x" 0x$addr] 1 1]} {
256
                     return "OK"
257
                 }
258
                 return ""
259
                }
260
 
261
        default {return ""}
262
    }
263
}
264
 
265
#-----------------------------------------------------------------------------#
266
# Remove breakpoint.                                                          #
267
#-----------------------------------------------------------------------------#
268
proc rsp_z {sock cmd} {
269
 
270
    global mem_breakpoint
271
 
272
    # Parse command
273
    regexp {(.),(.*),(.*)} $cmd match type addr length
274
    set addr   [format %04x "0x$addr"]
275
 
276
    switch -exact -- $type {
277
        "0"     {# Memory breakpoint
278
                 WriteMem 0 "0x$addr" $mem_breakpoint($addr)
279
                 unset mem_breakpoint($addr)
280
                 return "OK"
281
                }
282
 
283
        "1"     {# Hardware breakpoint
284
                 if {[ClearHWBreak 1 [format "0x%04x" 0x$addr]]} {
285
                     return "OK"
286
                 }
287
                 return ""
288
                }
289
 
290
        "2"     {# Write watchpoint
291
                 if {[ClearHWBreak 0 [format "0x%04x" 0x$addr]]} {
292
                     return "OK"
293
                 }
294
                 return ""
295
                }
296
 
297
        "3"     {# Read watchpoint
298
                 if {[ClearHWBreak 0 [format "0x%04x" 0x$addr]]} {
299
                     return "OK"
300
                 }
301
                 return ""
302
                }
303
 
304
        "4"     {# Access watchpoint
305
                 if {[ClearHWBreak 0 [format "0x%04x" 0x$addr]]} {
306
                     return "OK"
307
                 }
308
                 return ""
309
                }
310
 
311
        default {return ""}
312
    }
313
}
314
 
315
#-----------------------------------------------------------------------------#
316
# Continue.                                                                   #
317
#-----------------------------------------------------------------------------#
318
proc rsp_c {sock cmd} {
319
 
320
    # Set address if required
321
    if {$cmd!=""} {
322
        set cmd [format %04x "0x$cmd"]
323
        SetPC "0x$cmd"
324
    }
325
 
326
    # Clear status
327
    ClrStatus
328
 
329
    # Continue
330
    ReleaseCPU
331
 
332
 
333
    return [rsp_stop_reply $sock "c"]
334
}
335
 
336
#-----------------------------------------------------------------------------#
337
# Step.                                                                       #
338
#-----------------------------------------------------------------------------#
339
proc rsp_s {sock cmd} {
340
 
341
    # Set address if required
342
    if {$cmd!=""} {
343
        set cmd [format %04x "0x$cmd"]
344
        SetPC "0x$cmd"
345
    }
346
 
347
    # Clear status
348
    ClrStatus
349
 
350
    # Incremental step
351
    StepCPU
352
 
353
    return [rsp_stop_reply $sock "s"]
354
}
355
 
356
 
357
#-----------------------------------------------------------------------------#
358
# The `C', `c', `S', `s', `vCont', `vAttach', `vRun', `vStopped', and `?'     #
359
# packets can receive any of the below as a reply. Except for `?' and         #
360
# `vStopped', that reply is only returned when the target halts.              #
361
#-----------------------------------------------------------------------------#
362
proc rsp_stop_reply {sock cmd} {
363
 
364
    # Wait until halted
365
    while {![IsHalted]} {
366
 
367
        # Wait a few milliseconds to prevent the gui from freezing
368
        after 100 {set end 1}
369
        vwait end
370
 
371
        # Check if we are interrupted by GDB
372
        fconfigure $sock -blocking 0
373
        set break_char [read -nonewline $sock]
374
        fconfigure $sock -blocking 1
375
        binary scan $break_char H* break_char
376
        if {$break_char=="03"} {
377
            putsVerbose "--> BREAK"
378
            HaltCPU
379
        }
380
    }
381
 
382
    # Read some important registers
383
    set pc [ReadReg 0]
384
    regexp {0x(..)(..)} $pc match pc_hi pc_lo
385
    set r4 [ReadReg 4]
386
    regexp {0x(..)(..)} $r4 match r4_hi r4_lo
387
 
388
#    return "S05"
389
    return "T0500:$pc_lo$pc_hi;04:$r4_lo$r4_hi;"
390
}
391
 
392
 
393
#-----------------------------------------------------------------------------#
394
#                                                                             #
395
#-----------------------------------------------------------------------------#
396
proc rsp_q {sock cmd} {
397
 
398
       switch -regexp -- $cmd {
399
 
400
        "C"       {set rsp_answer ""}
401
        "Offsets" {set rsp_answer "Text=0;Data=0;Bss=0"}
402
        "Rcmd,.+" {set rsp_answer [rsp_qRcmd $sock $cmd]}
403
        default   {set rsp_answer ""}
404
    }
405
    return $rsp_answer
406
}
407
 
408
#-----------------------------------------------------------------------------#
409
# qRcmd,command'                                                              #
410
#    command (hex encoded) is passed to the local interpreter for execution.  #
411
#    Invalid commands should be reported using the output string. Before the  #
412
#    final result packet, the target may also respond with a number of        #
413
#    intermediate `Ooutput' console output packets. Implementors should note  #
414
#    that providing access to a stubs's interpreter may have security         #
415
#    implications.                                                            #
416
#-----------------------------------------------------------------------------#
417
proc rsp_qRcmd {sock cmd} {
418
 
419
    regsub {^Rcmd,} $cmd {} cmd
420
    set cmd [binary format H* $cmd];  # Convert hex to ascii
421
 
422
    switch -exact -- $cmd {
423
        "erase all" {;# Convert ascii to hex
424
                     binary scan "Erasing target program memory..." H* text1
425
                     binary scan " Erased OK\n"                     H* text2
426
                     ;# Execute erase command
427
                     sendRSPpacket $sock "O$text1"
428
                     EraseROM
429
                     sendRSPpacket $sock "O$text2"
430
                     set rsp_answer "OK"
431
                    }
432
        default     {set rsp_answer "OK"}
433
    }
434
 
435
    return $rsp_answer
436
 
437
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.