Kevin Veroneau avatar Kevin Veroneau committed 39192c7 Draft

Added screen clear/color support, and single key input.

Comments (0)

Files changed (1)

 from cStringIO import StringIO
 import sys
 import zlib
+try:
+    import termios
+except ImportError:
+    print "termios not loaded, simulation will be limited."
+    termios = None
 
 class Unit(object):
     def __init__(self, default=0):
             raise InvalidInterrupt("INT %d is not defined." % i)
         func()
     def int_2(self):
-        print "CPU: Screen support not implemented."
+        sys.stdout.write('\033[2J\033[0;0H')
     def int_3(self):
         sys.stdout.write("%s" % self.ax.c)
     def int_4(self):
-        print "CPU: Screen support not implemented."
+        sys.stdout.write('\033[1;%dm' % self.ax.b)
     def int_5(self):
         self.mem.memcopy(self.ax.b, self.bx.b, self.cx.b)
     def int_6(self):
     def int_10(self):
         ptr = self.mem.ptr
         self.mem.ptr = self.ax.b
-        print self.mem.readstring()
+        sys.stdout.write(self.mem.readstring())
         self.mem.ptr = ptr
     def int_11(self):
-        print "Single key input not implemented."
-        self.mem[self.cx.b] = 'A'
+        if termios:
+            sys.stdin.flush()
+            self.mem[self.cx.b] = sys.stdin.read(1)
+        else:
+            print "CPU: Single key input not supported on this platform."
+            self.mem[self.cx.b] = 'A'
     def int_12(self):
         ptr = self.mem.ptr
         self.mem.ptr = self.ax.b
         self.mem.ptr = ptr
         exitcode = 0
         sjmp = ptr
+        if termios:
+            attr = termios.tcgetattr(sys.stdin)
+            oldattr = attr[3]
+            attr[3] = attr[3] & ~termios.ICANON
+            termios.tcsetattr(sys.stdin, termios.TCSANOW, attr)
         while True:
             if 'bp' in self.__dict__ and self.bp == self.mem.ptr: break
             if self.mem.eom: break
                     self.mem.ptr = self.dx.b
             elif self.cpu_hooks.has_key(op):
                 self.cpu_hooks[op](self.mem.read().b)
+        if termios:
+            attr[3] = oldattr
+            termios.tcsetattr(sys.stdin, termios.TCSANOW, attr)
 
 if __name__ == '__main__':
     #import readline
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.