From 2f2269e36940705063adba3ff89ed7830c0b2edf Mon Sep 17 00:00:00 2001 From: Christian Femers Date: Thu, 11 Jul 2019 21:51:38 +0200 Subject: [PATCH] Refactored BitVector methods to resolve ambiguity --- .../net/mograsim/logic/core/types/Bit.java | 5 ++ .../mograsim/logic/core/types/BitVector.java | 89 ++++++++++--------- .../logic/core/types/BitVectorFormatter.java | 6 +- .../net/mograsim/logic/core/wires/Wire.java | 17 +++- .../components/mi/nandbased/GUI_rsLatch.java | 2 +- .../components/mi/nandbased/GUIdlatch4.java | 10 +-- .../mi/nandbased/am2901/GUIAm2901QReg.java | 10 +-- .../mograsim/logic/model/LogicUICanvas.java | 2 +- 8 files changed, 78 insertions(+), 63 deletions(-) diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java index 4fce8dde..8bff357a 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java @@ -91,6 +91,11 @@ public enum Bit implements StrictLogicType 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); diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java index 9e09f43a..cce83f43 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java @@ -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, Iterable, IterablebitIndex. (leftmost bit of a binary number at the given index) + */ + public Bit getMSBit(int bitIndex) { return bits[bitIndex]; } + /** + * Returns the least significant bit at bitIndex. (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, IterablebitIndex. (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 bitIndex. (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 bitIndex. (leftmost bit of a binary number at the given index) + */ + public Bit getMSBit(int bitIndex) { return bits[bitIndex]; } + /** + * Returns the least significant bit at bitIndex. (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, Iterable, Iterablefrom MSB to LSB (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 iterator() { @@ -361,7 +362,7 @@ public final class BitVector implements StrictLogicType, Iterable