diff --git a/src/Simulation.cpp b/src/Simulation.cpp index 77e772b..82d7f4d 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -1,6 +1,7 @@ #include "Simulation.h" #include #include +#include bool Simulation::parseCircuit(string fileName) { @@ -138,7 +139,7 @@ bool Simulation::parseVector(string fileName) { } } -void Simulation::simulate() { +void Simulation::simulate(int simTime) { // loop through event queue while(!e.empty()) { bool changed; @@ -151,7 +152,7 @@ void Simulation::simulate() { output->setValue(tmpEvent.getValue(), tmpEvent.getTime()); // if the wire value changes, evaluate gates - if(changed && !(tmpEvent.getTime() > 60)) { + if(changed && !(tmpEvent.getTime() > simTime)) { Gate * tmpGate; Event gateEvent(-1, -1, nullptr); int index = 0; @@ -170,7 +171,7 @@ void Simulation::simulate() { } } -void Simulation::print() +void Simulation::print(int simTime) { int lastTime = 0; int tmpTime = 0; @@ -181,18 +182,22 @@ void Simulation::print() } } + + cout << "\n\nWire Traces: \n"; // now iterate through wires, printing each of them for(auto i = wires.begin(); i != wires.end(); ++i) { (**i).setLast(lastTime); + (**i).setPrintLen(simTime); cout << **i; } int t = 0; - cout << setw(10) << "TIME "; - while(t <= 60 && t <= lastTime) { - cout << setw(5) << left << t; + cout << setw(6) << "TIME: "; + while(t < simTime && t <= lastTime) { + cout << setfill('.') << setw(5) << left << t; t += 5; } + cout << t << endl; } Wire * Simulation::findWire(int n) diff --git a/src/Simulation.h b/src/Simulation.h index dc3c4b8..9707f54 100644 --- a/src/Simulation.h +++ b/src/Simulation.h @@ -22,8 +22,8 @@ class Simulation { public: bool parseCircuit(string fileName); bool parseVector(string fileName); - void simulate(); - void print(); + void simulate(int); + void print(int); private: Wire* findWire(int n); int getDelay(string d); diff --git a/src/Wire.cpp b/src/Wire.cpp index 04eb556..79d291b 100644 --- a/src/Wire.cpp +++ b/src/Wire.cpp @@ -60,7 +60,7 @@ void Wire::convertToIO(string newName) isPrint = true; } -Gate * Wire::getGate(int index) +Gate * Wire::getGate(int index) const { if (index < gates.size()) { return gates[index]; @@ -73,6 +73,11 @@ void Wire::setLast(int last) lastEvent = last; } +void Wire::setPrintLen(int len) +{ + printLen = len; +} + int Wire::getNumber() const { return WireNumber; @@ -97,12 +102,12 @@ ostream & operator<<(ostream & out, const Wire & c) { if (c.isPrint) { int len = 0; - out << setw(10) << c.name + " "; - while (len <= 60 && len <= c.lastEvent) + out << setw(6) << c.name + ": "; + while (len <= c.printLen && len <= c.lastEvent) { out << setw(0) << c.getChar(len++); } - out << endl; + out << endl << endl; } return out; } diff --git a/src/Wire.h b/src/Wire.h index f874183..6b96bec 100644 --- a/src/Wire.h +++ b/src/Wire.h @@ -25,13 +25,14 @@ class Wire { void setValue(int newValue, int setTime); bool doesChange(int newValue, int setTime); void convertToIO(string newName); - Gate* getGate(int index); + Gate* getGate(int index) const; void setLast(int last); + void setPrintLen(int); friend ostream& operator<<(ostream &out, const Wire &c); private: - int WireNumber, value, lastEvent; + int WireNumber, value, lastEvent, printLen; vector historyTimes; vector historyValues; string name; diff --git a/src/radec.cpp b/src/radec.cpp index dc5281c..ca180ea 100644 --- a/src/radec.cpp +++ b/src/radec.cpp @@ -7,9 +7,17 @@ int main() { // to simulate string fileName; Simulation e; + int len = 60; cout << "Please enter filename: "; getline(cin, fileName); + cout << "How long do you want to simulate to (default 60)? "; + if (cin.peek() == '\n') { + len = 60; + } + else if (!(cin >> len)) { + cout << "Invalid input using 60.\n"; + } e.parseCircuit(fileName); // 2. Parse the vector file to initialize the simulation Queue with initial @@ -21,8 +29,9 @@ int main() { // second, determine if e causes a future Wire state change // third, create and queue any future Wire state changes as new Events // fourth, apply e's effects - e.simulate(); + e.simulate(len); // 4. Print the results of the simulation - e.print(); + e.print(len); + system("pause"); }