chiark / gitweb /
crashread seems to work host-only, needs for-real testing
authorian <ian>
Tue, 13 Dec 2005 22:43:50 +0000 (22:43 +0000)
committerian <ian>
Tue, 13 Dec 2005 22:43:50 +0000 (22:43 +0000)
detpic/crashread

index 445168b63a4aad235a8f8be4aa9b070e9153fa3c..70b97d80b8608a63b6f04887d020bab5c4453257 100755 (executable)
@@ -127,30 +127,63 @@ fconfigure $p -blocking yes -buffering none \
 
 set ms [expr {
     $slave < 0 ? "t" :
-    $slave ? "m" :
-    "s"
+    $slave ? "s" :
+    "m"
 }]
 
 proc xmit {b} {
     global p
-    p $p [format %c $b]
+#puts stderr >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 [expr {$b | 0x80}]
-    xmit [expr {$slave ^ 0x30}]
+    xmit "$b | 0x80"
+    selectslave_s
+    recv 1
 }
 
 proc setup_m {} { xmit 0 }
 proc setup_s {} { xmit 0; xmit_s 0 }
 proc setup_t {} { }
 
-proc selectaddr_m {a} { error }
-proc selectaddr_s {a} { error }
+proc selectaddr_ms {xmit a} {
+    $xmit "($a >> 6) | 0x40"
+    $xmit "($a & 0x3c) | 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} { error }
-proc readbytes_s {n} { error }
+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 {}
@@ -170,9 +203,10 @@ proc readbytes {addr n} {
        if {$addr & ~0x0fff} { error "bad addr $addr" }
        if {$n > (0x1000 - $addr)} { error "bad len $addr+$n" }
        selectaddr_$ms $addr
-       set readcursor [expr {$addr + $n}]
     }
-    return [readbytes_$ms $n]
+    set r [readbytes_$ms $n]
+    set readcursor [expr {$addr + $n}]
+    return $r
 }
 
 proc thingbynum {thing nnum} {
@@ -211,10 +245,10 @@ set now_max 4
 set inline -1
 set displine 0
 
-setup_$ms
-
 proc p {s} { puts -nonewline $s }
 
+setup_$ms
+
 proc endline {} {
     global inline displine
     if {$inline} { p "\n"; incr displine }
@@ -283,3 +317,4 @@ while {$sectionnum < [llength $sections]} {
     }
     set inline 1
 }
+endline