diff --git a/src/Simulation.cpp b/src/Simulation.cpp index e643a1e..7fa3b42 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -1,9 +1,9 @@ #include "Simulation.h" -bool Simulation::parse(string fileName) +bool Simulation::parseCircuit(string fileName) { ifstream in; - circuit.open(fileName + ".txt"); + in.open(fileName + ".txt"); if (in.fail()) { cerr << endl << fileName << ".txt could not be opened :("; exit(1); @@ -16,10 +16,11 @@ bool Simulation::parse(string fileName) getline(in, tmpString); while (!in.eof()) { - tmpType << in; + in >> tmpType; + + in >> tmpString; + in >> tmp1; - tmpString << in; - tmp1 << in; if (tmpType == "INPUT") { tmpWire = new wire(tmp1, true, tmpString); wires.push_back(tmpWire); @@ -29,49 +30,49 @@ bool Simulation::parse(string fileName) wires.push_back(tmpWire); } else if (tmpType == "NOT") { - tmp2 << in; + in >> tmp2; tmpGate = new notGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2)); gates.push_back(tmpGate); } else if (tmpType == "AND") { - tmp2 << in; - tmp3 << in; + in >> tmp2; + in >> tmp3; tmpGate = new andGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2), findWire(tmp3)); gates.push_back(tmpGate); } else if (tmpType == "NAND") { - tmp2 << in; - tmp3 << in; + in >> tmp2; + in >> tmp3; tmpGate = new nandGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2), findWire(tmp3)); gates.push_back(tmpGate); } else if (tmpType == "OR") { - tmp2 << in; - tmp3 << in; + in >> tmp2; + in >> tmp3; tmpGate = new orGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2), findWire(tmp3)); gates.push_back(tmpGate); } else if (tmpType == "XOR") { - tmp2 << in; - tmp3 << in; + in >> tmp2; + in >> tmp3; tmpGate = new xorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2), findWire(tmp3)); gates.push_back(tmpGate); } else if (tmpType == "NOR") { - tmp2 << in; - tmp3 << in; + in >> tmp2; + in >> tmp3; tmpGate = new norGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2), findWire(tmp3)); gates.push_back(tmpGate); } else if (tmpType == "XNOR") { - tmp2 << in; - tmp3 << in; + in >> tmp2; + in >> tmp3; tmpGate = new xnorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2), findWire(tmp3)); gates.push_back(tmpGate); @@ -86,3 +87,17 @@ void Simulation::simulate() void Simulation::print() { } + +wire * Simulation::findWire(int n) +{ + for (auto i = wires.begin(); i != wires.end(); ++i) { + if (n == (**i).getNumber()) return *i; + } + return nullptr; +} + +int Simulation::getDelay(string d) +{ + d.resize(d.size() - 2); + return atoi(d.c_str()); +} diff --git a/src/Simulation.h b/src/Simulation.h index ac2f5f7..0ff5b5f 100644 --- a/src/Simulation.h +++ b/src/Simulation.h @@ -20,10 +20,13 @@ using namespace std; class Simulation { public: - bool parse(string fileName); + bool parseCircuit(string fileName); void simulate(); void print(); private: + wire* findWire(int n); + int getDelay(string d); + priority_queue e; vector gates; vector wires; diff --git a/src/andGate.cpp b/src/andGate.cpp index eba33fd..9ccaac8 100644 --- a/src/andGate.cpp +++ b/src/andGate.cpp @@ -24,3 +24,8 @@ int andGate::evaluate(int evTime) { } } } + +int andGate::getDelay() +{ + return delay; +} diff --git a/src/andGate.h b/src/andGate.h index 795f28b..78e8470 100644 --- a/src/andGate.h +++ b/src/andGate.h @@ -9,6 +9,7 @@ class andGate : public gate { andGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, wire* wire3); void evaluate(int evTime); + int getDelay(); }; #endif // !AND diff --git a/src/nandGate.cpp b/src/nandGate.cpp index 7e87da9..d35ff35 100644 --- a/src/nandGate.cpp +++ b/src/nandGate.cpp @@ -23,4 +23,9 @@ void nandGate::evaluate(int evTime) out->setValue(0, evTime + delay); } } -} \ No newline at end of file +} + +int nandGate::getDelay() +{ + return delay; +} diff --git a/src/nandGate.h b/src/nandGate.h index bb0c001..e1b8163 100644 --- a/src/nandGate.h +++ b/src/nandGate.h @@ -10,6 +10,7 @@ class nandGate : public gate { public: nandGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, wire* wire3); void evaluate(int time); + int getDelay(); }; #endif // !NAND diff --git a/src/norGate.cpp b/src/norGate.cpp index 1538430..934bcec 100644 --- a/src/norGate.cpp +++ b/src/norGate.cpp @@ -25,3 +25,8 @@ void norGate::evaluate(int evTime) { } } } + +int norGate::getDelay() +{ + return delay; +} diff --git a/src/norGate.h b/src/norGate.h index 2a8fa5d..e87a50a 100644 --- a/src/norGate.h +++ b/src/norGate.h @@ -9,6 +9,7 @@ class norGate : public gate { norGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, wire* wire3); void evaluate(int evTime); + int getDelay(); }; #endif // !NOR diff --git a/src/notGate.cpp b/src/notGate.cpp index 1513603..60c8787 100644 --- a/src/notGate.cpp +++ b/src/notGate.cpp @@ -24,3 +24,8 @@ void notGate::evaluate(int evTime) { } } } + +int notGate::getDelay() +{ + return delay; +} diff --git a/src/notGate.h b/src/notGate.h index 0760c1e..ac742f8 100644 --- a/src/notGate.h +++ b/src/notGate.h @@ -8,6 +8,7 @@ class notGate : public gate { public: notGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2); void evaluate(int evTime); + int getDelay(); }; #endif // !NOT diff --git a/src/orGate.cpp b/src/orGate.cpp index 031f0d9..9ba2f6d 100644 --- a/src/orGate.cpp +++ b/src/orGate.cpp @@ -25,3 +25,8 @@ void orGate::evaluate(int evTime) { } } } + +int orGate::getDelay() +{ + return delay; +} diff --git a/src/orGate.h b/src/orGate.h index dfcbd8e..0a9f9e5 100644 --- a/src/orGate.h +++ b/src/orGate.h @@ -9,6 +9,7 @@ class orGate : public gate { orGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, wire* wire3); void evaluate(int evTime); + int getDelay(); }; #endif // !OR diff --git a/src/xnorGate.cpp b/src/xnorGate.cpp index 1b8fb33..8d102ea 100644 --- a/src/xnorGate.cpp +++ b/src/xnorGate.cpp @@ -27,3 +27,8 @@ void xnorGate::evaluate(int evTime) { } } } + +int xnorGate::getDelay() +{ + return delay; +} diff --git a/src/xnorGate.h b/src/xnorGate.h index 334714c..1b4ecda 100644 --- a/src/xnorGate.h +++ b/src/xnorGate.h @@ -9,6 +9,7 @@ class xnorGate : public gate { xnorGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, wire* wire3); void evaluate(int evTime); + int getDelay(); }; #endif // !XNOR diff --git a/src/xorGate.cpp b/src/xorGate.cpp index b5cb7d4..74292bf 100644 --- a/src/xorGate.cpp +++ b/src/xorGate.cpp @@ -27,3 +27,8 @@ void xorGate::evaluate(int evTime) { } } } + +int xorGate::getDelay() +{ + return delay; +} diff --git a/src/xorGate.h b/src/xorGate.h index a429fa2..eb593c4 100644 --- a/src/xorGate.h +++ b/src/xorGate.h @@ -9,6 +9,7 @@ class xorGate : public gate { xorGate(priority_queue *eQueue, int d, wire* wire1, wire* wire2, wire* wire3); void evaluate(int evTime); + int getDelay(); }; #endif // !XOR