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