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_ONE_DASH;
5 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_U_COLOR;
6 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_U_DASH;
7 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_X_COLOR;
8 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_X_DASH;
9 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_ZERO_COLOR;
10 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_ZERO_DASH;
11 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_Z_COLOR;
12 import static net.mograsim.logic.model.preferences.RenderPreferences.BIT_Z_DASH;
14 import java.math.BigInteger;
16 import net.mograsim.logic.core.types.BitVector;
17 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
18 import net.mograsim.logic.model.preferences.RenderPreferences;
19 import net.mograsim.preferences.ColorDefinition;
20 import net.mograsim.preferences.ColorDefinition.BuiltInColor;
22 public class BitVectorFormatter
24 public static String formatValueAsString(ReadEnd end, boolean useDashInsteadOfZ)
26 return formatAsString(end == null ? null : end.getValues(), useDashInsteadOfZ);
29 public static String toBitstring(BitVector bitVector)
31 return bitVector.toBitstring();
34 public static String formatAsString(BitVector bitVector, boolean useDashInsteadOfZ)
36 if (bitVector == null)
38 if (useDashInsteadOfZ && bitVector.isHighImpedance())
40 if (bitVector.length() == 1)
41 return bitVector.toBitstring();
42 if (bitVector.isBinary())
44 String hexdigits = bitVector.getUnsignedValue().toString(16);
45 StringBuilder sb = new StringBuilder();
47 sb.append("0".repeat((bitVector.length() + 3) / 4 - hexdigits.length()));
51 return bitVector.toBitstring();
54 // TODO this method overlaps in functionality with AsmNumberUtil (in plugin.core)
55 public static BitVector parseUserBitVector(String userInput, int width)
57 BitVector bitvector = null;
58 if (width > 0 && userInput.matches("0x[0-9a-fA-F]+"))
59 // TODO should we check for overflows?
60 bitvector = BitVector.from(new BigInteger(userInput.substring(2), 16), width);
61 else if (width <= 0 || userInput.length() == width)
62 // TODO do this without exceptions
65 bitvector = BitVector.parseBitstring(userInput);
67 catch (@SuppressWarnings("unused") NullPointerException x)
71 if (bitvector == null && width > 0)
74 // TODO should we check for overflows?
75 bitvector = BitVector.from(new BigInteger(userInput), width);
77 catch (@SuppressWarnings("unused") NumberFormatException x)
84 public static double[] formatAsLineDash(RenderPreferences renderPrefs, ReadEnd end)
86 return formatAsLineDash(renderPrefs, end == null ? null : end.getValues());
89 public static double[] formatAsLineDash(RenderPreferences renderPrefs, BitVector bitVector)
91 // TODO maybe find a line dash assignment for multiple-bit bit vectors?
92 if (bitVector == null || bitVector.length() != 1)
95 switch (bitVector.getLSBit(0))
98 prefName = BIT_ONE_DASH;
101 prefName = BIT_U_DASH;
104 prefName = BIT_X_DASH;
107 prefName = BIT_Z_DASH;
110 prefName = BIT_ZERO_DASH;
113 throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getLSBit(0));
115 return parseLineDashes(renderPrefs.getString(prefName));
118 // TODO this method does not belong here
119 public static double[] parseLineDashes(String dashesString)
121 if (dashesString.isEmpty())
123 String[] dashesStrings = dashesString.split(",");
124 double[] dashes = new double[dashesStrings.length];
125 for (int i = 0; i < dashesStrings.length; i++)
126 dashes[i] = Double.parseDouble(dashesStrings[i]);
130 public static ColorDefinition formatAsColor(RenderPreferences renderPrefs, ReadEnd end)
132 return formatAsColor(renderPrefs, end == null ? null : end.getValues());
135 public static ColorDefinition formatAsColor(RenderPreferences renderPrefs, BitVector bitVector)
137 // TODO maybe find a color assignment for multiple-bit bit vectors?
138 if (bitVector == null || bitVector.length() != 1)
139 return new ColorDefinition(BuiltInColor.COLOR_BLACK);
140 switch (bitVector.getLSBit(0))
143 return renderPrefs.getColorDefinition(BIT_ONE_COLOR);
145 return renderPrefs.getColorDefinition(BIT_U_COLOR);
147 return renderPrefs.getColorDefinition(BIT_X_COLOR);
149 return renderPrefs.getColorDefinition(BIT_Z_COLOR);
151 return renderPrefs.getColorDefinition(BIT_ZERO_COLOR);
153 throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getLSBit(0));
157 private BitVectorFormatter()
159 throw new UnsupportedOperationException("No BitVectorFormatter instances");