added some convenience methods that make our lives easier
[Mograsim.git] / era.mi / src / era / mi / logic / Bit.java
1 package era.mi.logic;
2
3 public enum Bit
4 {
5         ONE, ZERO, Z, X;
6
7         public static Bit and(Bit a, Bit b)
8         {
9                 return a.and(b);
10         }
11
12         public Bit and(Bit other)
13         {
14                 if (equals(Bit.ZERO) || other.equals(Bit.ZERO))
15                         return Bit.ZERO;
16                 else if (equals(other) && equals(Bit.ONE))
17                         return Bit.ONE;
18                 else
19                         return Bit.X;
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                 if (equals(Bit.ONE) || other.equals(Bit.ONE))
30                         return Bit.ONE;
31                 else if (equals(other) && equals(Bit.ZERO))
32                         return Bit.ZERO;
33                 else
34                         return Bit.X;
35         }
36
37         public static Bit xor(Bit a, Bit b)
38         {
39                 return a.xor(b);
40         }
41
42         public Bit xor(Bit other)
43         {
44                 // I'm uncertain how this should behave for cases where one value is neither 1 nor 0.
45                 // TODO: Implement xor
46                 return Bit.X;
47         }
48
49         public Bit not()
50         {
51                 switch (this)
52                         {
53                         case ONE:
54                                 return Bit.ZERO;
55                         case ZERO:
56                                 return Bit.ONE;
57                         default:
58                                 return Bit.X;
59                         }
60         }
61
62         /**
63          * Rules for two bits that get directly connected<br>
64          * <code><table>
65          * <tbody>
66          * <tr><td><td>X<td>0<td>1<td>Z</tr>
67          * <tr><td>X<td>X<td>X<td>X<td>X</tr>
68          * <tr><td>0<td>X<td>0<td>X<td>0</tr>
69          * <tr><td>1<td>X<td>X<td>1<td>1</tr>
70          * <tr><td>Z<td>X<td>0<td>1<td>Z</tr>
71          * </tbody>
72          * </table><code>
73          * 
74          * @return the result according to the table
75          * 
76          * @author Christian Femers
77          */
78         public Bit combineWith(Bit other)
79         {
80                 if (this == other)
81                         return this;
82                 if (this == X || other == X)
83                         return X;
84                 if (other == Z)
85                         return this;
86                 if (this == Z)
87                         return other;
88                 return X;
89         }
90
91         public static Bit combine(Bit a, Bit b)
92         {
93                 return a.combineWith(b);
94         }
95 }