1 package era.mi.logic.wires;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Arrays;
\r
5 import java.util.Iterator;
\r
6 import java.util.List;
\r
8 import era.mi.logic.Bit;
\r
9 import era.mi.logic.Simulation;
\r
10 import era.mi.logic.Util;
\r
13 * Represents an array of wires that can store n bits of information.
\r
15 * @author Fabian Stemmler
\r
18 public class WireArray
\r
20 private Bit[] values;
\r
21 public final int travelTime;
\r
22 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
\r
23 public final int length;
\r
24 private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
\r
26 public WireArray(int length, int travelTime)
\r
29 throw new IllegalArgumentException(String.format("Tried to create an array of wires with length %o, but a length of less than 1 makes no sense.", length));
\r
30 this.length = length;
\r
31 this.travelTime = travelTime;
\r
35 private void initValues()
\r
37 values = Bit.Z.makeArray(length);
\r
40 private void recalculateSingleInput()
\r
42 WireArrayInput input = inputs.get(0);
\r
43 if (!Arrays.equals(input.getValues(), values))
\r
45 System.arraycopy(input.getValues(), 0, values, 0, length);
\r
50 private void recalculateMultipleInputs()
\r
52 Iterator<WireArrayInput> it = inputs.iterator();
\r
53 Bit[] newValues = it.next().inputValues.clone();
\r
55 while (it.hasNext())
\r
57 WireArrayInput input = it.next();
\r
58 Bit[] bits = input.getValues();
\r
59 for (int i = 0; i < length; i++)
\r
61 if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
\r
63 else if (Bit.Z.equals(newValues[i]))
\r
64 newValues[i] = bits[i];
\r
66 newValues[i] = Bit.X;
\r
70 if (!Arrays.equals(newValues, values))
\r
77 private void recalculate()
\r
79 switch (inputs.size())
\r
84 recalculateSingleInput();
\r
87 recalculateMultipleInputs();
\r
92 * The WireArray 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
\r
95 * <code>Bit.ZERO</code> (they do not all have to have the same value),
\r
96 * not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is
\r
97 * returned otherwise.
\r
99 * @author Fabian Stemmler
\r
101 public boolean hasNumericValue()
\r
103 for (Bit b : values)
\r
105 if (b != Bit.ZERO && b != Bit.ONE)
\r
112 * The WireArray is interpreted as an unsigned integer with n bits.
\r
114 * @return The unsigned value of the {@link WireArray}'s bits, where value 0
\r
115 * corresponds with 2^0, value 1 is 2^1 and so on.
\r
117 * @author Fabian Stemmler
\r
119 public long getUnsignedValue()
\r
123 for (int i = 0; i < length; i++)
\r
130 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
\r
143 * The WireArray is interpreted as a signed integer with n bits.
\r
145 * @return The signed value of the {@link WireArray}'s bits, where value 0
\r
146 * corresponds with 2^0, value 1 is 2^1 and so on.
\r
148 * @author Fabian Stemmler
\r
150 public long getSignedValue()
\r
152 long val = getUnsignedValue();
\r
153 long mask = 1 << (length - 1);
\r
154 if ((mask & val) != 0)
\r
156 int shifts = 64 - length;
\r
157 return (val << shifts) >> shifts;
\r
163 * Included for convenient use on {@link WireArray}s of length 1.
\r
165 * @return The value of bit 0.
\r
167 * @author Fabian Stemmler
\r
169 public Bit getValue()
\r
171 return getValue(0);
\r
176 * @param index Index of the requested bit.
\r
177 * @return The value of the indexed bit.
\r
179 * @author Fabian Stemmler
\r
181 public Bit getValue(int index)
\r
183 return values[index];
\r
186 public Bit[] getValues(int start, int end)
\r
188 int length = end - start;
\r
189 Bit[] bits = new Bit[length];
\r
190 System.arraycopy(values, start, bits, 0, length);
\r
195 * @return An array of length n containing the values of the n bits in the
\r
196 * {@link WireArray}. Can be safely modified.
\r
198 * @author Fabian Stemmler
\r
200 public Bit[] getValues()
\r
202 return values.clone();
\r
206 * Adds an {@link WireArrayObserver}, who will be notified when the value of the
\r
207 * {@link WireArray} is updated.
\r
209 * @param ob The {@link WireArrayObserver} to be notified of changes.
\r
210 * @return true if the given {@link WireArrayObserver} was not already
\r
211 * registered, false otherwise
\r
213 * @author Fabian Stemmler
\r
215 public boolean addObserver(WireArrayObserver ob)
\r
217 return observers.add(ob);
\r
220 private void notifyObservers()
\r
222 for (WireArrayObserver o : observers)
\r
227 * Create and register a {@link WireArrayInput} object, which is tied to this
\r
228 * {@link WireArray}.
\r
230 public WireArrayInput createInput()
\r
232 return new WireArrayInput(this);
\r
235 private void registerInput(WireArrayInput toRegister)
\r
237 inputs.add(toRegister);
\r
241 * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}
\r
242 * it is tied to. The combination of all inputs determines the
\r
243 * {@link WireArray}s final value. X dominates all other inputs Z does not
\r
244 * affect the final value, unless there are no other inputs than Z 0 and 1 turn
\r
245 * into X when they are mixed
\r
247 * @author Fabian Stemmler
\r
249 public class WireArrayInput
\r
251 public final WireArray owner;
\r
252 private Bit[] inputValues;
\r
254 private WireArrayInput(WireArray owner)
\r
257 this.owner = owner;
\r
259 owner.registerInput(this);
\r
262 private void initValues()
\r
264 inputValues = Bit.Z.makeArray(length);
\r
268 * Sets the wires values. This takes up time, as specified by the
\r
269 * {@link WireArray}s travel time.
\r
271 * @param newValues The new values the wires should take on.
\r
273 * @author Fabian Stemmler
\r
275 public void feedSignals(Bit... newValues)
\r
277 if (newValues.length == length)
\r
279 feedSignals(0, newValues);
\r
281 throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));
\r
285 * Sets values of a subarray of wires. This takes up time, as specified by the
\r
286 * {@link WireArray}s travel time.
\r
288 * @param newValues The new values the wires should take on.
\r
289 * @param startingBit The first index of the subarray of wires.
\r
291 * @author Fabian Stemmler
\r
293 public void feedSignals(int startingBit, Bit... newValues)
\r
295 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
\r
298 private void setValues(int startingBit, Bit... newValues)
\r
300 int exclLastIndex = startingBit + newValues.length;
\r
301 if (length < exclLastIndex)
\r
302 throw new ArrayIndexOutOfBoundsException(String.format("Attempted to input bits from index %o to %o when there are only %o wires.", startingBit, exclLastIndex - 1, length));
\r
303 if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
\r
305 System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
\r
306 owner.recalculate();
\r
311 * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}.
\r
313 public Bit[] getValues()
\r
315 return inputValues.clone();
\r
319 * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}.
\r
321 public void clearSignals()
\r
323 feedSignals(Bit.Z.makeArray(length));
\r
326 public Bit[] wireValuesExcludingMe()
\r
328 Bit[] bits = Bit.Z.makeArray(length);
\r
329 for (WireArrayInput wai : inputs)
\r
333 Util.combineInto(bits, wai.getValues());
\r
339 public String toString()
\r
341 return Arrays.toString(inputValues);
\r
346 public String toString()
\r
348 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
\r
351 public static WireArrayInput[] extractInputs(WireArray[] w)
\r
353 WireArrayInput[] inputs = new WireArrayInput[w.length];
\r
354 for (int i = 0; i < w.length; i++)
\r
355 inputs[i] = w[i].createInput();
\r