X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Ftypes%2FBitVectorFormatter.java;h=8ffe07aa264fffc7121d9b19c24c76264c3b011b;hb=6579fc1c18c0f008f259c3c48c47e604adac2a4c;hp=5f645a2ebcf8a8acb53f4669c1ea9fa93079b0c2;hpb=7d05144c25daa53e60fc9ed9fd503546a86567f8;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java b/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java index 5f645a2e..8ffe07aa 100644 --- a/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java +++ b/plugins/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java @@ -1,5 +1,7 @@ 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; @@ -7,16 +9,62 @@ import net.mograsim.preferences.Preferences; public class BitVectorFormatter { - public static String formatValueAsString(ReadEnd end) + public static String formatValueAsString(ReadEnd end, boolean useDashInsteadOfZ) { - return formatAsString(end == null ? null : end.getValues()); + return formatAsString(end == null ? null : end.getValues(), useDashInsteadOfZ); } - public static String formatAsString(BitVector bitVector) + public static String toBitstring(BitVector bitVector) + { + return bitVector.toBitstring(); + } + + public static String formatAsString(BitVector bitVector, boolean useDashInsteadOfZ) { 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 (useDashInsteadOfZ && 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?