X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2FBit.java;h=3f25d77e6545e799913ce97b0188cd41db0316a3;hb=fb0f58aee59134ab9178baea80716dc0d62ffd83;hp=e7c93968da7e958b886340067af22d54894d70cf;hpb=af660226c30929777520ffefea850ab890086716;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..3f25d77e 100644
--- a/era.mi/src/era/mi/logic/Bit.java
+++ b/era.mi/src/era/mi/logic/Bit.java
@@ -1,95 +1,109 @@
-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
- *
- *
- * | 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 static Bit combine(Bit a, Bit b)
- {
- return a.combineWith(b);
- }
-}
+package era.mi.logic;
+
+import java.util.Arrays;
+
+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 (this == ZERO || other == ZERO)
+ return ZERO;
+ else if (this == other && this == ONE)
+ return ONE;
+ else if (this == X || other == X)
+ return X;
+ else
+ return ZERO;
+ }
+
+ public static Bit or(Bit a, Bit b)
+ {
+ return a.or(b);
+ }
+
+ public Bit or(Bit other)
+ {
+ 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 ZERO;
+ }
+
+ public static Bit xor(Bit a, Bit b)
+ {
+ return a.xor(b);
+ }
+
+ public Bit xor(Bit other)
+ {
+ if(this == X || this == Z || other == X || other == Z)
+ return Bit.X;
+ else
+ return this == other ? ZERO : ONE;
+ }
+
+ public Bit not()
+ {
+ switch (this)
+ {
+ case ONE:
+ return Bit.ZERO;
+ case ZERO:
+ return Bit.ONE;
+ default:
+ 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
+ *
+ *
+ * | 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 static Bit combine(Bit a, Bit b)
+ {
+ return a.combineWith(b);
+ }
+}