Did some clean up
[Mograsim.git] / era.mi / src / era / mi / logic / components / gates / MultiInputGate.java
1 package era.mi.logic.components.gates;\r
2 \r
3 import java.util.List;\r
4 \r
5 import era.mi.logic.Bit;\r
6 import era.mi.logic.components.BasicComponent;\r
7 import era.mi.logic.wires.WireArray;\r
8 import era.mi.logic.wires.WireArray.WireArrayEnd;\r
9 \r
10 public abstract class MultiInputGate extends BasicComponent\r
11 {\r
12         protected WireArray[] in;\r
13         protected WireArray out;\r
14         protected WireArrayEnd outI;\r
15         protected final int length;\r
16         protected Operation op;\r
17 \r
18         protected MultiInputGate(int processTime, Operation op, WireArray out, WireArray... in)\r
19         {\r
20                 super(processTime);\r
21                 this.op = op;\r
22                 length = out.length;\r
23                 this.in = in.clone();\r
24                 if (in.length < 1)\r
25                         throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));\r
26                 for (WireArray w : in)\r
27                 {\r
28                         if (w.length != length)\r
29                                 throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");\r
30                         w.addObserver(this);\r
31                 }\r
32                 this.out = out;\r
33                 outI = out.createInput();\r
34         }\r
35 \r
36         @Override\r
37         public List<WireArray> getAllInputs()\r
38         {\r
39                 return List.of(in);\r
40         }\r
41 \r
42         @Override\r
43         public List<WireArray> getAllOutputs()\r
44         {\r
45                 return List.of(out);\r
46         }\r
47 \r
48         @Override\r
49         protected void compute()\r
50         {\r
51                 Bit[] result = in[0].getValues();\r
52                 for (int i = 1; i < in.length; i++)\r
53                         result = op.execute(result, in[i].getValues());\r
54                 outI.feedSignals(result);\r
55         }\r
56 \r
57         protected interface Operation\r
58         {\r
59                 public Bit[] execute(Bit[] a, Bit[] b);\r
60         }\r
61 }\r