Added 'U' to Bit and made code IEEE 1164 compliant (w.o. W,H,L,-)
[Mograsim.git] / era.mi / src / era / mi / logic / Bit.java
1 package era.mi.logic;
2
3 import java.util.Arrays;
4
5 /**
6  * stdlogic according to IEEE 1164
7  */
8 public enum Bit {
9         U, X, ZERO, ONE, Z;
10
11         public static Bit and(Bit a, Bit b) {
12                 return a.and(b);
13         }
14
15         public Bit and(Bit other) {
16                 return fromTable(AND_TABLE, this, other);
17         }
18
19         public static Bit or(Bit a, Bit b) {
20                 return a.or(b);
21         }
22
23         public Bit or(Bit other) {
24                 return fromTable(OR_TABLE, this, other);
25         }
26
27         public static Bit xor(Bit a, Bit b) {
28                 return a.xor(b);
29         }
30
31         public Bit xor(Bit other) {
32                 return fromTable(XOR_TABLE, this, other);
33         }
34
35         public Bit not() {
36                 switch (this) {
37                 case U:
38                         return U;
39                 case ONE:
40                         return ZERO;
41                 case ZERO:
42                         return ONE;
43                 default:
44                         return X;
45                 }
46         }
47
48         public Bit[] makeArray(int length) {
49                 Bit[] bits = new Bit[length];
50                 Arrays.fill(bits, this);
51                 return bits;
52         }
53
54         public Bit combineWith(Bit other) {
55                 return fromTable(JOIN_TABLE, this, other);
56         }
57
58         public static Bit combine(Bit a, Bit b) {
59                 return a.combineWith(b);
60         }
61
62         private static Bit fromTable(Bit[][] table, Bit a, Bit b) {
63                 return table[a.ordinal()][b.ordinal()];
64         }
65
66         // @formatter:off
67         private static Bit[][] JOIN_TABLE = 
68                 { { U, U, U,    U,   U    }, 
69                   { U, X, X,    X,   X    }, 
70                   { U, X, ZERO, X,   ZERO },
71                   { U, X, X,    ONE, ONE  }, 
72                   { U, X, ZERO, ONE, Z    } };
73
74         private static Bit[][] AND_TABLE = 
75                 { { U,    U,    ZERO, U,    U    }, 
76                   { U,    X,    ZERO, X,    X    },
77                   { ZERO, ZERO, ZERO, ZERO, ZERO }, 
78                   { U,    X,    ZERO, ONE,  X    }, 
79                   { U,    X,    ZERO, X,    X    } };
80
81         private static Bit[][] OR_TABLE =
82         { { U,   U,   U,    ONE, U    },    
83           { U,   X,   X,    ONE, X    },    
84           { U,   X,   ZERO, ONE, X    },    
85           { ONE, ONE, ONE,  ONE, ONE  },    
86           { U,   X,   X,    ONE, X    } };
87         
88         private static Bit[][] XOR_TABLE =
89         { { U, U, U,    U,    U },    
90           { U, X, X,    X,    X },    
91           { U, X, ZERO, ONE,  X },    
92           { U, X, ONE,  ZERO, X },    
93           { U, X, X,    X,    X } }; 
94         // @formatter:on
95 }