be77c93bc839661fc797aa5007cfceaf4a69e756
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / BitVectorFormatter.java
1 package net.mograsim.logic.model;
2
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;
8
9 import java.math.BigInteger;
10
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;
16
17 public class BitVectorFormatter
18 {
19         public static String formatValueAsString(ReadEnd end, boolean useDashInsteadOfZ)
20         {
21                 return formatAsString(end == null ? null : end.getValues(), useDashInsteadOfZ);
22         }
23
24         public static String toBitstring(BitVector bitVector)
25         {
26                 return bitVector.toBitstring();
27         }
28
29         public static String formatAsString(BitVector bitVector, boolean useDashInsteadOfZ)
30         {
31                 if (bitVector == null)
32                         return "null";
33                 if (useDashInsteadOfZ && bitVector.isHighImpedance())
34                         return "-";
35                 if (bitVector.length() == 1)
36                         return bitVector.toBitstring();
37                 if (bitVector.isBinary())
38                 {
39                         String hexdigits = bitVector.getUnsignedValue().toString(16);
40                         StringBuilder sb = new StringBuilder();
41                         sb.append("0x");
42                         sb.append("0".repeat((bitVector.length() + 3) / 4 - hexdigits.length()));
43                         sb.append(hexdigits);
44                         return sb.toString();
45                 }
46                 return bitVector.toBitstring();
47         }
48
49         // TODO this method overlaps in functionality with AsmNumberUtil (in plugin.core)
50         public static BitVector parseUserBitVector(String userInput, int width)
51         {
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
58                         try
59                         {
60                                 bitvector = BitVector.parseBitstring(userInput);
61                         }
62                         catch (@SuppressWarnings("unused") NullPointerException x)
63                         {
64                                 // ignore
65                         }
66                 if (bitvector == null && width > 0)
67                         try
68                         {
69                                 // TODO should we check for overflows?
70                                 bitvector = BitVector.from(new BigInteger(userInput), width);
71                         }
72                         catch (@SuppressWarnings("unused") NumberFormatException x)
73                         {
74                                 // ignore
75                         }
76                 return bitvector;
77         }
78
79         public static ColorDefinition formatAsColor(RenderPreferences renderPrefs, ReadEnd end)
80         {
81                 return formatAsColor(renderPrefs, end == null ? null : end.getValues());
82         }
83
84         public static ColorDefinition formatAsColor(RenderPreferences renderPrefs, BitVector bitVector)
85         {
86                 // TODO maybe find a color assignment for multiple-bit bit vectors?
87                 if (bitVector == null || bitVector.length() != 1)
88                         return new ColorDefinition(BuiltInColor.COLOR_BLACK);
89                 switch (bitVector.getLSBit(0))
90                 {
91                 case ONE:
92                         return renderPrefs.getColorDefinition(BIT_ONE_COLOR);
93                 case U:
94                         return renderPrefs.getColorDefinition(BIT_U_COLOR);
95                 case X:
96                         return renderPrefs.getColorDefinition(BIT_X_COLOR);
97                 case Z:
98                         return renderPrefs.getColorDefinition(BIT_Z_COLOR);
99                 case ZERO:
100                         return renderPrefs.getColorDefinition(BIT_ZERO_COLOR);
101                 default:
102                         throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getLSBit(0));
103                 }
104         }
105
106         private BitVectorFormatter()
107         {
108                 throw new UnsupportedOperationException("No BitVectorFormatter instances");
109         }
110 }