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