Fixed Merger, Mux, Splitter: onedirectional again. Tests added.
[Mograsim.git] / era.mi / src / era / mi / logic / components / Mapper.java
1 package era.mi.logic.components;
2
3 import era.mi.logic.wires.WireArray;
4 import era.mi.logic.wires.WireArray.WireArrayInput;
5 import era.mi.logic.wires.WireArrayObserver;
6
7 public class Mapper implements WireArrayObserver
8 {
9     private int[] inBeginningIndex, outBeginningIndex;
10     private WireArray[] inputs;
11     private WireArrayInput[] outputsI;
12
13     public Mapper(WireArray[] inputs, WireArray[] outputs)
14     {
15         this.inputs = inputs.clone();
16
17         int beginningIndex = 0;
18         for (int i = 0; i < inputs.length; i++)
19         {
20             inBeginningIndex[i] = beginningIndex;
21             beginningIndex += inputs[i].length;
22             inputs[i].addObserver(this);
23         }
24         int inputsLength = beginningIndex;
25         beginningIndex = 0;
26         for (int i = 0; i < outputs.length; i++)
27         {
28             outputsI[i] = outputs[i].createInput();
29             outBeginningIndex[i] = beginningIndex;
30             beginningIndex += outputs[i].length;
31         }
32         if (inputsLength != beginningIndex)
33             throw new IllegalArgumentException("Mapper inputs must add up to the same length as outputs. ("
34                     + inputsLength + ", " + beginningIndex + ").");
35     }
36
37     @Override
38     public void update(WireArray initiator)
39     {
40         for (int i = 0; i < inputs.length; i++)
41         {
42             if (inputs[i] == initiator)
43             {
44                 int inB = inBeginningIndex[i];
45 //              int outB = outBeginningIndex TODO
46             }
47         }
48     }
49
50     // binary search
51     private int findLower(int[] sorted, int value)
52     {
53         int a = 0, b = sorted.length;
54         while (a < (b - 1))
55         {
56             int inspect = (a + b) >> 1;
57             if (sorted[inspect] == value)
58                 return inspect;
59             if (sorted[inspect] > value)
60                 b = inspect;
61             else
62                 a = inspect;
63         }
64         return a;
65     }
66 }