Removed old and incorrect CoreWire#get(Un)SignedValue
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 15 Sep 2019 16:15:06 +0000 (18:15 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 15 Sep 2019 16:15:06 +0000 (18:15 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/components/CoreDemux.java
net.mograsim.logic.core/src/net/mograsim/logic/core/components/CoreMux.java
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
net.mograsim.logic.core/src/net/mograsim/logic/core/wires/CoreWire.java
net.mograsim.logic.core/test/net/mograsim/logic/core/tests/CoreComponentTest.java
net.mograsim.machine/src/net/mograsim/machine/mi/components/CoreMicroInstructionMemory.java
net.mograsim.machine/src/net/mograsim/machine/standard/memory/CoreWordAddressableMemory.java
net.mograsim.machine/test/net/mograsim/machine/standard/memory/WordAddressableMemoryTest.java

index 76982e0..8f4fe42 100644 (file)
@@ -57,7 +57,7 @@ public class CoreDemux extends BasicCoreComponent
        @Override
        public TimelineEventHandler compute()
        {
-               int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
+               int selectValue = select.getValues().isBinary() ? (int) select.getValues().getUnsignedValueLong() : -1;
                if (selectValue >= outputs.length)
                        selectValue = -1;
 
index a47ad6b..5b4220a 100644 (file)
@@ -71,7 +71,7 @@ public class CoreMux extends BasicCoreComponent
        public TimelineEventHandler compute()
        {
                int selectValue;
-               if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
+               if (!select.getValues().isBinary() || (selectValue = (int) select.getValues().getUnsignedValueLong()) >= inputs.length)
                {
                        return e -> out.clearSignals();
                }
index ee95756..1346694 100644 (file)
@@ -437,6 +437,31 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return new BigInteger(bytes);
        }
 
+       public long getUnsignedValueLong()
+       {
+               return getUnsignedValue().longValue();
+       }
+
+       /**
+        * Returns the value of the BitVector as BigInteger interpreted as a two's complement number.
+        * 
+        * @throws NumberFormatException if the BitVector is not {@link #isBinary() binary}.
+        * 
+        * @author Daniel Kirschten
+        */
+       public BigInteger getSignedValue()
+       {
+               BigInteger unsignedValue = getUnsignedValue();
+               if (bits[bits.length - 1] == Bit.ZERO)
+                       return unsignedValue;
+               return unsignedValue.subtract(BitVector.of(Bit.ONE, bits.length).getUnsignedValue()).subtract(BigInteger.ONE);// TODO speed this up!
+       }
+
+       public long getSignedValueLong()
+       {
+               return getSignedValue().longValue();
+       }
+
        /**
         * Parses a String containing solely {@link Bit} symbols (MSB first)
         * 
index f130cd4..13f54a0 100644 (file)
@@ -129,67 +129,6 @@ public class CoreWire
                notifyObservers();
        }
 
-       /**
-        * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-        * 
-        * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same
-        *         value), not <code>Bit.U</code>, <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
-        * 
-        * @author Fabian Stemmler
-        */
-       public boolean hasNumericValue()
-       {
-               return getValues().isBinary();
-       }
-
-       /**
-        * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-        * 
-        * @return The unsigned value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-        * 
-        * @author Fabian Stemmler
-        */
-       public long getUnsignedValue()
-       {
-               long val = 0;
-               long mask = 1;
-               for (Bit bit : getValues())
-               {
-                       switch (bit)
-                       {
-                       default:
-                       case Z:
-                       case X:
-                               return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
-                       case ONE:
-                               val |= mask;
-                               break;
-                       case ZERO:
-                       }
-                       mask = mask << 1;
-               }
-               return val;
-       }
-
-       /**
-        * The {@link CoreWire} is interpreted as a signed integer with n bits.
-        * 
-        * @return The signed value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-        * 
-        * @author Fabian Stemmler
-        */
-       public long getSignedValue()
-       {
-               long val = getUnsignedValue();
-               long mask = 1 << (width - 1);
-               if ((mask & val) != 0)
-               {
-                       int shifts = 64 - width;
-                       return (val << shifts) >> shifts;
-               }
-               return val;
-       }
-
        /**
         * Returns the least significant bit (LSB)
         */
@@ -326,43 +265,6 @@ public class CoreWire
                        return CoreWire.this.getValues(start, end);
                }
 
-               /**
-                * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-                * 
-                * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the
-                *         same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
-                * 
-                * @author Fabian Stemmler
-                */
-               public boolean hasNumericValue()
-               {
-                       return CoreWire.this.hasNumericValue();
-               }
-
-               /**
-                * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-                * 
-                * @return The unsigned value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-                * 
-                * @author Fabian Stemmler
-                */
-               public long getUnsignedValue()
-               {
-                       return CoreWire.this.getUnsignedValue();
-               }
-
-               /**
-                * The {@link CoreWire} is interpreted as a signed integer with n bits.
-                * 
-                * @return The signed value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-                * 
-                * @author Fabian Stemmler
-                */
-               public long getSignedValue()
-               {
-                       return CoreWire.this.getSignedValue();
-               }
-
                @Override
                public String toString()
                {
index ebebd6b..12df584 100644 (file)
@@ -218,7 +218,7 @@ class CoreComponentTest
                t.executeAll();
 
                assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
-               selectIn.feedSignals(Bit.ZERO, Bit.ONE);
+               selectIn.feedSignals(Bit.ONE, Bit.ZERO);
                t.executeAll();
 
                assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
@@ -247,7 +247,7 @@ class CoreComponentTest
                assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
                assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
                assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
-               selectIn.feedSignals(Bit.ZERO, Bit.ONE);
+               selectIn.feedSignals(Bit.ONE, Bit.ZERO);
                t.executeAll();
 
                assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
@@ -382,12 +382,12 @@ class CoreComponentTest
        void numericValueTest()
        {
                CoreWire a = new CoreWire(t, 4, 1);
-               a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
+               a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ONE);
 
                t.executeAll();
 
-               assertEquals(15, a.getUnsignedValue());
-               assertEquals(-1, a.getSignedValue());
+               assertEquals(11, a.getValues().getUnsignedValueLong());
+               assertEquals(-5, a.getValues().getSignedValueLong());
        }
 
        boolean flag = false;
index 12b44dd..7394028 100644 (file)
@@ -47,11 +47,11 @@ public class CoreMicroInstructionMemory extends BasicCoreComponent
        @Override
        protected TimelineEventHandler compute()
        {
-               if (!address.hasNumericValue())
+               if (!address.getValues().isBinary())
                {
                        return e -> data.feedSignals(Bit.U.toVector(data.width()));// TODO don't always feed U, but decide to feed X or U.
                }
-               long addressed = address.getUnsignedValue();
+               long addressed = address.getValues().getUnsignedValueLong();
                BitVector storedData = memory.getCell(addressed).toBitVector();
                return e -> data.feedSignals(storedData);
        }
index dcbd25e..ca557a6 100644 (file)
@@ -58,13 +58,13 @@ public class CoreWordAddressableMemory extends BasicCoreComponent
        @Override
        protected TimelineEventHandler compute()
        {
-               if (!address.hasNumericValue())
+               if (!address.getValues().isBinary())
                {
                        if (read.equals(rWBit.getValue()))
                                return e -> data.feedSignals(Bit.U.toVector(data.width()));// TODO don't always feed U, but decide to feed X or U.
                        return e -> data.clearSignals();
                }
-               long addressed = address.getUnsignedValue();
+               long addressed = address.getValues().getUnsignedValueLong();
                if (read.equals(rWBit.getValue()))
                {
                        BitVector storedData = memory.getCell(addressed);
index 5adfc25..9d38b89 100644 (file)
@@ -40,7 +40,6 @@ class WordAddressableMemoryTest
                        for (int i = 0; i < 50; i++)
                        {
                                String sAddress = String.format("%64s", BigInteger.valueOf(4096 + i + j).toString(2)).replace(' ', '0');
-                               sAddress = new StringBuilder(sAddress).reverse().toString();
                                BitVector bAddress = BitVector.parse(sAddress);
                                addressI.feedSignals(bAddress);
                                t.executeAll();