Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Extra credit: one-bit adder.
- */
- static Circuit* oneBitAdder_Circuit() {
- Boolean* x = new_Boolean(false);
- Boolean* y = new_Boolean(false);
- Boolean* c = new_Boolean(false);
- Boolean** inputs = new_Boolean_array(2);
- inputs[0] = x;
- inputs[1] = y;
- inputs[2] = c;
- Boolean* z = new_Boolean(false);
- Boolean* d = new_Boolean(false);
- Boolean** outputs = new_Boolean_array(2);
- outputs[0] = z;
- outputs[1] = d;
- Gate* N1 = new_Inverter();
- Gate* N2 = new_Inverter();
- Gate* N3 = new_Inverter();
- Gate* A1 = new_And3Gate();
- Gate* A2 = new_And3Gate();
- Gate* A3 = new_And3Gate();
- Gate* A4 = new_And3Gate();
- Gate* A5 = new_And3Gate();
- Gate* A6 = new_And3Gate();
- Gate* A7 = new_And3Gate();
- Gate* O1 = new_OrGate();
- Gate* O2 = new_OrGate();
- Gate* O3 = new_OrGate();
- Gate* O4 = new_OrGate();
- Gate* O5 = new_OrGate();
- Gate* O6 = new_OrGate();
- Gate** gates = new_Gate_array(15);
- gates[0] = N1;
- gates[1] = N2;
- gates[2] = N3;
- gates[3] = A1;
- gates[4] = A2;
- gates[5] = A3;
- gates[6] = A4;
- gates[7] = A5;
- gates[8] = A6;
- gates[9] = A7;
- gates[10] = O1;
- gates[11] = O2;
- gates[12] = O3;
- gates[13] = O4;
- gates[14] = O5;
- gates[15] = O6;
- Circuit *circuit = new_Circuit(3, inputs, 2, outputs, 15, gates);
- Circuit_connect(circuit, x, Gate_getInput(N1, 0));
- Circuit_connect(circuit, y, Gate_getInput(N2, 0));
- Circuit_connect(circuit, c, Gate_getInput(N3, 0));
- Circuit_connect(circuit, x, Gate_getInput(A4, 0));
- Circuit_connect(circuit, x, Gate_getInput(A5, 0));
- Circuit_connect(circuit, x, Gate_getInput(A6, 2));
- Circuit_connect(circuit, x, Gate_getInput(A7, 0));
- Circuit_connect(circuit, Gate_getOutput(N1), Gate_getInput(A1, 0));
- Circuit_connect(circuit, Gate_getOutput(N1), Gate_getInput(A2, 0));
- Circuit_connect(circuit, Gate_getOutput(N1), Gate_getInput(A3, 0));
- Circuit_connect(circuit, y, Gate_getInput(A2, 1));
- Circuit_connect(circuit, y, Gate_getInput(A3, 1));
- Circuit_connect(circuit, y, Gate_getInput(A6, 1));
- Circuit_connect(circuit, y, Gate_getInput(A7, 1));
- Circuit_connect(circuit, Gate_getOutput(N2), Gate_getInput(A1, 1));
- Circuit_connect(circuit, Gate_getOutput(N2), Gate_getInput(A4, 1));
- Circuit_connect(circuit, Gate_getOutput(N2), Gate_getInput(A5, 1));
- Circuit_connect(circuit, c, Gate_getInput(A1, 2));
- Circuit_connect(circuit, c, Gate_getInput(A3, 2));
- Circuit_connect(circuit, c, Gate_getInput(A5, 2));
- Circuit_connect(circuit, c, Gate_getInput(A7, 2));
- Circuit_connect(circuit, Gate_getOutput(N3), Gate_getInput(A2, 2));
- Circuit_connect(circuit, Gate_getOutput(N3), Gate_getInput(A4, 2));
- Circuit_connect(circuit, Gate_getOutput(N3), Gate_getInput(A6, 0));
- Circuit_connect(circuit, Gate_getOutput(A1), Gate_getInput(O1, 0));
- Circuit_connect(circuit, Gate_getOutput(A2), Gate_getInput(O1, 1));
- Circuit_connect(circuit, Gate_getOutput(A4), Gate_getInput(O2, 0));
- Circuit_connect(circuit, Gate_getOutput(A7), Gate_getInput(O2, 1));
- Circuit_connect(circuit, Gate_getOutput(A3), Gate_getInput(O3, 0));
- Circuit_connect(circuit, Gate_getOutput(A5), Gate_getInput(O3, 1));
- Circuit_connect(circuit, Gate_getOutput(A6), Gate_getInput(O4, 0));
- Circuit_connect(circuit, Gate_getOutput(A7), Gate_getInput(O4, 1));
- Circuit_connect(circuit, Gate_getOutput(O1), Gate_getInput(O5, 0));
- Circuit_connect(circuit, Gate_getOutput(O2), Gate_getInput(O5, 1));
- Circuit_connect(circuit, Gate_getOutput(O3), Gate_getInput(O6, 0));
- Circuit_connect(circuit, Gate_getOutput(O4), Gate_getInput(O6, 1));
- Circuit_connect(circuit, Gate_getOutput(O5), z);
- Circuit_connect(circuit, Gate_getOutput(O6), d);
- return circuit;
- }
- static void test3In2Out(Circuit* circuit, bool in0, bool in1, bool in2) {
- Circuit_setInput(circuit, 0, in0);
- Circuit_setInput(circuit, 1, in1);
- Circuit_setInput(circuit, 2, in2);
- Circuit_update(circuit);
- bool out0 = Circuit_getOutput(circuit, 0);
- bool out1 = Circuit_getOutput(circuit, 1);
- printf("%s %s %s -> %s, %s\n", b2s(in0), b2s(in1), b2s(in2), b2s(out0), b2s(out1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement