Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1.  
  2. /**
  3. * Extra credit: one-bit adder.
  4. */
  5.  
  6. static Circuit* oneBitAdder_Circuit() {
  7. Boolean* x = new_Boolean(false);
  8. Boolean* y = new_Boolean(false);
  9. Boolean* c = new_Boolean(false);
  10. Boolean** inputs = new_Boolean_array(2);
  11. inputs[0] = x;
  12. inputs[1] = y;
  13. inputs[2] = c;
  14.  
  15. Boolean* z = new_Boolean(false);
  16. Boolean* d = new_Boolean(false);
  17. Boolean** outputs = new_Boolean_array(2);
  18. outputs[0] = z;
  19. outputs[1] = d;
  20.  
  21. Gate* N1 = new_Inverter();
  22. Gate* N2 = new_Inverter();
  23. Gate* N3 = new_Inverter();
  24. Gate* A1 = new_And3Gate();
  25. Gate* A2 = new_And3Gate();
  26. Gate* A3 = new_And3Gate();
  27. Gate* A4 = new_And3Gate();
  28. Gate* A5 = new_And3Gate();
  29. Gate* A6 = new_And3Gate();
  30. Gate* A7 = new_And3Gate();
  31. Gate* O1 = new_OrGate();
  32. Gate* O2 = new_OrGate();
  33. Gate* O3 = new_OrGate();
  34. Gate* O4 = new_OrGate();
  35. Gate* O5 = new_OrGate();
  36. Gate* O6 = new_OrGate();
  37.  
  38. Gate** gates = new_Gate_array(15);
  39. gates[0] = N1;
  40. gates[1] = N2;
  41. gates[2] = N3;
  42. gates[3] = A1;
  43. gates[4] = A2;
  44. gates[5] = A3;
  45. gates[6] = A4;
  46. gates[7] = A5;
  47. gates[8] = A6;
  48. gates[9] = A7;
  49. gates[10] = O1;
  50. gates[11] = O2;
  51. gates[12] = O3;
  52. gates[13] = O4;
  53. gates[14] = O5;
  54. gates[15] = O6;
  55.  
  56. Circuit *circuit = new_Circuit(3, inputs, 2, outputs, 15, gates);
  57. Circuit_connect(circuit, x, Gate_getInput(N1, 0));
  58. Circuit_connect(circuit, y, Gate_getInput(N2, 0));
  59. Circuit_connect(circuit, c, Gate_getInput(N3, 0));
  60.  
  61. Circuit_connect(circuit, x, Gate_getInput(A4, 0));
  62. Circuit_connect(circuit, x, Gate_getInput(A5, 0));
  63. Circuit_connect(circuit, x, Gate_getInput(A6, 2));
  64. Circuit_connect(circuit, x, Gate_getInput(A7, 0));
  65.  
  66. Circuit_connect(circuit, Gate_getOutput(N1), Gate_getInput(A1, 0));
  67. Circuit_connect(circuit, Gate_getOutput(N1), Gate_getInput(A2, 0));
  68. Circuit_connect(circuit, Gate_getOutput(N1), Gate_getInput(A3, 0));
  69.  
  70. Circuit_connect(circuit, y, Gate_getInput(A2, 1));
  71. Circuit_connect(circuit, y, Gate_getInput(A3, 1));
  72. Circuit_connect(circuit, y, Gate_getInput(A6, 1));
  73. Circuit_connect(circuit, y, Gate_getInput(A7, 1));
  74.  
  75. Circuit_connect(circuit, Gate_getOutput(N2), Gate_getInput(A1, 1));
  76. Circuit_connect(circuit, Gate_getOutput(N2), Gate_getInput(A4, 1));
  77. Circuit_connect(circuit, Gate_getOutput(N2), Gate_getInput(A5, 1));
  78.  
  79. Circuit_connect(circuit, c, Gate_getInput(A1, 2));
  80. Circuit_connect(circuit, c, Gate_getInput(A3, 2));
  81. Circuit_connect(circuit, c, Gate_getInput(A5, 2));
  82. Circuit_connect(circuit, c, Gate_getInput(A7, 2));
  83.  
  84. Circuit_connect(circuit, Gate_getOutput(N3), Gate_getInput(A2, 2));
  85. Circuit_connect(circuit, Gate_getOutput(N3), Gate_getInput(A4, 2));
  86. Circuit_connect(circuit, Gate_getOutput(N3), Gate_getInput(A6, 0));
  87.  
  88. Circuit_connect(circuit, Gate_getOutput(A1), Gate_getInput(O1, 0));
  89. Circuit_connect(circuit, Gate_getOutput(A2), Gate_getInput(O1, 1));
  90. Circuit_connect(circuit, Gate_getOutput(A4), Gate_getInput(O2, 0));
  91. Circuit_connect(circuit, Gate_getOutput(A7), Gate_getInput(O2, 1));
  92. Circuit_connect(circuit, Gate_getOutput(A3), Gate_getInput(O3, 0));
  93. Circuit_connect(circuit, Gate_getOutput(A5), Gate_getInput(O3, 1));
  94. Circuit_connect(circuit, Gate_getOutput(A6), Gate_getInput(O4, 0));
  95. Circuit_connect(circuit, Gate_getOutput(A7), Gate_getInput(O4, 1));
  96.  
  97. Circuit_connect(circuit, Gate_getOutput(O1), Gate_getInput(O5, 0));
  98. Circuit_connect(circuit, Gate_getOutput(O2), Gate_getInput(O5, 1));
  99. Circuit_connect(circuit, Gate_getOutput(O3), Gate_getInput(O6, 0));
  100. Circuit_connect(circuit, Gate_getOutput(O4), Gate_getInput(O6, 1));
  101.  
  102. Circuit_connect(circuit, Gate_getOutput(O5), z);
  103. Circuit_connect(circuit, Gate_getOutput(O6), d);
  104.  
  105. return circuit;
  106. }
  107.  
  108.  
  109. static void test3In2Out(Circuit* circuit, bool in0, bool in1, bool in2) {
  110. Circuit_setInput(circuit, 0, in0);
  111. Circuit_setInput(circuit, 1, in1);
  112. Circuit_setInput(circuit, 2, in2);
  113. Circuit_update(circuit);
  114. bool out0 = Circuit_getOutput(circuit, 0);
  115. bool out1 = Circuit_getOutput(circuit, 1);
  116. printf("%s %s %s -> %s, %s\n", b2s(in0), b2s(in1), b2s(in2), b2s(out0), b2s(out1));
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement