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) {
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);
}
}

View File

@ -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;

View File

@ -1,4 +1,5 @@
#include "NandGate.h"
#include <iostream>
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 if (in1->getValue(evTime) == 1 && in2->getValue(evTime) == 1) {
return Event(0, evTime + delay, out);
}
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) {
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);
}
}

View File

@ -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);
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) {
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);
}
}

View File

@ -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());
}

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);
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)

View File

@ -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()

View File

@ -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()