Translate cr to crlf at output similarly to how Apple ROM console routines work
authorNick Downing <nick@ndcode.org>
Mon, 16 May 2022 12:17:44 +0000 (22:17 +1000)
committerNick Downing <nick@ndcode.org>
Mon, 16 May 2022 12:17:44 +0000 (22:17 +1000)
apple_io.py
applesoft_basic.t

index e5e2a62..ab13c6e 100755 (executable)
@@ -7,33 +7,44 @@ import sys
 import termios
 import tty
 
+# global state
 attr = None
+fd_in = sys.stdin.fileno()
+fd_out = sys.stdout.fileno()
+poll_in = select.poll()
+poll_in.register(fd_in, select.POLLIN)
+
 def init():
   global attr
-  if attr is None and os.isatty(sys.stdin.fileno()):
-    attr = termios.tcgetattr(sys.stdin.fileno())
+
+  if attr is None and os.isatty(fd_in):
+    attr = termios.tcgetattr(fd_in)
     atexit.register(deinit)
-    tty.setraw(sys.stdin)
+    tty.setraw(fd_in)
+
 def deinit():
   global attr
+
   if attr is not None:
-    termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, attr)
-    attr = None
+    termios.tcsetattr(fd_in, termios.TCSADRAIN, attr)
     atexit.unregister(deinit)
+    attr = None
+
+def read_ready():
+  return len(poll_in.poll(1)) != 0
 
 def read(n):
-  data = os.read(sys.stdin.fileno(), n)
+  data = os.read(fd_in, 1)
   if len(data) == 0:
     raise Exception('end of input') # due to piping or input redirection
   return str(data, 'ascii')
 
 def write(str):
-  os.write(sys.stdout.fileno(), bytes(str, 'ascii'))
+  os.write(fd_out, bytes(data, 'ascii'))
 
-poll_stdin = select.poll()
-poll_stdin.register(sys.stdin.fileno(), select.POLLIN)
-def read_ready():
-  return len(poll_stdin.poll(1)) != 0
+def cr():
+  # apple treats \r as \r\n, so if you write e.g. \r\n you'll get \r\n\n
+  write('\r\n')
 
 def htab(x):
   write(f'\x1b[{x:d}G')
@@ -66,8 +77,9 @@ if __name__ == '__main__':
     while not read_ready():
       pass
     k = ord(read(1))
-    write(f'k {k:02x}\r\n')
+    write(f'k {k:02x}\r')
     if k == 0x1b:
       break
-  for i in range(12):
-    tone(int(round(220 * 2 ** (i / 12.))), 250)
+  #for i in range(12):
+  #  tone(int(round(220 * 2 ** (i / 12.))), 250)
+  deinit()
index 0a05c7d..6b2640a 100644 (file)
@@ -295,9 +295,15 @@ def execute(self, context):
       if value[i] != '.':
         i += 1
       value = sign + value[:i] + exponent
+    else:
+      lines = value.split('\r')
+      for i in lines[:-1]:
+        apple_io.write(i)
+        apple_io.cr() # really writes \r\n
+      value = lines[-1]
     apple_io.write(value)
   if not self.semicolon:
-    apple_io.write('\r\n')
+    apple_io.cr() # really writes \r\n
 @method(NodeStatementGoto)
 def execute(self, context):
   context.i = context.find_line(self.children[0].int_value)