Fusing Wires will no longer initialize Wires with value U
authorFabian Stemmler <stemmler@in.tum.de>
Fri, 21 Jun 2019 17:27:50 +0000 (19:27 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Fri, 21 Jun 2019 17:27:50 +0000 (19:27 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/tests/ComponentTest.java
net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java

index b80b2f3..29bc971 100644 (file)
@@ -101,6 +101,7 @@ class ComponentTest
                rB.feedSignals(Bit.ONE, Bit.ZERO);
                ReadWriteEnd rC = c.createReadWriteEnd();
                rC.feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
+
                t.executeAll();
                assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
                out.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
@@ -123,11 +124,39 @@ class ComponentTest
                t.reset();
                Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 1);
                Wire.fuse(a, b);
-               a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.U, Bit.Z);
+               ReadWriteEnd rw = a.createReadWriteEnd();
+               t.executeAll();
+               assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U);
+
+               rw.feedSignals(Bit.ONE, Bit.U, Bit.Z);
                t.executeAll();
                assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.U, Bit.Z);
        }
 
+       @Test
+       void fusionTest3()
+       {
+               t.reset();
+               Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 1);
+               a.createReadWriteEnd().feedSignals(Bit.Z, Bit.U, Bit.X);
+               t.executeAll();
+               Wire.fuse(a, b);
+               t.executeAll();
+               assertBitArrayEquals(b.getValues(), Bit.Z, Bit.U, Bit.X);
+       }
+
+//     @Test
+//     void connectorTest()
+//     {
+//             t.reset();
+//             Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 1);
+//             new Connector(t, a.createReadWriteEnd(), b.createReadWriteEnd()).connect();
+////           b.createReadWriteEnd();
+//             a.createReadWriteEnd();
+//             t.executeAll();
+//             assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U);
+//     }
+
        @Test
        void triStateBufferTest()
        {
index ec30040..479054a 100644 (file)
@@ -78,7 +78,8 @@ public class Wire
                switch (inputs.size())
                {
                case 0:
-                       return;
+                       setNewValues(BitVector.of(Bit.U, length));
+                       break;
                case 1:
                        recalculateSingleInput();
                        break;
@@ -366,13 +367,14 @@ public class Wire
 
        public class ReadWriteEnd extends ReadEnd
        {
-               private boolean open;
+               private boolean open, isWriting;
                private BitVector inputValues;
 
                ReadWriteEnd()
                {
                        super();
                        open = true;
+                       isWriting = true;
                        initValues();
                        registerInput(this);
                }
@@ -501,6 +503,31 @@ public class Wire
                {
                        return inputValues.toString();
                }
+
+               @Override
+               public void close()
+               {
+                       super.close();
+                       open = false;
+               }
+
+               void setWriting(boolean isWriting)
+               {
+                       if (this.isWriting != isWriting)
+                       {
+                               this.isWriting = isWriting;
+                               if (isWriting)
+                                       inputs.add(this);
+                               else
+                                       inputs.remove(this);
+                               Wire.this.recalculate();
+                       }
+               }
+
+               boolean isWriting()
+               {
+                       return isWriting;
+               }
        }
 
        @Override
@@ -518,31 +545,36 @@ public class Wire
                return inputs;
        }
 
+       // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits
        /**
-        * @formatter:off
-        * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately.
-        * Warning: The bits are permanently fused together.
-        * @formatter:on
-        * @param a The {@link Wire} to be (partially) fused with b
-        * @param b The {@link Wire} to be (partially) fused with a
-        * @param fromA The first bit of {@link Wire} a to be fused
-        * @param fromB The first bit of {@link Wire} b to be fused
+        * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
+        * The bits are permanently fused together.
+        * 
+        * @param a      The {@link Wire} to be (partially) fused with b
+        * @param b      The {@link Wire} to be (partially) fused with a
+        * @param fromA  The first bit of {@link Wire} a to be fused
+        * @param fromB  The first bit of {@link Wire} b to be fused
         * @param length The amount of bits to fuse
         */
-       public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
+       private static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
        {
                ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
+               rA.setWriting(false);
+               rB.setWriting(false);
                rA.setValues(BitVector.of(Bit.Z, a.length));
                rB.setValues(BitVector.of(Bit.Z, b.length));
-               rA.registerObserver(new Fusion(rB, fromA, fromB, length));
-               rB.registerObserver(new Fusion(rA, fromB, fromA, length));
+               Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);
+               rA.registerObserver(aF);
+               rB.registerObserver(bF);
+               aF.update(rA);
+               bF.update(rB);
        }
 
        /**
-        * @formatter:off
-        * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately.
-        * Warning: The bits are permanently fused together.
-        * @formatter:on
+        * 
+        * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
+        * permanently fused together.
+        * 
         * @param a The {@link Wire} to be fused with b
         * @param b The {@link Wire} to be fused with a
         */
@@ -568,10 +600,14 @@ public class Wire
                public void update(LogicObservable initiator)
                {
                        ReadWriteEnd source = (ReadWriteEnd) initiator;
-                       BitVector targetInput = (source.getWire().inputs.size() > 1)
-                                       ? source.wireValuesExcludingMe().subVector(fromSource, fromSource + length)
-                                       : BitVector.of(Bit.Z, length);
-                       target.setValues(fromTarget, targetInput);
+                       if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)
+                               target.setWriting(false);
+                       else
+                       {
+                               target.setWriting(true);
+                               BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);
+                               target.setValues(fromTarget, targetInput);
+                       }
                }
        }
 }
\ No newline at end of file