new WireEnds as in/outputs are now initialized with U again
[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 {
10         U, X, ZERO, ONE, Z;
11
12         public static Bit and(Bit a, Bit b)
13         {
14                 return a.and(b);
15         }
16
17         public Bit and(Bit other)
18         {
19                 return fromTable(AND_TABLE, this, other);
20         }
21
22         public static Bit or(Bit a, Bit b)
23         {
24                 return a.or(b);
25         }
26
27         public Bit or(Bit other)
28         {
29                 return fromTable(OR_TABLE, this, other);
30         }
31
32         public static Bit xor(Bit a, Bit b)
33         {
34                 return a.xor(b);
35         }
36
37         public Bit xor(Bit other)
38         {
39                 return fromTable(XOR_TABLE, this, other);
40         }
41
42         public Bit not()
43         {
44                 switch (this)
45                 {
46                 case U:
47                         return U;
48                 case ONE:
49                         return ZERO;
50                 case ZERO:
51                         return ONE;
52                 default:
53                         return X;
54                 }
55         }
56
57         public Bit[] makeArray(int length)
58         {
59                 Bit[] bits = new Bit[length];
60                 Arrays.fill(bits, this);
61                 return bits;
62         }
63
64         public Bit combineWith(Bit other)
65         {
66                 return fromTable(JOIN_TABLE, this, other);
67         }
68
69         public static Bit combine(Bit a, Bit b)
70         {
71                 return a.combineWith(b);
72         }
73
74         private static Bit fromTable(Bit[][] table, Bit a, Bit b)
75         {
76                 return table[a.ordinal()][b.ordinal()];
77         }
78
79         // @formatter:off
80         private static Bit[][] JOIN_TABLE = 
81                 { { U, U, U,    U,   U    }, 
82                   { U, X, X,    X,   X    }, 
83                   { U, X, ZERO, X,   ZERO },
84                   { U, X, X,    ONE, ONE  }, 
85                   { U, X, ZERO, ONE, Z    } };
86
87         private static Bit[][] AND_TABLE = 
88                 { { U,    U,    ZERO, U,    U    }, 
89                   { U,    X,    ZERO, X,    X    },
90                   { ZERO, ZERO, ZERO, ZERO, ZERO }, 
91                   { U,    X,    ZERO, ONE,  X    }, 
92                   { U,    X,    ZERO, X,    X    } };
93
94         private static Bit[][] OR_TABLE =
95         { { U,   U,   U,    ONE, U    },    
96           { U,   X,   X,    ONE, X    },    
97           { U,   X,   ZERO, ONE, X    },    
98           { ONE, ONE, ONE,  ONE, ONE  },    
99           { U,   X,   X,    ONE, X    } };
100         
101         private static Bit[][] XOR_TABLE =
102         { { U, U, U,    U,    U },    
103           { U, X, X,    X,    X },    
104           { U, X, ZERO, ONE,  X },    
105           { U, X, ONE,  ZERO, X },    
106           { U, X, X,    X,    X } }; 
107         // @formatter:on
108 }