From 369a988586e29afc28857afdb6bd0aab69e26b36 Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Sat, 12 Oct 2019 13:48:09 +0200 Subject: [PATCH] Improved RegisterView: Values are now viewed in hex (if they are binary) and can be written in hex, dec or as BitVector --- .../plugin/launch/MachineRegister.java | 63 ++++++++++++++----- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java index d2a23128..59e22f83 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegister.java @@ -1,8 +1,10 @@ package net.mograsim.plugin.launch; +import java.math.BigInteger; import java.util.Arrays; import java.util.function.Consumer; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.PlatformObject; import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.DebugEvent; @@ -95,16 +97,56 @@ public class MachineRegister extends PlatformObject implements IRegister public String getValueString() { - // TODO view in hex - return getMachine().getRegister(machineRegister).toString(); + 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(); } @Override public void setValue(String expression) throws DebugException { - // TODO support hex - // TODO exception handling - getMachine().setRegister(machineRegister, BitVector.parse(expression)); + BitVector bitvector = parseValue(expression); + 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 @@ -124,16 +166,7 @@ public class MachineRegister extends PlatformObject implements IRegister @Override public boolean verifyValue(String expression) throws DebugException { - // TODO do this prettier; also check length too - try - { - BitVector.parse(expression); - } - catch (@SuppressWarnings("unused") Exception e) - { - return false; - } - return true; + return parseValue(expression) != null; } @Override -- 2.17.1