Refactored BitVector methods to resolve ambiguity
authorChristian Femers <femers@in.tum.de>
Thu, 11 Jul 2019 19:51:38 +0000 (21:51 +0200)
committerChristian Femers <femers@in.tum.de>
Thu, 11 Jul 2019 19:51:38 +0000 (21:51 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java
net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUI_rsLatch.java
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUIdlatch4.java
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/am2901/GUIAm2901QReg.java
net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java

index 4fce8dd..8bff357 100644 (file)
@@ -91,6 +91,11 @@ public enum Bit implements StrictLogicType<Bit>
                return values()[2 + (value & 1)];
        }
 
+       public static Bit of(boolean binaryValue)
+       {
+               return binaryValue ? ONE : ZERO;
+       }
+
        public static Bit parse(String s)
        {
                Bit bit = SYMBOL_MAP.get(s);
index 9e09f43..cce83f4 100644 (file)
@@ -13,7 +13,6 @@ import java.util.function.UnaryOperator;
 /**
  * Immutable class representing a {@link Bit}Vector
  *
- *
  * @author Christian Femers
  *
  */
@@ -23,7 +22,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
 
        private BitVector(Bit[] bits)
        {
-               this.bits = Objects.requireNonNull(bits);// do this first to "catch" bits==null before the foreach loop
+               this.bits = Objects.requireNonNull(bits); // do this first to "catch" bits==null before the foreach loop
                for (Bit bit : bits)
                        if (bit == null)
                                throw new NullPointerException();
@@ -44,11 +43,22 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return BitVectorMutator.of(this);
        }
 
-       public Bit getBit(int bitIndex)
+       /**
+        * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+        */
+       public Bit getMSBit(int bitIndex)
        {
                return bits[bitIndex];
        }
 
+       /**
+        * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+        */
+       public Bit getLSBit(int bitIndex)
+       {
+               return bits[bits.length - bitIndex - 1];
+       }
+
        public Bit[] getBits()
        {
                return bits.clone();
@@ -224,16 +234,38 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        return this;
                }
 
-               public void setBit(int bitIndex, Bit bit)
+               /**
+                * Set the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+                */
+               public void setMSBit(int bitIndex, Bit bit)
                {
                        bits[bitIndex] = bit;
                }
 
-               public Bit getBit(int bitIndex)
+               /**
+                * Set the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+                */
+               public void setLSBit(int bitIndex, Bit bit)
+               {
+                       bits[bits.length - bitIndex - 1] = bit;
+               }
+
+               /**
+                * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+                */
+               public Bit getMSBit(int bitIndex)
                {
                        return bits[bitIndex];
                }
 
+               /**
+                * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+                */
+               public Bit getLSBit(int bitIndex)
+               {
+                       return bits[bits.length - bitIndex - 1];
+               }
+
                public int length()
                {
                        return bits.length;
@@ -286,45 +318,26 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
        }
 
-       @Override
-       public String toString()
-       {
-               return toBitStringMSBFirst();
-       }
-
-       /**
-        * All {@link Bit}s symbols concatenated together
-        * 
-        * @see #parse(String)
-        */
-       public String toBitStringLSBFirst()
-       {
-               StringBuilder sb = new StringBuilder(bits.length);
-               for (Bit bit : bits)
-                       sb.append(bit);
-               return sb.toString();
-       }
-
        /**
-        * All {@link Bit}s symbols concatenated together, with the MSB coming first (like a binary number)
+        * All {@link Bit}s symbols concatenated together (MSB first)
         * 
         * @see #parse(String)
         */
-       public String toBitStringMSBFirst()
+       @Override
+       public String toString()
        {
                StringBuilder sb = new StringBuilder(bits.length);
                for (Bit bit : bits)
                        sb.append(bit);
-               sb.reverse();
                return sb.toString();
        }
 
        /**
-        * Parses a String containing solely {@link Bit} symbols
+        * Parses a String containing solely {@link Bit} symbols (MSB first)
         * 
-        * @see #toBitStringLSBFirst()
+        * @see #toString()
         */
-       public static BitVector parseLSBFirst(String s)
+       public static BitVector parse(String s)
        {
                Bit[] values = new Bit[s.length()];
                for (int i = 0; i < s.length(); i++)
@@ -335,20 +348,8 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        }
 
        /**
-        * Parses a String containing solely {@link Bit} symbols, with the MSB coming first (like a binary number)
-        * 
-        * @see #toBitStringLSBFirst()
+        * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
         */
-       public static BitVector parseMSBFirst(String s)
-       {
-               Bit[] values = new Bit[s.length()];
-               for (int i = 0, j = s.length() - 1; j >= 0; i++, j--)
-               {
-                       values[i] = Bit.parse(s, j);
-               }
-               return new BitVector(values);
-       }
-
        @Override
        public Iterator<Bit> iterator()
        {
@@ -361,7 +362,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        {
                                if (!hasNext())
                                        throw new NoSuchElementException();
-                               return getBit(pos++);
+                               return getMSBit(pos++);
                        }
 
                        @Override
index 5fd1e2c..987f361 100644 (file)
@@ -16,7 +16,7 @@ public class BitVectorFormatter
        {
                if (bitVector == null)
                        return "null";
-               return bitVector.toBitStringMSBFirst();
+               return bitVector.toString();
        }
 
        // TODO doesn't this belong to logic.ui?
@@ -30,7 +30,7 @@ public class BitVectorFormatter
                // TODO maybe find a color assignment for multiple-bit bit vectors?
                if (bitVector == null || bitVector.length() != 1)
                        return new ColorDefinition(BuiltInColor.COLOR_BLACK);
-               switch (bitVector.getBit(0))
+               switch (bitVector.getLSBit(0))
                {
                case ONE:
                        return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.one");
@@ -43,7 +43,7 @@ public class BitVectorFormatter
                case ZERO:
                        return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.zero");
                default:
-                       throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getBit(0));
+                       throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getLSBit(0));
                }
        }
 
index a14efb0..b0e96f1 100644 (file)
@@ -150,14 +150,20 @@ public class Wire
                return val;
        }
 
+       /**
+        * Returns the least significant bit (LSB)
+        */
        public Bit getValue()
        {
                return getValue(0);
        }
 
+       /**
+        * Returns the least significant bit (LSB) of the given index
+        */
        public Bit getValue(int index)
        {
-               return values.getBit(index);
+               return values.getLSBit(index);
        }
 
        public BitVector getValues(int start, int end)
@@ -446,7 +452,8 @@ public class Wire
                }
 
                /**
-                * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
+                * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
+                *         significant bit (LSB)
                 */
                public Bit getInputValue()
                {
@@ -455,10 +462,12 @@ public class Wire
 
                /**
                 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
+                *         Returns the least significant bit (LSB)
+                * 
                 */
                public Bit getInputValue(int index)
                {
-                       return inputValues.getBit(index);
+                       return inputValues.getLSBit(index);
                }
 
                /**
@@ -466,7 +475,7 @@ public class Wire
                 */
                public BitVector getInputValues()
                {
-                       return getInputValues(0, length);
+                       return inputValues;
                }
 
                public BitVector getInputValues(int start, int end)
index c5d54c2..20fbbaa 100644 (file)
@@ -94,7 +94,7 @@ public class GUI_rsLatch extends SimpleRectangularSubmodelComponent
                {
                case "q":
                        if (wireQ.hasLogicModelBinding())
-                               return wireQ.getWireValues().getBit(0);
+                               return wireQ.getWireValues().getLSBit(0);
                        return null;
                default:
                        // should not happen because we tell SubmodelComponent to only allow these state IDs.
index 3468798..875843f 100644 (file)
@@ -103,10 +103,10 @@ public class GUIdlatch4 extends SimpleRectangularSubmodelComponent
                        break;
                case "q":
                        BitVector newStateCasted = (BitVector) newState;
-                       setHighLevelState("q1", newStateCasted.getBit(0));
-                       setHighLevelState("q2", newStateCasted.getBit(1));
-                       setHighLevelState("q3", newStateCasted.getBit(2));
-                       setHighLevelState("q4", newStateCasted.getBit(3));
+                       setHighLevelState("q1", newStateCasted.getLSBit(0));
+                       setHighLevelState("q2", newStateCasted.getLSBit(1));
+                       setHighLevelState("q3", newStateCasted.getLSBit(2));
+                       setHighLevelState("q4", newStateCasted.getLSBit(3));
                        break;
                default:
                        // should not happen because we tell SubmodelComponent to only allow these state IDs.
@@ -132,7 +132,7 @@ public class GUIdlatch4 extends SimpleRectangularSubmodelComponent
                        Bit q2 = (Bit) getHighLevelState("q2");
                        Bit q3 = (Bit) getHighLevelState("q3");
                        Bit q4 = (Bit) getHighLevelState("q4");
-                       return BitVector.of(q1, q2, q3, q4);
+                       return BitVector.of(q4, q3, q2, q1);
                default:
                        // should not happen because we tell SubmodelComponent to only allow these state IDs.
                        throw new IllegalStateException("Illegal atomic state ID: " + stateID);
index 9902b24..efae9af 100644 (file)
@@ -111,10 +111,10 @@ public class GUIAm2901QReg extends SimpleRectangularSubmodelComponent
                        break;
                case "q":
                        BitVector newStateCasted = (BitVector) newState;
-                       setHighLevelState("q1", newStateCasted.getBit(0));
-                       setHighLevelState("q2", newStateCasted.getBit(1));
-                       setHighLevelState("q3", newStateCasted.getBit(2));
-                       setHighLevelState("q4", newStateCasted.getBit(3));
+                       setHighLevelState("q1", newStateCasted.getLSBit(0));
+                       setHighLevelState("q2", newStateCasted.getLSBit(1));
+                       setHighLevelState("q3", newStateCasted.getLSBit(2));
+                       setHighLevelState("q4", newStateCasted.getLSBit(3));
                        break;
                default:
                        // should not happen because we tell SubmodelComponent to only allow these state IDs.
@@ -140,7 +140,7 @@ public class GUIAm2901QReg extends SimpleRectangularSubmodelComponent
                        Bit q2 = (Bit) getHighLevelState("q2");
                        Bit q3 = (Bit) getHighLevelState("q3");
                        Bit q4 = (Bit) getHighLevelState("q4");
-                       return BitVector.of(q1, q2, q3, q4);
+                       return BitVector.of(q4, q3, q2, q1);
                default:
                        // should not happen because we tell SubmodelComponent to only allow these state IDs.
                        throw new IllegalStateException("Illegal atomic state ID: " + stateID);
index 55fd8cf..609bf74 100644 (file)
@@ -122,7 +122,7 @@ public class LogicUICanvas extends ZoomableCanvas
                                if (radioBit.getSelection())
                                        value = Bit.parse(valueString);
                                else if (radioBitVector.getSelection())
-                                       value = BitVector.parseMSBFirst(valueString);
+                                       value = BitVector.parse(valueString);
                                else
                                        throw new RuntimeException("No value type selected");
                                target.setHighLevelState(stateIDText.getText(), value);