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.Bit;
9 import era.mi.logic.Simulation;
10 import era.mi.logic.Util;
13 * Represents an array of wires that can store n bits of information.
15 * @author Fabian Stemmler
18 public class WireArray
21 public final int travelTime;
22 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
23 public final int length;
24 private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
26 public WireArray(int length, int travelTime)
29 throw new IllegalArgumentException("Tried to create an array of wires with length " + length
30 + ", but a length of less than 1 makes no sense.");
32 this.travelTime = travelTime;
36 private void initValues()
38 values = new Bit[length];
39 for (int i = 0; i < length; i++)
43 private void recalculateSingleInput()
45 WireArrayInput input = inputs.get(0);
46 if (!Arrays.equals(input.getValues(), values))
48 System.arraycopy(input.getValues(), 0, values, 0, length);
53 private void recalculateMultipleInputs()
55 Iterator<WireArrayInput> it = inputs.iterator();
56 Bit[] newValues = it.next().values.clone();
60 WireArrayInput input = it.next();
61 Bit[] bits = input.getValues();
62 for (int i = 0; i < length; i++)
64 if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
66 else if (Bit.Z.equals(newValues[i]))
67 newValues[i] = bits[i];
73 if (!Arrays.equals(newValues, values))
80 private void recalculate()
82 switch (inputs.size())
87 recalculateSingleInput();
90 recalculateMultipleInputs();
95 * The WireArray is interpreted as an unsigned integer with n bits.
97 * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or
98 * <code>Bit.ZERO</code> (they do not all have to have the same value),
99 * not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is
100 * returned otherwise.
102 * @author Fabian Stemmler
104 public boolean hasNumericValue()
108 if (b != Bit.ZERO && b != Bit.ONE)
115 * The WireArray is interpreted as an unsigned integer with n bits.
117 * @return The unsigned value of the {@link WireArray}'s bits, where value 0
118 * corresponds with 2^0, value 1 is 2^1 and so on.
120 * @author Fabian Stemmler
122 public long getUnsignedValue()
126 for (int i = 0; i < length; i++)
133 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
146 * The WireArray is interpreted as a signed integer with n bits.
148 * @return The signed value of the {@link WireArray}'s bits, where value 0
149 * corresponds with 2^0, value 1 is 2^1 and so on.
151 * @author Fabian Stemmler
153 public long getSignedValue()
155 long val = getUnsignedValue();
156 long mask = 1 << (length - 1);
157 if ((mask & val) != 0)
159 int shifts = 64 - length;
160 return (val << shifts) >> shifts;
166 * Included for convenient use on {@link WireArray}s of length 1.
168 * @return The value of bit 0.
170 * @author Fabian Stemmler
172 public Bit getValue()
179 * @param index Index of the requested bit.
180 * @return The value of the indexed bit.
182 * @author Fabian Stemmler
184 public Bit getValue(int index)
186 return values[index];
189 public Bit[] getValues(int start, int end)
191 int length = end - start;
192 Bit[] bits = new Bit[length];
193 System.arraycopy(values, start, bits, 0, length);
198 * @return An array of length n containing the values of the n bits in the
199 * {@link WireArray}. Can be safely modified.
201 * @author Fabian Stemmler
203 public Bit[] getValues()
205 return values.clone();
209 * Adds an {@link WireArrayObserver}, who will be notified when the value of the
210 * {@link WireArray} is updated.
212 * @param ob The {@link WireArrayObserver} to be notified of changes.
213 * @return true if the given {@link WireArrayObserver} was not already
214 * registered, false otherwise
216 * @author Fabian Stemmler
218 public boolean addObserver(WireArrayObserver ob)
220 return observers.add(ob);
223 private void notifyObservers()
225 for (WireArrayObserver o : observers)
230 * Create and register a {@link WireArrayInput} object, which is tied to this
233 public WireArrayInput createInput()
235 return new WireArrayInput(this);
238 private void registerInput(WireArrayInput toRegister)
240 inputs.add(toRegister);
244 * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}
245 * it is tied to. The combination of all inputs determines the
246 * {@link WireArray}s final value. X dominates all other inputs Z does not
247 * affect the final value, unless there are no other inputs than Z 0 and 1 turn
248 * into X when they are mixed
250 * @author Fabian Stemmler
252 public class WireArrayInput
254 public final WireArray owner;
255 private Bit[] values;
257 private WireArrayInput(WireArray owner)
262 owner.registerInput(this);
265 private void initValues()
267 values = new Bit[length];
268 for (int i = 0; i < length; i++)
273 * Sets the wires values. This takes up time, as specified by the
274 * {@link WireArray}s travel time.
276 * @param newValues The new values the wires should take on.
278 * @author Fabian Stemmler
280 public void feedSignals(Bit... newValues)
282 if (newValues.length == length)
284 feedSignals(0, newValues);
286 throw new IllegalArgumentException(
287 "Attempted to input " + newValues.length + " bits instead of " + length + " bits.");
291 * Sets values of a subarray of wires. This takes up time, as specified by the
292 * {@link WireArray}s travel time.
294 * @param newValues The new values the wires should take on.
295 * @param startingBit The first index of the subarray of wires.
297 * @author Fabian Stemmler
299 public void feedSignals(int startingBit, Bit... newValues)
301 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
304 private void setValues(int startingBit, Bit... newValues)
306 int exclLastIndex = startingBit + newValues.length;
307 if (length < exclLastIndex)
308 throw new ArrayIndexOutOfBoundsException("Attempted to input bits from index " + startingBit + " to "
309 + exclLastIndex + " when there are only " + length + "wires.");
310 if (!Arrays.equals(values, startingBit, exclLastIndex, newValues, 0, newValues.length))
312 System.arraycopy(newValues, 0, values, startingBit, newValues.length);
317 public Bit[] getValues()
319 return values.clone();
322 public Bit[] wireValuesExcludingMe()
324 Bit[] bits = Util.arrayOfZ(length);
325 for (WireArrayInput wai : inputs)
329 Util.combineInto(bits, wai.getValues());
334 public void clearSignals()
336 Bit[] bits = new Bit[length];
337 for (int i = 0; i < length; i++)
343 public String toString()
345 return Arrays.toString(values);
350 public String toString()
352 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
355 public static WireArrayInput[] extractInputs(WireArray[] w)
357 WireArrayInput[] inputs = new WireArrayInput[w.length];
358 for(int i = 0; i < w.length; i++)
359 inputs[i] = w[i].createInput();