return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
}
+ @Override
+ public String toString()
+ {
+ return toBitstring();
+ }
+
/**
* All {@link Bit}s symbols concatenated together (MSB first)
*
- * @see #parse(String)
+ * @see #parseBitstring(String)
*/
- @Override
- public String toString()
+ public String toBitstring()
{
StringBuilder sb = new StringBuilder(bits.length);
for (Bit bit : bits)
/**
* Parses a String containing solely {@link Bit} symbols (MSB first)
*
- * @see #toString()
+ * @see #toBitString()
*/
- public static BitVector parse(String s)
+ public static BitVector parseBitstring(String s)
{
Bit[] values = new Bit[s.length()];
for (int i = 0; i < s.length(); i++)
package net.mograsim.logic.core.types;
+import java.math.BigInteger;
+
import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
import net.mograsim.preferences.ColorDefinition;
import net.mograsim.preferences.ColorDefinition.BuiltInColor;
return formatAsString(end == null ? null : end.getValues());
}
+ public static String toBitstring(BitVector bitVector)
+ {
+ return bitVector.toBitstring();
+ }
+
public static String formatAsString(BitVector bitVector)
{
if (bitVector == null)
return "null";
- return bitVector.toString();
+ if (bitVector.isBinary())
+ {
+ String hexdigits = bitVector.getUnsignedValue().toString(16);
+ StringBuilder sb = new StringBuilder();
+ sb.append("0x");
+ sb.append("0".repeat((bitVector.length() + 3) / 4 - hexdigits.length()));
+ sb.append(hexdigits);
+ return sb.toString();
+ }
+ if (bitVector.isHighImpedance())
+ return "-";
+ return bitVector.toBitstring();
+ }
+
+ // TODO this method overlaps in functionality with AsmNumberUtil (in plugin.core)
+ public static BitVector parseUserBitVector(String userInput, int width)
+ {
+ BitVector bitvector = null;
+ if (width > 0 && userInput.matches("0x[0-9a-fA-F]+"))
+ // TODO should we check for overflows?
+ bitvector = BitVector.from(new BigInteger(userInput.substring(2), 16), width);
+ else if (width <= 0 || userInput.length() == width)
+ // TODO do this without exceptions
+ try
+ {
+ bitvector = BitVector.parseBitstring(userInput);
+ }
+ catch (@SuppressWarnings("unused") NullPointerException x)
+ {
+ // ignore
+ }
+ if (bitvector == null && width > 0)
+ try
+ {
+ // TODO should we check for overflows?
+ bitvector = BitVector.from(new BigInteger(userInput), width);
+ }
+ catch (@SuppressWarnings("unused") NumberFormatException x)
+ {
+ // ignore
+ }
+ return bitvector;
}
// TODO doesn't this belong to logic.model?
import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas;
import net.mograsim.logic.core.types.Bit;
import net.mograsim.logic.core.types.BitVector;
+import net.mograsim.logic.core.types.BitVectorFormatter;
import net.mograsim.logic.model.model.LogicModel;
import net.mograsim.logic.model.model.components.ModelComponent;
import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
throw new RuntimeException("No component selected");
ModelComponent target = componentsByItemIndex.get(componentIndex);
String valueString = valueText.getText();
+ String stateID = stateIDText.getText();
Object value;
if (radioBit.getSelection())
value = Bit.parse(valueString);
else if (radioBitVector.getSelection())
- value = BitVector.parse(valueString);
- else
+ {
+ Object hls = target.getHighLevelState(stateID);
+ int width;
+ if (hls instanceof Bit)
+ width = 1;
+ else if (hls instanceof BitVector)
+ width = ((BitVector) hls).length();
+ else
+ width = -1;
+ value = BitVectorFormatter.parseUserBitVector(valueString, width);
+ } else
throw new RuntimeException("No value type selected");
- target.setHighLevelState(stateIDText.getText(), value);
+ target.setHighLevelState(stateID, value);
output.setText("Success!");
}
catch (Exception x)
import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
import net.mograsim.logic.core.LogicObserver;
import net.mograsim.logic.core.components.CoreBitDisplay;
-import net.mograsim.logic.core.types.BitVector;
import net.mograsim.logic.core.types.BitVectorFormatter;
import net.mograsim.logic.model.model.LogicModelModifiable;
import net.mograsim.logic.model.model.components.ModelComponent;
if (foreground != null)
gc.setForeground(foreground);
gc.drawRectangle(getBounds());
- String label;
- if (bitDisplay == null)
- label = BitVectorFormatter.formatAsString(null);
- else
- {
- BitVector toDisplay = bitDisplay.getDisplayedValue();
- label = toDisplay != null && toDisplay.isHighImpedance() ? "-"
- : BitVectorFormatter.formatAsString(bitDisplay.getDisplayedValue());
- }
+ String label = BitVectorFormatter.formatAsString(bitDisplay == null ? null : bitDisplay.getDisplayedValue());
Font oldFont = gc.getFont();
Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
gc.setFont(labelFont);
{
for (; i <= maxAddr && reader.ready() && !"".equals((line = reader.readLine())); i++)
{
- memory.setCell(i, BitVector.parse(line));
+ memory.setCell(i, BitVector.parseBitstring(line));
}
}
catch (IOException e)
int val = instStream.read();
if (val == -1 && instIndex <= maxAddress)
{
- instStream = new ByteArrayInputStream((memory.getCell(instIndex++).toString() + lineSeparator).getBytes());
+ instStream = new ByteArrayInputStream((memory.getCell(instIndex++).toBitstring() + lineSeparator).getBytes());
val = instStream.read();
}
return val;
package net.mograsim.plugin.launch;
-import java.math.BigInteger;
import java.util.Arrays;
import java.util.function.Consumer;
import org.eclipse.swt.SWT;
import net.mograsim.logic.core.types.BitVector;
+import net.mograsim.logic.core.types.BitVectorFormatter;
import net.mograsim.machine.Machine;
import net.mograsim.machine.registers.Register;
import net.mograsim.plugin.MograsimActivator;
public String getValueString()
{
- BitVector bitvector = getMachine().getRegister(machineRegister);
- if (bitvector.isBinary())
- {
- String hexdigits = bitvector.getUnsignedValue().toString(16);
- StringBuilder sb = new StringBuilder();
- sb.append("0x");
- sb.append("0".repeat((machineRegister.getWidth() + 3) / 4 - hexdigits.length()));
- sb.append(hexdigits);
- return sb.toString();
- }
- return bitvector.toString();
+ return BitVectorFormatter.formatAsString(getMachine().getRegister(machineRegister));
}
@Override
public void setValue(String expression) throws DebugException
{
- BitVector bitvector = parseValue(expression);
+ BitVector bitvector = BitVectorFormatter.parseUserBitVector(expression, machineRegister.getWidth());
if (bitvector == null)
throw new DebugException(
new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Couldn't parse value string: " + expression, null));
getMachine().setRegister(machineRegister, bitvector);
}
- private BitVector parseValue(String expression)
- {
- BitVector bitvector = null;
- if (expression.matches("0x[0-9a-fA-F]+"))
- // TODO should we check for overflows?
- bitvector = BitVector.from(new BigInteger(expression.substring(2), 16), machineRegister.getWidth());
- else if (expression.length() == machineRegister.getWidth())
- // TODO do this without exceptions
- try
- {
- bitvector = BitVector.parse(expression);
- }
- catch (@SuppressWarnings("unused") NullPointerException x)
- {
- // ignore
- }
- if (bitvector == null)
- try
- {
- // TODO should we check for overflows?
- bitvector = BitVector.from(new BigInteger(expression), machineRegister.getWidth());
- }
- catch (@SuppressWarnings("unused") NumberFormatException x)
- {
- // ignore
- }
- return bitvector;
- }
-
@Override
public void setValue(IValue value) throws DebugException
{
@Override
public boolean verifyValue(String expression) throws DebugException
{
- return parseValue(expression) != null;
+ return BitVectorFormatter.parseUserBitVector(expression, machineRegister.getWidth()) != null;
}
@Override
@Test
void testGetUnsignedValue()
{
- assertEquals(BigInteger.valueOf(0b101), BitVector.parse("101").getUnsignedValue());
- assertEquals(BigInteger.valueOf(0b01010), BitVector.parse("01010").getUnsignedValue());
- assertEquals(BigInteger.valueOf(0), BitVector.parse("0000").getUnsignedValue());
- assertEquals(BigInteger.valueOf(0b0000000101), BitVector.parse("0000000101").getUnsignedValue());
- assertEquals(BigInteger.valueOf(0b1010000000), BitVector.parse("1010000000").getUnsignedValue());
+ assertEquals(BigInteger.valueOf(0b101), BitVector.parseBitstring("101").getUnsignedValue());
+ assertEquals(BigInteger.valueOf(0b01010), BitVector.parseBitstring("01010").getUnsignedValue());
+ assertEquals(BigInteger.valueOf(0), BitVector.parseBitstring("0000").getUnsignedValue());
+ assertEquals(BigInteger.valueOf(0b0000000101), BitVector.parseBitstring("0000000101").getUnsignedValue());
+ assertEquals(BigInteger.valueOf(0b1010000000), BitVector.parseBitstring("1010000000").getUnsignedValue());
- assertThrows(NumberFormatException.class, () -> BitVector.parse("00X1").getUnsignedValue());
+ assertThrows(NumberFormatException.class, () -> BitVector.parseBitstring("00X1").getUnsignedValue());
}
@Test
void testOfLongInt()
{
- assertEquals(BitVector.parse("101"), BitVector.from(0b101L, 3));
- assertEquals(BitVector.parse("01010"), BitVector.from(0b01010L, 5));
- assertEquals(BitVector.parse("10101"), BitVector.from(-11L, 5));
- assertEquals(BitVector.parse("0000"), BitVector.from(0L, 4));
+ assertEquals(BitVector.parseBitstring("101"), BitVector.from(0b101L, 3));
+ assertEquals(BitVector.parseBitstring("01010"), BitVector.from(0b01010L, 5));
+ assertEquals(BitVector.parseBitstring("10101"), BitVector.from(-11L, 5));
+ assertEquals(BitVector.parseBitstring("0000"), BitVector.from(0L, 4));
}
@Test
void testOfBigIntegerInt()
{
- assertEquals(BitVector.parse("101"), BitVector.from(BigInteger.valueOf(0b101), 3));
- assertEquals(BitVector.parse("01010"), BitVector.from(BigInteger.valueOf(0b01010), 5));
- assertEquals(BitVector.parse("10101"), BitVector.from(BigInteger.valueOf(-11), 5));
- assertEquals(BitVector.parse("0000"), BitVector.from(BigInteger.valueOf(0), 4));
+ assertEquals(BitVector.parseBitstring("101"), BitVector.from(BigInteger.valueOf(0b101), 3));
+ assertEquals(BitVector.parseBitstring("01010"), BitVector.from(BigInteger.valueOf(0b01010), 5));
+ assertEquals(BitVector.parseBitstring("10101"), BitVector.from(BigInteger.valueOf(-11), 5));
+ assertEquals(BitVector.parseBitstring("0000"), BitVector.from(BigInteger.valueOf(0), 4));
}
@Test
void testGetBits()
{
assertArrayEquals(new Bit[] { X, ONE, Z }, BitVector.of(X, ONE, Z).getBits());
- assertArrayEquals(new Bit[] { X, ONE, Z }, BitVector.parse("X1Z").getBits());
+ assertArrayEquals(new Bit[] { X, ONE, Z }, BitVector.parseBitstring("X1Z").getBits());
}
@Test
@Test
void testParse()
{
- assertEquals(SINGLE_U, BitVector.parse("U"));
- assertEquals(SINGLE_X, BitVector.parse("X"));
- assertEquals(SINGLE_0, BitVector.parse("0"));
- assertEquals(SINGLE_1, BitVector.parse("1"));
- assertEquals(SINGLE_Z, BitVector.parse("Z"));
+ assertEquals(SINGLE_U, BitVector.parseBitstring("U"));
+ assertEquals(SINGLE_X, BitVector.parseBitstring("X"));
+ assertEquals(SINGLE_0, BitVector.parseBitstring("0"));
+ assertEquals(SINGLE_1, BitVector.parseBitstring("1"));
+ assertEquals(SINGLE_Z, BitVector.parseBitstring("Z"));
- assertEquals(BitVector.of(X, U, Z, ONE, ZERO), BitVector.parse("XUZ10"));
+ assertEquals(BitVector.of(X, U, Z, ONE, ZERO), BitVector.parseBitstring("XUZ10"));
- assertThrows(RuntimeException.class, () -> BitVector.parse("01BX"));
+ assertThrows(RuntimeException.class, () -> BitVector.parseBitstring("01BX"));
}
@Test
@Override
public void setReg_A(String val_4_bit)
{
- A.setState(BitVector.parse(val_4_bit));
+ A.setState(BitVector.parseBitstring(val_4_bit));
}
@Override
public void setReg_B(String val_4_bit)
{
- B.setState(BitVector.parse(val_4_bit));
+ B.setState(BitVector.parseBitstring(val_4_bit));
}
@Override
@Override
public void setD(String val_4_bit)
{
- D.setState(BitVector.parse(val_4_bit));
+ D.setState(BitVector.parseBitstring(val_4_bit));
}
@Override
@Override
public String getQ_0()
{
- return Qn.getDisplayedValue().toString();
+ return Qn.getDisplayedValue().toBitstring();
}
@Override
public String getQ_3()
{
- return Qn_3.getDisplayedValue().toString();
+ return Qn_3.getDisplayedValue().toBitstring();
}
@Override
public String getRAM_0()
{
- return RAMn.getDisplayedValue().toString();
+ return RAMn.getDisplayedValue().toBitstring();
}
@Override
public String getRAM_3()
{
- return RAMn_3.getDisplayedValue().toString();
+ return RAMn_3.getDisplayedValue().toBitstring();
}
@Override
@Override
public String getCarryOut()
{
- return Cn_4.getDisplayedValue().toString();
+ return Cn_4.getDisplayedValue().toBitstring();
}
@Override
public String getSign()
{
- return F3.getDisplayedValue().toString();
+ return F3.getDisplayedValue().toBitstring();
}
@Override
public String getZero()
{
- return F_0.getDisplayedValue().toString();
+ return F_0.getDisplayedValue().toBitstring();
}
@Override
public String getOverflow()
{
- return OVR.getDisplayedValue().toString();
+ return OVR.getDisplayedValue().toBitstring();
}
@Override
public String getY()
{
- return Y.getDisplayedValue().toString();
+ return Y.getDisplayedValue().toBitstring();
}
@Override
public void setDirectly(Register r, String val_4_bit)
{
- am2901.setHighLevelState(regToStateID(r), BitVector.parse(val_4_bit));
+ am2901.setHighLevelState(regToStateID(r), BitVector.parseBitstring(val_4_bit));
}
@Override
public String getDirectly(Register r)
{
- return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toString();
+ return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toBitstring();
}
private static String regToStateID(Register r)
@Override
public void setShiftCode(String val_4_bit)
{
- I_6_9.setState(BitVector.parse(val_4_bit));
+ I_6_9.setState(BitVector.parseBitstring(val_4_bit));
}
@Override
@Override
public void setCX(String val_1_bit)
{
- Cx.setState(BitVector.parse(val_1_bit));
+ Cx.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setY(String z_c_n_ovr)
{
- var bv = BitVector.parse(z_c_n_ovr);
+ var bv = BitVector.parseBitstring(z_c_n_ovr);
// correct order apparently unknown, most likely Z-C-N-OVR
YZ.setState(bv.getLSBit(3).toVector());
YC.setState(bv.getLSBit(2).toVector());
@Override
public void setIZ(String val_1_bit)
{
- IZ.setState(BitVector.parse(val_1_bit));
+ IZ.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setIC(String val_1_bit)
{
- IC.setState(BitVector.parse(val_1_bit));
+ IC.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setIOVR(String val_1_bit)
{
- IOVR.setState(BitVector.parse(val_1_bit));
+ IOVR.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setIN(String val_1_bit)
{
- IN.setState(BitVector.parse(val_1_bit));
+ IN.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_CEM(String val_1_bit)
{
- _CEM.setState(BitVector.parse(val_1_bit));
+ _CEM.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_CEĀµ(String val_1_bit)
{
- _CEmu.setState(BitVector.parse(val_1_bit));
+ _CEmu.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_OEY(String val_1_bit)
{
- _OEY.setState(BitVector.parse(val_1_bit));
+ _OEY.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_OECT(String val_1_bit)
{
- _OECT.setState(BitVector.parse(val_1_bit));
+ _OECT.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_SE(String val_1_bit)
{
- _SE.setState(BitVector.parse(val_1_bit));
+ _SE.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_EZ(String val_1_bit)
{
- _EZ.setState(BitVector.parse(val_1_bit));
+ _EZ.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_EC(String val_1_bit)
{
- _EC.setState(BitVector.parse(val_1_bit));
+ _EC.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_EOVR(String val_1_bit)
{
- _EOVR.setState(BitVector.parse(val_1_bit));
+ _EOVR.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_EN(String val_1_bit)
{
- _EN.setState(BitVector.parse(val_1_bit));
+ _EN.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setSIO0(String val_1_bit)
{
- SIO0.setState(BitVector.parse(val_1_bit));
+ SIO0.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setSIO3(String val_1_bit)
{
- SIOn.setState(BitVector.parse(val_1_bit));
+ SIOn.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setQIO0(String val_1_bit)
{
- QIO0.setState(BitVector.parse(val_1_bit));
+ QIO0.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setQIO3(String val_1_bit)
{
- QIOn.setState(BitVector.parse(val_1_bit));
+ QIOn.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
@Override
public void setDirectly(CompleteStatus r, String z_c_n_ovr)
{
- am2904.setHighLevelState(regToStateID(r), BitVector.parse(z_c_n_ovr));
+ am2904.setHighLevelState(regToStateID(r), BitVector.parseBitstring(z_c_n_ovr));
}
@Override
public String getC0()
{
- return C0.getDisplayedValue().toString();
+ return C0.getDisplayedValue().toBitstring();
}
@Override
public String getCT()
{
- return CT.getDisplayedValue().toString();
+ return CT.getDisplayedValue().toBitstring();
}
@Override
var y2 = YC.getDisplayedValue();
var y1 = YN.getDisplayedValue();
var y0 = YOVR.getDisplayedValue();
- return y3.concat(y2).concat(y1).concat(y0).toString();
+ return y3.concat(y2).concat(y1).concat(y0).toBitstring();
}
@Override
public String getSIO0()
{
- return SIO0.getDisplayedValue().toString();
+ return SIO0.getDisplayedValue().toBitstring();
}
@Override
public String getSIO3()
{
- return SIOn.getDisplayedValue().toString();
+ return SIOn.getDisplayedValue().toBitstring();
}
@Override
public String getQIO0()
{
- return QIO0.getDisplayedValue().toString();
+ return QIO0.getDisplayedValue().toBitstring();
}
@Override
public String getQIO3()
{
- return QIOn.getDisplayedValue().toString();
+ return QIOn.getDisplayedValue().toBitstring();
}
@Override
public String getDirectly(CompleteStatus r)
{
var bv = (BitVector) am2904.getHighLevelState(regToStateID(r));
- return bv.toString();
+ return bv.toBitstring();
}
private static String regToStateID(Register r)
@EnumSource(Register.class)
void testDirectAccess(Register r)
{
- String us = U.toVector(r.size()).toString();
- String three = BitVector.from(3, r.size()).toString();
+ String us = U.toVector(r.size()).toBitstring();
+ String three = BitVector.from(3, r.size()).toBitstring();
assertEquals(us, am2910.getDirectly(r));
@Override
public void set_CCEN(String val_1_bit)
{
- _CCEN.setState(BitVector.parse(val_1_bit));
+ _CCEN.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setD(String val_12_bit)
{
- D.setState(BitVector.parse(val_12_bit));
+ D.setState(BitVector.parseBitstring(val_12_bit));
}
@Override
public void set_CC(String val_1_bit)
{
- _CC.setState(BitVector.parse(val_1_bit));
+ _CC.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setCI(String val_1_bit)
{
- CI.setState(BitVector.parse(val_1_bit));
+ CI.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_RLD(String val_1_bit)
{
- _RLD.setState(BitVector.parse(val_1_bit));
+ _RLD.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void set_OE(String val_1_bit)
{
- _OE.setState(BitVector.parse(val_1_bit));
+ _OE.setState(BitVector.parseBitstring(val_1_bit));
}
@Override
public void setDirectly(Register r, String val_X_bit)
{
- am2901.setHighLevelState(regToStateID(r), BitVector.parse(val_X_bit));
+ am2901.setHighLevelState(regToStateID(r), BitVector.parseBitstring(val_X_bit));
}
@Override
public String getY()
{
- return Y.getDisplayedValue().toString();
+ return Y.getDisplayedValue().toBitstring();
}
@Override
public String get_FULL()
{
- return _FULL.getDisplayedValue().toString();
+ return _FULL.getDisplayedValue().toBitstring();
}
@Override
public String get_PL()
{
- return _PL.getDisplayedValue().toString();
+ return _PL.getDisplayedValue().toBitstring();
}
@Override
public String get_MAP()
{
- return _MAP.getDisplayedValue().toString();
+ return _MAP.getDisplayedValue().toBitstring();
}
@Override
public String get_VECT()
{
- return _VECT.getDisplayedValue().toString();
+ return _VECT.getDisplayedValue().toBitstring();
}
@Override
public String getDirectly(Register r)
{
- return am2901.getHighLevelState(regToStateID(r)).toString();
+ return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toBitstring();
}
private static String regToStateID(Register r)