diff --git a/src/AndGate.cpp b/src/AndGate.cpp index e257945..72262d6 100644 --- a/src/AndGate.cpp +++ b/src/AndGate.cpp @@ -10,11 +10,14 @@ AndGate::AndGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { } Event AndGate::evaluate(int evTime) { if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) { - return Event(-1, 0, evTime + delay, out); - + return Event(0, evTime + delay, out); + } else if(in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1){ - return Event(-1, 1, evTime + delay, out); + return Event(1, evTime + delay, out); + } + else{ + return Event(-1, evTime + delay, out); } } diff --git a/src/Event.cpp b/src/Event.cpp index 993c911..757ef43 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -1,7 +1,9 @@ #include "Event.h" -Event::Event(int num, int value, int setTime, Wire * output){ - evNum = num; +int Event::numOfEvents = 0; + +Event::Event(int value, int setTime, Wire * output){ + evNum = numOfEvents++; evValue = value; evTime = setTime; out = output; diff --git a/src/NandGate.cpp b/src/NandGate.cpp index bc403ae..7f428dd 100644 --- a/src/NandGate.cpp +++ b/src/NandGate.cpp @@ -1,4 +1,5 @@ #include "NandGate.h" +#include NandGate::NandGate(int d, Wire * wire1, Wire * wire2, Wire * wire3) { @@ -10,11 +11,15 @@ NandGate::NandGate(int d, Wire * wire1, Wire * wire2, Wire * wire3) Event NandGate::evaluate(int evTime) { + cout << evTime << " " << in1->getValue(evTime) << " " << in2->getValue(evTime) << endl; if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) { - return Event(-1, 1, evTime + delay, out); + return Event(1, evTime + delay, out); } - else { - return Event(-1, 0, evTime + delay, out); + else if (in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1) { + return Event(0, evTime + delay, out); + } + else{ + return Event(-1, evTime + delay, out); } } diff --git a/src/NorGate.cpp b/src/NorGate.cpp index 45c5001..be5c3fd 100644 --- a/src/NorGate.cpp +++ b/src/NorGate.cpp @@ -11,10 +11,13 @@ NorGate::NorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { Event NorGate::evaluate(int evTime) { if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) { - return Event(-1, 0, evTime + delay, out); + return Event(0, evTime + delay, out); } else if (in1->getValue(evTime) == 0 && in2->getValue(evTime) == 0) { - return Event(-1, 1, evTime + delay, out); + return Event(1, evTime + delay, out); + } + else{ + return Event(-1, evTime + delay, out); } } diff --git a/src/NotGate.cpp b/src/NotGate.cpp index 90e5684..62aa764 100644 --- a/src/NotGate.cpp +++ b/src/NotGate.cpp @@ -9,11 +9,13 @@ NotGate::NotGate(int d, Wire* wire1, Wire* wire2) { } Event NotGate::evaluate(int evTime) { - if (in1->getValue(evTime) == 0) { - return Event(-1, 0, evTime + delay, out); + if (in1->getValue(evTime) == 1) { + return Event(0, evTime + delay, out); + }else if (in1->getValue(evTime) == 0) { + return Event(1, evTime + delay, out); } - else{ - return Event(-1, 1, evTime + delay, out); + else { + return Event(-1, evTime + delay, out); } } diff --git a/src/OrGate.cpp b/src/OrGate.cpp index 7015261..b185678 100644 --- a/src/OrGate.cpp +++ b/src/OrGate.cpp @@ -11,10 +11,13 @@ OrGate::OrGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { Event OrGate::evaluate(int evTime) { if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) { - return Event(-1, 1, evTime + delay, out); + return Event(1, evTime + delay, out); } else if (in1->getValue(evTime) == 0 && in2->getValue(evTime) == 0) { - return Event(-1, 0, evTime + delay, out); + return Event(0, evTime + delay, out); + } + else{ + return Event(-1, evTime + delay, out); } } diff --git a/src/Simulation.cpp b/src/Simulation.cpp index fd76799..3aa9e32 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -120,7 +120,7 @@ bool Simulation::parseVector(string fileName) { while(true) { if (!(in >> tmpString)) break; if (!(in >> tmpString)) break; - if (!(in >> tmpInt)) break; + if (!(in >> timeInt)) break; if (!(in >> valInt)) break; for(auto i = wires.begin(); i != wires.end(); ++i) { @@ -129,32 +129,31 @@ bool Simulation::parseVector(string fileName) { } } - e.push(Event(eventNum++, valInt, timeInt, tmpWire)); + e.push(Event(valInt, timeInt, tmpWire)); } } void Simulation::simulate() { // loop through event queue while(!e.empty()) { - bool doesChange; + bool changed; Wire * output; Event tmpEvent = e.top(); - e.pop(); output = tmpEvent.getOutput(); - doesChange = output->setValue(tmpEvent.getValue(), tmpEvent.getTime()); + changed = output->doesChange(tmpEvent.getValue(), tmpEvent.getTime()); + output->setValue(tmpEvent.getValue(), tmpEvent.getTime()); // if the wire value changes, evaluate gates - if(doesChange) { + if(changed && !(tmpEvent.getTime() > 60)) { Gate * tmpGate; - Event gateEvent(-1, -1, -1, nullptr); + Event gateEvent(-1, -1, nullptr); int index = 0; while(true){ tmpGate = output->getGate(index++); if (tmpGate != nullptr) { gateEvent = tmpGate->evaluate(tmpEvent.getTime()); - gateEvent.setNum(eventNum++); e.push(gateEvent); } else { @@ -162,6 +161,7 @@ void Simulation::simulate() { } } } + e.pop(); } } @@ -207,3 +207,4 @@ int Simulation::getDelay(string d) d.resize(d.size() - 2); return atoi(d.c_str()); } + diff --git a/src/Wire.cpp b/src/Wire.cpp index 8eeb31d..04eb556 100644 --- a/src/Wire.cpp +++ b/src/Wire.cpp @@ -39,17 +39,19 @@ char Wire::getChar(int wantedTime) const } } -bool Wire::setValue(int newValue, int setTime) +void Wire::setValue(int newValue, int setTime) { - if (getValue(setTime) != newValue) { + if (doesChange(newValue, setTime)) { historyTimes.push_back(setTime); historyValues.push_back(newValue); if (lastEvent < setTime) { lastEvent = setTime; } - return true; // I changed the value } - return false; // Nothing changed +} + +bool Wire::doesChange(int newValue, int setTime) { + return getValue(setTime) != newValue; } void Wire::convertToIO(string newName) diff --git a/src/XnorGate.cpp b/src/XnorGate.cpp index 3e6d33f..e7fd91b 100644 --- a/src/XnorGate.cpp +++ b/src/XnorGate.cpp @@ -12,12 +12,15 @@ XnorGate::XnorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { Event XnorGate::evaluate(int evTime) { if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) { if (in1->getValue(evTime) == in2->getValue(evTime)) { - return Event(-1, 1, evTime + delay, out); + return Event(1, evTime + delay, out); } else { - return Event(-1, 0, evTime + delay, out); + return Event(0, evTime + delay, out); } } + else { + return Event(-1, evTime + delay, out); + } } int XnorGate::getDelay() diff --git a/src/XorGate.cpp b/src/XorGate.cpp index f19f86b..29b5f2b 100644 --- a/src/XorGate.cpp +++ b/src/XorGate.cpp @@ -12,12 +12,15 @@ XorGate::XorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) { Event XorGate::evaluate(int evTime) { if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) { if (in1->getValue(evTime) != in2->getValue(evTime)) { - return Event(-1, 1, evTime + delay, out); + return Event(1, evTime + delay, out); } else { - return Event(-1, 0, evTime + delay, out); + return Event(0, evTime + delay, out); } } + else { + return Event(-1, evTime + delay, out); + } } int XorGate::getDelay()