/
logicsim.cpp
161 lines (143 loc) · 3.46 KB
/
logicsim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "logicsim.h"
#include <iostream>
#include <fstream>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <vector>
using namespace std;
static unsigned int lineNum = 0;
enum{INPUTS, VALUES, BLANK};
LogicSim::LogicSim(string file)
{
ifstream ifile(file.c_str(), ifstream::in);
while(ifile.good())
{
lineNum++;
string currentline;
getline(ifile, currentline);
stringstream ss(currentline);
string firsttoken;
ss >> firsttoken;
try{
switch(defineLine(firsttoken))
{
case INPUTS :
{
pi_order.push_back(firsttoken);
pi_set.insert(firsttoken);
while(ss.good())
{
string temp;
ss >> temp;
pi_order.push_back(temp);
pi_set.insert(temp);
}
break;
}
case VALUES :
{
map<string, char> line;
int index = 0;
line[pi_order.at(index)] = firsttoken[0];
while(ss.good()) {
index++;
char tmp;
ss >> tmp;
if(index < pi_order.size())
line[pi_order.at(index)] = tmp;
}
values.push_back(line);
break;
}
case BLANK :
{
// done reading
break;
}
default :
{
throw runtime_error("grammar error");
break;
}
}
} catch(runtime_error &ex) {
string line;
stringstream ss;
ss << lineNum;
line = ss.str();
throw runtime_error("On line " + line + " " + ex.what());
}
}
}
void LogicSim::runSimulation(deque<Net *> topolist)
{
// For each line in the sim
for(vector<map<string,char> >::iterator lineit=values.begin();
lineit!=values.end();
lineit++)
{
map<string,char> values = *lineit;
map<string,char> result;
for(deque<Net*>::iterator it=topolist.begin();
it!=topolist.end();
it++)
{
Net *n = *it;
string name = n->name();
if(pi_set.find(name) != pi_set.end()) {
n->setVal(values[name]);
LOG("Set input value of " << name << " to " << values[name]);
} else {
n->computeVal();
LOG("Computed value of " << name << " is " << n->getVal());
result[name] = n->getVal();
}
}
cout << endl;
results.push_back(result);
}
}
void LogicSim::outputTheFile(string file, Design* design)
{
ofstream outfile(file.c_str());
// Header
for(vector<string>::iterator it=pi_order.begin(); it!=pi_order.end(); it++) {
outfile << *it << " ";
}
outfile << "=> ";
for(vector<Net*>::iterator it=design->get_po_nets().begin(); it!=design->get_po_nets().end(); it++) {
outfile << (*it)->name() << " ";
}
outfile << endl;
// For each line
int index = 0;
for(vector<map<string,char> >::iterator it=values.begin(); it!=values.end(); it++){
map<string,char> value = *it;
for(vector<string>::iterator jt=pi_order.begin(); jt!=pi_order.end(); jt++) {
outfile << value[*jt] << " ";
}
outfile << "=> ";
for(vector<Net*>::iterator jt=design->get_po_nets().begin(); jt!=design->get_po_nets().end(); jt++) {
outfile << results.at(index)[(*jt)->name()] << "@" << (*jt)->computeDelay() << " ";
}
index++;
outfile << endl;
}
outfile.close();
}
int defineLine(string identifier)
{
if((identifier == "0") | (identifier == "1"))
{
return VALUES;
}
else if((identifier == " ") | (identifier == "\n") | (identifier == "\t") | (identifier == ""))
{
return BLANK;
}
else if(isalpha(identifier.c_str()[0]))
{
return INPUTS;
}
}