+\r
+ // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits\r
+ /**\r
+ * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:\r
+ * The bits are permanently fused together.\r
+ * \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.setWriting(false);\r
+ rB.setWriting(false);\r
+ rA.setValues(BitVector.of(Bit.Z, a.length));\r
+ rB.setValues(BitVector.of(Bit.Z, b.length));\r
+ Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);\r
+ rA.registerObserver(aF);\r
+ rB.registerObserver(bF);\r
+ aF.update(rA);\r
+ bF.update(rB);\r
+ }\r
+\r
+ /**\r
+ * \r
+ * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are\r
+ * permanently fused together.\r
+ * \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 source = (ReadWriteEnd) initiator;\r
+ if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)\r
+ target.setWriting(false);\r
+ else\r
+ {\r
+ target.setWriting(true);\r
+ BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);\r
+ target.setValues(fromTarget, targetInput);\r
+ }\r
+ }\r
+ }\r