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