From bd1aae66e33914e0f9b8cc0f2c6bb5b72320fb82 Mon Sep 17 00:00:00 2001 From: Mike Naberezny Date: Tue, 18 Aug 2009 22:19:05 -0700 Subject: [PATCH] Renamed getch() to getch_noblock(). It now returns a string with the character instead of the ordinal value. --- src/py65/monitor.py | 9 +++++++-- src/py65/utils/console.py | 32 +++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/py65/monitor.py b/src/py65/monitor.py index 7d70294..b652006 100644 --- a/src/py65/monitor.py +++ b/src/py65/monitor.py @@ -11,7 +11,7 @@ from py65.devices.mpu65c02 import MPU as CMOS65C02 from py65.disassembler import Disassembler from py65.assembler import Assembler from py65.utils.addressing import AddressParser -from py65.utils.console import getch +from py65.utils.console import getch_noblock from py65.utils.conversions import itoa from py65.memory import ObservableMemory @@ -92,7 +92,12 @@ class Monitor(cmd.Cmd): self.stdout.flush() def getc(address): - return getch(self.stdin) + char = getch_noblock(self.stdin) + if char: + byte = ord(char) + else: + byte = 0 + return byte m = ObservableMemory() m.subscribe_to_write([0xF001], putc) diff --git a/src/py65/utils/console.py b/src/py65/utils/console.py index 995167c..481f4dd 100644 --- a/src/py65/utils/console.py +++ b/src/py65/utils/console.py @@ -3,15 +3,15 @@ import sys if sys.platform[:3] == "win": import msvcrt - def getch(stdin): - """ Performs a nonblocking read of one byte from the Windows - console and returns its ordinal value. The stdin argument - is for function signature compatibility and is ignored. If - no byte is available, 0 is returned. + def getch_noblock(stdin): + """ Performs a nonblocking read of one character from the Windows + console and returns it. The stdin argument is for function + signature compatibility and is ignored. If no character is + available, an empty string is returned. """ if msvcrt.kbhit(): - return ord(msvcrt.getch()) - return 0 + return msvcrt.getch() + return '' else: import select @@ -19,9 +19,10 @@ else: import termios import fcntl - def getch(stdin): - """ Performs a nonblocking read of one byte from stdin and returns - its ordinal value. If no byte is available, 0 is returned. + def getch_noblock(stdin): + """ Performs a nonblocking read of one character from stdin + returns it. If no character is available, an empty string + is returned. """ fd = stdin.fileno() @@ -38,11 +39,12 @@ else: byte = 0 r, w, e = select.select([fd], [], [], 0.1) if r: - c = stdin.read(1) - byte = ord(c) - if byte == 0x0a: - byte = 0x0d + char = stdin.read(1) + if char == "\n": + char = "\r" + else: + char = '' finally: termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) - return byte + return char