1 package net.mograsim.logic.core.wires;
\r
3 import static net.mograsim.logic.core.types.Bit.U;
\r
4 import static net.mograsim.logic.core.types.Bit.Z;
\r
6 import java.util.ArrayList;
\r
7 import java.util.List;
\r
9 import net.mograsim.logic.core.LogicObservable;
\r
10 import net.mograsim.logic.core.LogicObserver;
\r
11 import net.mograsim.logic.core.timeline.Timeline;
\r
12 import net.mograsim.logic.core.types.Bit;
\r
13 import net.mograsim.logic.core.types.BitVector;
\r
14 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
\r
17 * Represents an array of wires that can store n bits of information.
\r
19 * @author Fabian Stemmler
\r
24 public final String name;
\r
25 private BitVector values;
\r
26 public final int travelTime;
\r
27 private List<ReadEnd> attached = new ArrayList<>();
\r
28 public final int length;
\r
29 List<ReadWriteEnd> inputs = new ArrayList<>();
\r
32 public Wire(Timeline timeline, int length, int travelTime)
\r
34 this(timeline, length, travelTime, null);
\r
37 public Wire(Timeline timeline, int length, int travelTime, String name)
\r
40 throw new IllegalArgumentException(
\r
41 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
\r
43 this.timeline = timeline;
\r
44 this.length = length;
\r
45 this.travelTime = travelTime;
\r
49 private void initValues()
\r
51 values = U.toVector(length);
\r
54 private void recalculateSingleInput()
\r
56 setNewValues(inputs.get(0).getInputValues());
\r
59 private void recalculateMultipleInputs()
\r
61 BitVectorMutator mutator = BitVectorMutator.empty();
\r
62 for (ReadWriteEnd wireArrayEnd : inputs)
\r
63 mutator.join(wireArrayEnd.getInputValues());
\r
64 setNewValues(mutator.get());
\r
67 private void setNewValues(BitVector newValues)
\r
69 if (values.equals(newValues))
\r
71 // BitVector oldValues = values;
\r
78 switch (inputs.size())
\r
81 setNewValues(BitVector.of(Bit.U, length));
\r
84 recalculateSingleInput();
\r
87 recalculateMultipleInputs();
\r
92 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
94 * @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
\r
95 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
97 * @author Fabian Stemmler
\r
99 public boolean hasNumericValue()
\r
101 for (Bit b : values)
\r
103 if (b != Bit.ZERO && b != Bit.ONE)
\r
110 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
112 * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
\r
114 * @author Fabian Stemmler
\r
116 public long getUnsignedValue()
\r
120 for (Bit bit : values)
\r
127 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
\r
139 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
141 * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
\r
143 * @author Fabian Stemmler
\r
145 public long getSignedValue()
\r
147 long val = getUnsignedValue();
\r
148 long mask = 1 << (length - 1);
\r
149 if ((mask & val) != 0)
\r
151 int shifts = 64 - length;
\r
152 return (val << shifts) >> shifts;
\r
157 public Bit getValue()
\r
159 return getValue(0);
\r
162 public Bit getValue(int index)
\r
164 return values.getBit(index);
\r
167 public BitVector getValues(int start, int end)
\r
169 return values.subVector(start, end);
\r
172 public BitVector getValues()
\r
178 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
\r
180 * @param ob The {@link LogicObserver} to be notified of changes.
\r
181 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
\r
183 * @author Fabian Stemmler
\r
185 void attachEnd(ReadEnd end)
\r
190 void detachEnd(ReadEnd end)
\r
192 attached.remove(end);
\r
195 private void notifyObservers()
\r
197 attached.forEach(r -> r.update());
\r
201 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
\r
203 public ReadWriteEnd createReadWriteEnd()
\r
205 return new ReadWriteEnd();
\r
209 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
\r
211 public ReadEnd createReadOnlyEnd()
\r
213 return new ReadEnd();
\r
216 void registerInput(ReadWriteEnd toRegister)
\r
218 inputs.add(toRegister);
\r
222 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
\r
223 * {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than Z 0
\r
224 * and 1 turn into X when they are mixed
\r
226 * @author Fabian Stemmler
\r
228 public class ReadEnd implements LogicObservable
\r
230 private List<LogicObserver> observers = new ArrayList<>();
\r
235 Wire.this.attachEnd(this);
\r
238 public void update()
\r
244 * Included for convenient use on {@link Wire}s of length 1.
\r
246 * @return The value of bit 0.
\r
248 * @author Fabian Stemmler
\r
250 public Bit getValue()
\r
252 return Wire.this.getValue();
\r
256 * @param index Index of the requested bit.
\r
257 * @return The value of the indexed bit.
\r
259 * @author Fabian Stemmler
\r
261 public Bit getValue(int index)
\r
263 return Wire.this.getValue(index);
\r
267 * @param index Index of the requested bit.
\r
268 * @return The value of the indexed bit.
\r
270 * @author Fabian Stemmler
\r
272 public BitVector getValues()
\r
274 return Wire.this.getValues();
\r
278 * @param start Start of the wanted segment. (inclusive)
\r
279 * @param end End of the wanted segment. (exclusive)
\r
280 * @return The values of the segment of {@link Bit}s indexed.
\r
282 * @author Fabian Stemmler
\r
284 public BitVector getValues(int start, int end)
\r
286 return Wire.this.getValues(start, end);
\r
290 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
292 * @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
\r
293 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
295 * @author Fabian Stemmler
\r
297 public boolean hasNumericValue()
\r
299 return Wire.this.hasNumericValue();
\r
303 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
305 * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
\r
307 * @author Fabian Stemmler
\r
309 public long getUnsignedValue()
\r
311 return Wire.this.getUnsignedValue();
\r
315 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
317 * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
\r
319 * @author Fabian Stemmler
\r
321 public long getSignedValue()
\r
323 return Wire.this.getSignedValue();
\r
327 public String toString()
\r
329 return Wire.this.toString();
\r
332 public void close()
\r
334 inputs.remove(this);
\r
339 public int length()
\r
344 public Wire getWire()
\r
350 public void registerObserver(LogicObserver ob)
\r
356 public void deregisterObserver(LogicObserver ob)
\r
358 observers.remove(ob);
\r
362 public void notifyObservers()
\r
364 observers.forEach(ob -> ob.update(this));
\r
368 public class ReadWriteEnd extends ReadEnd
\r
370 private boolean open, isWriting;
\r
371 private BitVector inputValues;
\r
379 registerInput(this);
\r
382 private void initValues()
\r
384 inputValues = U.toVector(length);
\r
388 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
\r
390 * @param newValues The new values the wires should take on.
\r
392 * @author Fabian Stemmler
\r
394 public void feedSignals(Bit... newValues)
\r
396 feedSignals(BitVector.of(newValues));
\r
399 public void feedSignals(BitVector newValues)
\r
401 if (newValues.length() != length)
\r
402 throw new IllegalArgumentException(
\r
403 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
\r
405 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
406 timeline.addEvent(e -> setValues(newValues), travelTime);
\r
410 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
\r
412 * @param bitVector The new values the wires should take on.
\r
413 * @param startingBit The first index of the subarray of wires.
\r
415 * @author Fabian Stemmler
\r
417 public void feedSignals(int startingBit, BitVector bitVector)
\r
420 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
421 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
\r
425 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
\r
426 * feedValues(...) with a delay.
\r
428 void setValues(int startingBit, BitVector newValues)
\r
430 // index check covered in equals
\r
431 if (!inputValues.equalsWithOffset(newValues, startingBit))
\r
433 Bit[] vals = inputValues.getBits();
\r
434 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
\r
435 inputValues = BitVector.of(vals);
\r
436 Wire.this.recalculate();
\r
441 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
\r
442 * feedValues(...) with a delay.
\r
444 void setValues(BitVector newValues)
\r
446 if (inputValues.equals(newValues))
\r
448 inputValues = newValues;
\r
449 Wire.this.recalculate();
\r
453 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
455 public Bit getInputValue()
\r
457 return getInputValue(0);
\r
461 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
\r
463 public Bit getInputValue(int index)
\r
465 return inputValues.getBit(index);
\r
469 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
471 public BitVector getInputValues()
\r
473 return getInputValues(0, length);
\r
476 public BitVector getInputValues(int start, int end)
\r
478 return inputValues.subVector(start, end);
\r
482 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
\r
484 public void clearSignals()
\r
486 feedSignals(Z.toVector(length));
\r
489 public BitVector wireValuesExcludingMe()
\r
491 BitVectorMutator mutator = BitVectorMutator.empty();
\r
492 for (ReadWriteEnd wireEnd : inputs)
\r
494 if (wireEnd == this)
\r
496 mutator.join(wireEnd.inputValues);
\r
498 return mutator.get();
\r
502 public String toString()
\r
504 return inputValues.toString();
\r
508 public void close()
\r
514 void setWriting(boolean isWriting)
\r
516 if (this.isWriting != isWriting)
\r
518 this.isWriting = isWriting;
\r
522 inputs.remove(this);
\r
523 Wire.this.recalculate();
\r
527 boolean isWriting()
\r
534 public String toString()
\r
536 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
\r
537 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
\r
540 public static ReadEnd[] extractEnds(Wire[] w)
\r
542 ReadEnd[] inputs = new ReadEnd[w.length];
\r
543 for (int i = 0; i < w.length; i++)
\r
544 inputs[i] = w[i].createReadWriteEnd();
\r
548 // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits
\r
550 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
\r
551 * The bits are permanently fused together.
\r
553 * @param a The {@link Wire} to be (partially) fused with b
\r
554 * @param b The {@link Wire} to be (partially) fused with a
\r
555 * @param fromA The first bit of {@link Wire} a to be fused
\r
556 * @param fromB The first bit of {@link Wire} b to be fused
\r
557 * @param length The amount of bits to fuse
\r
559 public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
\r
561 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
\r
562 rA.setWriting(false);
\r
563 rB.setWriting(false);
\r
564 rA.setValues(BitVector.of(Bit.Z, a.length));
\r
565 rB.setValues(BitVector.of(Bit.Z, b.length));
\r
566 Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);
\r
567 rA.registerObserver(aF);
\r
568 rB.registerObserver(bF);
\r
575 * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
\r
576 * permanently fused together.
\r
578 * @param a The {@link Wire} to be fused with b
\r
579 * @param b The {@link Wire} to be fused with a
\r
581 public static void fuse(Wire a, Wire b)
\r
583 fuse(a, b, 0, 0, a.length);
\r
586 private static class Fusion implements LogicObserver
\r
588 private ReadWriteEnd target;
\r
589 int fromSource, fromTarget, length;
\r
591 public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)
\r
593 this.target = target;
\r
594 this.fromSource = fromSource;
\r
595 this.fromTarget = fromTarget;
\r
596 this.length = length;
\r
600 public void update(LogicObservable initiator)
\r
602 ReadWriteEnd source = (ReadWriteEnd) initiator;
\r
603 if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)
\r
604 target.setWriting(false);
\r
607 target.setWriting(true);
\r
608 BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);
\r
609 target.setValues(fromTarget, targetInput);
\r