1 package era.mi.logic.components.gates;
\r
3 import java.util.List;
\r
5 import era.mi.logic.components.BasicComponent;
\r
6 import era.mi.logic.types.Bit;
\r
7 import era.mi.logic.wires.Wire.WireEnd;
\r
9 public abstract class MultiInputGate extends BasicComponent
\r
11 protected WireEnd[] in;
\r
12 protected WireEnd out;
\r
13 protected final int length;
\r
14 protected Operation op;
\r
16 protected MultiInputGate(int processTime, Operation op, WireEnd out, WireEnd... in)
\r
20 length = out.length();
\r
21 this.in = in.clone();
\r
23 throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));
\r
24 for (WireEnd w : in)
\r
26 if (w.length() != length)
\r
27 throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");
\r
28 w.addObserver(this);
\r
34 public List<WireEnd> getAllInputs()
\r
40 public List<WireEnd> getAllOutputs()
\r
42 return List.of(out);
\r
46 protected void compute()
\r
48 Bit[] result = in[0].getValues();
\r
49 for (int i = 1; i < in.length; i++)
\r
50 result = op.execute(result, in[i].getValues());
\r
51 out.feedSignals(result);
\r
54 protected interface Operation
\r
56 public Bit[] execute(Bit[] a, Bit[] b);
\r