Commits

Kevin Veroneau  committed 19dcd68

Updated 16-bit implementation to use a new Unit16 class.

  • Participants
  • Parent commits 9d66eda

Comments (0)

Files changed (1)

     termios = None
 
 class Unit(object):
-    """
-    This klass now has a grand purpose!  It will handle the 6/16-bit value storage transparently.
-    """
-    def __init__(self, v1=0, v2=0):
-        if isinstance(v1, int) and isinstance(v2, int) and v2 > 0:
-            object.__setattr__(self, '_value1', v1)
-            object.__setattr__(self, '_value2', v2)
-        else:
-            self.value = v1
+    max_int = 255
+    def __init__(self, default=0):
+        self.value = default
+    def from_str(self, value):
+        return ord(value)
+    def to_str(self, value):
+        return chr(value)
     def __setattr__(self, name, value):
         if name == 'value':
             if isinstance(value, str):
-                value = ord(value)
+                value = self.from_str(value)
             if isinstance(value, int):
-                if value > 65535 or value < 0:
+                if value > self.max_int or value < 0:
                     raise ValueError
-                if value < 256:
-                    # Set an 8-bit value.
-                    object.__setattr__(self, '_value1', value)
-                    object.__setattr__(self, '_value2', 0)
-                else:
-                    # Set a 16-bit value.
-                    object.__setattr__(self, '_value1', value-int(value/256)*256)
-                    object.__setattr__(self, '_value2', int(value/256))
+                object.__setattr__(self, '_value', value)
             else:
                 raise TypeError
     def __add__(self, other):
         if isinstance(other, int):
-            return Unit(self.w + other)
+            return Unit(self._value + other)
         elif isinstance(other, Unit):
-            return Unit(self.w + other.w)
+            return Unit(self._value + other.b)
         else:
             raise NotImplemented
     def __sub__(self, other):
         if isinstance(other, int):
-            return Unit(self.w - other)
+            return Unit(self._value - other)
         elif isinstance(other, Unit):
-            return Unit(self.w - other.w)
+            return Unit(self._value - other.b)
         else:
             raise NotImplemented
     def __eq__(self, other):
-        if isinstance(other, int) and self.w == other:
+        if isinstance(other, int) and self._value == other:
             return True
-        elif isinstance(other, Unit) and self.w == other.w:
+        elif isinstance(other, Unit) and self._value == other.b:
             return True
         else:
             return False
     def __ne__(self, other):
-        if isinstance(other, int) and self.w != other:
+        if isinstance(other, int) and self._value != other:
             return True
-        elif isinstance(other, Unit) and self.w != other.w:
+        elif isinstance(other, Unit) and self._value != other.b:
             return True
         else:
             return False
     @property
     def b(self):
         """Get the byte value."""
-        return self._value1
+        return self._value
     @property
     def c(self):
         """Get the ascii value."""
-        if self._value2 > 0:
-            return chr(self._value1)+chr(self._value2)
-        return chr(self._value1)
-    @property
-    def w(self):
-        """Get the 16-bit integer value."""
-        return self._value2*256+self._value1
+        return self.to_str(self._value)
+
+class Unit16(Unit):
+    max_int = 65536
+    def from_str(self, value):
+        return ord(value[1])*256+ord(value[0])
+    def to_str(self, value):
+        return chr(value-int(value/256)*256)+chr(int(value/256))
 
 class Memory(object):
     def __init__(self, size):
     def read16(self):
         if self.eom:
             print "Memory error: %d" % self.ptr
-        return Unit(ord(self.mem.read(1)), ord(self.mem.read(1)))
+        return Unit16(self.mem.read(2))
     def write(self, value):
         if isinstance(value, Unit):
             self.mem.write(value.c)
         elif isinstance(value, int):
-            self.mem.write(chr(value))
-        elif isinstance(value, str):
-            self.mem.write(value)
-        else:
-            raise ValueError
-    def write16(self, value):
-        if isinstance(value, Unit):
-            self.mem.write(value.c)
-        elif isinstance(value, int):
-            self.mem.write(Unit(value).c)
+            if value < 256:
+                self.mem.write(chr(value))
+            else:
+                self.mem.write(Unit16(value).c)
         elif isinstance(value, str):
             self.mem.write(value)
         else: