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 private BitVector values;
25 public final int travelTime;
26 private List<ReadEnd> attached = new ArrayList<>();
27 public final int length;
28 List<ReadWriteEnd> inputs = new ArrayList<>();
31 public Wire(Timeline timeline, int length, int travelTime)
34 throw new IllegalArgumentException(
35 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
36 this.timeline = timeline;
38 this.travelTime = travelTime;
42 private void initValues()
44 values = U.toVector(length);
47 private void recalculateSingleInput()
49 setNewValues(inputs.get(0).getInputValues());
52 private void recalculateMultipleInputs()
54 BitVectorMutator mutator = BitVectorMutator.empty();
55 for (ReadWriteEnd wireArrayEnd : inputs)
56 mutator.join(wireArrayEnd.getInputValues());
57 setNewValues(mutator.get());
60 private void setNewValues(BitVector newValues)
62 if (values.equals(newValues))
64 // BitVector oldValues = values;
71 switch (inputs.size())
76 recalculateSingleInput();
79 recalculateMultipleInputs();
84 * The {@link Wire} is interpreted as an unsigned integer with n bits.
86 * @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
87 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
89 * @author Fabian Stemmler
91 public boolean hasNumericValue()
95 if (b != Bit.ZERO && b != Bit.ONE)
102 * The {@link Wire} is interpreted as an unsigned integer with n bits.
104 * @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.
106 * @author Fabian Stemmler
108 public long getUnsignedValue()
112 for (Bit bit : values)
119 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
131 * The {@link Wire} is interpreted as a signed integer with n bits.
133 * @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.
135 * @author Fabian Stemmler
137 public long getSignedValue()
139 long val = getUnsignedValue();
140 long mask = 1 << (length - 1);
141 if ((mask & val) != 0)
143 int shifts = 64 - length;
144 return (val << shifts) >> shifts;
149 public Bit getValue()
154 public Bit getValue(int index)
156 return values.getBit(index);
159 public BitVector getValues(int start, int end)
161 return values.subVector(start, end);
164 public BitVector getValues()
170 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
172 * @param ob The {@link LogicObserver} to be notified of changes.
173 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
175 * @author Fabian Stemmler
177 void attachEnd(ReadEnd end)
182 void detachEnd(ReadEnd end)
184 attached.remove(end);
187 private void notifyObservers()
189 attached.forEach(r -> r.update());
193 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
195 public ReadWriteEnd createReadWriteEnd()
197 return new ReadWriteEnd();
201 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
203 public ReadEnd createReadOnlyEnd()
205 return new ReadEnd();
208 void registerInput(ReadWriteEnd toRegister)
210 inputs.add(toRegister);
214 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
215 * {@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
216 * and 1 turn into X when they are mixed
218 * @author Fabian Stemmler
220 public class ReadEnd implements LogicObservable
222 private List<LogicObserver> observers = new ArrayList<>();
227 Wire.this.attachEnd(this);
236 * Included for convenient use on {@link Wire}s of length 1.
238 * @return The value of bit 0.
240 * @author Fabian Stemmler
242 public Bit getValue()
244 return Wire.this.getValue();
248 * @param index Index of the requested bit.
249 * @return The value of the indexed bit.
251 * @author Fabian Stemmler
253 public Bit getValue(int index)
255 return Wire.this.getValue(index);
259 * @param index Index of the requested bit.
260 * @return The value of the indexed bit.
262 * @author Fabian Stemmler
264 public BitVector getValues()
266 return Wire.this.getValues();
270 * @param start Start of the wanted segment. (inclusive)
271 * @param end End of the wanted segment. (exclusive)
272 * @return The values of the segment of {@link Bit}s indexed.
274 * @author Fabian Stemmler
276 public BitVector getValues(int start, int end)
278 return Wire.this.getValues(start, end);
282 * The {@link Wire} is interpreted as an unsigned integer with n bits.
284 * @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
285 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
287 * @author Fabian Stemmler
289 public boolean hasNumericValue()
291 return Wire.this.hasNumericValue();
295 * The {@link Wire} is interpreted as an unsigned integer with n bits.
297 * @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.
299 * @author Fabian Stemmler
301 public long getUnsignedValue()
303 return Wire.this.getUnsignedValue();
307 * The {@link Wire} is interpreted as a signed integer with n bits.
309 * @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.
311 * @author Fabian Stemmler
313 public long getSignedValue()
315 return Wire.this.getSignedValue();
319 public String toString()
321 return Wire.this.toString();
336 public Wire getWire()
342 public void registerObserver(LogicObserver ob)
348 public void deregisterObserver(LogicObserver ob)
350 observers.remove(ob);
354 public void notifyObservers()
356 observers.forEach(ob -> ob.update(this));
360 public class ReadWriteEnd extends ReadEnd
362 private boolean open;
363 private BitVector inputValues;
373 private void initValues()
375 inputValues = U.toVector(length);
379 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
381 * @param newValues The new values the wires should take on.
383 * @author Fabian Stemmler
385 public void feedSignals(Bit... newValues)
387 feedSignals(BitVector.of(newValues));
390 public void feedSignals(BitVector newValues)
392 if (newValues.length() != length)
393 throw new IllegalArgumentException(
394 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
396 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
397 timeline.addEvent(e -> setValues(newValues), travelTime);
401 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
403 * @param bitVector The new values the wires should take on.
404 * @param startingBit The first index of the subarray of wires.
406 * @author Fabian Stemmler
408 public void feedSignals(int startingBit, BitVector bitVector)
411 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
412 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
415 private void setValues(int startingBit, BitVector newValues)
417 // index check covered in equals
418 if (!inputValues.equalsWithOffset(newValues, startingBit))
420 Bit[] vals = inputValues.getBits();
421 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
422 inputValues = BitVector.of(vals);
423 Wire.this.recalculate();
427 private void setValues(BitVector newValues)
429 if (inputValues.equals(newValues))
431 inputValues = newValues;
432 Wire.this.recalculate();
436 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
438 public Bit getInputValue()
440 return getInputValue(0);
444 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
446 public Bit getInputValue(int index)
448 return inputValues.getBit(index);
452 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
454 public BitVector getInputValues()
456 return getInputValues(0, length);
459 public BitVector getInputValues(int start, int end)
461 return inputValues.subVector(start, end);
465 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
467 public void clearSignals()
469 feedSignals(Z.toVector(length));
472 public BitVector wireValuesExcludingMe()
474 BitVectorMutator mutator = BitVectorMutator.empty();
475 for (ReadWriteEnd wireEnd : inputs)
479 mutator.join(wireEnd.inputValues);
481 return mutator.get();
485 public String toString()
487 return inputValues.toString();
492 public String toString()
494 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);
497 public static ReadEnd[] extractEnds(Wire[] w)
499 ReadEnd[] inputs = new ReadEnd[w.length];
500 for (int i = 0; i < w.length; i++)
501 inputs[i] = w[i].createReadWriteEnd();