2017-04-20 12:41:43 -04:00
|
|
|
#include "Simulation.h"
|
|
|
|
|
2017-04-20 13:01:04 -04:00
|
|
|
bool Simulation::parseCircuit(string fileName)
|
2017-04-20 12:41:43 -04:00
|
|
|
{
|
|
|
|
ifstream in;
|
2017-04-20 13:01:04 -04:00
|
|
|
in.open(fileName + ".txt");
|
2017-04-20 12:41:43 -04:00
|
|
|
if (in.fail()) {
|
|
|
|
cerr << endl << fileName << ".txt could not be opened :(";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
string tmpString, tmpType;
|
|
|
|
int tmp1, tmp2, tmp3;
|
|
|
|
wire *tmpWire;
|
|
|
|
gate *tmpGate;
|
|
|
|
getline(in, tmpString);
|
|
|
|
|
|
|
|
while (!in.eof()) {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmpType;
|
|
|
|
|
|
|
|
in >> tmpString;
|
|
|
|
in >> tmp1;
|
2017-04-20 12:41:43 -04:00
|
|
|
|
|
|
|
if (tmpType == "INPUT") {
|
|
|
|
tmpWire = new wire(tmp1, true, tmpString);
|
|
|
|
wires.push_back(tmpWire);
|
|
|
|
}
|
|
|
|
else if (tmpType == "OUTPUT") {
|
|
|
|
tmpWire = new wire(tmp1, false, tmpString);
|
|
|
|
wires.push_back(tmpWire);
|
|
|
|
}
|
|
|
|
else if (tmpType == "NOT") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
2017-04-20 12:41:43 -04:00
|
|
|
tmpGate = new notGate(getDelay(tmpString), findWire(tmp1),
|
|
|
|
findWire(tmp2));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "AND") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-20 12:41:43 -04:00
|
|
|
tmpGate = new andGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "NAND") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-20 12:41:43 -04:00
|
|
|
tmpGate = new nandGate(getDelay(tmpString), findWire(tmp1),
|
|
|
|
findWire(tmp2), findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "OR") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-20 12:41:43 -04:00
|
|
|
tmpGate = new orGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "XOR") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-20 12:41:43 -04:00
|
|
|
tmpGate = new xorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "NOR") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-20 12:41:43 -04:00
|
|
|
tmpGate = new norGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "XNOR") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-20 12:41:43 -04:00
|
|
|
tmpGate = new xnorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulation::simulate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulation::print()
|
|
|
|
{
|
|
|
|
}
|
2017-04-20 13:01:04 -04:00
|
|
|
|
|
|
|
wire * Simulation::findWire(int n)
|
|
|
|
{
|
|
|
|
for (auto i = wires.begin(); i != wires.end(); ++i) {
|
|
|
|
if (n == (**i).getNumber()) return *i;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Simulation::getDelay(string d)
|
|
|
|
{
|
|
|
|
d.resize(d.size() - 2);
|
|
|
|
return atoi(d.c_str());
|
|
|
|
}
|