Added 'U' to Bit and made code IEEE 1164 compliant (w.o. W,H,L,-)
authorChristian Femers <femers@in.tum.de>
Wed, 15 May 2019 18:52:24 +0000 (20:52 +0200)
committerChristian Femers <femers@in.tum.de>
Wed, 15 May 2019 18:52:24 +0000 (20:52 +0200)
era.mi/src/era/mi/logic/Bit.java
era.mi/src/era/mi/logic/components/ManualSwitch.java

index 3f25d77..812ee27 100644 (file)
@@ -2,108 +2,94 @@ package era.mi.logic;
 \r
 import java.util.Arrays;\r
 \r
-public enum Bit\r
-{\r
-       ONE, ZERO, Z, X;\r
+/**\r
+ * stdlogic according to IEEE 1164\r
+ */\r
+public enum Bit {\r
+       U, X, ZERO, ONE, Z;\r
 \r
-       public static Bit and(Bit a, Bit b)\r
-       {\r
+       public static Bit and(Bit a, Bit b) {\r
                return a.and(b);\r
        }\r
 \r
-       public Bit and(Bit other)\r
-       {\r
-               if (this == ZERO || other == ZERO)\r
-                       return ZERO;\r
-               else if (this == other && this == ONE)\r
-                       return ONE;\r
-               else if (this == X || other == X)\r
-                       return X;\r
-               else\r
-                       return ZERO;\r
+       public Bit and(Bit other) {\r
+               return fromTable(AND_TABLE, this, other);\r
        }\r
 \r
-       public static Bit or(Bit a, Bit b)\r
-       {\r
+       public static Bit or(Bit a, Bit b) {\r
                return a.or(b);\r
        }\r
 \r
-       public Bit or(Bit other)\r
-       {\r
-               if (this == ONE || other == ONE)\r
-                       return ONE;\r
-               else if (this == other && this == ZERO)\r
-                       return ZERO;\r
-               else if (this == X || other == X)\r
-                       return X;\r
-               else\r
-                       return ZERO;\r
+       public Bit or(Bit other) {\r
+               return fromTable(OR_TABLE, this, other);\r
        }\r
 \r
-       public static Bit xor(Bit a, Bit b)\r
-       {\r
+       public static Bit xor(Bit a, Bit b) {\r
                return a.xor(b);\r
        }\r
 \r
-       public Bit xor(Bit other)\r
-       {\r
-               if(this == X || this == Z || other == X || other == Z)\r
-                       return Bit.X;\r
-               else\r
-                       return this == other ? ZERO : ONE;\r
+       public Bit xor(Bit other) {\r
+               return fromTable(XOR_TABLE, this, other);\r
        }\r
 \r
-       public Bit not()\r
-       {\r
-               switch (this)\r
-                       {\r
-                       case ONE:\r
-                               return Bit.ZERO;\r
-                       case ZERO:\r
-                               return Bit.ONE;\r
-                       default:\r
-                               return Bit.X;\r
-                       }\r
+       public Bit not() {\r
+               switch (this) {\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
+\r
+       public Bit[] makeArray(int length) {\r
                Bit[] bits = new Bit[length];\r
                Arrays.fill(bits, this);\r
                return bits;\r
        }\r
 \r
-       /**\r
-        * Rules for two bits that get directly connected<br>\r
-        * <code><table>\r
-        * <tbody>\r
-        * <tr><td><td>X<td>0<td>1<td>Z</tr>\r
-        * <tr><td>X<td>X<td>X<td>X<td>X</tr>\r
-        * <tr><td>0<td>X<td>0<td>X<td>0</tr>\r
-        * <tr><td>1<td>X<td>X<td>1<td>1</tr>\r
-        * <tr><td>Z<td>X<td>0<td>1<td>Z</tr>\r
-        * </tbody>\r
-        * </table><code>\r
-        * \r
-        * @return the result according to the table\r
-        * \r
-        * @author Christian Femers\r
-        */\r
-       public Bit combineWith(Bit other)\r
-       {\r
-               if (this == other)\r
-                       return this;\r
-               if (this == X || other == X)\r
-                       return X;\r
-               if (other == Z)\r
-                       return this;\r
-               if (this == Z)\r
-                       return other;\r
-               return X;\r
+       public Bit combineWith(Bit other) {\r
+               return fromTable(JOIN_TABLE, this, other);\r
        }\r
 \r
-       public static Bit combine(Bit a, Bit b)\r
-       {\r
+       public static Bit combine(Bit a, Bit b) {\r
                return a.combineWith(b);\r
        }\r
-}\r
+\r
+       private static Bit fromTable(Bit[][] table, Bit a, Bit b) {\r
+               return table[a.ordinal()][b.ordinal()];\r
+       }\r
+\r
+       // @formatter:off\r
+       private static 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 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 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 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
index 19d04cc..142c55d 100644 (file)
@@ -12,7 +12,7 @@ import era.mi.logic.wires.WireArray.WireArrayInput;
  * @author Christian Femers\r
  *\r
  */\r
-public class ManualSwitch implements Component \r
+public final class ManualSwitch implements Component \r
 {\r
        private WireArray output;\r
        private WireArrayInput outputI;\r