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