*/\r
public class Wire\r
{\r
+ public final String name;\r
private BitVector values;\r
public final int travelTime;\r
private List<ReadEnd> attached = new ArrayList<>();\r
Timeline timeline;\r
\r
public Wire(Timeline timeline, int length, int travelTime)\r
+ {\r
+ this(timeline, length, travelTime, null);\r
+ }\r
+\r
+ public Wire(Timeline timeline, int length, int travelTime, String name)\r
{\r
if (length < 1)\r
throw new IllegalArgumentException(\r
String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));\r
+ this.name = name;\r
this.timeline = timeline;\r
this.length = length;\r
this.travelTime = travelTime;\r
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
}\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
@Override\r
public String toString()\r
{\r
- return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);\r
+ String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;\r
+ return String.format("wire %s value: %s inputs: %s", name, values, inputs);\r
}\r
\r
public static ReadEnd[] extractEnds(Wire[] w)\r
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 source = (ReadWriteEnd) initiator;\r
+ BitVector targetInput = (source.getWire().inputs.size() > 1)\r
+ ? source.wireValuesExcludingMe().subVector(fromSource, fromSource + length)\r
+ : BitVector.of(Bit.Z, length);\r
+ target.setValues(fromTarget, targetInput);\r
+ }\r
+ }\r
}
\ No newline at end of file