Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / wires / CoreWire.java
index f643f8e..13f54a0 100644 (file)
@@ -5,7 +5,9 @@ import static net.mograsim.logic.core.types.Bit.Z;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import net.mograsim.logic.core.LogicObservable;
 import net.mograsim.logic.core.LogicObserver;
@@ -122,68 +124,9 @@ public class CoreWire
         */
        public void forceValues(BitVector values)
        {
-               setNewValues(values);
-       }
-
-       /**
-        * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-        * 
-        * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same
-        *         value), not <code>Bit.U</code>, <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
-        * 
-        * @author Fabian Stemmler
-        */
-       public boolean hasNumericValue()
-       {
-               return getValues().isBinary();
-       }
-
-       /**
-        * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-        * 
-        * @return The unsigned value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-        * 
-        * @author Fabian Stemmler
-        */
-       public long getUnsignedValue()
-       {
-               long val = 0;
-               long mask = 1;
-               for (Bit bit : getValues())
-               {
-                       switch (bit)
-                       {
-                       default:
-                       case Z:
-                       case X:
-                               return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
-                       case ONE:
-                               val |= mask;
-                               break;
-                       case ZERO:
-                       }
-                       mask = mask << 1;
-               }
-               return val;
-       }
-
-       /**
-        * The {@link CoreWire} is interpreted as a signed integer with n bits.
-        * 
-        * @return The signed value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-        * 
-        * @author Fabian Stemmler
-        */
-       public long getSignedValue()
-       {
-               long val = getUnsignedValue();
-               long mask = 1 << (width - 1);
-               if ((mask & val) != 0)
-               {
-                       int shifts = 64 - width;
-                       return (val << shifts) >> shifts;
-               }
-               return val;
+               bitsWithoutFusions = values.getBits();
+               invalidateCachedValuesForAllFusedWires();
+               notifyObservers();
        }
 
        /**
@@ -238,7 +181,8 @@ public class CoreWire
        }
 
        /**
-        * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link CoreWire}. This {@link ReadWriteEnd} can be written to.
+        * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link CoreWire}. This {@link ReadWriteEnd} can be written
+        * to.
         */
        public ReadWriteEnd createReadWriteEnd()
        {
@@ -261,8 +205,8 @@ public class CoreWire
 
        /**
         * A {@link ReadEnd} feeds a constant signal into the {@link CoreWire} it is tied to. The combination of all inputs determines the
-        * {@link CoreWire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than Z 0
-        * and 1 turn into X when they are mixed
+        * {@link CoreWire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than
+        * Z 0 and 1 turn into X when they are mixed
         * 
         * @author Fabian Stemmler
         */
@@ -321,43 +265,6 @@ public class CoreWire
                        return CoreWire.this.getValues(start, end);
                }
 
-               /**
-                * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-                * 
-                * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the
-                *         same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
-                * 
-                * @author Fabian Stemmler
-                */
-               public boolean hasNumericValue()
-               {
-                       return CoreWire.this.hasNumericValue();
-               }
-
-               /**
-                * The {@link CoreWire} is interpreted as an unsigned integer with n bits.
-                * 
-                * @return The unsigned value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-                * 
-                * @author Fabian Stemmler
-                */
-               public long getUnsignedValue()
-               {
-                       return CoreWire.this.getUnsignedValue();
-               }
-
-               /**
-                * The {@link CoreWire} is interpreted as a signed integer with n bits.
-                * 
-                * @return The signed value of the {@link CoreWire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
-                * 
-                * @author Fabian Stemmler
-                */
-               public long getSignedValue()
-               {
-                       return CoreWire.this.getSignedValue();
-               }
-
                @Override
                public String toString()
                {
@@ -442,6 +349,7 @@ public class CoreWire
                        feedSignals(BitVector.of(newValues));
                }
 
+               // TODO what if this is called multiple times at the same simulation time? (happens in component unit tests)
                public void feedSignals(BitVector newValues)
                {
                        if (newValues.length() != width)
@@ -665,11 +573,11 @@ public class CoreWire
 
        private static class FusedBit
        {
-               private final List<WireBit> participatingWireBits;
+               private final Set<WireBit> participatingWireBits;
 
                public FusedBit()
                {
-                       this.participatingWireBits = new ArrayList<>();
+                       this.participatingWireBits = new HashSet<>();
                }
 
                public void addParticipatingWireBit(CoreWire w, int bit)
@@ -719,5 +627,36 @@ public class CoreWire
                        this.wire = wire;
                        this.bit = bit;
                }
+
+               @Override
+               public int hashCode()
+               {
+                       final int prime = 31;
+                       int result = 1;
+                       result = prime * result + bit;
+                       result = prime * result + ((wire == null) ? 0 : wire.hashCode());
+                       return result;
+               }
+
+               @Override
+               public boolean equals(Object obj)
+               {
+                       if (this == obj)
+                               return true;
+                       if (obj == null)
+                               return false;
+                       if (getClass() != obj.getClass())
+                               return false;
+                       WireBit other = (WireBit) obj;
+                       if (bit != other.bit)
+                               return false;
+                       if (wire == null)
+                       {
+                               if (other.wire != null)
+                                       return false;
+                       } else if (!wire.equals(other.wire))
+                               return false;
+                       return true;
+               }
        }
 }
\ No newline at end of file