X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2FBit.java;h=11184439127c887dc8fe655359739d7f1400e2ba;hb=f2cd90fa2b844507bc9697d3af1f3e18aac80b37;hp=3f25d77e6545e799913ce97b0188cd41db0316a3;hpb=fb0f58aee59134ab9178baea80716dc0d62ffd83;p=Mograsim.git
diff --git a/era.mi/src/era/mi/logic/Bit.java b/era.mi/src/era/mi/logic/Bit.java
index 3f25d77e..11184439 100644
--- a/era.mi/src/era/mi/logic/Bit.java
+++ b/era.mi/src/era/mi/logic/Bit.java
@@ -2,9 +2,12 @@ package era.mi.logic;
import java.util.Arrays;
+/**
+ * stdlogic according to IEEE 1164
+ */
public enum Bit
{
- ONE, ZERO, Z, X;
+ U, X, ZERO, ONE, Z;
public static Bit and(Bit a, Bit b)
{
@@ -13,14 +16,7 @@ public enum Bit
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;
+ return fromTable(AND_TABLE, this, other);
}
public static Bit or(Bit a, Bit b)
@@ -30,14 +26,7 @@ public enum Bit
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;
+ return fromTable(OR_TABLE, this, other);
}
public static Bit xor(Bit a, Bit b)
@@ -47,25 +36,24 @@ public enum Bit
public Bit xor(Bit other)
{
- if(this == X || this == Z || other == X || other == Z)
- return Bit.X;
- else
- return this == other ? ZERO : ONE;
+ 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;
- }
+ {
+ case U:
+ return U;
+ case ONE:
+ return ZERO;
+ case ZERO:
+ return ONE;
+ default:
+ return X;
+ }
}
-
+
public Bit[] makeArray(int length)
{
Bit[] bits = new Bit[length];
@@ -73,37 +61,48 @@ public enum Bit
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;
+ return fromTable(JOIN_TABLE, this, other);
}
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 final 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 final 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 final 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 final 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