508e21c0dfeaa11a47bee6d0eef84426dbd720f5
[Mograsim.git] / era.mi / src / net / mograsim / logic / core / components / gates / MultiInputGate.java
1 package net.mograsim.logic.core.components.gates;\r
2 \r
3 import java.util.List;\r
4 \r
5 import net.mograsim.logic.core.components.BasicComponent;\r
6 import net.mograsim.logic.core.timeline.Timeline;\r
7 import net.mograsim.logic.core.types.MutationOperation;\r
8 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;\r
9 import net.mograsim.logic.core.wires.Wire.ReadEnd;\r
10 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;\r
11 \r
12 public abstract class MultiInputGate extends BasicComponent\r
13 {\r
14         protected ReadEnd[] in;\r
15         protected ReadWriteEnd out;\r
16         protected final int length;\r
17         protected MutationOperation op;\r
18 \r
19         protected MultiInputGate(Timeline timeline, int processTime, MutationOperation op, ReadWriteEnd out, ReadEnd... in)\r
20         {\r
21                 super(timeline, processTime);\r
22                 this.op = op;\r
23                 length = out.length();\r
24                 this.in = in.clone();\r
25                 if (in.length < 1)\r
26                         throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));\r
27                 for (ReadEnd w : in)\r
28                 {\r
29                         if (w.length() != length)\r
30                                 throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");\r
31                         w.addObserver(this);\r
32                 }\r
33                 this.out = out;\r
34         }\r
35 \r
36         @Override\r
37         public List<ReadEnd> getAllInputs()\r
38         {\r
39                 return List.of(in);\r
40         }\r
41 \r
42         @Override\r
43         public List<ReadWriteEnd> 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                 BitVectorMutator mutator = BitVectorMutator.empty();\r
52                 for (ReadEnd w : in)\r
53                         op.apply(mutator, w.getValues());\r
54                 out.feedSignals(mutator.get());\r
55         }\r
56 }\r