3 set port [lindex $argv 0]
4 set map [lindex $argv 1]
5 set slave [lindex $argv 2]
12 proc debug {s} { puts "$s" }
14 proc manyset {list args} {
15 foreach val $list var $args { upvar 1 $var my; set my $val }
18 proc inblk {bre hre} {
19 global inblk block headings
22 [regexp $bre $block] &&
23 [regexp $hre $headings]
27 proc wantlockind {lockind thing l} {
28 switch -exact $lockind program { return -1 } data { return 1 } \
29 default { error "unknown $thing lockind $lockind in $l" }
32 while {[gets $m l] >= 0} {
33 if {![regexp {\S} $l]} {
37 } elseif {$inblk==0 && [regexp {^\s+(\S.*\S)\s*$} $l dummy block]} {
39 } elseif {$inblk==1} {
40 set headings [string trim $l]
42 } elseif {$inblk==2 && [regexp {^[- \t]+$} $l]} {
44 } elseif {[inblk {^Section Info$} \
45 {^Section\s+Type\s+Address\s+Location\s+Size\(Bytes\)$}]} {
46 manyset $l sec type addr lockind size
47 switch -exact $type code { continue } udata { } \
48 default { error "unknown section type $type in $l" }
49 if {[wantlockind $lockind section $l]<=0} continue
50 set addr [format 0x%08x $addr]
51 lappend sections [list $addr $sec [format 0x%08x $size]]
52 } elseif {[inblk {^Symbols$} \
53 {^Name\s+Address\s+Location\s+Storage\s+File$}]} {
54 manyset $l sym addr lockind storage file
55 if {![wantlockind $lockind symbol $l]} continue
56 switch -exact $storage {
57 extern { set sym [list {} $sym] }
59 regexp {^(.*)\.asm$} $file dummy file
60 set sym [list $file: $sym]
62 default { error "unknown storage $storage in $l" }
64 set addr [format 0x%08x $addr]
65 if {[string compare $lockind data]} {
66 set sv symbolsbylockind($lockind)
70 lappend $sv [list $addr $sym]
71 } elseif {$inblk==3} {
73 error "unknown $inblk <$block> <$headings> $l"
79 OSCCON LVDCON WDTCON RCON
81 SSPADD SSPSTAT SSPCON1 SSPCON2
84 ECCPR1* ECCP1DEL ECCPAS
92 set h [open /usr/share/gputils/header/p18f458.inc]
95 while {[gets $h l]>=0} {
96 if {[regexp {^\;\-\-+\s+(\S.*\S)\s+\-\-+$} $l dummy section]} {
98 } elseif {![regexp {^Register Files$} $section]} {
100 } elseif {[regexp -nocase \
101 {^([a-z][a-z0-9]*)\s+equ\s+h\'0(f[0-9a-f]{2})\'\s*$} \
103 set addr [format 0x%08x 0x$loc]
105 if {[string match $pat $sym]} {
106 if {$addr != $lastaddr} {
107 lappend sections [list $addr =SFRs= 0x1]
110 lappend symbols [list $addr [list SFR $sym]]
113 } elseif {[regexp -nocase {^\;\s*reserved} $l]} {
114 } elseif {![regexp {\S} $l]} {
116 error "unknown <$section> $l"
120 lappend symbols {0x00000060 {=udata,!acs=}}
121 #lappend symbols {0x00000f00 {=SFRs,!acs=}}
122 lappend symbols {0x00000f60 {=SFRs,acs= {}}}
123 lappend sections {0x00000f00 {=SFRs=} 0}
124 #lappend sections {0x00000060 {==========UDATA,!ACS===========} 0}
125 #lappend sections {0x00000f60 {==========SFRs,ACS===========} 0}
127 lappend sections [list 0x1000 =END= 0]
128 foreach tosort {sections symbols symbolsbylockind(program)} {
129 set $tosort [lsort [set $tosort]]
132 proc addendlast {v ev evn} {
134 lappend syms [list [lindex [lindex $syms end] 0] [list {} (lastsymbol)]]
135 lappend syms [list [format 0x%08lx $ev] [list {} $evn]]
138 addendlast symbolsbylockind(program) 0x8000 (end)
140 set p [open $port {RDWR NONBLOCK} 0]
141 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
142 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
144 9600 clocal cread -crtscts -hup -parenb cs8 -cstopb \
145 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
147 fconfigure $p -blocking yes -buffering none \
148 -translation binary -encoding binary
150 fconfigure stdout -buffering none
156 set c [binary format c* $b]
157 puts -nonewline $p [format %c $b]
165 if {![llength $d]} { error "comms eof" }
173 fconfigure $p -blocking no
174 while {[string length [read $p 1024]]} { }
175 fconfigure $p -blocking yes
178 proc selectslave_slave {slave} {
182 proc selectslave_s {} {
184 selectslave_slave $slave
195 catch { unset pause_var }
196 after $t {set pause_var y}
200 proc setup_m {} { xmit 0; xmit 0; xmit 0; pause 250; junkrecv }
201 proc setup_s {} { setup_m; xmit 128; pause 256; junkrecv; xmit_s 0; xmit_s 0 }
204 proc selectaddr_ms {xmit a} {
205 $xmit "($a >> 6) | 0x40"
206 $xmit "($a & 0x3f) | 0x40"
208 proc selectaddr_m {a} { selectaddr_ms xmit $a }
209 proc selectaddr_s {a} { selectaddr_ms xmit_s $a }
210 proc selectaddr_t {a} { global tsa; set tsa $a }
212 proc readbytes_m {n} {
216 proc readbytes_s {n} {
222 proc readbytes_t {n} {
226 lappend l [expr {$tsa - ($tsa >> 8)}]
233 proc readbytes {addr n} {
235 if {$readcursor != $addr} {
236 if {$addr & ~0x0fff} { error "bad addr $addr" }
237 if {$n > (0x1000 - $addr)} { error "bad len $addr+$n" }
240 set r [readbytes_$ms $n]
241 set readcursor [expr {$addr + $n}]
245 proc thingbynum {thing nnum} {
246 upvar #0 ${thing}num num
247 upvar #0 ${thing}s things
248 upvar #0 ${thing}info info
249 upvar #0 ${thing}addr addr
250 upvar #0 ${thing} name
252 if {$num < [llength $things]} {
253 set info [lindex $things $num]
255 set info {0x7fffffff =DUMMY-END= 1}
257 manyset $info addr name
258 if {![string compare $thing section]} {
259 global sectionsize sectionend
260 set sectionsize [lindex $info 2]
261 set sectionend [expr {$addr + $sectionsize}]
265 proc thingnext {thing} {
266 upvar #0 ${thing}num num
268 thingbynum $thing $num
272 proc p {s} { puts -nonewline $s }
274 proc ms_reset {arg} {
275 if {[regexp {^(\d+)\.(\d+)} $arg min max]} {
276 } elseif {[regexp {^(\d+)} $arg max]} {
279 error "--reset arg $arg wrong"
283 for {set slave $min} {$slave < $max} {incr slave} {
284 selectslave_slave $slave
290 switch -exact [llength $argv].[lindex $argv 0] {
291 1.--reset { ms_reset 0 }
292 2.--reset { ms_reset [lindex $argv 1] }
294 default { error "need args: serial port, map file, picno" }
318 proc queue_show {kind value} {
319 upvar #0 q_$kind queued
320 lappend queued $value
323 proc p_addr_symbol {a s} {
326 p [format "%08x %-15s %-20s" $a [lindex $s 0] [lindex $s 1]]
338 global addr inline insection q_symbol q_section shownss
340 #debug "do_show [format %x $addr] $insection $q_symbol $q_section"
341 if {$inline} { error "do_show inline $addr" }
342 foreach s $q_section {
343 if {![string compare $s $shownsection]} continue
344 p "---------- $s ----------\n"
348 foreach s $q_symbol {
349 p_addr_symbol $addr $s
350 set shownss [concat $s]
352 #debug "shownss>$shownss<"
353 if {!$insection && ![string compare =SFRs= $shownsection]} {
359 if {![llength $q_symbol]} { p_addr_symbol $addr {} }
362 # p "------------------------------\n"
369 foreach v {q_section q_symbol} {
376 global insection section displine addr shownsection inline shownss
378 if {$insection && [string compare $section $shownsection]} {
380 set shownsection $section
382 } elseif {!$insection && [string length $shownsection]} {
387 if {[string length $sym]} {
388 if {$displine && $inline && !($displine&3)} {
395 set shownss $shownsection-$sym
399 set stkptr {panic: psave_stkptr}
400 set stack {panic: panic_stack}
403 foreach ss [list $stkptr $stack] { set ccontents($ss) {} }
405 while {$sectionnum < [llength $sections]} {
406 # So what happens at this address ?
408 set now_section [expr {$sectionchange - $addr}]
409 if {!$now_section && !$insection} {
410 queue_show section $section
412 set sectionchange $sectionend
415 if {!$now_section && $insection} {
418 set sectionchange $sectionaddr
422 set now_symbol [expr {$symboladdr - $addr}]
424 queue_show symbol $symbol
429 # OK, that's all the things that we need to say
432 # Decide how much to do:
434 if {$now > $now_section} { set now $now_section }
446 if {$nownow > $now_max} { set nownow $now_max }
447 set bytes [readbytes $addr $nownow]
449 set h [format "%02x" [expr {$b & 0xff}]]
451 if {[info exists ccontents($shownss)]} {
452 append ccontents($shownss) $h
463 proc stack_chkptr {si} {
465 if {$si == $stkptr+1} { p " - - - - -\n" }
468 foreach v {stkptr stack} {
469 #debug ">$v|[set $v]|$ccontents([set $v])<"
470 set $v $ccontents([set $v])
474 p "---------- =Execution Stack= ----------\n"
475 set stkptr [expr "0x$stkptr & $stackdepth"]
477 for {set si 1} {$si <= $stackdepth} {incr si} {
479 for {set ch 2; set se 0x} {$ch >= 0} {incr ch -1} {
480 append se [string range $stack \
481 [expr ($si-1)*6+$ch*2] [expr ($si-1)*6+$ch*2+1]]
483 set symaddr 0; set symsym (start)
484 foreach symi $symbolsbylockind(program) {
485 if {[lindex $symi 0] > $se} break
486 manyset $symi symaddr symsym
488 p [format " 0d%02d %6x = %6x + %s\n" $si $se \
489 [expr {$se-$symaddr}] [join $symsym]]
491 stack_chkptr [expr {$stackdepth+1}]
493 p "---------- ========== ----------\n"