| e8555c89 Andy McCall |
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 |
namespace eval tcl_bridge { |
| 55 |
|
| 56 |
variable wp {} |
| 57 |
variable latch 0 |
| 58 |
variable address |
| 59 |
|
| 60 |
set_help_text tcl_bridge \ |
| 61 |
{tcl_bridge -- allows guest/host communication through a MSX-to-Tcl bridge. |
| 62 |
|
| 63 |
Usage: |
| 64 |
tcl_bridge start Create communication channel |
| 65 |
tcl_bridge stop Destroy communication channel |
| 66 |
} |
| 67 |
|
| 68 |
proc tcl_bridge {cmd} { |
| 69 |
switch -- $cmd { |
| 70 |
start { tcl_bridge::start } |
| 71 |
stop { tcl_bridge::stop } |
| 72 |
default { error "Unknown sub-command \"$cmd\"." } |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
proc start {} { |
| 77 |
variable wp |
| 78 |
if {$wp eq {}} { |
| 79 |
set wp [debug watchpoint create -type write_io -address 6 -command {tcl_bridge::read_command $::wp_last_value}] |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
proc stop {} { |
| 84 |
variable wp |
| 85 |
if {$wp ne {}} { |
| 86 |
debug watchpoint remove $wp |
| 87 |
set wp {} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
proc min {x y} { |
| 92 |
expr {$x < $y ? $x : $y} |
| 93 |
} |
| 94 |
|
| 95 |
proc execute_command {base} { |
| 96 |
set cmd_addr [peek16 $base] |
| 97 |
set cmd_len [peek16 [expr {$base + 2}]] |
| 98 |
set res_addr [peek16 [expr {$base + 4}]] |
| 99 |
set res_max_len [peek16 [expr {$base + 6}]] |
| 100 |
set res_real_len [expr {$base + 8}] |
| 101 |
set status [expr {$base + 10}] |
| 102 |
set cmd [debug read_block memory $cmd_addr $cmd_len] |
| 103 |
|
| 104 |
set response [catch {set output [eval $cmd]} output] |
| 105 |
|
| 106 |
debug write_block memory $res_real_len [binary format s [min [string length $output] 0xFFFF]] |
| 107 |
debug write_block memory $res_addr [string range $output 0 [min [string length $output] $res_max_len]-1]] |
| 108 |
debug write memory $status $response |
| 109 |
} |
| 110 |
|
| 111 |
proc read_command {byte} { |
| 112 |
variable latch |
| 113 |
variable address |
| 114 |
switch -- $latch { |
| 115 |
0 { |
| 116 |
set address $byte |
| 117 |
set latch 1 |
| 118 |
} |
| 119 |
1 { |
| 120 |
incr address [expr {$byte << 8}] |
| 121 |
set latch 0 |
| 122 |
execute_command $address |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
namespace export tcl_bridge |
| 128 |
|
| 129 |
} ; |
| 130 |
|
| 131 |
namespace import tcl_bridge::* |