Merge commit '28314e7a9a3c3ebfcc4db8e9f1875507063ae6e6' into development
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 19 Jun 2019 11:04:01 +0000 (13:04 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 19 Jun 2019 11:04:01 +0000 (13:04 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObserver.java
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 9375004..9fef176 100644 (file)
@@ -1,5 +1,6 @@
 package net.mograsim.logic.core;\r
 \r
+@FunctionalInterface\r
 public interface LogicObserver\r
 {\r
        public void update(LogicObservable initiator);\r
index 0c8a4b8..0505b4c 100644 (file)
@@ -87,6 +87,36 @@ class ComponentTest
                assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
        }\r
 \r
+       @Test\r
+       void fusionTest()\r
+       {\r
+               t.reset();\r
+               Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), out = new Wire(t, 8, 1);\r
+               Wire.fuse(a, out, 0, 0, a.length);\r
+               Wire.fuse(b, out, 0, a.length, b.length);\r
+               Wire.fuse(c, out, 0, a.length + b.length, c.length);\r
+               ReadWriteEnd rA = a.createReadWriteEnd();\r
+               rA.feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+               ReadWriteEnd rB = b.createReadWriteEnd();\r
+               rB.feedSignals(Bit.ONE, Bit.ZERO);\r
+               ReadWriteEnd rC = c.createReadWriteEnd();\r
+               rC.feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);\r
+               t.executeAll();\r
+               assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);\r
+               out.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+               t.executeAll();\r
+               assertBitArrayEquals(rA.getValues(), Bit.X, Bit.X, Bit.X);\r
+               assertBitArrayEquals(rB.getValues(), Bit.X, Bit.X);\r
+               assertBitArrayEquals(rC.getValues(), Bit.X, Bit.X, Bit.X);\r
+               rA.clearSignals();\r
+               rB.clearSignals();\r
+               rC.clearSignals();\r
+               t.executeAll();\r
+               assertBitArrayEquals(rA.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);\r
+               assertBitArrayEquals(rB.getValues(), Bit.ZERO, Bit.ONE);\r
+               assertBitArrayEquals(rC.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);\r
+       }\r
+\r
        @Test\r
        void triStateBufferTest()\r
        {\r
index 6e4e50a..20bcad1 100644 (file)
@@ -419,7 +419,11 @@ public class Wire
                        timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);\r
                }\r
 \r
-               private void setValues(int startingBit, BitVector newValues)\r
+               /**\r
+                * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via\r
+                * feedValues(...) with a delay.\r
+                */\r
+               void setValues(int startingBit, BitVector newValues)\r
                {\r
                        // index check covered in equals\r
                        if (!inputValues.equalsWithOffset(newValues, startingBit))\r
@@ -431,7 +435,11 @@ public class Wire
                        }\r
                }\r
 \r
-               private void setValues(BitVector newValues)\r
+               /**\r
+                * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via\r
+                * feedValues(...) with a delay.\r
+                */\r
+               void setValues(BitVector newValues)\r
                {\r
                        if (inputValues.equals(newValues))\r
                                return;\r
@@ -509,4 +517,58 @@ public class Wire
                        inputs[i] = w[i].createReadWriteEnd();\r
                return inputs;\r
        }\r
+\r
+       /**\r
+        * @formatter:off\r
+        * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately.\r
+        * Warning: The bits are permanently fused together.\r
+        * @formatter:on\r
+        * @param a The {@link Wire} to be (partially) fused with b\r
+        * @param b The {@link Wire} to be (partially) fused with a\r
+        * @param fromA The first bit of {@link Wire} a to be fused\r
+        * @param fromB The first bit of {@link Wire} b to be fused\r
+        * @param length The amount of bits to fuse\r
+        */\r
+       public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)\r
+       {\r
+               ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();\r
+               rA.setValues(BitVector.of(Bit.Z, a.length));\r
+               rB.setValues(BitVector.of(Bit.Z, b.length));\r
+               rA.registerObserver(new Fusion(rB, fromA, fromB, length));\r
+               rB.registerObserver(new Fusion(rA, fromB, fromA, length));\r
+       }\r
+\r
+       /**\r
+        * @formatter:off\r
+        * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately.\r
+        * Warning: The bits are permanently fused together.\r
+        * @formatter:on\r
+        * @param a The {@link Wire} to be fused with b\r
+        * @param b The {@link Wire} to be fused with a\r
+        */\r
+       public static void fuse(Wire a, Wire b)\r
+       {\r
+               fuse(a, b, 0, 0, a.length);\r
+       }\r
+\r
+       private static class Fusion implements LogicObserver\r
+       {\r
+               private ReadWriteEnd target;\r
+               int fromSource, fromTarget, length;\r
+\r
+               public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)\r
+               {\r
+                       this.target = target;\r
+                       this.fromSource = fromSource;\r
+                       this.fromTarget = fromTarget;\r
+                       this.length = length;\r
+               }\r
+\r
+               @Override\r
+               public void update(LogicObservable initiator)\r
+               {\r
+                       ReadWriteEnd read = (ReadWriteEnd) initiator;\r
+                       target.setValues(fromTarget, read.wireValuesExcludingMe().subVector(fromSource, fromSource + length));\r
+               }\r
+       }\r
 }
\ No newline at end of file