ajusted or() behaviour when Z is input
[Mograsim.git] / era.mi / src / era / mi / logic / Bit.java
index e7c9396..0e70985 100644 (file)
@@ -1,5 +1,7 @@
 package era.mi.logic;
 
+import java.util.Arrays;
+
 public enum Bit
 {
        ONE, ZERO, Z, X;
@@ -11,12 +13,14 @@ public enum Bit
 
        public Bit and(Bit other)
        {
-               if (equals(Bit.ZERO) || other.equals(Bit.ZERO))
-                       return Bit.ZERO;
-               else if (equals(other) && equals(Bit.ONE))
-                       return Bit.ONE;
+               if (this == ZERO || other == ZERO)
+                       return ZERO;
+               else if (this == other && this == ONE)
+                       return ONE;
+               else if (this == X || other == X)
+                       return X;
                else
-                       return Bit.X;
+                       return ZERO;
        }
 
        public static Bit or(Bit a, Bit b)
@@ -26,12 +30,14 @@ public enum Bit
 
        public Bit or(Bit other)
        {
-               if (equals(Bit.ONE) || other.equals(Bit.ONE))
-                       return Bit.ONE;
-               else if (equals(other) && equals(Bit.ZERO))
-                       return Bit.ZERO;
+               if (this == ONE || other == ONE)
+                       return ONE;
+               else if (this == other && this == ZERO)
+                       return ZERO;
+               else if (this == X || other == X)
+                       return X;
                else
-                       return Bit.X;
+                       return ZERO;
        }
 
        public static Bit xor(Bit a, Bit b)
@@ -41,9 +47,10 @@ public enum Bit
 
        public Bit xor(Bit other)
        {
-               // I'm uncertain how this should behave for cases where one value is neither 1 nor 0.
-               // TODO: Implement xor
-               return Bit.X;
+               if(this == X || this == Z || other == X || other == Z)
+                       return Bit.X;
+               else
+                       return this == other ? ZERO : ONE;
        }
 
        public Bit not()
@@ -58,6 +65,13 @@ public enum Bit
                                return Bit.X;
                        }
        }
+       
+       public Bit[] makeArray(int length)
+       {
+               Bit[] bits = new Bit[length];
+               Arrays.fill(bits, this);
+               return bits;
+       }
 
        /**
         * Rules for two bits that get directly connected<br>