-
Notifications
You must be signed in to change notification settings - Fork 1
/
systemdevices.py
78 lines (61 loc) · 2.5 KB
/
systemdevices.py
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
#
# Emulator-specific pseudo-devices
#
import os
import selectors
import socket
import sys
from device import Device
from musashi import m68k
class RootDevice(Device):
def __init__(self, args, **options):
super().__init__(args=args, name='root')
self.emu.add_reset_hook(self.reset)
def add_system_devices(self, args):
if args.stdout_console:
self.emu.add_device(args, StdoutConsole)
else:
self.emu.add_device(args, SocketConsole)
@classmethod
def add_arguments(cls, parser):
parser.add_argument('--stdout-console',
action='store_true',
default=False,
help='sends console output to stdout instead of connecting '
'to the console server. Disconnects console input.')
# StdoutConsole.add_arguments(parser)
# SocketConsole.add_arguments(parser)
class SocketConsole(Device):
def __init__(self, args, **options):
super().__init__(args=args, name='console')
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self._socket.connect(('localhost', 6809))
self._selector = selectors.DefaultSelector()
self._selector.register(self._socket,
selectors.EVENT_READ,
self._recv)
except ConnectionRefusedError as e:
self.emu.fatal('console server not listening, run \'py68k.py --console-server\' in another window.')
super().register_console_output_handler(self._send)
self.callback_after(self.cycle_rate / 100, 'console', self._check_socket)
def _send(self, output):
self._socket.send(output)
def _check_socket(self):
events = self._selector.select(timeout=0)
for key, mask in events:
callback = key.data
callback(key.fileobj, mask)
self.callback_after(self.cycle_rate / 100, 'console', self._check_socket)
def _recv(self, conn, mask):
input = conn.recv(10)
if len(input) == 0:
self.emu.fatal('console server disconnected')
super().console_handle_input(input)
class StdoutConsole(Device):
def __init__(self, args, address, interrupt):
super().__init__(args=args, name='console')
super().register_console_output_handler(self._send)
def _send(self, output):
sys.stdout.write(output.decode('latin-1'))
sys.stdout.flush()