Further renaming of length to width
[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         /**
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(int width)
73         {
74                 return BitVector.of(this, width);
75         }
76
77         @Override
78         public Bit join(Bit other)
79         {
80                 return fromTable(JOIN_TABLE, this, other);
81         }
82
83         @Override
84         public String toString()
85         {
86                 return getSymbol();
87         }
88
89         public String getSymbol()
90         {
91                 return symbol;
92         }
93
94         public static Bit lastBitOf(int value)
95         {
96                 return values()[2 + (value & 1)];
97         }
98
99         public static Bit of(boolean binaryValue)
100         {
101                 return binaryValue ? ONE : ZERO;
102         }
103
104         public static Bit parse(String s)
105         {
106                 Bit bit = SYMBOL_MAP.get(s);
107                 Objects.requireNonNull(bit, "No Bit found for symbol " + s);
108                 return bit;
109         }
110
111         public static Bit parse(String s, int symbolPosition)
112         {
113                 return parse(s.substring(symbolPosition, symbolPosition + 1));
114         }
115
116         private static Bit fromTable(Bit[][] table, Bit a, Bit b)
117         {
118                 return table[a.ordinal()][b.ordinal()];
119         }
120
121         static final Map<String, Bit> SYMBOL_MAP = Map.of(U.symbol, U, X.symbol, X, ZERO.symbol, ZERO, ONE.symbol, ONE, Z.symbol, Z);
122
123         // @formatter:off
124         private static final Bit[][] JOIN_TABLE = 
125                 { { U, U, U,    U,   U    }, 
126                   { U, X, X,    X,   X    }, 
127                   { U, X, ZERO, X,   ZERO },
128                   { U, X, X,    ONE, ONE  }, 
129                   { U, X, ZERO, ONE, Z    } };
130
131         private static final Bit[][] AND_TABLE = 
132                 { { U,    U,    ZERO, U,    U    }, 
133                   { U,    X,    ZERO, X,    X    },
134                   { ZERO, ZERO, ZERO, ZERO, ZERO }, 
135                   { U,    X,    ZERO, ONE,  X    }, 
136                   { U,    X,    ZERO, X,    X    } };
137
138         private static final Bit[][] OR_TABLE =
139         { { U,   U,   U,    ONE, U    },    
140           { U,   X,   X,    ONE, X    },    
141           { U,   X,   ZERO, ONE, X    },    
142           { ONE, ONE, ONE,  ONE, ONE  },    
143           { U,   X,   X,    ONE, X    } };
144         
145         private static final Bit[][] XOR_TABLE =
146         { { U, U, U,    U,    U },    
147           { U, X, X,    X,    X },    
148           { U, X, ZERO, ONE,  X },    
149           { U, X, ONE,  ZERO, X },    
150           { U, X, X,    X,    X } }; 
151         // @formatter:on
152 }