Removed unused code of Bit
[Mograsim.git] / plugins / net.mograsim.logic.core / src / net / mograsim / logic / core / types / Bit.java
1 package net.mograsim.logic.core.types;
2
3 import java.util.Arrays;
4 import java.util.Map;
5 import java.util.Objects;
6
7 /**
8  * stdlogic according to IEEE 1164
9  */
10 public enum Bit implements StrictLogicType<Bit>
11 {
12         U("U"), X("X"), ZERO("0"), ONE("1"), Z("Z");
13
14         private final String symbol;
15
16         private Bit(String symbol)
17         {
18                 this.symbol = symbol;
19         }
20
21         /**
22          * Returns if the Bit is binary, this is only true for <code>ZERO</code> and <code>ONE</code>.
23          * 
24          * @return true if and only if <code>this == ONE || this == ZERO</code>
25          */
26         public boolean isBinary()
27         {
28                 return this == ONE || this == ZERO;
29         }
30
31         @Override
32         public Bit and(Bit other)
33         {
34                 return fromTable(AND_TABLE, this, other);
35         }
36
37         @Override
38         public Bit or(Bit other)
39         {
40                 return fromTable(OR_TABLE, this, other);
41         }
42
43         @Override
44         public Bit xor(Bit other)
45         {
46                 return fromTable(XOR_TABLE, this, other);
47         }
48
49         @Override
50         public Bit not()
51         {
52                 switch (this)
53                 {
54                 case U:
55                         return U;
56                 case ONE:
57                         return ZERO;
58                 case ZERO:
59                         return ONE;
60                 default:
61                         return X;
62                 }
63         }
64
65         public Bit[] makeArray(int length)
66         {
67                 Bit[] bits = new Bit[length];
68                 Arrays.fill(bits, this);
69                 return bits;
70         }
71
72         public BitVector toVector()
73         {
74                 return BitVector.of(this, 1);
75         }
76
77         public BitVector toVector(int length)
78         {
79                 return BitVector.of(this, length);
80         }
81
82         @Override
83         public Bit join(Bit other)
84         {
85                 return fromTable(JOIN_TABLE, this, other);
86         }
87
88         public static void join(Bit[] dst, Bit[] src)
89         {
90                 for (int i = 0; i < dst.length; i++)
91                         dst[i] = dst[i].join(src[i]);
92         }
93
94         @Override
95         public String toString()
96         {
97                 return getSymbol();
98         }
99
100         public String getSymbol()
101         {
102                 return symbol;
103         }
104
105         public static Bit lastBitOf(int value)
106         {
107                 return values()[2 + (value & 1)];
108         }
109
110         public static Bit of(boolean binaryValue)
111         {
112                 return binaryValue ? ONE : ZERO;
113         }
114
115         public static Bit parse(String s)
116         {
117                 Bit bit = SYMBOL_MAP.get(s);
118                 Objects.requireNonNull(bit, "No Bit found for symbol " + s);
119                 return bit;
120         }
121
122         public static Bit parse(String s, int symbolPosition)
123         {
124                 return parse(s.substring(symbolPosition, symbolPosition + 1));
125         }
126
127         private static Bit fromTable(Bit[][] table, Bit a, Bit b)
128         {
129                 return table[a.ordinal()][b.ordinal()];
130         }
131
132         static final Map<String, Bit> SYMBOL_MAP = Map.of(U.symbol, U, X.symbol, X, ZERO.symbol, ZERO, ONE.symbol, ONE, Z.symbol, Z);
133
134         // @formatter:off
135         private static final Bit[][] JOIN_TABLE = 
136                 { { U, U, U,    U,   U    }, 
137                   { U, X, X,    X,   X    }, 
138                   { U, X, ZERO, X,   ZERO },
139                   { U, X, X,    ONE, ONE  }, 
140                   { U, X, ZERO, ONE, Z    } };
141
142         private static final Bit[][] AND_TABLE = 
143                 { { U,    U,    ZERO, U,    U    }, 
144                   { U,    X,    ZERO, X,    X    },
145                   { ZERO, ZERO, ZERO, ZERO, ZERO }, 
146                   { U,    X,    ZERO, ONE,  X    }, 
147                   { U,    X,    ZERO, X,    X    } };
148
149         private static final Bit[][] OR_TABLE =
150         { { U,   U,   U,    ONE, U    },    
151           { U,   X,   X,    ONE, X    },    
152           { U,   X,   ZERO, ONE, X    },    
153           { ONE, ONE, ONE,  ONE, ONE  },    
154           { U,   X,   X,    ONE, X    } };
155         
156         private static final Bit[][] XOR_TABLE =
157         { { U, U, U,    U,    U },    
158           { U, X, X,    X,    X },    
159           { U, X, ZERO, ONE,  X },    
160           { U, X, ONE,  ZERO, X },    
161           { U, X, X,    X,    X } }; 
162         // @formatter:on
163 }