forked from MSXALL/pymsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen_kb.py
147 lines (106 loc) · 4.03 KB
/
screen_kb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# (C) 2020 by Folkert van Heusden <[email protected]>
# released under AGPL v3.0
import os
import signal
import struct
import sys
import threading
from enum import IntEnum
from vdp import vdp
class screen_kb:
class Msg(IntEnum):
SET_IO = 0
GET_IO = 1
INTERRUPT = 2
GET_REG = 3
def __init__(self, io, options):
self.stop_flag = False
self.io = io
self.keyboard_queue = []
self.k_lock = threading.Lock()
self.debug_msg_lock = threading.Lock()
self.debug_msg = None
self.init_screen(options)
super(screen_kb, self).__init__()
def get_ios(self):
return [ [ 0x98, 0x99, 0xa9, 0xaa ] , [ 0x98, 0x99, 0x9a, 0x9b, 0xaa ] ]
def get_name(self):
return 'screen/keyboard'
def init_screen(self, options):
# pipes for data to the VDP
self.pipe_tv_in, self.pipe_tv_out = os.pipe()
# pipes for data from the VDP
self.pipe_fv_in, self.pipe_fv_out = os.pipe()
self.pid = os.fork()
if self.pid == 0:
wrescale = options.wrescale if options.wrescale else 1
hrescale = options.hrescale if options.hrescale else 1
self.vdp = vdp(wrescale, hrescale)
self.vdp.start()
while True:
type_ = os.read(self.pipe_tv_in, 1)[0]
if type_ == screen_kb.Msg.SET_IO:
data = os.read(self.pipe_tv_in, 2)
a = data[0]
v = data[1]
self.vdp.write_io(a, v)
elif type_ == screen_kb.Msg.GET_IO:
a = os.read(self.pipe_tv_in, 1)[0]
v = self.vdp.read_io(a)
packet = ( screen_kb.Msg.GET_IO, a, v )
os.write(self.pipe_fv_out, bytearray(packet))
elif type_ == screen_kb.Msg.INTERRUPT:
self.vdp.interrupt()
# interrupt ack
packet = ( screen_kb.Msg.INTERRUPT, 123 )
os.write(self.pipe_fv_out, bytearray(packet))
elif type_ == screen_kb.Msg.GET_REG:
a = os.read(self.pipe_tv_in, 1)[0]
packet = ( screen_kb.Msg.GET_REG, self.vdp.registers[a] )
os.write(self.pipe_fv_out, bytearray(packet))
else:
print('Unexpected message %d' % type_)
sys.exit(1)
os.close(self.pipe_tv_in)
os.close(self.pipe_fv_out)
def interrupt(self):
os.write(self.pipe_tv_out, screen_kb.Msg.INTERRUPT.to_bytes(1, 'big'))
data = os.read(self.pipe_fv_in, 2)
assert data[0] == screen_kb.Msg.INTERRUPT
assert data[1] == 123
def IE0(self) -> bool:
packet = [ screen_kb.Msg.GET_REG, 1 ] # request VDP status register 1
os.write(self.pipe_tv_out, bytearray(packet))
data = os.read(self.pipe_fv_in, 2)
type_ = data[0]
assert type_ == screen_kb.Msg.GET_REG
v = data[1]
return (v & 32) == 32
def write_io(self, a: int, v: int) -> None:
packet = ( screen_kb.Msg.SET_IO, a, v )
os.write(self.pipe_tv_out, bytearray(packet))
def read_io(self, a: int) -> None:
if a in (0x98, 0x99, 0xa9):
packet = ( screen_kb.Msg.GET_IO, a )
os.write(self.pipe_tv_out, bytearray(packet))
data = os.read(self.pipe_fv_in, 3)
type_ = data[0]
assert type_ == screen_kb.Msg.GET_IO
a_ = data[1]
assert a_ == a
v = data[2]
return v
elif a == 0xaa:
return 0 # FIXME
print('unexpected port %02x' % a)
return 0x00
def debug(self, str_):
self.debug_msg_lock.acquire()
self.debug_msg = str_
self.debug_msg_lock.release()
def stop(self):
self.stop_flag = True
os.kill(self.pid, signal.SIGKILL)
os.wait()
os.close(self.pipe_tv_out)
os.close(self.pipe_fv_in)