chiark / gitweb /
send two zero bytes in case we were mid-message
[trains.git] / detpic / crashread
1 #!/usr/bin/tclsh8.4
2
3 if {[llength $argv] != 3} { error "need args: serial port, map file, picno" }
4 set port [lindex $argv 0]
5 set map [lindex $argv 1]
6 set slave [lindex $argv 2]
7
8 set m [open $map]
9 set block preable
10 set headings unknown
11 set inblk 3
12
13 proc debug {s} { puts "$s" }
14
15 proc manyset {list args} {
16     foreach val $list var $args { upvar 1 $var my; set my $val }
17 }
18
19 proc inblk {bre hre} {
20     global inblk block headings
21     return [expr {
22         $inblk==3 &&
23         [regexp $bre $block] &&
24         [regexp $hre $headings]
25     }]
26 }
27
28 proc wantlockind {lockind thing l} {
29     switch -exact $lockind  program { return -1 }  data { return 1 }  \
30         default { error "unknown $thing lockind $lockind in $l" }
31 }
32
33 while {[gets $m l] >= 0} {
34     if {![regexp {\S} $l]} {
35         set inblk 0
36         set section unknown
37         set headings n/a
38     } elseif {$inblk==0 && [regexp {^\s+(\S.*\S)\s*$} $l dummy block]} {
39         incr inblk
40     } elseif {$inblk==1} {
41         set headings [string trim $l]
42         incr inblk
43     } elseif {$inblk==2 && [regexp {^[- \t]+$} $l]} {
44         incr inblk
45     } elseif {[inblk {^Section Info$} \
46             {^Section\s+Type\s+Address\s+Location\s+Size\(Bytes\)$}]} {
47         manyset $l sec type addr lockind size
48         switch -exact $type  code { continue }  udata { } \
49                 default { error "unknown section type $type in $l" }
50         if {[wantlockind $lockind section $l]<=0} continue
51         set addr [format 0x%08x $addr]
52         lappend sections [list $addr $sec [format 0x%08x $size]]
53     } elseif {[inblk {^Symbols$} \
54             {^Name\s+Address\s+Location\s+Storage\s+File$}]} {
55         manyset $l sym addr lockind storage file
56         if {![wantlockind $lockind symbol $l]} continue
57         switch -exact $storage {
58             extern { set sym [list {} $sym] }
59             static {
60                 regexp {^(.*)\.asm$} $file dummy file
61                 set sym [list $file: $sym]
62             }
63             default { error "unknown storage $storage in $l" }
64         }
65         set addr [format 0x%08x $addr]
66         if {[string compare $lockind data]} {
67             set sv symbolsbylockind($lockind)
68         } else {
69             set sv symbols
70         }
71         lappend $sv [list $addr $sym]
72     } elseif {$inblk==3} {
73     } else {
74         error "unknown $inblk <$block> <$headings> $l"
75     }
76 }
77
78 set ok {
79     INTCON* FSR2*
80     OSCCON LVDCON WDTCON RCON
81     T1CON T2CON
82     SSPADD SSPSTAT SSPCON1 SSPCON2
83     ADRESH ADRESL ADCON*
84     CCPR1* CCP1CON
85     ECCPR1* ECCP1DEL ECCPAS
86     CMCON CVRCON T3CON
87     SPBRG TXSTA RXSTA
88     EEADR EEDATA
89     IPR* PIR* PIE*
90     TRIS* LAT*
91 }
92
93 set h [open /usr/share/gputils/header/p18f458.inc]
94 set section unknown
95 set lastaddr -1
96 while {[gets $h l]>=0} {
97     if {[regexp {^\;\-\-+\s+(\S.*\S)\s+\-\-+$} $l dummy section]} {
98         continue
99     } elseif {![regexp {^Register Files$} $section]} {
100         continue
101     } elseif {[regexp -nocase \
102             {^([a-z][a-z0-9]*)\s+equ\s+h\'0(f[0-9a-f]{2})\'\s*$} \
103             $l dummy sym loc]} {
104         set addr [format 0x%08x 0x$loc]
105         foreach pat $ok {
106             if {[string match $pat $sym]} {
107                 if {$addr != $lastaddr} {
108                     lappend sections [list $addr =SFRs= 0x1]
109                     set lastaddr $addr
110                 }
111                 lappend symbols [list $addr [list SFR $sym]]
112             }
113         }
114     } elseif {[regexp -nocase {^\;\s*reserved} $l]} {
115     } elseif {![regexp {\S} $l]} {
116     } else {
117         error "unknown <$section> $l"
118     }
119 }
120
121 lappend symbols {0x00000060 {=udata,!acs=}}
122 #lappend symbols {0x00000f00 {=SFRs,!acs=}}
123 lappend symbols {0x00000f60 {=SFRs,acs= {}}}
124 lappend sections {0x00000f00 {=SFRs=} 0}
125 #lappend sections {0x00000060 {==========UDATA,!ACS===========} 0}
126 #lappend sections {0x00000f60 {==========SFRs,ACS===========} 0}
127
128 lappend sections [list 0x1000 =END= 0]
129 set sections [lsort $sections]
130 set symbols [lsort $symbols]
131 #debug >$symbols<
132
133 proc addendlast {v ev evn} {
134     upvar #0 $v syms
135     lappend syms [list [lindex [lindex $syms end] 0] [list {} (lastsymbol)]]
136     lappend syms [list [format 0x%08lx $ev] [list {} $evn]]
137 }
138
139 addendlast symbolsbylockind(program) 0x8000 (end)
140
141 set p [open $port {RDWR NONBLOCK} 0]
142 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
143         -ctlecho -echo -echoe -echok -echonl -iexten -isig \
144         -icanon -icrnl \
145         9600 clocal cread -crtscts -hup -parenb cs8 -cstopb \
146         -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
147
148 fconfigure $p -blocking yes -buffering none \
149         -translation binary -encoding binary
150
151 fconfigure stdout -buffering none
152
153 set ms [expr {
154     $slave < 0 ? "t" :
155     $slave ? "s" :
156     "m"
157 }]
158
159 proc xmit {b} {
160     global p
161     #debug >xmit|$b<
162     set b [expr $b]
163     set c [binary format c* $b]
164     puts -nonewline $p [format %c $b]
165 }
166 proc recv {n} {
167     global p
168     set l {}
169     while {$n > 0} {
170         set c [read $p 1]
171         binary scan $c c* d
172         if {![llength $d]} { error "comms eof" }
173         lappend l $d
174         incr n -1
175     }
176     return $l
177 }
178
179 proc selectslave_s {} {
180     global slave
181     xmit "$slave ^ 0x30"
182 }
183
184 proc xmit_s {b} {
185     xmit "$b | 0x80"
186     selectslave_s
187     recv 1
188 }
189
190 proc setup_m {} { xmit 0; xmit 0 }
191 proc setup_s {} { setup_m; xmit_s 0 }
192 proc setup_t {} { }
193
194 proc selectaddr_ms {xmit a} {
195     $xmit "($a >> 6) | 0x40"
196     $xmit "($a & 0x3f) | 0x40"
197 }
198 proc selectaddr_m {a} { selectaddr_ms xmit $a }
199 proc selectaddr_s {a} { selectaddr_ms xmit_s $a }
200 proc selectaddr_t {a} { global tsa; set tsa $a }
201     
202 proc readbytes_m {n} {
203     xmit "$n | 0x10"
204     return [recv $n]
205 }
206 proc readbytes_s {n} {
207     xmit $n
208     selectslave_s
209     return [recv $n]
210 }
211     
212 proc readbytes_t {n} {
213     global tsa
214     set l {}
215     while {$n > 0} {
216         lappend l [expr {$tsa - ($tsa >> 8)}]
217         incr tsa
218         incr n -1
219     }
220     return $l
221 }
222
223 set readcursor -1
224
225 proc readbytes {addr n} {
226     global readcursor ms
227     if {$readcursor != $addr} {
228         if {$addr & ~0x0fff} { error "bad addr $addr" }
229         if {$n > (0x1000 - $addr)} { error "bad len $addr+$n" }
230         selectaddr_$ms $addr
231     }
232     set r [readbytes_$ms $n]
233     set readcursor [expr {$addr + $n}]
234     return $r
235 }
236
237 proc thingbynum {thing nnum} {
238     upvar #0 ${thing}num num
239     upvar #0 ${thing}s things
240     upvar #0 ${thing}info info
241     upvar #0 ${thing}addr addr
242     upvar #0 ${thing} name
243     set num $nnum
244     if {$num < [llength $things]} {
245         set info [lindex $things $num]
246     } else {
247         set info {0x7fffffff =DUMMY-END= 1}
248     }
249     manyset $info addr name
250     if {![string compare $thing section]} {
251         global sectionsize sectionend
252         set sectionsize [lindex $info 2]
253         set sectionend [expr {$addr + $sectionsize}]
254     }
255 }
256
257 proc thingnext {thing} {
258     upvar #0 ${thing}num num
259     incr num
260     thingbynum $thing $num
261 }
262
263 thingbynum section 0
264 thingbynum symbol 0
265 set shownsection {}
266 set sectionchange 0
267 set insection 0
268 set addr 0
269 set now_max 4
270 set displine 0
271 set inline 0
272 set shownsection {}
273
274 proc p {s} { puts -nonewline $s }
275
276 setup_$ms
277
278 proc queue_show {kind value} {
279     upvar #0 q_$kind queued
280     lappend queued $value
281 }
282
283 proc p_addr_symbol {a s} {
284     global inline
285     endline
286     p [format "%08x %-15s %-20s" $a [lindex $s 0] [lindex $s 1]]
287     set inline 1
288 }
289
290 proc endline {} {
291     global inline
292     if {!$inline} return
293     p "\n"
294     set inline 0
295 }
296
297 proc do_show {} {
298     global addr inline insection q_symbol q_section shownss
299     global shownsection
300     #debug "do_show [format %x $addr] $insection $q_symbol $q_section"
301     if {$inline} { error "do_show inline $addr" }
302     foreach s $q_section {
303         if {![string compare $s $shownsection]} continue
304         p "---------- $s ----------\n"
305         set shownsection $s
306     }
307     set shownss {}
308     foreach s $q_symbol {
309         p_addr_symbol $addr $s
310         set shownss [concat $s]
311     }
312     #debug "shownss>$shownss<"
313     if {!$insection && ![string compare =SFRs= $shownsection]} {
314         endline
315         p "\n"
316         reset_show
317         return
318     }
319     if {![llength $q_symbol]} { p_addr_symbol $addr {} }
320     if {!$insection} {
321         endline
322 #       p "------------------------------\n"
323         set shownsection {}
324     }
325     reset_show
326 }
327
328 proc reset_show {} {
329     foreach v {q_section q_symbol} {
330         global $v
331         set $v {}
332     }
333 }
334
335 proc show {sym} {
336     global insection section displine addr shownsection inline shownss
337     set showsectionend 0
338     if {$insection && [string compare $section $shownsection]} {
339         endline
340         set shownsection $section
341         set displine 0
342     } elseif {!$insection && [string length $shownsection]} {
343         endline
344         set shownsection {}
345         set displine 0
346     }
347     if {[string length $sym]} {
348         if {$displine && $inline && !($displine&3)} {
349             p "\n"
350         }
351         endline
352     }
353     if {!$inline} {
354         set inline 1
355         set shownss $shownsection-$sym
356     }
357 }
358
359 set stkptr {panic: psave_stkptr}
360 set stack {panic: panic_stack}
361
362 reset_show
363 foreach ss [list $stkptr $stack] { set ccontents($ss) {} }
364
365 while {$sectionnum < [llength $sections]} {
366     # So what happens at this address ?
367
368     set now_section [expr {$sectionchange - $addr}]
369     if {!$now_section && !$insection} {
370         queue_show section $section
371         set insection 1
372         set sectionchange $sectionend
373         continue
374     }
375     if {!$now_section && $insection} {
376         thingnext section
377         set insection 0
378         set sectionchange $sectionaddr
379         continue
380     }
381
382     set now_symbol [expr {$symboladdr - $addr}]
383     if {!$now_symbol} {
384         queue_show symbol $symbol
385         thingnext symbol
386         continue
387     }
388
389     # OK, that's all the things that we need to say
390     # about this addr.
391
392     # Decide how much to do:
393     set now $now_symbol
394     if {$now > $now_section} { set now $now_section }
395
396     #debug "now $now"
397     do_show
398
399     if {!$insection} {
400         incr addr $now
401         continue
402     }
403
404     while {$now > 0} {
405         set nownow $now
406         if {$nownow > $now_max} { set nownow $now_max }
407         set bytes [readbytes $addr $nownow]
408         foreach b $bytes {
409             set h [format "%02x" [expr {$b & 0xff}]]
410             p " $h"
411             if {[info exists ccontents($shownss)]} {
412                 append ccontents($shownss) $h
413             }
414             incr addr
415         }
416         incr now -$nownow
417     }
418     endline
419 }
420
421 set stackdepth 31
422
423 proc stack_chkptr {si} {
424     global stkptr
425     if {$si == $stkptr+1} { p " - - - - -\n" }
426 }
427
428 foreach v {stkptr stack} {
429     #debug ">$v|[set $v]|$ccontents([set $v])<"
430     set $v $ccontents([set $v])
431 }
432
433 endline
434 p "---------- =Execution Stack= ----------\n"
435 set stkptr [expr "0x$stkptr & $stackdepth"]
436
437 for {set si 1} {$si <= $stackdepth} {incr si} {
438     stack_chkptr $si
439     for {set ch 2; set se 0x} {$ch >= 0} {incr ch -1} {
440         append se [string range $stack \
441                 [expr ($si-1)*6+$ch*2] [expr ($si-1)*6+$ch*2+1]]
442     }
443     set symaddr 0; set symsym (start)
444     foreach symi $symbolsbylockind(program) {
445         if {[lindex $symi 0] > $se} break
446         manyset $symi symaddr symsym
447     }
448     p [format " 0d%02d  %6x = %6x + %s\n" $si $se \
449             [expr {$se-$symaddr}] [join $symsym]]
450 }
451 stack_chkptr [expr {$stackdepth+1}]
452
453 p "---------- ========== ----------\n"