@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;
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();
}
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)
*
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)
*/
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()
{
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);
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);
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;
@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);
}
@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);
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();