X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2FBit.java;h=8010ca760b852ad3cf99f4018f48a93549c233e2;hb=de79184d60c80d6775b368e61d3368de032952e8;hp=e7c93968da7e958b886340067af22d54894d70cf;hpb=33d4533c5e48fbb5d1d0057f2b08d3d6f8e29a87;p=Mograsim.git
diff --git a/era.mi/src/era/mi/logic/Bit.java b/era.mi/src/era/mi/logic/Bit.java
index e7c93968..8010ca76 100644
--- a/era.mi/src/era/mi/logic/Bit.java
+++ b/era.mi/src/era/mi/logic/Bit.java
@@ -1,95 +1,95 @@
package era.mi.logic;
-public enum Bit
-{
- ONE, ZERO, Z, X;
+import java.util.Arrays;
- public static Bit and(Bit a, Bit b)
- {
+/**
+ * stdlogic according to IEEE 1164
+ */
+public enum Bit {
+ U, X, ZERO, ONE, Z;
+
+ 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 Bit and(Bit other) {
+ return fromTable(AND_TABLE, this, other);
}
- public static Bit or(Bit a, Bit b)
- {
+ 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 Bit or(Bit other) {
+ return fromTable(OR_TABLE, this, other);
}
- public static Bit xor(Bit a, Bit b)
- {
+ 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 xor(Bit other) {
+ return fromTable(XOR_TABLE, this, other);
}
- public Bit not()
- {
- switch (this)
- {
- case ONE:
- return Bit.ZERO;
- case ZERO:
- return Bit.ONE;
- default:
- return Bit.X;
- }
+ public Bit not() {
+ switch (this) {
+ case U:
+ return U;
+ case ONE:
+ return ZERO;
+ case ZERO:
+ return ONE;
+ default:
+ return X;
+ }
}
- /**
- * Rules for two bits that get directly connected
- *
- *
- * | X | 0 | 1 | Z |
- * X | X | X | X | X |
- * 0 | X | 0 | X | 0 |
- * 1 | X | X | 1 | 1 |
- * Z | X | 0 | 1 | Z |
- *
- *
- *
- * @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 Bit[] makeArray(int length) {
+ Bit[] bits = new Bit[length];
+ Arrays.fill(bits, this);
+ return bits;
+ }
+
+ public Bit combineWith(Bit other) {
+ return fromTable(JOIN_TABLE, this, other);
}
- public static Bit combine(Bit a, Bit b)
- {
+ public static Bit combine(Bit a, Bit b) {
return a.combineWith(b);
}
-}
+
+ private static Bit fromTable(Bit[][] table, Bit a, Bit b) {
+ return table[a.ordinal()][b.ordinal()];
+ }
+
+ // @formatter:off
+ private static Bit[][] JOIN_TABLE =
+ { { U, U, U, U, U },
+ { U, X, X, X, X },
+ { U, X, ZERO, X, ZERO },
+ { U, X, X, ONE, ONE },
+ { U, X, ZERO, ONE, Z } };
+
+ private static Bit[][] AND_TABLE =
+ { { U, U, ZERO, U, U },
+ { U, X, ZERO, X, X },
+ { ZERO, ZERO, ZERO, ZERO, ZERO },
+ { U, X, ZERO, ONE, X },
+ { U, X, ZERO, X, X } };
+
+ private static Bit[][] OR_TABLE =
+ { { U, U, U, ONE, U },
+ { U, X, X, ONE, X },
+ { U, X, ZERO, ONE, X },
+ { ONE, ONE, ONE, ONE, ONE },
+ { U, X, X, ONE, X } };
+
+ private static Bit[][] XOR_TABLE =
+ { { U, U, U, U, U },
+ { U, X, X, X, X },
+ { U, X, ZERO, ONE, X },
+ { U, X, ONE, ZERO, X },
+ { U, X, X, X, X } };
+ // @formatter:on
+}
\ No newline at end of file