4c985b4379595e32c6d64fa458494144c6cd51e4
[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.components.BasicComponent;\r
6 import era.mi.logic.types.BitVector.BitVectorMutator;\r
7 import era.mi.logic.types.MutationOperation;\r
8 import era.mi.logic.wires.Wire.ReadEnd;\r
9 import era.mi.logic.wires.Wire.ReadWriteEnd;\r
10 \r
11 public abstract class MultiInputGate extends BasicComponent\r
12 {\r
13         protected ReadEnd[] in;\r
14         protected ReadWriteEnd out;\r
15         protected final int length;\r
16         protected MutationOperation op;\r
17 \r
18         protected MultiInputGate(int processTime, MutationOperation op, ReadWriteEnd out, ReadEnd... 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 (ReadEnd 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         }\r
34 \r
35         @Override\r
36         public List<ReadEnd> getAllInputs()\r
37         {\r
38                 return List.of(in);\r
39         }\r
40 \r
41         @Override\r
42         public List<ReadWriteEnd> getAllOutputs()\r
43         {\r
44                 return List.of(out);\r
45         }\r
46 \r
47         @Override\r
48         protected void compute()\r
49         {\r
50                 BitVectorMutator mutator = BitVectorMutator.empty();\r
51                 for (ReadEnd w : in)\r
52                         op.apply(mutator, w.getValues());\r
53                 out.feedSignals(mutator.get());\r
54         }\r
55 }\r