Skip to content

Tutorial: Create a new circuit

yhuanguva edited this page Sep 11, 2011 · 1 revision

To create a composite circuit using basic logic gates, first create a class inheriting CompositeCircuits class in package YaoGC. Suppose we want to create a simple full adder such as this:

The sample code is HERE The full adder extends CompositeCircuits and it combines basic gates to assemble a more complicated addition circuit. Constructor The constructor of this class calls its superclass constructor, super(3, 2, 5, "ADD_3_2"), where 3 is the number of input wires, X, Y and CIN, 2 is the number of output wires, S and COUT, 5 is the number of logic gates this composite circuit will use, and "ADD_3_2" is the name of this circuit. createSubCircuits The createSubCircuits() method should call the constructors for all the logic gates that would be used in the circuit. For example, to initialize an XOR gate, we can call subCircuits[i] = new XOR_2_1();. This line of code will initialize a XOR gate at the ith position in the subCircuits array. After all subcircuits have been initialized, the method calls its superclass method super.createSubCircuits(). connectWires The connectWires() method should connect all the wires in the circuit between input wires and logic gates. For example, if we want to connect the input wires X and CIN to an XOR gate, we can call: inputWires[X].connectTo(subCircuits[XOR0].inputWires, 0); inputWires[CIN].connectTo(subCircuits[XOR0].inputWires, 1); Here, X and CIN are two integers that represent the positions of input wires X and CIN. Similarly, XOR0 is also an arbitrary number. To connect the wire to the first input line of the gate, use 0 as the second parameter of the connectTo method, 1 for the second input line of the gate, and so on. defineOutputWires This method defines the output wires. For example, if the outputWires[0] should be the first output wire of the circuit subCircuits[0], then call outputWires[0] = subCircuits[0].outputWires[0]. fixInternalWires This method is useful when a fixed constant wire is needed in the circuit, such as in a simple NOT gate that can be built by XORing the original input with a wire of 1.

The sample code is HERE To connect a constant value to a gate, we can simply call fixWire method of the target wire: subCircuits[0].inputWires[1].fixWire(1); This way, the second input wire of the circuit subCircuits[0] will be a constant 1.

Clone this wiki locally