add static variable for tracking event number

This commit is contained in:
Joel Beckmeyer 2017-04-25 10:36:51 -04:00
parent fb0c023cf4
commit 848c471b14
10 changed files with 38 additions and 34 deletions

View File

@ -10,14 +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{ else{
return Event(-1, -1, evTime + delay, out); 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

@ -11,13 +11,13 @@ NandGate::NandGate(int d, Wire * wire1, Wire * wire2, Wire * wire3)
Event NandGate::evaluate(int evTime) Event NandGate::evaluate(int evTime)
{ {
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) { else if (in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1) {
return Event(-1, 0, evTime + delay, out); return Event(0, evTime + delay, out);
} }
else{ else{
return Event(-1, -1, evTime + delay, out); return Event(-1, evTime + delay, out);
} }
} }

View File

@ -11,13 +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{ else{
return Event(-1, -1, evTime + delay, out); return Event(-1, evTime + delay, out);
} }
} }

View File

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

View File

@ -11,13 +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{ else{
return Event(-1, -1, evTime + delay, out); return Event(-1, evTime + delay, out);
} }
} }

View File

@ -129,32 +129,30 @@ 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());
// 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 +160,8 @@ void Simulation::simulate() {
} }
} }
} }
output->setValue(tmpEvent.getValue(), tmpEvent.getTime());
e.pop();
} }
} }

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,14 +12,14 @@ 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 { else {
return Event(-1, -1, evTime + delay, out); return Event(-1, evTime + delay, out);
} }
} }

View File

@ -12,14 +12,14 @@ 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 { else {
return Event(-1, -1, evTime + delay, out); return Event(-1, evTime + delay, out);
} }
} }