Did some clean up
[Mograsim.git] / era.mi / src / era / mi / logic / Bit.java
index e7c9396..1118443 100644 (file)
-package era.mi.logic;
-
-public enum Bit
-{
-       ONE, ZERO, Z, X;
-
-       public static Bit and(Bit a, Bit b)
-       {
-               return a.and(b);
-       }
-
-       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;
-               else
-                       return Bit.X;
-       }
-
-       public static Bit or(Bit a, Bit b)
-       {
-               return a.or(b);
-       }
-
-       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;
-               else
-                       return Bit.X;
-       }
-
-       public static Bit xor(Bit a, Bit b)
-       {
-               return a.xor(b);
-       }
-
-       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;
-       }
-
-       public Bit not()
-       {
-               switch (this)
-                       {
-                       case ONE:
-                               return Bit.ZERO;
-                       case ZERO:
-                               return Bit.ONE;
-                       default:
-                               return Bit.X;
-                       }
-       }
-
-       /**
-        * Rules for two bits that get directly connected<br>
-        * <code><table>
-        * <tbody>
-        * <tr><td><td>X<td>0<td>1<td>Z</tr>
-        * <tr><td>X<td>X<td>X<td>X<td>X</tr>
-        * <tr><td>0<td>X<td>0<td>X<td>0</tr>
-        * <tr><td>1<td>X<td>X<td>1<td>1</tr>
-        * <tr><td>Z<td>X<td>0<td>1<td>Z</tr>
-        * </tbody>
-        * </table><code>
-        * 
-        * @return the result according to the table
-        * 
-        * @author Christian Femers
-        */
-       public Bit combineWith(Bit other)
-       {
-               if (this == other)
-                       return this;
-               if (this == X || other == X)
-                       return X;
-               if (other == Z)
-                       return this;
-               if (this == Z)
-                       return other;
-               return X;
-       }
-
-       public static Bit combine(Bit a, Bit b)
-       {
-               return a.combineWith(b);
-       }
-}
+package era.mi.logic;\r
+\r
+import java.util.Arrays;\r
+\r
+/**\r
+ * stdlogic according to IEEE 1164\r
+ */\r
+public enum Bit\r
+{\r
+       U, X, ZERO, ONE, Z;\r
+\r
+       public static Bit and(Bit a, Bit b)\r
+       {\r
+               return a.and(b);\r
+       }\r
+\r
+       public Bit and(Bit other)\r
+       {\r
+               return fromTable(AND_TABLE, this, other);\r
+       }\r
+\r
+       public static Bit or(Bit a, Bit b)\r
+       {\r
+               return a.or(b);\r
+       }\r
+\r
+       public Bit or(Bit other)\r
+       {\r
+               return fromTable(OR_TABLE, this, other);\r
+       }\r
+\r
+       public static Bit xor(Bit a, Bit b)\r
+       {\r
+               return a.xor(b);\r
+       }\r
+\r
+       public Bit xor(Bit other)\r
+       {\r
+               return fromTable(XOR_TABLE, this, other);\r
+       }\r
+\r
+       public Bit not()\r
+       {\r
+               switch (this)\r
+               {\r
+               case U:\r
+                       return U;\r
+               case ONE:\r
+                       return ZERO;\r
+               case ZERO:\r
+                       return ONE;\r
+               default:\r
+                       return X;\r
+               }\r
+       }\r
+\r
+       public Bit[] makeArray(int length)\r
+       {\r
+               Bit[] bits = new Bit[length];\r
+               Arrays.fill(bits, this);\r
+               return bits;\r
+       }\r
+\r
+       public Bit combineWith(Bit other)\r
+       {\r
+               return fromTable(JOIN_TABLE, this, other);\r
+       }\r
+\r
+       public static Bit combine(Bit a, Bit b)\r
+       {\r
+               return a.combineWith(b);\r
+       }\r
+\r
+       private static Bit fromTable(Bit[][] table, Bit a, Bit b)\r
+       {\r
+               return table[a.ordinal()][b.ordinal()];\r
+       }\r
+\r
+       // @formatter:off\r
+       private static final Bit[][] JOIN_TABLE = \r
+               { { U, U, U,    U,   U    }, \r
+                 { U, X, X,    X,   X    }, \r
+                 { U, X, ZERO, X,   ZERO },\r
+                 { U, X, X,    ONE, ONE  }, \r
+                 { U, X, ZERO, ONE, Z    } };\r
+\r
+       private static final Bit[][] AND_TABLE = \r
+               { { U,    U,    ZERO, U,    U    }, \r
+                 { U,    X,    ZERO, X,    X    },\r
+                 { ZERO, ZERO, ZERO, ZERO, ZERO }, \r
+                 { U,    X,    ZERO, ONE,  X    }, \r
+                 { U,    X,    ZERO, X,    X    } };\r
+\r
+       private static final Bit[][] OR_TABLE =\r
+       { { U,   U,   U,    ONE, U    },    \r
+         { U,   X,   X,    ONE, X    },    \r
+         { U,   X,   ZERO, ONE, X    },    \r
+         { ONE, ONE, ONE,  ONE, ONE  },    \r
+         { U,   X,   X,    ONE, X    } };\r
+       \r
+       private static final Bit[][] XOR_TABLE =\r
+       { { U, U, U,    U,    U },    \r
+         { U, X, X,    X,    X },    \r
+         { U, X, ZERO, ONE,  X },    \r
+         { U, X, ONE,  ZERO, X },    \r
+         { U, X, X,    X,    X } }; \r
+       // @formatter:on\r
+}
\ No newline at end of file