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;
|
2017-04-23 19:29:29 -04:00
|
|
|
Wire *tmpWire;
|
|
|
|
Gate *tmpGate;
|
2017-04-20 12:41:43 -04:00
|
|
|
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") {
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpWire = new Wire(tmp1, true, tmpString);
|
2017-04-20 12:41:43 -04:00
|
|
|
wires.push_back(tmpWire);
|
|
|
|
}
|
|
|
|
else if (tmpType == "OUTPUT") {
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpWire = new Wire(tmp1, false, tmpString);
|
2017-04-20 12:41:43 -04:00
|
|
|
wires.push_back(tmpWire);
|
|
|
|
}
|
|
|
|
else if (tmpType == "NOT") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpGate = new NotGate(getDelay(tmpString), findWire(tmp1),
|
2017-04-20 12:41:43 -04:00
|
|
|
findWire(tmp2));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "AND") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpGate = new AndGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
2017-04-20 12:41:43 -04:00
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "NAND") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpGate = new NandGate(getDelay(tmpString), findWire(tmp1),
|
2017-04-20 12:41:43 -04:00
|
|
|
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-23 19:29:29 -04:00
|
|
|
tmpGate = new OrGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
2017-04-20 12:41:43 -04:00
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "XOR") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpGate = new XorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
2017-04-20 12:41:43 -04:00
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "NOR") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpGate = new NorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
2017-04-20 12:41:43 -04:00
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
else if (tmpType == "XNOR") {
|
2017-04-20 13:01:04 -04:00
|
|
|
in >> tmp2;
|
|
|
|
in >> tmp3;
|
2017-04-23 19:29:29 -04:00
|
|
|
tmpGate = new XnorGate(getDelay(tmpString), findWire(tmp1), findWire(tmp2),
|
2017-04-20 12:41:43 -04:00
|
|
|
findWire(tmp3));
|
|
|
|
gates.push_back(tmpGate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulation::simulate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Simulation::print()
|
|
|
|
{
|
|
|
|
}
|
2017-04-20 13:01:04 -04:00
|
|
|
|
2017-04-23 19:29:29 -04:00
|
|
|
Wire * Simulation::findWire(int n)
|
2017-04-20 13:01:04 -04:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|