Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[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         public static void and(Bit[] dst, Bit[] src)
38         {
39                 for (int i = 0; i < dst.length; i++)
40                         dst[i] = dst[i].and(src[i]);
41         }
42
43         @Override
44         public Bit or(Bit other)
45         {
46                 return fromTable(OR_TABLE, this, other);
47         }
48
49         public static void or(Bit[] dst, Bit[] src)
50         {
51                 for (int i = 0; i < dst.length; i++)
52                         dst[i] = dst[i].or(src[i]);
53         }
54
55         @Override
56         public Bit xor(Bit other)
57         {
58                 return fromTable(XOR_TABLE, this, other);
59         }
60
61         public static void xor(Bit[] dst, Bit[] src)
62         {
63                 for (int i = 0; i < dst.length; i++)
64                         dst[i] = dst[i].xor(src[i]);
65         }
66
67         @Override
68         public Bit not()
69         {
70                 switch (this)
71                 {
72                 case U:
73                         return U;
74                 case ONE:
75                         return ZERO;
76                 case ZERO:
77                         return ONE;
78                 default:
79                         return X;
80                 }
81         }
82
83         public Bit[] makeArray(int length)
84         {
85                 Bit[] bits = new Bit[length];
86                 Arrays.fill(bits, this);
87                 return bits;
88         }
89
90         public BitVector toVector()
91         {
92                 return BitVector.of(this, 1);
93         }
94
95         public BitVector toVector(int length)
96         {
97                 return BitVector.of(this, length);
98         }
99
100         @Override
101         public Bit join(Bit other)
102         {
103                 return fromTable(JOIN_TABLE, this, other);
104         }
105
106         public static void join(Bit[] dst, Bit[] src)
107         {
108                 for (int i = 0; i < dst.length; i++)
109                         dst[i] = dst[i].join(src[i]);
110         }
111
112         @Override
113         public String toString()
114         {
115                 return getSymbol();
116         }
117
118         public String getSymbol()
119         {
120                 return symbol;
121         }
122
123         public static Bit lastBitOf(int value)
124         {
125                 return values()[2 + (value & 1)];
126         }
127
128         public static Bit of(boolean binaryValue)
129         {
130                 return binaryValue ? ONE : ZERO;
131         }
132
133         public static Bit parse(String s)
134         {
135                 Bit bit = SYMBOL_MAP.get(s);
136                 Objects.requireNonNull(bit, "No Bit found for symbol " + s);
137                 return bit;
138         }
139
140         public static Bit parse(String s, int symbolPosition)
141         {
142                 return parse(s.substring(symbolPosition, symbolPosition + 1));
143         }
144
145         private static Bit fromTable(Bit[][] table, Bit a, Bit b)
146         {
147                 return table[a.ordinal()][b.ordinal()];
148         }
149
150         static final Map<String, Bit> SYMBOL_MAP = Map.of(U.symbol, U, X.symbol, X, ZERO.symbol, ZERO, ONE.symbol, ONE, Z.symbol, Z);
151
152         // @formatter:off
153         private static final Bit[][] JOIN_TABLE = 
154                 { { U, U, U,    U,   U    }, 
155                   { U, X, X,    X,   X    }, 
156                   { U, X, ZERO, X,   ZERO },
157                   { U, X, X,    ONE, ONE  }, 
158                   { U, X, ZERO, ONE, Z    } };
159
160         private static final Bit[][] AND_TABLE = 
161                 { { U,    U,    ZERO, U,    U    }, 
162                   { U,    X,    ZERO, X,    X    },
163                   { ZERO, ZERO, ZERO, ZERO, ZERO }, 
164                   { U,    X,    ZERO, ONE,  X    }, 
165                   { U,    X,    ZERO, X,    X    } };
166
167         private static final Bit[][] OR_TABLE =
168         { { U,   U,   U,    ONE, U    },    
169           { U,   X,   X,    ONE, X    },    
170           { U,   X,   ZERO, ONE, X    },    
171           { ONE, ONE, ONE,  ONE, ONE  },    
172           { U,   X,   X,    ONE, X    } };
173         
174         private static final Bit[][] XOR_TABLE =
175         { { U, U, U,    U,    U },    
176           { U, X, X,    X,    X },    
177           { U, X, ZERO, ONE,  X },    
178           { U, X, ONE,  ZERO, X },    
179           { U, X, X,    X,    X } }; 
180         // @formatter:on
181 }