Some work on improving BitVector<->String conversions
[Mograsim.git] / plugins / net.mograsim.logic.core / src / net / mograsim / logic / core / types / BitVectorFormatter.java
1 package net.mograsim.logic.core.types;
2
3 import java.math.BigInteger;
4
5 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
6 import net.mograsim.preferences.ColorDefinition;
7 import net.mograsim.preferences.ColorDefinition.BuiltInColor;
8 import net.mograsim.preferences.Preferences;
9
10 public class BitVectorFormatter
11 {
12         public static String formatValueAsString(ReadEnd end)
13         {
14                 return formatAsString(end == null ? null : end.getValues());
15         }
16
17         public static String toBitstring(BitVector bitVector)
18         {
19                 return bitVector.toBitstring();
20         }
21
22         public static String formatAsString(BitVector bitVector)
23         {
24                 if (bitVector == null)
25                         return "null";
26                 if (bitVector.isBinary())
27                 {
28                         String hexdigits = bitVector.getUnsignedValue().toString(16);
29                         StringBuilder sb = new StringBuilder();
30                         sb.append("0x");
31                         sb.append("0".repeat((bitVector.length() + 3) / 4 - hexdigits.length()));
32                         sb.append(hexdigits);
33                         return sb.toString();
34                 }
35                 if (bitVector.isHighImpedance())
36                         return "-";
37                 return bitVector.toBitstring();
38         }
39
40         // TODO this method overlaps in functionality with AsmNumberUtil (in plugin.core)
41         public static BitVector parseUserBitVector(String userInput, int width)
42         {
43                 BitVector bitvector = null;
44                 if (width > 0 && userInput.matches("0x[0-9a-fA-F]+"))
45                         // TODO should we check for overflows?
46                         bitvector = BitVector.from(new BigInteger(userInput.substring(2), 16), width);
47                 else if (width <= 0 || userInput.length() == width)
48                         // TODO do this without exceptions
49                         try
50                         {
51                                 bitvector = BitVector.parseBitstring(userInput);
52                         }
53                         catch (@SuppressWarnings("unused") NullPointerException x)
54                         {
55                                 // ignore
56                         }
57                 if (bitvector == null && width > 0)
58                         try
59                         {
60                                 // TODO should we check for overflows?
61                                 bitvector = BitVector.from(new BigInteger(userInput), width);
62                         }
63                         catch (@SuppressWarnings("unused") NumberFormatException x)
64                         {
65                                 // ignore
66                         }
67                 return bitvector;
68         }
69
70         // TODO doesn't this belong to logic.model?
71         public static ColorDefinition formatAsColor(ReadEnd end)
72         {
73                 return formatAsColor(end == null ? null : end.getValues());
74         }
75
76         public static ColorDefinition formatAsColor(BitVector bitVector)
77         {
78                 // TODO maybe find a color assignment for multiple-bit bit vectors?
79                 if (bitVector == null || bitVector.length() != 1)
80                         return new ColorDefinition(BuiltInColor.COLOR_BLACK);
81                 switch (bitVector.getLSBit(0))
82                 {
83                 case ONE:
84                         return Preferences.current().getColorDefinition("net.mograsim.logic.model.color.bit.one");
85                 case U:
86                         return Preferences.current().getColorDefinition("net.mograsim.logic.model.color.bit.u");
87                 case X:
88                         return Preferences.current().getColorDefinition("net.mograsim.logic.model.color.bit.x");
89                 case Z:
90                         return Preferences.current().getColorDefinition("net.mograsim.logic.model.color.bit.z");
91                 case ZERO:
92                         return Preferences.current().getColorDefinition("net.mograsim.logic.model.color.bit.zero");
93                 default:
94                         throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getLSBit(0));
95                 }
96         }
97
98         private BitVectorFormatter()
99         {
100                 throw new UnsupportedOperationException("No BitVectorFormatter instances");
101         }
102 }