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