010ae74f9cd463d649491f3b90c43878cba4a2e4
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / gates / MultiInputCoreGate.java
1 package net.mograsim.logic.core.components.gates;
2
3 import java.util.List;
4
5 import net.mograsim.logic.core.components.BasicCoreComponent;
6 import net.mograsim.logic.core.timeline.Timeline;
7 import net.mograsim.logic.core.timeline.TimelineEventHandler;
8 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
9 import net.mograsim.logic.core.types.MutationOperation;
10 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
11 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
12
13 public abstract class MultiInputCoreGate extends BasicCoreComponent
14 {
15         protected ReadEnd[] in;
16         protected ReadWriteEnd out;
17         protected final int width;
18         protected MutationOperation op;
19         protected boolean invert = false;
20
21         protected MultiInputCoreGate(Timeline timeline, int processTime, MutationOperation op, ReadWriteEnd out, ReadEnd... in)
22         {
23                 super(timeline, processTime);
24                 this.op = op;
25                 width = out.width();
26                 this.in = in.clone();
27                 if (in.length < 1)
28                         throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));
29                 for (ReadEnd w : in)
30                 {
31                         if (w.width() != width)
32                                 throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");
33                         w.registerObserver(this);
34                 }
35                 this.out = out;
36         }
37
38         protected MultiInputCoreGate(Timeline timeline, int processTime, MutationOperation op, boolean invert, ReadWriteEnd out, ReadEnd... in)
39         {
40                 this(timeline, processTime, op, out, in);
41                 this.invert = invert;
42         }
43
44         @Override
45         public List<ReadEnd> getAllInputs()
46         {
47                 return List.of(in);
48         }
49
50         @Override
51         public List<ReadWriteEnd> getAllOutputs()
52         {
53                 return List.of(out);
54         }
55
56         @Override
57         public TimelineEventHandler compute()
58         {
59                 BitVectorMutator mutator = BitVectorMutator.empty();
60                 for (ReadEnd w : in)
61                         op.apply(mutator, w.getValues());
62                 return e -> out.feedSignals(invert ? mutator.toBitVector().not() : mutator.toBitVector());
63         }
64 }