-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream.cfg
79 lines (71 loc) · 2.13 KB
/
stream.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
proc jtagstream_poll {tap tx n} {
set m [string length $tx]
set n [expr ($m>$n)?$m:$n]
set txi [lrepeat $n {10 0x001}]
set i 0
foreach txj [split $tx ""] {
lset txi $i 1 [format 0x%4.4X [expr { 0x201 | ([scan $txj %c] << 1) }]]
incr i
#echo tx[scan $txj %c]
}
set txi [concat {*}$txi]
set rxi [split [drscan $tap {*}$txi ] " "]
#echo $txi:$rxi
set rx ""
set writable 1
foreach {rxj} $rxi {
set readable [expr { "0x${rxj}" & 0x200 }]
set writable [expr { "0x${rxj}" & $writable }]
if {$readable} {
append rx [format %c [expr { ("0x${rxj}" >> 1) & 0xff }]]
}
}
return [list $rx $readable $writable]
}
proc jtagstream_drain {tap tx chunk_rx max_rx} {
lassign [jtagstream_poll $tap $tx $chunk_rx] rx readable writable
while {[expr { $writable && ($readable > 0) && ([string length $rx] < $max_rx) }]} {
lassign [jtagstream_poll $tap "" $chunk_rx] rxi readable writable
append rx $rxi
}
#if {!$writable} {
# echo "write overflow"
#}
return $rx
}
proc jtagstream_rxtx {tap client is_poll} {
if {![$client eof]} {
set tx [$client read 16]
set rx [jtagstream_drain $tap $tx 128 4096]
if {[string length $rx]} {
#echo [string length $rx]
$client puts -nonewline $rx
}
if {$is_poll} {
after 1 [list jtagstream_rxtx $tap $client 1]
}
} else {
$client readable {}
$client onexception {}
$client close
}
}
proc jtagstream_client {tap sock} {
set client [$sock accept]
fconfigure $client -buffering none
fconfigure $client -blocking 0
$client readable [list jtagstream_rxtx $tap $client 0]
$client onexception [list $client close]
after 1 [list jtagstream_rxtx $tap $client 1]
}
proc jtagstream_exit {sock} {
stdin readable {}
$sock readable {}
}
proc jtagstream_serve {tap port} {
set sock [socket stream.server $port]
$sock readable [list jtagstream_client $tap $sock]
stdin readable [list jtagstream_exit $sock]
vwait forever
$sock close
}