Added character input to tty machine

This commit is contained in:
Kyler 2024-03-08 14:06:47 -07:00
parent 5ac213a779
commit c655bb8982
1 changed files with 25 additions and 0 deletions

View File

@ -4,8 +4,33 @@
from .emulator import Device
try:
# Windows
import msvcrt
def getch() -> int:
return msvcrt.getch()[0]
except ImportError:
# Unix
import sys
import tty, termios
def getch() -> int:
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
return sys.stdin.read(1).encode('utf-8')[0]
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
class tty(Device):
def __getitem__(self, index: int) -> int:
if index & 0xf == 0xd: return 0
elif index & 0xf == 0xe: return 0
elif index & 0xf == 0xf: return getch()
else: return 0
def __setitem__(self, index: int, value: int):
if index & 0xf == 0xd:
if value & 0x800: