This commit is contained in:
daniel 2017-04-25 13:50:17 -04:00
commit 40d0957420
10 changed files with 59 additions and 32 deletions

View File

@ -10,11 +10,14 @@ AndGate::AndGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
} }
Event AndGate::evaluate(int evTime) { Event AndGate::evaluate(int evTime) {
if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) { 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){ 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);
} }
} }

View File

@ -1,7 +1,9 @@
#include "Event.h" #include "Event.h"
Event::Event(int num, int value, int setTime, Wire * output){ int Event::numOfEvents = 0;
evNum = num;
Event::Event(int value, int setTime, Wire * output){
evNum = numOfEvents++;
evValue = value; evValue = value;
evTime = setTime; evTime = setTime;
out = output; out = output;

View File

@ -1,4 +1,5 @@
#include "NandGate.h" #include "NandGate.h"
#include <iostream>
NandGate::NandGate(int d, Wire * wire1, Wire * wire2, Wire * wire3) 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) Event NandGate::evaluate(int evTime)
{ {
cout << evTime << " " << in1->getValue(evTime) << " " << in2->getValue(evTime) << endl;
if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) { if (in1->getValue(evTime) == 0 || in2->getValue(evTime) == 0) {
return Event(-1, 1, evTime + delay, out); return Event(1, evTime + delay, out);
}
else if (in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1) {
return Event(0, evTime + delay, out);
} }
else{ else{
return Event(-1, 0, evTime + delay, out); return Event(-1, evTime + delay, out);
} }
} }

View File

@ -11,10 +11,13 @@ NorGate::NorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
Event NorGate::evaluate(int evTime) { Event NorGate::evaluate(int evTime) {
if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) { 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) { 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);
} }
} }

View File

@ -9,11 +9,13 @@ NotGate::NotGate(int d, Wire* wire1, Wire* wire2) {
} }
Event NotGate::evaluate(int evTime) { Event NotGate::evaluate(int evTime) {
if (in1->getValue(evTime) == 0) { if (in1->getValue(evTime) == 1) {
return Event(-1, 0, evTime + delay, out); return Event(0, evTime + delay, out);
}else if (in1->getValue(evTime) == 0) {
return Event(1, evTime + delay, out);
} }
else { else {
return Event(-1, 1, evTime + delay, out); return Event(-1, evTime + delay, out);
} }
} }

View File

@ -11,10 +11,13 @@ OrGate::OrGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
Event OrGate::evaluate(int evTime) { Event OrGate::evaluate(int evTime) {
if (in1->getValue(evTime) == 1 || in2->getValue(evTime) == 1) { 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) { 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);
} }
} }

View File

@ -120,7 +120,7 @@ bool Simulation::parseVector(string fileName) {
while(true) { while(true) {
if (!(in >> tmpString)) break; if (!(in >> tmpString)) break;
if (!(in >> tmpString)) break; if (!(in >> tmpString)) break;
if (!(in >> tmpInt)) break; if (!(in >> timeInt)) break;
if (!(in >> valInt)) break; if (!(in >> valInt)) break;
for(auto i = wires.begin(); i != wires.end(); ++i) { 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() { void Simulation::simulate() {
// loop through event queue // loop through event queue
while(!e.empty()) { while(!e.empty()) {
bool doesChange; bool changed;
Wire * output; Wire * output;
Event tmpEvent = e.top(); Event tmpEvent = e.top();
e.pop();
output = tmpEvent.getOutput(); 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 the wire value changes, evaluate gates
if(doesChange) { if(changed && !(tmpEvent.getTime() > 60)) {
Gate * tmpGate; Gate * tmpGate;
Event gateEvent(-1, -1, -1, nullptr); Event gateEvent(-1, -1, nullptr);
int index = 0; int index = 0;
while(true){ while(true){
tmpGate = output->getGate(index++); tmpGate = output->getGate(index++);
if (tmpGate != nullptr) { if (tmpGate != nullptr) {
gateEvent = tmpGate->evaluate(tmpEvent.getTime()); gateEvent = tmpGate->evaluate(tmpEvent.getTime());
gateEvent.setNum(eventNum++);
e.push(gateEvent); e.push(gateEvent);
} }
else { else {
@ -162,6 +161,7 @@ void Simulation::simulate() {
} }
} }
} }
e.pop();
} }
} }
@ -207,3 +207,4 @@ int Simulation::getDelay(string d)
d.resize(d.size() - 2); d.resize(d.size() - 2);
return atoi(d.c_str()); return atoi(d.c_str());
} }

View File

@ -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); historyTimes.push_back(setTime);
historyValues.push_back(newValue); historyValues.push_back(newValue);
if (lastEvent < setTime) { if (lastEvent < setTime) {
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) void Wire::convertToIO(string newName)

View File

@ -12,12 +12,15 @@ XnorGate::XnorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
Event XnorGate::evaluate(int evTime) { Event XnorGate::evaluate(int evTime) {
if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) { if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) {
if (in1->getValue(evTime) == in2->getValue(evTime)) { if (in1->getValue(evTime) == in2->getValue(evTime)) {
return Event(-1, 1, evTime + delay, out); return Event(1, evTime + delay, out);
} }
else { else {
return Event(-1, 0, evTime + delay, out); return Event(0, evTime + delay, out);
} }
} }
else {
return Event(-1, evTime + delay, out);
}
} }
int XnorGate::getDelay() int XnorGate::getDelay()

View File

@ -12,12 +12,15 @@ XorGate::XorGate(int d, Wire* wire1, Wire* wire2, Wire* wire3) {
Event XorGate::evaluate(int evTime) { Event XorGate::evaluate(int evTime) {
if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) { if (in1->getValue(evTime) != -1 && in2->getValue(evTime) != -1) {
if (in1->getValue(evTime) != in2->getValue(evTime)) { if (in1->getValue(evTime) != in2->getValue(evTime)) {
return Event(-1, 1, evTime + delay, out); return Event(1, evTime + delay, out);
} }
else { else {
return Event(-1, 0, evTime + delay, out); return Event(0, evTime + delay, out);
} }
} }
else {
return Event(-1, evTime + delay, out);
}
} }
int XorGate::getDelay() int XorGate::getDelay()