chiark / gitweb /
show debugp in debug section
[trains.git] / detpic / crashread
index 27c66baccdb7c116afcf145b5613a0645cf044f4..ce7b86e1f3e9d5fc7303929eeb08170056500685 100755 (executable)
@@ -1,15 +1,5 @@
 #!/usr/bin/tclsh8.4
 
-if {[llength $argv] != 3} { error "need args: serial port, map file, picno" }
-set port [lindex $argv 0]
-set map [lindex $argv 1]
-set slave [lindex $argv 2]
-
-set m [open $map]
-set block preable
-set headings unknown
-set inblk 3
-
 proc debug {s} { puts "$s" }
 
 proc manyset {list args} {
@@ -30,6 +20,198 @@ proc wantlockind {lockind thing l} {
        default { error "unknown $thing lockind $lockind in $l" }
 }
 
+proc addendlast {v ev evn} {
+    upvar #0 $v syms
+    lappend syms [list [lindex [lindex $syms end] 0] [list {} (lastsymbol)]]
+    lappend syms [list [format 0x%08lx $ev] [list {} $evn]]
+}
+
+proc xmit {b} {
+    global p
+    #debug >xmit|$b<
+    set b [expr $b]
+    set c [binary format c* $b]
+    puts -nonewline $p [format %c $b]
+}
+proc recv {n} {
+    global p
+    set l {}
+    while {$n > 0} {
+       set c [read $p 1]
+       binary scan $c c* d
+       if {![llength $d]} { error "comms eof" }
+       lappend l [expr {$d & 0x0ff}]
+       incr n -1
+    }
+    return $l
+}
+proc junkrecv {} {
+    global p
+    fconfigure $p -blocking no
+    while {[string length [read $p 1024]]} { }
+    fconfigure $p -blocking yes
+}
+
+proc selectslave_slave {slave} {
+    xmit "$slave ^ 0x30"
+}
+
+proc selectslave_s {} {
+    global slave
+    selectslave_slave $slave
+}
+
+proc xmit_slave {slave b} {
+    xmit "$b | 0x80"
+    selectslave_slave $slave
+    recv 1
+}
+
+proc xmit_s {b} {
+    global slave
+    xmit_slave $slave $b
+}
+
+proc pause {t} {
+    global pause_var
+    catch { unset pause_var }
+    after $t {set pause_var y}
+    vwait pause_var
+}
+
+proc setup_m {} { xmit 0; xmit 0; xmit 0; pause 250; junkrecv }
+proc setup_s {} { setup_m; xmit 128; pause 256; junkrecv; xmit_s 0; xmit_s 0 }
+proc setup_t {} { }
+
+proc selectaddr_ms {xmit a} {
+    $xmit "($a >> 6) | 0x40"
+    $xmit "($a & 0x3f) | 0x40"
+}
+proc selectaddr_m {a} { selectaddr_ms xmit $a }
+proc selectaddr_s {a} { selectaddr_ms xmit_s $a }
+proc selectaddr_t {a} { global tsa; set tsa $a }
+    
+proc readbytes_m {n} {
+    xmit "$n | 0x10"
+    return [recv $n]
+}
+proc readbytes_s {n} {
+    xmit $n
+    selectslave_s
+    return [recv $n]
+}
+    
+proc readbytes_t {n} {
+    global tsa
+    set l {}
+    while {$n > 0} {
+       lappend l [expr {$tsa - ($tsa >> 8)}]
+       incr tsa
+       incr n -1
+    }
+    return $l
+}
+
+proc readbytes {addr n} {
+    global readcursor ms
+    if {$readcursor != $addr} {
+       if {$addr & ~0x0fff} { error "bad addr $addr" }
+       if {$n > (0x1000 - $addr)} { error "bad len $addr+$n" }
+       selectaddr_$ms $addr
+    }
+    set r [readbytes_$ms $n]
+    set readcursor [expr {$addr + $n}]
+    return $r
+}
+
+proc thingbynum {thing nnum} {
+    upvar #0 ${thing}num num
+    upvar #0 ${thing}s things
+    upvar #0 ${thing}info info
+    upvar #0 ${thing}addr addr
+    upvar #0 ${thing} name
+    set num $nnum
+    if {$num < [llength $things]} {
+       set info [lindex $things $num]
+    } else {
+       set info {0x7fffffff =DUMMY-END= 1}
+    }
+    manyset $info addr name
+    if {![string compare $thing section]} {
+       global sectionsize sectionend
+       set sectionsize [lindex $info 2]
+       set sectionend [expr {$addr + $sectionsize}]
+    }
+}
+
+proc thingnext {thing} {
+    upvar #0 ${thing}num num
+    incr num
+    thingbynum $thing $num
+}
+
+
+proc p {s} { puts -nonewline $s }
+
+proc reset_s {val min max} {
+    for {set slave $min} {$slave < $max} {incr slave} {
+       xmit_slave $slave $val
+    }
+}
+
+proc reset_m {arg} {
+    if {[regexp {^(\d+)\.(\d+)} $arg min max]} {
+    } elseif {[regexp {^(\d+)} $arg max]} {
+       set min 1
+    } else {
+       error "--reset arg $arg wrong"
+    }
+    setup_m
+    reset_s 0x00 $min $max
+    reset_s 0x00 $min $max
+    reset_s 0x09 $min $max
+    xmit 0x09
+}
+
+proc badusage {m} {
+    set m "bad usage: $m"
+    append m {
+usage: .../crashread <port> <map-file> <picno>
+ pass `-1' for <picno> for test (data memory map) mode only
+ pass `reset' for <map-file> to reset pics (then
+     <picno> should be <lastpicno+1> or <firstslave>-<lastpicno+1>)
+}
+    error $m
+}
+
+if {[llength $argv] != 3} { badusage "wrong # args" }
+
+set port [lindex $argv 0]
+set map [lindex $argv 1]
+set slave [lindex $argv 2]
+
+set p [open $port {RDWR NONBLOCK} 0]
+exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
+       -ctlecho -echo -echoe -echok -echonl -iexten -isig \
+       -icanon -icrnl \
+       9600 clocal cread -crtscts -hup -parenb cs8 -cstopb \
+       -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
+
+fconfigure $p -blocking yes -buffering none \
+       -translation binary -encoding binary
+
+fconfigure stdout -buffering none
+
+if {![string compare reset $map]} {
+    reset_m $slave
+    exit 0
+}
+
+set m [open $map]
+set block preable
+set headings unknown
+set inblk 3
+
 while {[gets $m l] >= 0} {
     if {![regexp {\S} $l]} {
        set inblk 0
@@ -67,6 +249,7 @@ while {[gets $m l] >= 0} {
            set sv symbolsbylockind($lockind)
        } else {
            set sv symbols
+           set smap($sym) $addr
        }
        lappend $sv [list $addr $sym]
     } elseif {$inblk==3} {
@@ -126,29 +309,14 @@ lappend sections {0x00000f00 {=SFRs=} 0}
 #lappend sections {0x00000f60 {==========SFRs,ACS===========} 0}
 
 lappend sections [list 0x1000 =END= 0]
-set sections [lsort $sections]
-set symbols [lsort $symbols]
-#debug >$symbols<
 
-proc addendlast {v ev evn} {
-    upvar #0 $v syms
-    lappend syms [list [lindex [lindex $syms end] 0] [list {} (lastsymbol)]]
-    lappend syms [list [format 0x%08lx $ev] [list {} $evn]]
+proc sortthings {} {
+    foreach tosort {sections symbols symbolsbylockind(program)} {
+       upvar #0 $tosort ts
+       set ts [lsort [set ts]]
+    }
 }
-
-addendlast symbolsbylockind(program) 0x8000 (end)
-
-set p [open $port {RDWR NONBLOCK} 0]
-exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
-       -ctlecho -echo -echoe -echok -echonl -iexten -isig \
-       -icanon -icrnl \
-       9600 clocal cread -crtscts -hup -parenb cs8 -cstopb \
-       -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
-
-fconfigure $p -blocking yes -buffering none \
-       -translation binary -encoding binary
-
-fconfigure stdout -buffering none
+set readcursor -1
 
 set ms [expr {
     $slave < 0 ? "t" :
@@ -156,110 +324,6 @@ set ms [expr {
     "m"
 }]
 
-proc xmit {b} {
-    global p
-    #debug >xmit|$b<
-    set b [expr $b]
-    set c [binary format c* $b]
-    puts -nonewline $p [format %c $b]
-}
-proc recv {n} {
-    global p
-    set l {}
-    while {$n > 0} {
-       set c [read $p 1]
-       binary scan $c c* d
-       if {![llength $d]} { error "comms eof" }
-       lappend l $d
-       incr n -1
-    }
-    return $l
-}
-
-proc selectslave_s {} {
-    global slave
-    xmit "$slave ^ 0x30"
-}
-
-proc xmit_s {b} {
-    xmit "$b | 0x80"
-    selectslave_s
-    recv 1
-}
-
-proc setup_m {} { xmit 0; xmit 0 }
-proc setup_s {} { setup_m; xmit_s 0 }
-proc setup_t {} { }
-
-proc selectaddr_ms {xmit a} {
-    $xmit "($a >> 6) | 0x40"
-    $xmit "($a & 0x3f) | 0x40"
-}
-proc selectaddr_m {a} { selectaddr_ms xmit $a }
-proc selectaddr_s {a} { selectaddr_ms xmit_s $a }
-proc selectaddr_t {a} { global tsa; set tsa $a }
-    
-proc readbytes_m {n} {
-    xmit "$n | 0x10"
-    return [recv $n]
-}
-proc readbytes_s {n} {
-    xmit $n
-    selectslave_s
-    return [recv $n]
-}
-    
-proc readbytes_t {n} {
-    global tsa
-    set l {}
-    while {$n > 0} {
-       lappend l [expr {$tsa - ($tsa >> 8)}]
-       incr tsa
-       incr n -1
-    }
-    return $l
-}
-
-set readcursor -1
-
-proc readbytes {addr n} {
-    global readcursor ms
-    if {$readcursor != $addr} {
-       if {$addr & ~0x0fff} { error "bad addr $addr" }
-       if {$n > (0x1000 - $addr)} { error "bad len $addr+$n" }
-       selectaddr_$ms $addr
-    }
-    set r [readbytes_$ms $n]
-    set readcursor [expr {$addr + $n}]
-    return $r
-}
-
-proc thingbynum {thing nnum} {
-    upvar #0 ${thing}num num
-    upvar #0 ${thing}s things
-    upvar #0 ${thing}info info
-    upvar #0 ${thing}addr addr
-    upvar #0 ${thing} name
-    set num $nnum
-    if {$num < [llength $things]} {
-       set info [lindex $things $num]
-    } else {
-       set info {0x7fffffff =DUMMY-END= 1}
-    }
-    manyset $info addr name
-    if {![string compare $thing section]} {
-       global sectionsize sectionend
-       set sectionsize [lindex $info 2]
-       set sectionend [expr {$addr + $sectionsize}]
-    }
-}
-
-proc thingnext {thing} {
-    upvar #0 ${thing}num num
-    incr num
-    thingbynum $thing $num
-}
-
 thingbynum section 0
 thingbynum symbol 0
 set shownsection {}
@@ -271,8 +335,6 @@ set displine 0
 set inline 0
 set shownsection {}
 
-proc p {s} { puts -nonewline $s }
-
 setup_$ms
 
 proc queue_show {kind value} {
@@ -356,6 +418,18 @@ proc show {sym} {
     }
 }
 
+if {[info exists smap(misc:\ debugp)] && [info exists smap(misc:\ debug)]} {
+    set debugpval [readbytes $smap(misc:\ debugp) 1]
+#    puts y1-$debugpval
+#    puts y2-$smap(misc:\ debug)
+    set debugpaddr [format 0x%08x [expr {$debugpval+$smap(misc:\ debug)}]]
+#    puts y3-$debugpaddr
+    lappend symbols [list $debugpaddr [list "" (debugp)]]
+}
+
+sortthings
+addendlast symbolsbylockind(program) 0x8000 (end)
+
 set stkptr {panic: psave_stkptr}
 set stack {panic: panic_stack}