0e709852777d9c35bcd6cdce7e2331a77cc5f830
[Mograsim.git] / era.mi / src / era / mi / logic / Bit.java
1 package era.mi.logic;
2
3 import java.util.Arrays;
4
5 public enum Bit
6 {
7         ONE, ZERO, Z, X;
8
9         public static Bit and(Bit a, Bit b)
10         {
11                 return a.and(b);
12         }
13
14         public Bit and(Bit other)
15         {
16                 if (this == ZERO || other == ZERO)
17                         return ZERO;
18                 else if (this == other && this == ONE)
19                         return ONE;
20                 else if (this == X || other == X)
21                         return X;
22                 else
23                         return ZERO;
24         }
25
26         public static Bit or(Bit a, Bit b)
27         {
28                 return a.or(b);
29         }
30
31         public Bit or(Bit other)
32         {
33                 if (this == ONE || other == ONE)
34                         return ONE;
35                 else if (this == other && this == ZERO)
36                         return ZERO;
37                 else if (this == X || other == X)
38                         return X;
39                 else
40                         return ZERO;
41         }
42
43         public static Bit xor(Bit a, Bit b)
44         {
45                 return a.xor(b);
46         }
47
48         public Bit xor(Bit other)
49         {
50                 if(this == X || this == Z || other == X || other == Z)
51                         return Bit.X;
52                 else
53                         return this == other ? ZERO : ONE;
54         }
55
56         public Bit not()
57         {
58                 switch (this)
59                         {
60                         case ONE:
61                                 return Bit.ZERO;
62                         case ZERO:
63                                 return Bit.ONE;
64                         default:
65                                 return Bit.X;
66                         }
67         }
68         
69         public Bit[] makeArray(int length)
70         {
71                 Bit[] bits = new Bit[length];
72                 Arrays.fill(bits, this);
73                 return bits;
74         }
75
76         /**
77          * Rules for two bits that get directly connected<br>
78          * <code><table>
79          * <tbody>
80          * <tr><td><td>X<td>0<td>1<td>Z</tr>
81          * <tr><td>X<td>X<td>X<td>X<td>X</tr>
82          * <tr><td>0<td>X<td>0<td>X<td>0</tr>
83          * <tr><td>1<td>X<td>X<td>1<td>1</tr>
84          * <tr><td>Z<td>X<td>0<td>1<td>Z</tr>
85          * </tbody>
86          * </table><code>
87          * 
88          * @return the result according to the table
89          * 
90          * @author Christian Femers
91          */
92         public Bit combineWith(Bit other)
93         {
94                 if (this == other)
95                         return this;
96                 if (this == X || other == X)
97                         return X;
98                 if (other == Z)
99                         return this;
100                 if (this == Z)
101                         return other;
102                 return X;
103         }
104
105         public static Bit combine(Bit a, Bit b)
106         {
107                 return a.combineWith(b);
108         }
109 }