-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcom64.rb
More file actions
69 lines (63 loc) · 1.54 KB
/
com64.rb
File metadata and controls
69 lines (63 loc) · 1.54 KB
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
#!/usr/bin/env ruby
# coding: utf-8
# Proxy for this gdbstub + EverDrive-64
# usage: ruby $0 /dev/ttyUSB0 [port]
require 'io/console'
require 'socket'
def read_whole_timeout io, size, timeout
buf = ''.b
while buf.size < size
rs, _, _ = IO.select([io], nil, nil, timeout)
rs.nil? or rs.size == 0 and return buf
buf += io.readpartial size - buf.size
end
return buf
end
def interact remote, local, cut=false
loop {
rs, _, es = IO.select([remote, local[:r]], nil, [remote, local[:r]])
if not es.empty?
$stderr.puts es.inspect
break
end
if rs.include?(remote)
r = read_whole_timeout(remote, 512, 0.5)
cut and r.sub!(/\0*$/n, '')
cut and $stderr.puts "> #{r.inspect}"
local[:w].write(r)
end
if rs.include?(local[:r])
local[:r].eof? and break
r = local[:r].readpartial(2048)
if not cut and r[0] == '!'
r = '$' + r[1..-2] + '#' + '%02x' % [r[1..-2].bytes.inject(&:+) & 0xFF]
end
cut and $stderr.puts "< #{r.inspect}"
r = r.ljust((r.size + 511) & -512, "\0")
remote.write(r)
end
}
end
File.open(ARGV[0], 'r+b') { |com|
com.raw!
com.sync = true
if ARGV[1]
ARGV[1] =~ /\A(?:(.+):)?(.+)\z/
addr = $1 || '127.0.0.1'
port = $2.to_i.abs.to_s
verbose = $2.to_i < 0
TCPServer.open(addr, port) { |ss|
loop {
puts "listening #{addr}:#{port}"
s = ss.accept
puts "accept from #{s.peeraddr.values_at(3,1).inspect}"
interact com, {r: s, w: s}, verbose
puts "disconnected"
}
}
else
$stdout.puts "com64 ready"
$stdout.sync = true
interact com, {r: $stdin, w: $stdout}
end
}