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.WireArray;
10 import era.mi.logic.wires.WireArray.WireArrayInput;
12 public abstract class MultiInputGate extends BasicComponent
14 protected WireArray[] in;
15 protected WireArray out;
16 protected WireArrayInput outI;
17 protected final int length;
18 protected Operation op;
20 protected MultiInputGate(int processTime, Operation op, WireArray out, WireArray... in)
27 throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));
30 if(w.length != length)
31 throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");
35 outI = out.createInput();
40 public List<WireArray> getAllInputs()
42 return Collections.unmodifiableList(Arrays.asList(in));
46 public List<WireArray> getAllOutputs()
48 return Collections.unmodifiableList(Arrays.asList(out));
51 protected void compute()
53 Bit[] result = in[0].getValues();
54 for(int i = 1; i < in.length; i++)
55 result = op.execute(result, in[i].getValues());
56 outI.feedSignals(result);
59 protected interface Operation
61 public Bit[] execute(Bit[] a, Bit[] b);