1 package net.mograsim.logic.model;
3 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_ONE_COLOR;
4 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_U_COLOR;
5 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_X_COLOR;
6 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_ZERO_COLOR;
7 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_Z_COLOR;
9 import java.math.BigInteger;
11 import net.mograsim.logic.core.types.BitVector;
12 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
13 import net.mograsim.logic.model.preferences.RenderPreferences;
14 import net.mograsim.preferences.ColorDefinition;
15 import net.mograsim.preferences.ColorDefinition.BuiltInColor;
17 public class BitVectorFormatter
19 public static String formatValueAsString(ReadEnd end, boolean useDashInsteadOfZ)
21 return formatAsString(end == null ? null : end.getValues(), useDashInsteadOfZ);
24 public static String toBitstring(BitVector bitVector)
26 return bitVector.toBitstring();
29 public static String formatAsString(BitVector bitVector, boolean useDashInsteadOfZ)
31 if (bitVector == null)
33 if (useDashInsteadOfZ && bitVector.isHighImpedance())
35 if (bitVector.length() == 1)
36 return bitVector.toBitstring();
37 if (bitVector.isBinary())
39 String hexdigits = bitVector.getUnsignedValue().toString(16);
40 StringBuilder sb = new StringBuilder();
42 sb.append("0".repeat((bitVector.length() + 3) / 4 - hexdigits.length()));
46 return bitVector.toBitstring();
49 // TODO this method overlaps in functionality with AsmNumberUtil (in plugin.core)
50 public static BitVector parseUserBitVector(String userInput, int width)
52 BitVector bitvector = null;
53 if (width > 0 && userInput.matches("0x[0-9a-fA-F]+"))
54 // TODO should we check for overflows?
55 bitvector = BitVector.from(new BigInteger(userInput.substring(2), 16), width);
56 else if (width <= 0 || userInput.length() == width)
57 // TODO do this without exceptions
60 bitvector = BitVector.parseBitstring(userInput);
62 catch (@SuppressWarnings("unused") NullPointerException x)
66 if (bitvector == null && width > 0)
69 // TODO should we check for overflows?
70 bitvector = BitVector.from(new BigInteger(userInput), width);
72 catch (@SuppressWarnings("unused") NumberFormatException x)
79 // TODO doesn't this belong to logic.model?
80 public static ColorDefinition formatAsColor(RenderPreferences renderPrefs, ReadEnd end)
82 return formatAsColor(renderPrefs, end == null ? null : end.getValues());
85 public static ColorDefinition formatAsColor(RenderPreferences renderPrefs, BitVector bitVector)
87 // TODO maybe find a color assignment for multiple-bit bit vectors?
88 if (bitVector == null || bitVector.length() != 1)
89 return new ColorDefinition(BuiltInColor.COLOR_BLACK);
90 switch (bitVector.getLSBit(0))
93 return renderPrefs.getColorDefinition(BIT_ONE_COLOR);
95 return renderPrefs.getColorDefinition(BIT_U_COLOR);
97 return renderPrefs.getColorDefinition(BIT_X_COLOR);
99 return renderPrefs.getColorDefinition(BIT_Z_COLOR);
101 return renderPrefs.getColorDefinition(BIT_ZERO_COLOR);
103 throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getLSBit(0));
107 private BitVectorFormatter()
109 throw new UnsupportedOperationException("No BitVectorFormatter instances");