Merged SampleERCP into master
[Mograsim.git] / era.mi / src / era / mi / logic / components / gates / MultiInputGate.java
1 package era.mi.logic.components.gates;
2
3 import java.util.List;
4
5 import era.mi.logic.components.BasicComponent;
6 import era.mi.logic.types.BitVector.BitVectorMutator;
7 import era.mi.logic.types.MutationOperation;
8 import era.mi.logic.wires.Wire.WireEnd;
9
10 public abstract class MultiInputGate extends BasicComponent
11 {
12         protected WireEnd[] in;
13         protected WireEnd out;
14         protected final int length;
15         protected MutationOperation op;
16
17         protected MultiInputGate(int processTime, MutationOperation op, WireEnd out, WireEnd... in)
18         {
19                 super(processTime);
20                 this.op = op;
21                 length = out.length();
22                 this.in = in.clone();
23                 if (in.length < 1)
24                         throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));
25                 for (WireEnd w : in)
26                 {
27                         if (w.length() != length)
28                                 throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");
29                         w.addObserver(this);
30                 }
31                 this.out = out;
32         }
33
34         @Override
35         public List<WireEnd> getAllInputs()
36         {
37                 return List.of(in);
38         }
39
40         @Override
41         public List<WireEnd> getAllOutputs()
42         {
43                 return List.of(out);
44         }
45
46         @Override
47         protected void compute()
48         {
49                 BitVectorMutator mutator = BitVectorMutator.empty();
50                 for (WireEnd w : in)
51                         op.apply(mutator, w.getValues());
52                 out.feedSignals(mutator.get());
53         }
54 }