1 package era.mi.logic.components.gates;
3 import java.util.Arrays;
4 import java.util.Collections;
7 import era.mi.logic.Bit;
8 import era.mi.logic.components.BasicComponent;
9 import era.mi.logic.wires.Wire.WireEnd;
11 public abstract class MultiInputGate extends BasicComponent
13 protected WireEnd[] in;
14 protected WireEnd out;
15 protected final int length;
16 protected Operation op;
18 protected MultiInputGate(int processTime, Operation op, WireEnd out, WireEnd... in)
22 length = out.length();
25 throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));
28 if (w.length() != length)
29 throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");
36 public List<WireEnd> getAllInputs()
38 return Collections.unmodifiableList(Arrays.asList(in));
42 public List<WireEnd> getAllOutputs()
44 return Collections.unmodifiableList(Arrays.asList(out));
47 protected void compute()
49 Bit[] result = in[0].getValues();
50 for (int i = 1; i < in.length; i++)
51 result = op.execute(result, in[i].getValues());
52 out.feedSignals(result);
55 protected interface Operation
57 public Bit[] execute(Bit[] a, Bit[] b);