\r
import java.util.Arrays;\r
\r
+/**\r
+ * stdlogic according to IEEE 1164\r
+ */\r
public enum Bit\r
{\r
- ONE, ZERO, Z, X;\r
+ U, X, ZERO, ONE, Z;\r
\r
public static Bit and(Bit a, Bit 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
+ return fromTable(AND_TABLE, this, other);\r
}\r
\r
public static Bit or(Bit a, Bit b)\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
+ return fromTable(OR_TABLE, this, other);\r
}\r
\r
public static Bit xor(Bit a, Bit b)\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
+ 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
+ {\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
+\r
public Bit[] makeArray(int length)\r
{\r
Bit[] bits = new Bit[length];\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
+ 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
+\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 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