X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Ftypes%2FBitVector.java;h=74375210549cc280f52ec497ece927a76835df90;hb=4afd7c9afe4333815a488f90bdc3c3b9991028f4;hp=4d5365a0188a95961a37e867b36046cfaf165ae9;hpb=bec6c3a711448475c7b8e43dd031efe7b89aba24;p=Mograsim.git 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 4d5365a0..74375210 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,17 +13,30 @@ import java.util.function.UnaryOperator; /** * Immutable class representing a {@link Bit}Vector * - * * @author Christian Femers * */ public final class BitVector implements StrictLogicType, Iterable, RandomAccess { + public static final BitVector SINGLE_U = new BitVector(Bit.U); + public static final BitVector SINGLE_X = new BitVector(Bit.X); + public static final BitVector SINGLE_0 = new BitVector(Bit.ZERO); + public static final BitVector SINGLE_1 = new BitVector(Bit.ONE); + public static final BitVector SINGLE_Z = new BitVector(Bit.Z); + + private static final BitVector[] SINGLE_BIT_MAPPING = { SINGLE_U, SINGLE_X, SINGLE_0, SINGLE_1, SINGLE_Z }; + private final Bit[] bits; + private BitVector(Bit single) + { + Objects.requireNonNull(single); + bits = new Bit[] { single }; + } + 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(); @@ -31,11 +44,15 @@ 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(); @@ -58,6 +86,8 @@ public final class BitVector implements StrictLogicType, Iterable, Iterable, Iterable, Iterable, 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; @@ -287,7 +347,7 @@ public final class BitVector implements StrictLogicType, Iterable, Iterable, Iterablefrom MSB to LSB (left to right). + */ @Override public Iterator iterator() { @@ -327,7 +390,7 @@ public final class BitVector implements StrictLogicType, Iterable