Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.35 KB | None | 0 0
  1. /******************************************************
  2. **Group: 15
  3. **Semester: January 2018
  4. ** Student info:
  5. ** 1. Lim Wei Sean , 1702106, 3E
  6. ** 2.
  7. *******************************************************/
  8.  
  9. /******************************************************
  10. Pre-processor Directives
  11. *******************************************************/
  12.  
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <fstream>
  16. #include <string>
  17. #include <vector>
  18. #include <map>
  19.  
  20. #define vect vector
  21. #define boolmap map<string, bool>
  22. #define gatemap map<string, Gate*>
  23. #define inputmap map<string, int>
  24. #define TT map<string, string>
  25. #define toupper(a) transform(a.begin(), a.end(), a.begin(), toupper)
  26. #define tolower(a) transform(a.begin(), a.end(), a.begin(), tolower)
  27.  
  28. using namespace std;
  29.  
  30. /******************************************************
  31. Global Functions
  32. *******************************************************/
  33.  
  34. vect<string> CSVExtract(string str) {
  35. str = str.substr(str.find_first_not_of(" \t\n,"), str.find_last_not_of(" \t\n,") - str.find_first_not_of(" \t\n,") + 1); //to strip spaces on the sides
  36. size_t begin = 0;
  37. size_t end = str.find_first_of(",", begin);
  38. vect<string> returnVect;
  39.  
  40. while (1) {
  41. string extract = str.substr(begin, end - begin);
  42.  
  43. returnVect.push_back(extract);
  44.  
  45. if (end == string::npos) break;
  46.  
  47. begin = str.find_first_not_of(" \t,", end + 1);
  48. end = str.find_first_of(",", begin);
  49. }
  50.  
  51. return returnVect;
  52. }
  53.  
  54. /******************************************************
  55. Class Definitions
  56. *******************************************************/
  57.  
  58. class Circuit;
  59.  
  60. class Gate {
  61. protected:
  62. Circuit * circuit;
  63. string type, name;
  64. int in_max, out_max, not_connect_out;
  65. vect<string> in_list;
  66. vect<int> out_list;
  67. vect<bool> out_connect;
  68. public:
  69. Gate(Circuit* _circuit, string _name, string _type, int _in_max, int _out_max) {
  70. circuit = _circuit;
  71. name = _name;
  72. type = _type;
  73. in_max = _in_max;
  74. out_max = _out_max;
  75. not_connect_out = _out_max;
  76. for (int i = 0; i < in_max; i++) {
  77. in_list.push_back("");
  78. }
  79. for (int i = 0; i < out_max; i++) {
  80. out_list.push_back(-2);
  81. out_connect.push_back(0);
  82. }
  83. }
  84.  
  85. virtual vect<int> compute() = 0;
  86.  
  87. virtual void setInputs(int in_pin, string path) {
  88. if (in_pin < in_max) {
  89. in_list[in_pin] = path;
  90. }
  91. else {
  92. cout << "Input pin index out of range." << endl;
  93. cout << "Gate Name: " << name << endl;
  94. cout << "Gate in_max: " << in_max << endl;
  95. cout << "Gate out_max: " << out_max << endl;
  96. cout << "Exiting..." << endl;
  97. getchar();
  98. exit(0);
  99. }
  100. }
  101.  
  102. int getOutputVal(Circuit* cir, string str);
  103.  
  104. vect<int> getOutList() {
  105. return out_list;
  106. }
  107.  
  108. vect<bool> getOutConnect() {
  109. return out_connect;
  110. }
  111.  
  112. string operator^(const int _pin) {
  113. return ("O" + to_string(0) + " = " + to_string(this->out_list[_pin]));
  114. }
  115.  
  116. friend ostream& operator<<(ostream& os, const Gate& G);
  117. };
  118.  
  119. class OR : public Gate {
  120. public:
  121. OR(Circuit* _circuit, string _name, int _in_max) : Gate(_circuit, _name, "OR", _in_max, 1) {
  122. cout << "################################################################" << endl;
  123. cout << "# Register Gate:" << endl;
  124. cout << "# Gate Name: " << name << endl;
  125. cout << "# Gate Type: " << type << endl;
  126. cout << "# in_max: " << in_max << endl;
  127. cout << "# out_max: " << out_max << endl;
  128. cout << "################################################################" << endl;
  129. }
  130. vect<int> compute() {
  131. cout << "Computing " << name << endl;
  132. out_list[0] = -2;
  133. if (in_list[0] == "") return out_list;
  134.  
  135. out_list[0] = getOutputVal(circuit, in_list[0]);
  136.  
  137. for (int i = 1; i < in_max; i++) {
  138. if (in_list[i] == "") {
  139. out_list[0] = -2;
  140. return out_list;
  141. }
  142.  
  143. int _temp_val = getOutputVal(circuit, in_list[i]);
  144. out_list[0] = out_list[0] | getOutputVal(circuit, in_list[i]);
  145. }
  146. return out_list;
  147. }
  148. };
  149.  
  150. class AND : public Gate {
  151. public:
  152. AND(Circuit* _circuit, string _name, int _in_max) : Gate(_circuit, _name, "AND", _in_max, 1) {
  153. cout << "################################################################" << endl;
  154. cout << "# Register Gate:" << endl;
  155. cout << "# Gate Name: " << name << endl;
  156. cout << "# Gate Type: " << type << endl;
  157. cout << "# in_max: " << in_max << endl;
  158. cout << "# out_max: " << out_max << endl;
  159. cout << "################################################################" << endl;
  160. }
  161. vect<int> compute() {
  162. cout << "Computing " << name << endl;
  163. out_list[0] = -2;
  164. if (in_list[0] == "") return out_list;
  165.  
  166. out_list[0] = getOutputVal(circuit, in_list[0]);
  167.  
  168. for (int i = 1; i < in_max; i++) {
  169. if (in_list[i] == "") {
  170. out_list[0] = -2;
  171. return out_list;
  172. }
  173.  
  174. int _temp_val = getOutputVal(circuit, in_list[i]);
  175. out_list[0] = out_list[0] & getOutputVal(circuit, in_list[i]);
  176. }
  177. return out_list;
  178. }
  179. };
  180.  
  181. class NOT : public Gate {
  182. public:
  183. NOT(Circuit* _circuit, string _name) : Gate(_circuit, _name, "NOT", 1, 1) {
  184. cout << "################################################################" << endl;
  185. cout << "# Register Gate:" << endl;
  186. cout << "# Gate Name: " << name << endl;
  187. cout << "# Gate Type: " << type << endl;
  188. cout << "# in_max: " << in_max << endl;
  189. cout << "# out_max: " << out_max << endl;
  190. cout << "################################################################" << endl;
  191. }
  192. vect<int> compute() {
  193. cout << "Computing " << name << endl;
  194. out_list[0] = -2;
  195. if (in_list[0] == "") return out_list;
  196. out_list[0] = !(getOutputVal(circuit, in_list[0]));
  197. return out_list;
  198. }
  199. };
  200.  
  201. class NOR : public Gate {
  202. public:
  203. NOR(Circuit* _circuit, string _name, int _in_max) : Gate(_circuit, _name, "NOR", _in_max, 1) {
  204. cout << "################################################################" << endl;
  205. cout << "# Register Gate:" << endl;
  206. cout << "# Gate Name: " << name << endl;
  207. cout << "# Gate Type: " << type << endl;
  208. cout << "# in_max: " << in_max << endl;
  209. cout << "# out_max: " << out_max << endl;
  210. cout << "################################################################" << endl;
  211. }
  212. vect<int> compute() {
  213. cout << "Computing " << name << endl;
  214. out_list[0] = -2;
  215. if (in_list[0] == "") return out_list;
  216.  
  217. out_list[0] = getOutputVal(circuit, in_list[0]);
  218.  
  219. for (int i = 1; i < in_max; i++) {
  220. if (in_list[i] == "") {
  221. out_list[0] = -2;
  222. return out_list;
  223. }
  224.  
  225. int _temp_val = getOutputVal(circuit, in_list[i]);
  226. out_list[0] = out_list[0] | getOutputVal(circuit, in_list[i]);
  227. }
  228. out_list[0] = !(out_list[0]);
  229. return out_list;
  230. }
  231. };
  232.  
  233. class NAND : public Gate {
  234. public:
  235. NAND(Circuit* _circuit, string _name, int _in_max) : Gate(_circuit, _name, "NAND", _in_max, 1) {
  236. cout << "################################################################" << endl;
  237. cout << "# Register Gate:" << endl;
  238. cout << "# Gate Name: " << name << endl;
  239. cout << "# Gate Type: " << type << endl;
  240. cout << "# in_max: " << in_max << endl;
  241. cout << "# out_max: " << out_max << endl;
  242. cout << "################################################################" << endl;
  243. }
  244. vect<int> compute() {
  245. cout << "Computing " << name << endl;
  246. out_list[0] = -2;
  247. if (in_list[0] == "") return out_list;
  248.  
  249. out_list[0] = getOutputVal(circuit, in_list[0]);
  250.  
  251. for (int i = 1; i < in_max; i++) {
  252. if (in_list[i] == "") {
  253. out_list[0] = -2;
  254. return out_list;
  255. }
  256.  
  257. int _temp_val = getOutputVal(circuit, in_list[i]);
  258. out_list[0] = out_list[0] & getOutputVal(circuit, in_list[i]);
  259. }
  260. out_list[0] = !(out_list[0]);
  261. return out_list;
  262. }
  263. };
  264.  
  265. class XOR : public Gate {
  266. public:
  267. XOR(Circuit* _circuit, string _name, int _in_max) : Gate(_circuit, _name, "XOR", _in_max, 1) {
  268. cout << "################################################################" << endl;
  269. cout << "# Register Gate:" << endl;
  270. cout << "# Gate Name: " << name << endl;
  271. cout << "# Gate Type: " << type << endl;
  272. cout << "# in_max: " << in_max << endl;
  273. cout << "# out_max: " << out_max << endl;
  274. cout << "################################################################" << endl;
  275. }
  276. vect<int> compute() {
  277. cout << "Computing " << name << endl;
  278. out_list[0] = -2;
  279. if (in_list[0] == "") return out_list;
  280.  
  281. out_list[0] = getOutputVal(circuit, in_list[0]);
  282.  
  283. for (int i = 1; i < in_max; i++) {
  284. if (in_list[i] == "") {
  285. out_list[0] = -2;
  286. return out_list;
  287. }
  288.  
  289. int _temp_val = getOutputVal(circuit, in_list[i]);
  290. out_list[0] = out_list[0] ^ getOutputVal(circuit, in_list[i]);
  291. }
  292. return out_list;
  293. }
  294. };
  295.  
  296. class DFF : public Gate {
  297. //in_list[0] == D, in_list[1] == CLK, out_list[0] == Q
  298. int memory;
  299. public:
  300. DFF(Circuit* _circuit, string _name, int _memory) : Gate(_circuit, _name, "DFF", 2, 1) {
  301. if (_memory == 1 || _memory == 0)memory = _memory; //default memory
  302. else {
  303. cout << "Invalid memory data!" << endl;
  304. getchar();
  305. exit(0);
  306. }
  307. cout << "################################################################" << endl;
  308. cout << "# Register Gate:" << endl;
  309. cout << "# Gate Name: " << name << endl;
  310. cout << "# Gate Type: " << type << endl;
  311. cout << "# in_max: " << in_max << endl;
  312. cout << "# out_max: " << out_max << endl;
  313. cout << "# memory: " << memory << endl;
  314. cout << "################################################################" << endl;
  315. }
  316. vect<int> compute() {
  317. cout << "Computing " << name << endl;
  318. out_list[0] = -2;
  319. if (in_list[0] == "" || in_list[1] == "") return out_list;
  320.  
  321. int D = getOutputVal(circuit, in_list[0]);
  322. int CLK = getOutputVal(circuit, in_list[1]);
  323.  
  324. if (CLK == 0) {
  325. out_list[0] = memory;
  326. }
  327. else {
  328. memory = D;
  329. out_list[0] = D;
  330. }
  331.  
  332. return out_list;
  333. }
  334. };
  335.  
  336. class TFF : public Gate {
  337. //in_list[0] == T, in_list[1] == CLK, out_list[0] == Q
  338. int memory;
  339. public:
  340. TFF(Circuit* _circuit, string _name, int _memory) : Gate(_circuit, _name, "TFF", 2, 1) {
  341. if (_memory == 1 || _memory == 0)memory = _memory; //default memory
  342. else {
  343. cout << "Invalid memory data!" << endl;
  344. getchar();
  345. exit(0);
  346. }
  347. cout << "################################################################" << endl;
  348. cout << "# Register Gate:" << endl;
  349. cout << "# Gate Name: " << name << endl;
  350. cout << "# Gate Type: " << type << endl;
  351. cout << "# in_max: " << in_max << endl;
  352. cout << "# out_max: " << out_max << endl;
  353. cout << "# memory: " << memory << endl;
  354. cout << "################################################################" << endl;
  355. }
  356. vect<int> compute() {
  357. cout << "Computing " << name << endl;
  358. out_list[0] = -2;
  359. if (in_list[0] == "" || in_list[1] == "") return out_list;
  360.  
  361. int T = getOutputVal(circuit, in_list[0]);
  362. int CLK = getOutputVal(circuit, in_list[1]);
  363.  
  364. if (CLK == 0) {
  365. out_list[0] = memory;
  366. }
  367. else {
  368. if (T == 1) {
  369. memory = !(memory);
  370. out_list[0] = memory;
  371. }
  372. else out_list[0] = memory;
  373. }
  374. return out_list;
  375. }
  376. };
  377.  
  378. class Other : public Gate {
  379. TT truth_table;
  380. public:
  381. Other(Circuit* _circuit, string _name, int _in_max, int _out_max, string path) : Gate(_circuit, _name, path, _in_max, _out_max) {
  382. //construct Truth Table
  383. //read truth table from path
  384. tolower(path);//change path to lowercase letters
  385. fstream _TT(path + ".csv", ios::in);
  386.  
  387. if (_TT.is_open()) {
  388. cout << "Reading the Truth Table from " << path + ".csv" << "..." << endl;
  389. int cnt = 0;
  390. string input;
  391.  
  392. while (_TT.peek() != EOF) {
  393. getline(_TT, input);
  394.  
  395. if (cnt == 0) {//match the number of input and output with in_max and out_max
  396.  
  397. }
  398.  
  399. if (cnt > 0) { // skip the header in the file
  400. vect<string> _input = CSVExtract(input);//extract the CSV data
  401. int _temp = 0;
  402. for (auto const& a : _input) {//validate input
  403. if (atoi(a.c_str()) != 0 && atoi(a.c_str()) != 1) {
  404. cout << "There is an invalid input value in " << path << endl;
  405. cout << "Exiting..." << endl;
  406. getchar();
  407. exit(0);
  408. }
  409. _temp++;
  410. }
  411. string _in = "";
  412. string _out = "";
  413. for (int i = in_max; i > 0; i--) {
  414. _in += _input[in_max - i];
  415. if (i != 1)_in += ",";
  416. }
  417. for (int i = out_max; i > 0; i--) {
  418. _out += _input[out_max + in_max - i];
  419. if (i != 1)_out += ",";
  420. }
  421. truth_table[_in] = _out;
  422. }
  423. cnt++;
  424. }
  425. }
  426. else {
  427. cout << path + ".csv" << " not found. Exiting..." << endl;
  428. getchar();
  429. exit(0);
  430. }
  431. _TT.close();
  432.  
  433. //print details
  434. cout << "################################################################" << endl;
  435. cout << "# Register Gate:" << endl;
  436. cout << "# Gate Name: " << name << endl;
  437. cout << "# Gate Type: " << type << endl;
  438. cout << "# in_max: " << in_max << endl;
  439. cout << "# out_max: " << out_max << endl;
  440. cout << "# Truth Table: " << path << endl;
  441. cout << "################################################################" << endl;
  442. }
  443.  
  444. vect<int> compute() {
  445. cout << "Computing " << name << endl;
  446. string _in = "";
  447. string _out = "";
  448. int cnt = 0;
  449. for (auto const& a : in_list) {
  450. if (a == "") return vect<int>(out_max, -2);
  451. _in += to_string(getOutputVal(circuit, a));
  452. if (cnt != in_max - 1)_in += ",";
  453. cnt++;
  454. }
  455. try {
  456. _out = truth_table.at(_in);
  457. vect<string> extract = CSVExtract(_out);
  458. for (int i = 0; i < extract.size(); i++) {
  459. int num = atoi(extract.at(i).c_str());
  460. out_list[i] = num;
  461. }
  462. }
  463. catch (const out_of_range& oor) {
  464. cout << oor.what() << endl << "Exiting..." << endl;
  465. getchar();
  466. exit(0);
  467. }
  468. return out_list;
  469. }
  470. };
  471.  
  472. class Circuit {
  473. private:
  474. string filenames[4];//gates, inputs, connections, outputs
  475. gatemap gates;
  476. boolmap check;//1 if gate is compute
  477. boolmap out_check;//1 if all output of gate is connected
  478. inputmap inputs;
  479.  
  480. public:
  481. Circuit(string _gate_file, string _input_file, string _connection_file, string _output_file) {
  482. filenames[0] = _gate_file + ".csv";
  483. filenames[1] = _input_file + ".csv";
  484. filenames[2] = _connection_file + ".csv";
  485. filenames[3] = _output_file + ".txt";
  486. }
  487. friend int Gate::getOutputVal(Circuit* cir, string str);
  488. void getOut(string filepath) {
  489. //open output file
  490. fstream outputFile(filepath, ios::out);
  491.  
  492. if (outputFile.is_open()) {
  493. cout << "Writing to " << filepath << "..." << endl;
  494.  
  495. for (auto const& x : out_check) {
  496. if (x.second == 0) {
  497. Gate* _gate = gates[x.first];
  498. for (int b = 0; b < _gate->getOutConnect().size(); b++) {
  499. if (_gate->getOutConnect()[b] == 0) {
  500. outputFile << *_gate << ": " << (*_gate ^ b) << endl;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. else {
  507. cout << filepath << " not found. Exiting..." << endl;
  508. getchar();
  509. exit(0);
  510. }
  511. outputFile.close();
  512. }
  513. bool terminator() {
  514. for (auto const& a : check) {
  515. if (a.second == 0) {
  516. return true;
  517. }
  518. else {
  519. return false;
  520. }
  521. }
  522. }
  523. void init_gate(string filepath) {
  524. fstream gateFile(filepath, ios::in);
  525.  
  526. if (gateFile.is_open()) {
  527. cout << "Reading the gates..." << endl;
  528. int cnt = 0;
  529. string input;
  530.  
  531. while (gateFile.peek() != EOF) {
  532. getline(gateFile, input);
  533.  
  534. if (cnt > 0) { // skip the header in the file
  535. vect<string> _input = CSVExtract(input);//extract the CSV data
  536. toupper(_input[0]);
  537. Circuit* self = this;
  538. if (_input[0] == "OR") {
  539. if (atoi(_input[3].c_str()) != 1) cout << "Only one output is valid for OR gate." << endl << "Setting: out_max = 1" << endl;
  540. //create object
  541. OR* _gate = new OR(self, _input[1], atoi(_input[2].c_str()));
  542. //append to gates
  543. gates[_input[1]] = _gate;
  544. //append to check
  545. check[_input[1]] = 0;
  546. //append to out_check
  547. out_check[_input[1]] = 0;
  548.  
  549. }
  550. else if (_input[0] == "AND") {
  551. if (atoi(_input[3].c_str()) != 1) cout << "Only one output is valid for AND gate." << endl << "Setting: out_max = 1" << endl;
  552. //create object
  553. AND* _gate = new AND(self, _input[1], atoi(_input[2].c_str()));
  554. //append to gates
  555. gates[_input[1]] = _gate;
  556. //append to check
  557. check[_input[1]] = 0;
  558. //append to out_check
  559. out_check[_input[1]] = 0;
  560. }
  561. else if (_input[0] == "NOT") {
  562. if (atoi(_input[2].c_str()) != 1 || atoi(_input[3].c_str()) != 1) cout << "Only one input and output is valid for NOT gate." << endl << "Setting: in_max = 1, out_max = 1" << endl;
  563. //create object
  564. NOT* _gate = new NOT(self, _input[1]);
  565. //append to gates
  566. gates[_input[1]] = _gate;
  567. //append to check
  568. check[_input[1]] = 0;
  569. //append to out_check
  570. out_check[_input[1]] = 0;
  571. }
  572. else if (_input[0] == "NOR") {
  573. if (atoi(_input[3].c_str()) != 1) cout << "Only one output is valid for NOR gate." << endl << "Setting: out_max = 1" << endl;
  574. //create object
  575. NOR* _gate = new NOR(self, _input[1], atoi(_input[2].c_str()));
  576. //append to gates
  577. gates[_input[1]] = _gate;
  578. //append to check
  579. check[_input[1]] = 0;
  580. //append to out_check
  581. out_check[_input[1]] = 0;
  582. }
  583. else if (_input[0] == "NAND") {
  584. if (atoi(_input[3].c_str()) != 1) cout << "Only one output is valid for NAND gate." << endl << "Setting: out_max = 1" << endl;
  585. //create object
  586. NAND* _gate = new NAND(self, _input[1], atoi(_input[2].c_str()));
  587. //append to gates
  588. gates[_input[1]] = _gate;
  589. //append to check
  590. check[_input[1]] = 0;
  591. //append to out_check
  592. out_check[_input[1]] = 0;
  593. }
  594. else if (_input[0] == "XOR") {
  595. if (atoi(_input[3].c_str()) != 1) cout << "Only one output is valid for XOR gate." << endl << "Setting: out_max = 1" << endl;
  596. //create object
  597. XOR* _gate = new XOR(self, _input[1], atoi(_input[2].c_str()));
  598. //append to gates
  599. gates[_input[1]] = _gate;
  600. //append to check
  601. check[_input[1]] = 0;
  602. //append to out_check
  603. out_check[_input[1]] = 0;
  604. }
  605. else if (_input[0] == "DFF") {
  606. if (_input[4] != "0" && _input[4] != "1") {
  607. cout << "Unidentified memory!" << endl << "Setting: memory = 0";
  608. _input[4] = "0";
  609. }
  610. if (atoi(_input[2].c_str()) != 2 || atoi(_input[3].c_str()) != 1) cout << "Only 2 inputs and one output is valid for DFF gate." << endl << "Setting: in_max = 2, out_max = 1" << endl;
  611. //create object
  612. DFF* _gate = new DFF(self, _input[1], atoi(_input[4].c_str()));
  613. //append to gates
  614. gates[_input[1]] = _gate;
  615. //append to check
  616. check[_input[1]] = 0;
  617. //append to out_check
  618. out_check[_input[1]] = 0;
  619. }
  620. else if (_input[0] == "TFF") {
  621. if (_input[4] != "0" && _input[4] != "1") {
  622. cout << "Unidentified memory!" << endl << "Setting: memory = 0";
  623. _input[4] = "0";
  624. }
  625. if (atoi(_input[2].c_str()) != 2 || atoi(_input[3].c_str()) != 1) cout << "Only 2 inputs and one output is valid for TFF gate." << endl << "Setting: in_max = 2, out_max = 1" << endl;
  626. //create object
  627. TFF* _gate = new TFF(self, _input[1], atoi(_input[4].c_str()));
  628. //append to gates
  629. gates[_input[1]] = _gate;
  630. //append to check
  631. check[_input[1]] = 0;
  632. //append to out_check
  633. out_check[_input[1]] = 0;
  634. }
  635. else {
  636. //create object
  637. toupper(_input[0]);
  638. string _path = _input[0];
  639. Other* _gate = new Other(self, _input[1], atoi(_input[2].c_str()), atoi(_input[3].c_str()), _path);
  640. //append to gates
  641. gates[_input[1]] = _gate;
  642. //append to check
  643. check[_input[1]] = 0;
  644. //append to out_check
  645. out_check[_input[1]] = 0;
  646. }
  647. }
  648. cnt++;
  649. }
  650. }
  651. else {
  652. cout << filepath << " not found. Exiting..." << endl;
  653. getchar();
  654. exit(0);
  655. }
  656. gateFile.close();
  657. }
  658. void init_ext_input(string filepath) {
  659. fstream inputFile(filepath, ios::in);
  660.  
  661. if (inputFile.is_open()) {
  662. cout << "Reading the inputs..." << endl;
  663. int cnt = 0;
  664. string input;
  665.  
  666. while (inputFile.peek() != EOF) {
  667. getline(inputFile, input);
  668.  
  669. if (cnt > 0) { // skip the header in the file
  670. vect<string> _input = CSVExtract(input);//extract the CSV data
  671. if (_input[1] != "0" && _input[1] != "1") { //Validate input value
  672. cout << "Invalid input value" << endl;
  673. getchar();
  674. exit(0);
  675. }
  676. inputs[_input[0]] = atoi(_input[1].c_str());
  677. }
  678. cnt++;
  679. }
  680. }
  681. else {
  682. cout << filepath << " not found. Exiting..." << endl;
  683. getchar();
  684. exit(0);
  685. }
  686. inputFile.close();
  687. }
  688. void init_connection(string filepath) {
  689. fstream connectionFile(filepath, ios::in);
  690.  
  691. if (connectionFile.is_open()) {
  692. cout << "Reading the connections..." << endl;
  693. int cnt = 0;
  694. string input;
  695.  
  696. while (connectionFile.peek() != EOF) {
  697. getline(connectionFile, input);
  698. if (cnt > 0) { // skip the header in the file
  699. vect<string> _input = CSVExtract(input);//extract the CSV data
  700. try { gates.at(_input[0]); }
  701. catch (const out_of_range& oor) {
  702. cout << oor.what() << endl << "Exiting..." << endl;
  703. getchar();
  704. connectionFile.close();
  705. exit(0);
  706. }
  707. gates.at(_input[0])->setInputs(atoi(_input[1].c_str()), _input[2]);
  708. }
  709. cnt++;
  710. }
  711. }
  712. else {
  713. cout << filepath << " not found. Exiting..." << endl;
  714. getchar();
  715. exit(0);
  716. }
  717. connectionFile.close();
  718. }
  719. void compute() {//for lazy API users
  720. init_gate(filenames[0]);
  721. init_ext_input(filenames[1]);
  722. init_connection(filenames[2]);
  723.  
  724. while (terminator()) {
  725. for (auto const& a : check) {
  726. if (a.second == 0) {
  727. for (auto const& b : gates[a.first]->compute()) {
  728. if (b == -2) {
  729. cout << "Insufficient connection data!" << endl;
  730. cout << "Gate name: " << a.first << endl;
  731. cout << "Exiting..." << endl;
  732. getchar();
  733. exit(0);
  734. }
  735. else {
  736. check[a.first] = 1;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. getOut(filenames[3]);
  743. cout << "Circuit computation done." << endl;
  744. cout << "Data collected in " << filenames[3] << endl;
  745. }
  746. };
  747.  
  748. /******************************************************
  749. Friend Functions & Operator Overloading
  750. *******************************************************/
  751.  
  752. int Gate::getOutputVal(Circuit* cir, string str) {
  753. //str = "Gate_name Output_pin"
  754. string split = " ";
  755. size_t center = str.find_first_of(split);
  756. string gate_input_name, output_pin;
  757. int output_value;
  758. if (center == string::npos) {
  759. gate_input_name = str;
  760.  
  761. output_value = cir->inputs[gate_input_name];
  762. }
  763. else {
  764. gate_input_name = str.substr(0, center);
  765. output_pin = str.substr(center + 1);
  766. output_value = cir->gates[gate_input_name]->out_list[atoi(output_pin.c_str())];
  767. if (cir->gates[gate_input_name]->out_connect[atoi(output_pin.c_str())] == 0) {
  768. cir->gates[gate_input_name]->out_connect[atoi(output_pin.c_str())] = 1;
  769.  
  770. int _state = --cir->gates[gate_input_name]->not_connect_out;
  771. if (_state == 0)cir->out_check[gate_input_name] = 0;
  772. }
  773. if (output_value == -2) {
  774. output_value = cir->gates[gate_input_name]->compute()[atoi(output_pin.c_str())];
  775. if (output_value != -2) {
  776. cir->check[gate_input_name] = 1;
  777. }
  778. else {
  779. cout << "Insufficient connection data!" << endl;
  780. cout << "Gate name: " << gate_input_name << endl;
  781. cout << "Exiting..." << endl;
  782. getchar();
  783. exit(0);
  784. }
  785. }
  786. }
  787. return output_value;
  788. }
  789.  
  790. ostream& operator<<(ostream& os, const Gate& G) {
  791. os << G.type + ", " + G.name;
  792. return os;
  793. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement