mirror of
https://github.com/kbeckmann/game-and-watch-retro-go.git
synced 2025-12-16 13:15:55 +01:00
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
import socket
|
|
|
|
class OpenOCD():
|
|
COMMAND_TOKEN = '\x1a'
|
|
|
|
def __init__(self, host, port):
|
|
self.host = host
|
|
self.port = port
|
|
self.buffer_size = 4096
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
def __enter__(self):
|
|
self.sock.connect((self.host, self.port))
|
|
return self
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
try:
|
|
self.send("exit")
|
|
finally:
|
|
self.sock.close()
|
|
|
|
def send(self, cmd):
|
|
"""Send a command string to TCL RPC. Return the result that was read."""
|
|
data = (cmd + OpenOCD.COMMAND_TOKEN).encode("utf-8")
|
|
self.sock.send(data)
|
|
return self._recv()
|
|
|
|
def _recv(self):
|
|
"""Read from the stream until the token (\x1a) was received."""
|
|
data = bytes()
|
|
while True:
|
|
chunk = self.sock.recv(self.buffer_size)
|
|
data += chunk
|
|
if bytes(OpenOCD.COMMAND_TOKEN, encoding="utf-8") in chunk:
|
|
break
|
|
|
|
data = data.decode("utf-8").strip()
|
|
data = data[:-1] # strip trailing \x1a
|
|
|
|
return data
|
|
|
|
def read_memory(self, wordLen, address, n):
|
|
self.send("array unset output") # better to clear the array before
|
|
self.send("mem2array output %d 0x%x %d" % (wordLen, address, n))
|
|
|
|
output = self.send("capture \"echo \$output\"").split(" ")
|
|
|
|
return [int(output[2*i+1]) for i in range(len(output)//2)]
|