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<ReadEnd>();
27 public final int length;
28 private List<ReadWriteEnd> inputs = new ArrayList<ReadWriteEnd>();
29 private Timeline timeline;
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;
66 notifyObservers(oldValues);
69 private void recalculate()
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 private void attachEnd(ReadEnd end)
182 private void detachEnd(ReadEnd end)
184 attached.remove(end);
187 private void notifyObservers(BitVector oldValues)
189 for (ReadEnd o : attached)
194 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
196 public ReadWriteEnd createReadWriteEnd()
198 return new ReadWriteEnd();
202 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
204 public ReadEnd createReadOnlyEnd()
206 return new ReadEnd();
209 private void registerInput(ReadWriteEnd toRegister)
211 inputs.add(toRegister);
215 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
216 * {@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
217 * and 1 turn into X when they are mixed
219 * @author Fabian Stemmler
221 public class ReadEnd implements LogicObservable
223 private List<LogicObserver> observers = new ArrayList<LogicObserver>();
228 Wire.this.attachEnd(this);
231 public void update(BitVector oldValues)
237 * Included for convenient use on {@link Wire}s of length 1.
239 * @return The value of bit 0.
241 * @author Fabian Stemmler
243 public Bit getValue()
245 return Wire.this.getValue();
249 * @param index Index of the requested bit.
250 * @return The value of the indexed bit.
252 * @author Fabian Stemmler
254 public Bit getValue(int index)
256 return Wire.this.getValue(index);
260 * @param index Index of the requested bit.
261 * @return The value of the indexed bit.
263 * @author Fabian Stemmler
265 public BitVector getValues()
267 return Wire.this.getValues();
271 * @param start Start of the wanted segment. (inclusive)
272 * @param end End of the wanted segment. (exclusive)
273 * @return The values of the segment of {@link Bit}s indexed.
275 * @author Fabian Stemmler
277 public BitVector getValues(int start, int end)
279 return Wire.this.getValues(start, end);
283 * The {@link Wire} is interpreted as an unsigned integer with n bits.
285 * @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
286 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
288 * @author Fabian Stemmler
290 public boolean hasNumericValue()
292 return Wire.this.hasNumericValue();
296 * The {@link Wire} is interpreted as an unsigned integer with n bits.
298 * @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.
300 * @author Fabian Stemmler
302 public long getUnsignedValue()
304 return Wire.this.getUnsignedValue();
308 * The {@link Wire} is interpreted as a signed integer with n bits.
310 * @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.
312 * @author Fabian Stemmler
314 public long getSignedValue()
316 return Wire.this.getSignedValue();
320 public String toString()
322 return Wire.this.toString();
337 public Wire getWire()
343 public void registerObserver(LogicObserver ob)
349 public void notifyObservers()
351 for (LogicObserver ob : observers)
356 public class ReadWriteEnd extends ReadEnd
358 private boolean open;
359 private BitVector inputValues;
361 private ReadWriteEnd()
369 private void initValues()
371 inputValues = U.toVector(length);
375 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
377 * @param newValues The new values the wires should take on.
379 * @author Fabian Stemmler
381 public void feedSignals(Bit... newValues)
383 feedSignals(BitVector.of(newValues));
386 public void feedSignals(BitVector newValues)
388 if (newValues.length() != length)
389 throw new IllegalArgumentException(
390 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
392 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
393 timeline.addEvent(e -> setValues(newValues), travelTime);
397 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
399 * @param bitVector The new values the wires should take on.
400 * @param startingBit The first index of the subarray of wires.
402 * @author Fabian Stemmler
404 public void feedSignals(int startingBit, BitVector bitVector)
407 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
408 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
411 private void setValues(int startingBit, BitVector newValues)
413 // index check covered in equals
414 if (!inputValues.equalsWithOffset(newValues, startingBit))
416 Bit[] vals = inputValues.getBits();
417 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
418 inputValues = BitVector.of(vals);
419 Wire.this.recalculate();
423 private void setValues(BitVector newValues)
425 if (inputValues.equals(newValues))
427 inputValues = newValues;
428 Wire.this.recalculate();
432 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
434 public Bit getInputValue()
436 return getInputValue(0);
440 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
442 public Bit getInputValue(int index)
444 return inputValues.getBit(index);
448 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
450 public BitVector getInputValues()
452 return getInputValues(0, length);
455 public BitVector getInputValues(int start, int end)
457 return inputValues.subVector(start, end);
461 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
463 public void clearSignals()
465 feedSignals(Z.toVector(length));
468 public BitVector wireValuesExcludingMe()
470 BitVectorMutator mutator = BitVectorMutator.empty();
471 for (ReadWriteEnd wireEnd : inputs)
475 mutator.join(wireEnd.inputValues);
477 return mutator.get();
481 public String toString()
483 return inputValues.toString();
488 public String toString()
490 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);
493 public static ReadEnd[] extractEnds(Wire[] w)
495 ReadEnd[] inputs = new ReadEnd[w.length];
496 for (int i = 0; i < w.length; i++)
497 inputs[i] = w[i].createReadWriteEnd();