Added era.mi; Project containing provisional simulator core
[Mograsim.git] / era.mi / src / era / mi / logic / WireArray.java
1 package era.mi.logic;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 /**
8  * Represents an array of wires that can store n bits of information.
9  * @author Fabian Stemmler
10  *
11  */
12 public class WireArray
13 {
14         private Bit[] values;
15         private final int travelTime;
16         private List<WireArrayObserver> observers = new ArrayList<>();//<WireArrayObserver>();
17         private final int length;
18         
19         public WireArray(int length, int travelTime)
20         {
21                 if(length < 1)
22                         throw new IllegalArgumentException("Tried to create an array of wires with length " + length + ", but a length of less than 1 makes no sense.");
23                 this.length = length;
24                 this.travelTime = travelTime;
25                 initValues();
26         }
27         
28         private void initValues()
29         {
30                 values = new Bit[length];
31                 for(int i = 0; i < length; i++)
32                         values[i] = Bit.X;
33         }
34         
35         /**
36          * Sets the wires values. This takes up time, as specified by the {@link WireArray}s travel time.
37          * @param newValues The new values the wires should take on.
38          * 
39          * @author Fabian Stemmler
40          */
41         public void feedSignals(Bit... newValues)
42         {
43                 Simulation.TIMELINE.addEvent((e) -> setValues(newValues), travelTime);
44         }
45         
46         private void setValues(Bit... newValues)
47         {
48                 if(length != newValues.length)
49                         throw new IllegalArgumentException(String.format("Unexpected length for input array. Length was %d but expected %d", newValues.length, length)); //TODO: Proper handling
50                 if(!Arrays.equals(values, newValues))
51                 {
52                         values = newValues.clone();
53                         notifyObservers();
54                 }
55         }
56         
57         /**
58          * Sets values of a subarray of wires. This takes up time, as specified by the {@link WireArray}s travel time.
59          * @param newValues The new values the wires should take on.
60          * @param startingBit The first index of the subarray of wires.
61          * 
62          * @author Fabian Stemmler
63          */
64         public void feedSignals(int startingBit, Bit... newValues)
65         {
66                 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
67         }
68         
69         private void setValues(int startingBit, Bit... newValues)
70         {
71                 if(length < startingBit + newValues.length)
72                         throw new IllegalArgumentException(); //TODO: Proper handling   
73                 if(!Arrays.equals(values, startingBit, startingBit + newValues.length, newValues, 0, newValues.length))
74                 {
75                         System.arraycopy(newValues, 0, values, startingBit, newValues.length);
76                         notifyObservers();
77                 }
78         }
79         
80         /**
81          * The WireArray is interpreted as an unsigned integer with n bits.
82          * @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 value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
83          * 
84          * @author Fabian Stemmler
85          */
86         public boolean hasNumericValue()
87         {
88                 for(Bit b : values)
89                 {
90                         if(b != Bit.ZERO && b != Bit.ONE)
91                                 return false;
92                 }
93                 return true;
94         }
95         
96         /**
97          * The WireArray is interpreted as an unsigned integer with n bits.
98          * @return The unsigned value of the {@link WireArray}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
99          * 
100          * @author Fabian Stemmler
101          */
102         public int getUnsignedValue()
103         {
104                 int val = 0;
105                 int mask = 1;
106                 for(int i = 0; i < length; i++)
107                 {
108                         switch(values[i])
109                         {
110                         default:
111                         case Z:
112                         case X:
113                                 return 0; //TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; Random number?
114                         case ONE:
115                                 val |= mask;
116                                 break;
117                         case ZERO:
118                         }
119                         mask = mask << 1;
120                 }
121                 return val;
122         }
123         
124         /**
125          * The WireArray is interpreted as a signed integer with n bits.
126          * @return The signed value of the {@link WireArray}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
127          * 
128          * @author Fabian Stemmler
129          */
130         public int getSignedValue()
131         {
132                 int val = getUnsignedValue();
133                 int mask = 1 << (length - 1);
134                 if((mask & val) != 0)
135                 {
136                         int shifts = 32 - length;
137                         return (val << shifts) >> shifts;
138                 }
139                 return val;
140         }
141         
142         /**
143          * Included for convenient use on {@link WireArray}s of length 1.
144          * @return The value of bit 0.
145          * 
146          * @author Fabian Stemmler
147          */
148         public Bit getValue()
149         {
150                 return getValue(0);
151         }
152         
153         /**
154          * 
155          * @param index Index of the requested bit.
156          * @return The value of the indexed bit.
157          * 
158          * @author Fabian Stemmler
159          */
160         public Bit getValue(int index)
161         {
162                 //TODO: ArrayIndexOutOfBoundsException handling for accessing single bit in WireArray
163                 return values[index];
164         }
165         
166         public Bit[] getValues(int start, int end)
167         {
168                 int length = end - start;
169                 Bit[] bits = new Bit[length];
170                 System.arraycopy(values, start, bits, 0, length);               
171                 return bits;
172         }
173         
174         
175         /**
176          * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.
177          * 
178          * @author Fabian Stemmler
179          */
180         public Bit[] getValues()
181         {
182                 return values.clone();
183         }
184
185         public int length()
186         {
187                 return length;
188         }
189         
190         
191         /**
192          * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link WireArray} is updated.
193          * @param ob The {@link WireArrayObserver} to be notified of changes.
194          * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
195          * 
196          * @author Fabian Stemmler
197          */
198         public boolean addObserver(WireArrayObserver ob)
199         {
200                 return observers.add(ob);
201         }
202         
203         private void notifyObservers()
204         {
205                 for(WireArrayObserver o : observers)
206                         o.update(this);
207         }
208 }