added some convenience methods that make our lives easier
authorChristian Femers <femers@in.tum.de>
Fri, 10 May 2019 17:21:50 +0000 (19:21 +0200)
committerChristian Femers <femers@in.tum.de>
Fri, 10 May 2019 17:21:50 +0000 (19:21 +0200)
era.mi/src/era/mi/logic/Bit.java
era.mi/src/era/mi/logic/timeline/Timeline.java
era.mi/src/era/mi/logic/wires/WireArray.java

index b18c597..e7c9396 100644 (file)
@@ -1,62 +1,95 @@
 package era.mi.logic;
 
-
 public enum Bit
 {
        ONE, ZERO, Z, X;
-       
+
        public static Bit and(Bit a, Bit b)
        {
                return a.and(b);
        }
-       
+
        public Bit and(Bit other)
        {
-               if(equals(Bit.ZERO) || other.equals(Bit.ZERO))
+               if (equals(Bit.ZERO) || other.equals(Bit.ZERO))
                        return Bit.ZERO;
-               else if(equals(other) && equals(Bit.ONE))
+               else if (equals(other) && equals(Bit.ONE))
                        return Bit.ONE;
                else
                        return Bit.X;
        }
-       
+
        public static Bit or(Bit a, Bit b)
        {
                return a.or(b);
        }
-       
+
        public Bit or(Bit other)
        {
-               if(equals(Bit.ONE) || other.equals(Bit.ONE))
+               if (equals(Bit.ONE) || other.equals(Bit.ONE))
                        return Bit.ONE;
-               else if(equals(other) && equals(Bit.ZERO))
+               else if (equals(other) && equals(Bit.ZERO))
                        return Bit.ZERO;
                else
                        return Bit.X;
        }
-       
+
        public static Bit xor(Bit a, Bit b)
        {
                return a.xor(b);
        }
-       
+
        public Bit xor(Bit other)
        {
-               //I'm uncertain how this should behave for cases where one value is neither 1 nor 0.
-               //TODO: Implement xor
+               // I'm uncertain how this should behave for cases where one value is neither 1 nor 0.
+               // TODO: Implement xor
                return Bit.X;
        }
-       
+
        public Bit not()
        {
-               switch(this)
-               {
-               case ONE:
-                       return Bit.ZERO;
-               case ZERO:
-                       return Bit.ONE;
-               default:
-                       return Bit.X;
-               }
+               switch (this)
+                       {
+                       case ONE:
+                               return Bit.ZERO;
+                       case ZERO:
+                               return Bit.ONE;
+                       default:
+                               return Bit.X;
+                       }
+       }
+
+       /**
+        * Rules for two bits that get directly connected<br>
+        * <code><table>
+        * <tbody>
+        * <tr><td><td>X<td>0<td>1<td>Z</tr>
+        * <tr><td>X<td>X<td>X<td>X<td>X</tr>
+        * <tr><td>0<td>X<td>0<td>X<td>0</tr>
+        * <tr><td>1<td>X<td>X<td>1<td>1</tr>
+        * <tr><td>Z<td>X<td>0<td>1<td>Z</tr>
+        * </tbody>
+        * </table><code>
+        * 
+        * @return the result according to the table
+        * 
+        * @author Christian Femers
+        */
+       public Bit combineWith(Bit other)
+       {
+               if (this == other)
+                       return this;
+               if (this == X || other == X)
+                       return X;
+               if (other == Z)
+                       return this;
+               if (this == Z)
+                       return other;
+               return X;
+       }
+
+       public static Bit combine(Bit a, Bit b)
+       {
+               return a.combineWith(b);
        }
 }
index 125b69c..2392f4c 100644 (file)
@@ -33,6 +33,12 @@ public class Timeline
                currentTime = first.getTiming();
                first.run();
        }
+       
+       public void executeAll()
+       {
+               while (hasNext())
+                       executeNext();
+       }
 
        public long getSimulationTime()
        {
index cf960e0..8cceebe 100644 (file)
@@ -291,5 +291,17 @@ public class WireArray
                                bits[i] = Bit.Z;
                        feedSignals(bits);
                }
+               
+               @Override
+               public String toString()
+               {
+                       return Arrays.toString(values);
+               }
+       }
+
+       @Override
+       public String toString()
+       {
+               return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
        }
 }