1 package net.mograsim.logic.core.wires;
3 import static net.mograsim.logic.core.types.Bit.U;
4 import static net.mograsim.logic.core.types.Bit.Z;
6 import java.util.ArrayList;
9 import net.mograsim.logic.core.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.timeline.Timeline;
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVector;
14 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
17 * Represents an array of wires that can store n bits of information.
19 * @author Fabian Stemmler
24 public final String name;
25 private BitVector values;
26 public final int travelTime;
27 private List<ReadEnd> attached = new ArrayList<>();
28 public final int width;
29 List<ReadWriteEnd> inputs = new ArrayList<>();
32 public Wire(Timeline timeline, int width, int travelTime)
34 this(timeline, width, travelTime, null);
37 public Wire(Timeline timeline, int width, int travelTime, String name)
40 throw new IllegalArgumentException(
41 String.format("Tried to create an array of wires with width %d, but a width of less than 1 makes no sense.", width));
43 this.timeline = timeline;
45 this.travelTime = travelTime;
49 private void initValues()
51 values = U.toVector(width);
54 private void setNewValues(BitVector newValues)
63 setNewValues(U.toVector(width));
66 BitVectorMutator mutator = BitVectorMutator.empty();
67 for (ReadWriteEnd wireArrayEnd : inputs)
68 mutator.join(wireArrayEnd.getInputValues());
69 setNewValues(mutator.toBitVector());
74 * Forces a Wire to take on specific values. If the new values differ from the old ones, the observers of the Wire will be notified.
75 * WARNING! Use this with care! The preferred way of writing the values is ReadWriteEnd.feedSignals(BitVector)
77 * @param values The values the <code>Wire</code> will have immediately after this method is called
79 public void forceValues(BitVector values)
85 * The {@link Wire} is interpreted as an unsigned integer with n bits.
87 * @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
88 * value), not <code>Bit.U</code>, <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
90 * @author Fabian Stemmler
92 public boolean hasNumericValue()
94 return values.isBinary();
98 * The {@link Wire} is interpreted as an unsigned integer with n bits.
100 * @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.
102 * @author Fabian Stemmler
104 public long getUnsignedValue()
108 for (Bit bit : values)
115 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
127 * The {@link Wire} is interpreted as a signed integer with n bits.
129 * @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.
131 * @author Fabian Stemmler
133 public long getSignedValue()
135 long val = getUnsignedValue();
136 long mask = 1 << (width - 1);
137 if ((mask & val) != 0)
139 int shifts = 64 - width;
140 return (val << shifts) >> shifts;
146 * Returns the least significant bit (LSB)
148 public Bit getValue()
154 * Returns the least significant bit (LSB) of the given index
156 public Bit getValue(int index)
158 return values.getLSBit(index);
161 public BitVector getValues(int start, int end)
163 return values.subVector(start, end);
166 public BitVector getValues()
172 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
174 * @param ob The {@link LogicObserver} to be notified of changes.
175 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
177 * @author Fabian Stemmler
179 boolean attachEnd(ReadEnd end)
181 return attached.add(end);
184 void detachEnd(ReadEnd end)
186 attached.remove(end);
189 private void notifyObservers()
191 attached.forEach(ReadEnd::update);
195 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
197 public ReadWriteEnd createReadWriteEnd()
199 return new ReadWriteEnd();
203 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
205 public ReadEnd createReadOnlyEnd()
207 return new ReadEnd();
210 void registerInput(ReadWriteEnd toRegister)
212 inputs.add(toRegister);
217 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
218 * {@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
219 * and 1 turn into X when they are mixed
221 * @author Fabian Stemmler
223 public class ReadEnd implements LogicObservable
225 private List<LogicObserver> observers = new ArrayList<>();
230 Wire.this.attachEnd(this);
239 * Included for convenient use on {@link Wire}s of width 1.
241 * @return The value of bit 0.
243 * @author Fabian Stemmler
245 public Bit getValue()
247 return Wire.this.getValue();
251 * @param index Index of the requested bit.
252 * @return The value of the indexed bit.
254 * @author Fabian Stemmler
256 public Bit getValue(int index)
258 return Wire.this.getValue(index);
261 public BitVector getValues()
263 return Wire.this.getValues();
267 * @param start Start of the wanted segment. (inclusive)
268 * @param end End of the wanted segment. (exclusive)
269 * @return The values of the segment of {@link Bit}s indexed.
271 * @author Fabian Stemmler
273 public BitVector getValues(int start, int end)
275 return Wire.this.getValues(start, end);
279 * The {@link Wire} is interpreted as an unsigned integer with n bits.
281 * @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
282 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
284 * @author Fabian Stemmler
286 public boolean hasNumericValue()
288 return Wire.this.hasNumericValue();
292 * The {@link Wire} is interpreted as an unsigned integer with n bits.
294 * @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.
296 * @author Fabian Stemmler
298 public long getUnsignedValue()
300 return Wire.this.getUnsignedValue();
304 * The {@link Wire} is interpreted as a signed integer with n bits.
306 * @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.
308 * @author Fabian Stemmler
310 public long getSignedValue()
312 return Wire.this.getSignedValue();
316 public String toString()
318 return Wire.this.toString();
333 public Wire getWire()
339 public void registerObserver(LogicObserver ob)
345 public void deregisterObserver(LogicObserver ob)
347 observers.remove(ob);
350 // void registerCloseObserver(LogicObserver ob)
352 // closeObserver.add(ob);
355 // void deregisterCloseObserver(LogicObserver ob)
357 // closeObserver.remove(ob);
361 public void notifyObservers()
363 observers.forEach(ob -> ob.update(this));
367 public class ReadWriteEnd extends ReadEnd
369 private boolean open;
370 private boolean isWriting;
371 private BitVector inputValues;
382 private void initValues()
384 inputValues = U.toVector(width);
388 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
390 * @param newValues The new values the wires should take on.
392 * @author Fabian Stemmler
394 public void feedSignals(Bit... newValues)
396 feedSignals(BitVector.of(newValues));
399 public void feedSignals(BitVector newValues)
401 if (newValues.length() != width)
402 throw new IllegalArgumentException(
403 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), width));
405 throw new IllegalStateException("Attempted to write to closed WireArrayEnd.");
406 timeline.addEvent(e -> setValues(newValues), travelTime);
410 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
412 * @param bitVector The new values the wires should take on.
413 * @param startingBit The first index of the subarray of wires.
415 * @author Fabian Stemmler
417 public void feedSignals(int startingBit, BitVector bitVector)
420 throw new IllegalStateException("Attempted to write to closed WireArrayEnd.");
421 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
425 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
426 * feedValues(...) with a delay.
428 void setValues(int startingBit, BitVector newValues)
430 // index check covered in equals
431 if (!inputValues.equalsWithOffset(newValues, startingBit))
433 Bit[] vals = inputValues.getBits();
434 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
435 inputValues = BitVector.of(vals);
436 Wire.this.recalculate();
441 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
442 * feedValues(...) with a delay.
444 void setValues(BitVector newValues)
446 if (inputValues.equals(newValues))
448 inputValues = newValues;
449 Wire.this.recalculate();
453 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
454 * significant bit (LSB)
456 public Bit getInputValue()
458 return getInputValue(0);
462 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
463 * Returns the least significant bit (LSB)
466 public Bit getInputValue(int index)
468 return inputValues.getLSBit(index);
472 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
474 public BitVector getInputValues()
479 public BitVector getInputValues(int start, int end)
481 return inputValues.subVector(start, end);
485 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
487 public void clearSignals()
489 feedSignals(Z.toVector(width));
492 public BitVector wireValuesExcludingMe()
494 BitVectorMutator mutator = BitVectorMutator.empty();
495 boolean modified = false;
496 for (ReadWriteEnd wireEnd : inputs)
501 mutator.join(wireEnd.inputValues);
504 mutator.join(BitVector.of(Bit.Z, width));
505 return mutator.toBitVector();
509 public String toString()
511 return inputValues.toString();
521 void setWriting(boolean isWriting)
523 if (this.isWriting != isWriting)
525 this.isWriting = isWriting;
530 Wire.this.recalculate();
541 public String toString()
543 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
544 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
547 public static ReadEnd[] extractEnds(Wire[] w)
549 ReadEnd[] inputs = new ReadEnd[w.length];
550 for (int i = 0; i < w.length; i++)
551 inputs[i] = w[i].createReadWriteEnd();
557 * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
558 * permanently fused together.
560 * @param a The {@link Wire} to be fused with b
561 * @param b The {@link Wire} to be fused with a
563 public static void fuse(Wire a, Wire b)
565 fuse(a, b, 0, 0, a.width);
569 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
570 * The bits are permanently fused together.
572 * @param a The {@link Wire} to be (partially) fused with b
573 * @param b The {@link Wire} to be (partially) fused with a
574 * @param fromA The first bit of {@link Wire} a to be fused
575 * @param fromB The first bit of {@link Wire} b to be fused
576 * @param width The amount of bits to fuse
578 public static void fuse(Wire a, Wire b, int fromA, int fromB, int width)
580 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
581 rA.registerObserver(x -> rB.feedSignals(fromB, rA.wireValuesExcludingMe().subVector(fromA, fromA + width)));
582 rB.registerObserver(x -> rA.feedSignals(fromA, rB.wireValuesExcludingMe().subVector(fromB, fromB + width)));
584 rA.setValues(0, BitVector.of(Bit.Z, fromA));
585 rB.setValues(0, BitVector.of(Bit.Z, fromB));
586 rA.setValues(fromA + width, BitVector.of(Bit.Z, a.width - width - fromA));
587 rB.setValues(fromB + width, BitVector.of(Bit.Z, b.width - width - fromB));
589 rA.notifyObservers();
590 rB.notifyObservers();