--- /dev/null
+#!/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 inblk {bre hre} {
+ global inblk block headings
+ return [expr {
+ $inblk==3 &&
+ [regexp $bre $block] &&
+ [regexp $hre $headings]
+ }]
+}
+
+while {[gets $m l] >= 0} {
+ if {![regexp {\S} $l]} {
+ set inblk 0
+ set section unknown
+ set headings n/a
+ } elseif {$inblk==0 && [regexp {^\s+(\S.*\S)\s*$} $l dummy block]} {
+ incr inblk
+ } elseif {$inblk==1} {
+ set headings [string trim $l]
+ incr inblk
+ } elseif {$inblk==2 && [regexp {^[- \t]+$} $l]} {
+ incr inblk
+ } elseif {[inblk {^Section Info$} \
+ {^Section\s+Type\s+Address\s+Location\s+Size\(Bytes\)$}]} {
+ if {[string compare udata [lindex $l 1]]} continue
+ if {[string compare data [lindex $l 3]]} continue
+ set loc [format %08x [lindex $l 2]]
+ set s [lrange $l 0 end]
+ lappend sections [lrange $l 0 end]
+ } elseif {[inblk {^Symbols$} \
+ {^Name\s+Address\s+Location\s+Storage\s+File$}]} {
+ if {[string compare data [lindex $l 2]]} continue
+ lappend symbols [lrange $l 0 end]
+ } elseif {$inblk==3} {
+ } else {
+ error "unknown $inblk <$block> <$headings> $l"
+ }
+}
+
+set ok {
+ INTCON* FSR2*
+ OSCCON LVDCON WDTCON RCON
+ T1CON T2CON
+ SSPADD SSPSTAT SSPCON1 SSPCON2
+ ADRESH ADRESL ADCON*
+ CCPR1* CCP1CON
+ ECCPR1* ECCP1DEL ECCPAS
+ CMCON CVRCON T3CON
+ SPBRG TXSTA RXSTA
+ EEADR EEDATA
+ IPR* PIR* PIE*
+ TRIS* LAT*
+}
+
+set h [open /usr/share/gputils/header/p18f458.inc]
+set section unknown
+while {[gets $h l]>=0} {
+ if {[regexp {^\;\-\-+\s+(\S.*\S)\s+\-\-+$} $l dummy section]} {
+ continue
+ } elseif {![regexp {^Register Files$} $section]} {
+ continue
+ } elseif {[regexp -nocase \
+ {^([a-z][a-z0-9]*)\s+equ\s+h\'0(f[0-9a-f]{2})\'\s*$} \
+ $l dummy sym loc]} {
+ foreach pat $ok {
+ if {[string match $pat $sym]} {
+ set loc 0x$loc
+ lappend symbols [list $sym $loc data extern x]
+ lappend sections [list SFR sfr $loc data 0x1]
+ }
+ }
+ } elseif {[regexp -nocase {^\;\s*reserved} $l]} {
+ } elseif {![regexp {\S} $l]} {
+ } else {
+ error "unknown <$section> $l"
+ }
+}
+
+puts >$sections<
+puts >$symbols<
+
+exit 0
+
+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
+
+set ms [expr {$slave ? "m" : "s"}]
+
+proc xmit_s {b} {
+ xmit [expr {$b | 0x80}]
+ xmit [expr {$slave ^ 0x30}] }
+}
+
+proc setup_m {} { xmit 0 }
+proc setup_s {} { xmit 0; xmit_s 0 }
+
+setup_$ms
+
+xmit 0
+
+if {$slave} {
+ xmit 0x80
+xmit_slave 0x80
+ xmit
+}