1 package era.mi.logic.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
8 import era.mi.logic.Simulation;
9 import era.mi.logic.Util;
10 import era.mi.logic.types.Bit;
13 * Represents an array of wires that can store n bits of information.
15 * @author Fabian Stemmler
21 public final int travelTime;
22 private List<WireObserver> observers = new ArrayList<WireObserver>();
23 public final int length;
24 private List<WireEnd> inputs = new ArrayList<WireEnd>();
26 public Wire(int length, int travelTime)
29 throw new IllegalArgumentException(
30 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
32 this.travelTime = travelTime;
36 private void initValues()
38 values = Bit.U.makeArray(length);
41 private void recalculateSingleInput()
43 WireEnd input = inputs.get(0);
44 if (!Arrays.equals(input.getInputValues(), values))
46 Bit[] oldValues = values.clone();
47 System.arraycopy(input.getInputValues(), 0, values, 0, length);
48 notifyObservers(oldValues);
52 private void recalculateMultipleInputs()
54 Iterator<WireEnd> it = inputs.iterator();
55 Bit[] newValues = it.next().inputValues.clone();
59 WireEnd input = it.next();
60 Bit[] bits = input.getInputValues();
61 for (int i = 0; i < length; i++)
63 newValues[i] = newValues[i].join(bits[i]);
67 if (!Arrays.equals(newValues, values))
69 Bit[] oldValues = values;
71 notifyObservers(oldValues);
75 private void recalculate()
77 switch (inputs.size())
82 recalculateSingleInput();
85 recalculateMultipleInputs();
90 * The {@link Wire} is interpreted as an unsigned integer with n bits.
92 * @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
93 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
95 * @author Fabian Stemmler
97 public boolean hasNumericValue()
101 if (b != Bit.ZERO && b != Bit.ONE)
108 * The {@link Wire} is interpreted as an unsigned integer with n bits.
110 * @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.
112 * @author Fabian Stemmler
114 public long getUnsignedValue()
118 for (int i = 0; i < length; i++)
125 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
138 * The {@link Wire} is interpreted as a signed integer with n bits.
140 * @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.
142 * @author Fabian Stemmler
144 public long getSignedValue()
146 long val = getUnsignedValue();
147 long mask = 1 << (length - 1);
148 if ((mask & val) != 0)
150 int shifts = 64 - length;
151 return (val << shifts) >> shifts;
156 public Bit getValue()
161 public Bit getValue(int index)
163 return values[index];
166 public Bit[] getValues(int start, int end)
168 int length = end - start;
169 Bit[] bits = new Bit[length];
170 System.arraycopy(values, start, bits, 0, length);
174 public Bit[] getValues()
176 return values.clone();
180 * Adds an {@link WireObserver}, who will be notified when the value of the {@link Wire} is updated.
182 * @param ob The {@link WireObserver} to be notified of changes.
183 * @return true if the given {@link WireObserver} was not already registered, false otherwise
185 * @author Fabian Stemmler
187 public boolean addObserver(WireObserver ob)
189 return observers.add(ob);
192 private void notifyObservers(Bit[] oldValues)
194 for (WireObserver o : observers)
195 o.update(this, oldValues);
199 * Create and register a {@link WireEnd} object, which is tied to this {@link Wire}.
201 public WireEnd createEnd()
203 return new WireEnd(false);
207 * Create a {@link WireEnd} object, which is tied to this {@link Wire}. This {@link WireEnd} cannot written to.
209 public WireEnd createReadOnlyEnd()
211 return new WireEnd(true);
214 private void registerInput(WireEnd toRegister)
216 inputs.add(toRegister);
220 * A {@link WireEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
221 * {@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
222 * and 1 turn into X when they are mixed
224 * @author Fabian Stemmler
228 private boolean open;
229 private Bit[] inputValues;
231 private WireEnd(boolean readOnly)
240 private void initValues()
242 inputValues = Bit.U.makeArray(length);
246 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
248 * @param newValues The new values the wires should take on.
250 * @author Fabian Stemmler
252 public void feedSignals(Bit... newValues)
254 if (newValues.length != length)
255 throw new IllegalArgumentException(
256 String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
257 feedSignals(0, newValues);
261 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
263 * @param newValues The new values the wires should take on.
264 * @param startingBit The first index of the subarray of wires.
266 * @author Fabian Stemmler
268 public void feedSignals(int startingBit, Bit... newValues)
271 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
272 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
275 private void setValues(int startingBit, Bit... newValues)
277 int exclLastIndex = startingBit + newValues.length;
278 if (length < exclLastIndex)
279 throw new ArrayIndexOutOfBoundsException(
280 String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit,
281 exclLastIndex - 1, length));
282 if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
284 System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
285 Wire.this.recalculate();
290 * @return The value (of bit 0) the {@link WireEnd} is currently feeding into the associated {@link Wire}.
292 public Bit getInputValue()
294 return getInputValue(0);
298 * @return The value which the {@link WireEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
300 public Bit getInputValue(int index)
302 return inputValues[index];
306 * @return A copy (safe to modify) of the values the {@link WireEnd} is currently feeding into the associated {@link Wire}.
308 public Bit[] getInputValues()
310 return getInputValues(0, length);
313 public Bit[] getInputValues(int start, int end)
315 int length = end - start;
316 Bit[] bits = new Bit[length];
317 System.arraycopy(inputValues, start, bits, 0, length);
322 * {@link WireEnd} now feeds Z into the associated {@link Wire}.
324 public void clearSignals()
326 feedSignals(Bit.Z.makeArray(length));
329 public Bit[] wireValuesExcludingMe()
331 Bit[] bits = Bit.Z.makeArray(length);
332 for (WireEnd wai : inputs)
336 Util.combineInto(bits, wai.getInputValues());
342 * Included for convenient use on {@link Wire}s of length 1.
344 * @return The value of bit 0.
346 * @author Fabian Stemmler
348 public Bit getValue()
350 return Wire.this.getValue();
354 * @param index Index of the requested bit.
355 * @return The value of the indexed bit.
357 * @author Fabian Stemmler
359 public Bit getValue(int index)
361 return Wire.this.getValue(index);
365 * @param index Index of the requested bit.
366 * @return The value of the indexed bit.
368 * @author Fabian Stemmler
370 public Bit[] getValues()
372 return Wire.this.getValues();
376 * @param start Start of the wanted segment. (inclusive)
377 * @param end End of the wanted segment. (exclusive)
378 * @return The values of the segment of {@link Bit}s indexed.
380 * @author Fabian Stemmler
382 public Bit[] getValues(int start, int end)
384 return Wire.this.getValues(start, end);
388 * The {@link Wire} is interpreted as an unsigned integer with n bits.
390 * @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
391 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
393 * @author Fabian Stemmler
395 public boolean hasNumericValue()
397 return Wire.this.hasNumericValue();
401 * The {@link Wire} is interpreted as an unsigned integer with n bits.
403 * @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.
405 * @author Fabian Stemmler
407 public long getUnsignedValue()
409 return Wire.this.getUnsignedValue();
413 * The {@link Wire} is interpreted as a signed integer with n bits.
415 * @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.
417 * @author Fabian Stemmler
419 public long getSignedValue()
421 return Wire.this.getSignedValue();
425 public String toString()
427 return Arrays.toString(inputValues);
428 // return String.format("%s \nFeeding: %s", WireArray.this.toString(), Arrays.toString(inputValues));
442 public boolean addObserver(WireObserver ob)
444 return Wire.this.addObserver(ob);
447 public Wire getWire()
454 public String toString()
456 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
457 // Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
460 public static WireEnd[] extractEnds(Wire[] w)
462 WireEnd[] inputs = new WireEnd[w.length];
463 for (int i = 0; i < w.length; i++)
464 inputs[i] = w[i].createEnd();