Refactored BitVector methods to resolve ambiguity
[Mograsim.git] / 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         public boolean isBinary()
22         {
23                 return this == ONE || this == ZERO;
24         }
25
26         @Override
27         public Bit and(Bit other)
28         {
29                 return fromTable(AND_TABLE, this, other);
30         }
31
32         @Override
33         public Bit or(Bit other)
34         {
35                 return fromTable(OR_TABLE, this, other);
36         }
37
38         @Override
39         public Bit xor(Bit other)
40         {
41                 return fromTable(XOR_TABLE, this, other);
42         }
43
44         @Override
45         public Bit not()
46         {
47                 switch (this)
48                 {
49                 case U:
50                         return U;
51                 case ONE:
52                         return ZERO;
53                 case ZERO:
54                         return ONE;
55                 default:
56                         return X;
57                 }
58         }
59
60         public Bit[] makeArray(int length)
61         {
62                 Bit[] bits = new Bit[length];
63                 Arrays.fill(bits, this);
64                 return bits;
65         }
66
67         public BitVector toVector(int length)
68         {
69                 return BitVector.of(this, length);
70         }
71
72         @Override
73         public Bit join(Bit other)
74         {
75                 return fromTable(JOIN_TABLE, this, other);
76         }
77
78         @Override
79         public String toString()
80         {
81                 return getSymbol();
82         }
83
84         public String getSymbol()
85         {
86                 return symbol;
87         }
88
89         public static Bit lastBitOf(int value)
90         {
91                 return values()[2 + (value & 1)];
92         }
93
94         public static Bit of(boolean binaryValue)
95         {
96                 return binaryValue ? ONE : ZERO;
97         }
98
99         public static Bit parse(String s)
100         {
101                 Bit bit = SYMBOL_MAP.get(s);
102                 Objects.requireNonNull(bit, "No Bit found for symbol " + s);
103                 return bit;
104         }
105
106         public static Bit parse(String s, int symbolPosition)
107         {
108                 return parse(s.substring(symbolPosition, symbolPosition + 1));
109         }
110
111         private static Bit fromTable(Bit[][] table, Bit a, Bit b)
112         {
113                 return table[a.ordinal()][b.ordinal()];
114         }
115
116         static final Map<String, Bit> SYMBOL_MAP = Map.of(U.symbol, U, X.symbol, X, ZERO.symbol, ZERO, ONE.symbol, ONE, Z.symbol, Z);
117
118         // @formatter:off
119         private static final Bit[][] JOIN_TABLE = 
120                 { { U, U, U,    U,   U    }, 
121                   { U, X, X,    X,   X    }, 
122                   { U, X, ZERO, X,   ZERO },
123                   { U, X, X,    ONE, ONE  }, 
124                   { U, X, ZERO, ONE, Z    } };
125
126         private static final Bit[][] AND_TABLE = 
127                 { { U,    U,    ZERO, U,    U    }, 
128                   { U,    X,    ZERO, X,    X    },
129                   { ZERO, ZERO, ZERO, ZERO, ZERO }, 
130                   { U,    X,    ZERO, ONE,  X    }, 
131                   { U,    X,    ZERO, X,    X    } };
132
133         private static final Bit[][] OR_TABLE =
134         { { U,   U,   U,    ONE, U    },    
135           { U,   X,   X,    ONE, X    },    
136           { U,   X,   ZERO, ONE, X    },    
137           { ONE, ONE, ONE,  ONE, ONE  },    
138           { U,   X,   X,    ONE, X    } };
139         
140         private static final Bit[][] XOR_TABLE =
141         { { U, U, U,    U,    U },    
142           { U, X, X,    X,    X },    
143           { U, X, ZERO, ONE,  X },    
144           { U, X, ONE,  ZERO, X },    
145           { U, X, X,    X,    X } }; 
146         // @formatter:on
147 }