added more doc to Timeline; added functionality to Bit
[Mograsim.git] / era.mi / src / era / mi / logic / wires / WireArray.java
1 package era.mi.logic.wires;\r
2 \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
7 \r
8 import era.mi.logic.Bit;\r
9 import era.mi.logic.Simulation;\r
10 import era.mi.logic.Util;\r
11 \r
12 /**\r
13  * Represents an array of wires that can store n bits of information.\r
14  * \r
15  * @author Fabian Stemmler\r
16  *\r
17  */\r
18 public class WireArray\r
19 {\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
25 \r
26         public WireArray(int length, int travelTime)\r
27         {\r
28                 if (length < 1)\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
32                 initValues();\r
33         }\r
34 \r
35         private void initValues()\r
36         {\r
37                 values = Bit.Z.makeArray(length);\r
38         }\r
39 \r
40         private void recalculateSingleInput()\r
41         {\r
42                 WireArrayInput input = inputs.get(0);\r
43                 if (!Arrays.equals(input.getValues(), values))\r
44                 {\r
45                         System.arraycopy(input.getValues(), 0, values, 0, length);\r
46                         notifyObservers();\r
47                 }\r
48         }\r
49 \r
50         private void recalculateMultipleInputs()\r
51         {\r
52                 Iterator<WireArrayInput> it = inputs.iterator();\r
53                 Bit[] newValues = it.next().inputValues.clone();\r
54 \r
55                 while (it.hasNext())\r
56                 {\r
57                         WireArrayInput input = it.next();\r
58                         Bit[] bits = input.getValues();\r
59                         for (int i = 0; i < length; i++)\r
60                         {\r
61                                 if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))\r
62                                         continue;\r
63                                 else if (Bit.Z.equals(newValues[i]))\r
64                                         newValues[i] = bits[i];\r
65                                 else\r
66                                         newValues[i] = Bit.X;\r
67                         }\r
68                 }\r
69 \r
70                 if (!Arrays.equals(newValues, values))\r
71                 {\r
72                         notifyObservers();\r
73                         values = newValues;\r
74                 }\r
75         }\r
76 \r
77         private void recalculate()\r
78         {\r
79                 switch (inputs.size())\r
80                 {\r
81                 case 0:\r
82                         return;\r
83                 case 1:\r
84                         recalculateSingleInput();\r
85                         break;\r
86                 default:\r
87                         recalculateMultipleInputs();\r
88                 }\r
89         }\r
90 \r
91         /**\r
92          * The WireArray is interpreted as an unsigned integer with n bits.\r
93          * \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
98          * \r
99          * @author Fabian Stemmler\r
100          */\r
101         public boolean hasNumericValue()\r
102         {\r
103                 for (Bit b : values)\r
104                 {\r
105                         if (b != Bit.ZERO && b != Bit.ONE)\r
106                                 return false;\r
107                 }\r
108                 return true;\r
109         }\r
110 \r
111         /**\r
112          * The WireArray is interpreted as an unsigned integer with n bits.\r
113          * \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
116          * \r
117          * @author Fabian Stemmler\r
118          */\r
119         public long getUnsignedValue()\r
120         {\r
121                 long val = 0;\r
122                 long mask = 1;\r
123                 for (int i = 0; i < length; i++)\r
124                 {\r
125                         switch (values[i])\r
126                         {\r
127                         default:\r
128                         case Z:\r
129                         case X:\r
130                                 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;\r
131                         // Random number?\r
132                         case ONE:\r
133                                 val |= mask;\r
134                                 break;\r
135                         case ZERO:\r
136                         }\r
137                         mask = mask << 1;\r
138                 }\r
139                 return val;\r
140         }\r
141 \r
142         /**\r
143          * The WireArray is interpreted as a signed integer with n bits.\r
144          * \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
147          * \r
148          * @author Fabian Stemmler\r
149          */\r
150         public long getSignedValue()\r
151         {\r
152                 long val = getUnsignedValue();\r
153                 long mask = 1 << (length - 1);\r
154                 if ((mask & val) != 0)\r
155                 {\r
156                         int shifts = 64 - length;\r
157                         return (val << shifts) >> shifts;\r
158                 }\r
159                 return val;\r
160         }\r
161 \r
162         /**\r
163          * Included for convenient use on {@link WireArray}s of length 1.\r
164          * \r
165          * @return The value of bit 0.\r
166          * \r
167          * @author Fabian Stemmler\r
168          */\r
169         public Bit getValue()\r
170         {\r
171                 return getValue(0);\r
172         }\r
173 \r
174         /**\r
175          * \r
176          * @param index Index of the requested bit.\r
177          * @return The value of the indexed bit.\r
178          * \r
179          * @author Fabian Stemmler\r
180          */\r
181         public Bit getValue(int index)\r
182         {\r
183                 return values[index];\r
184         }\r
185 \r
186         public Bit[] getValues(int start, int end)\r
187         {\r
188                 int length = end - start;\r
189                 Bit[] bits = new Bit[length];\r
190                 System.arraycopy(values, start, bits, 0, length);\r
191                 return bits;\r
192         }\r
193 \r
194         /**\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
197          * \r
198          * @author Fabian Stemmler\r
199          */\r
200         public Bit[] getValues()\r
201         {\r
202                 return values.clone();\r
203         }\r
204 \r
205         /**\r
206          * Adds an {@link WireArrayObserver}, who will be notified when the value of the\r
207          * {@link WireArray} is updated.\r
208          * \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
212          * \r
213          * @author Fabian Stemmler\r
214          */\r
215         public boolean addObserver(WireArrayObserver ob)\r
216         {\r
217                 return observers.add(ob);\r
218         }\r
219 \r
220         private void notifyObservers()\r
221         {\r
222                 for (WireArrayObserver o : observers)\r
223                         o.update(this);\r
224         }\r
225 \r
226         /**\r
227          * Create and register a {@link WireArrayInput} object, which is tied to this\r
228          * {@link WireArray}.\r
229          */\r
230         public WireArrayInput createInput()\r
231         {\r
232                 return new WireArrayInput(this);\r
233         }\r
234 \r
235         private void registerInput(WireArrayInput toRegister)\r
236         {\r
237                 inputs.add(toRegister);\r
238         }\r
239 \r
240         /**\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
246          * \r
247          * @author Fabian Stemmler\r
248          */\r
249         public class WireArrayInput\r
250         {\r
251                 public final WireArray owner;\r
252                 private Bit[] inputValues;\r
253 \r
254                 private WireArrayInput(WireArray owner)\r
255                 {\r
256                         super();\r
257                         this.owner = owner;\r
258                         initValues();\r
259                         owner.registerInput(this);\r
260                 }\r
261 \r
262                 private void initValues()\r
263                 {\r
264                         inputValues = Bit.Z.makeArray(length);\r
265                 }\r
266 \r
267                 /**\r
268                  * Sets the wires values. This takes up time, as specified by the\r
269                  * {@link WireArray}s travel time.\r
270                  * \r
271                  * @param newValues The new values the wires should take on.\r
272                  * \r
273                  * @author Fabian Stemmler\r
274                  */\r
275                 public void feedSignals(Bit... newValues)\r
276                 {\r
277                         if (newValues.length == length)\r
278                         {\r
279                                 feedSignals(0, newValues);\r
280                         } else\r
281                                 throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));\r
282                 }\r
283 \r
284                 /**\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
287                  * \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
290                  * \r
291                  * @author Fabian Stemmler\r
292                  */\r
293                 public void feedSignals(int startingBit, Bit... newValues)\r
294                 {\r
295                         Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);\r
296                 }\r
297 \r
298                 private void setValues(int startingBit, Bit... newValues)\r
299                 {\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
304                         {\r
305                                 System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);\r
306                                 owner.recalculate();\r
307                         }\r
308                 }\r
309 \r
310                 /**\r
311                  * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}.\r
312                  */\r
313                 public Bit[] getValues()\r
314                 {\r
315                         return inputValues.clone();\r
316                 }\r
317 \r
318                 /**\r
319                  * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}.\r
320                  */\r
321                 public void clearSignals()\r
322                 {\r
323                         feedSignals(Bit.Z.makeArray(length));\r
324                 }\r
325 \r
326                 public Bit[] wireValuesExcludingMe() \r
327                 {\r
328                         Bit[] bits = Bit.Z.makeArray(length);\r
329                         for (WireArrayInput wai : inputs) \r
330                         {\r
331                                 if(wai == this)\r
332                                         continue;\r
333                                 Util.combineInto(bits, wai.getValues());\r
334                         }\r
335                         return bits;\r
336                 }\r
337                 \r
338                 @Override\r
339                 public String toString()\r
340                 {\r
341                         return Arrays.toString(inputValues);\r
342                 }\r
343         }\r
344 \r
345         @Override\r
346         public String toString()\r
347         {\r
348                 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);\r
349         }\r
350 \r
351         public static WireArrayInput[] extractInputs(WireArray[] w)\r
352         {\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
356                 return inputs;\r
357         }\r
358 }