-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircuit.cpp
More file actions
550 lines (485 loc) · 14.5 KB
/
circuit.cpp
File metadata and controls
550 lines (485 loc) · 14.5 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include "circuit.h"
/******************Circuit Implementation********************/
Circuit::~Circuit()
{
clear();
}
vector<Node*> Circuit::getPIs()
{
vector<Node*> PIs;
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == PRIMARY_INPUT)
PIs.push_back(it->second);
}
return PIs;
}
vector<Node*> Circuit::getPOs()
{
vector<Node*> POs;
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == PRIMARY_OUTPUT)
POs.push_back(it->second);
}
return POs;
}
int Circuit::setPI(const string &input)
{
Node *in = findNode(input);
assert(in != NULL);
in->type = PRIMARY_INPUT;
return 0;
}
int Circuit::setPO(const string &output)
{
Node *out = findNode(output);
assert(out != NULL);
out->type = PRIMARY_OUTPUT;
return 0;
}
int Circuit::setPIs(const string &input, unsigned int startBit, unsigned int endBit)
{
for (unsigned i = startBit; i < endBit; ++i)
{
stringstream sstr;
sstr << i;
string name = input + "[" + sstr.str() + "]";
assert(findNode(name) != NULL);
setPI(name);
}
return 0;
}
int Circuit::setPOs(const string &output, unsigned int startBit, unsigned int endBit)
{
for (unsigned i = startBit; i < endBit; ++i)
{
stringstream sstr;
sstr << i;
string name = output + "[" + sstr.str() + "]";
assert(findNode(name) != NULL);
setPO(name);
}
return 0;
}
Node* Circuit::findNode(const string &nodeName)
{
mapIter it = nodeMap.find(nodeName);
return (it != nodeMap.end() ? it->second : NULL);
}
Node* Circuit::createNode(const string &nodeName)
{
Node* newNode = findNode(nodeName);
if (newNode == NULL)
{
newNode = new Node(nodeName);
nodeMap.insert(make_pair(nodeName, newNode));
}
return newNode;
}
int Circuit::print()
{
cout << "Circuit Name: " << name << " contains " << nodeMap.size() << " nodes." << endl;
cout << "Primary Inputs: ";
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == PRIMARY_INPUT)
cout << it->second->name << " ";
}
cout << endl;
cout << "Primary Outputs: ";
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == PRIMARY_OUTPUT)
cout << it->second->name << " ";
}
cout << endl;
cout << "Nodes:" << endl;
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
it->second->print();
cout << endl;
}
return 0;
}
int Circuit::readBLIF(const string &filename)
{
cout << "reading file " << filename << "..." << endl;
ifstream inFile(filename.c_str());
if (!inFile.good())
{
cout << "ERROR in readBLIF() - cannot open "
<< filename << " for reading." << endl;
}
// clear circuit's contents
clear();
string line;
while (getline(inFile, line))
{
// cout << "processing line: " << line << endl;
stringstream sstr;
// skip empty lines
if (line.length() == 0) continue;
sstr << line;
string word;
vector<string> words;
// parse each line
while (sstr >> word)
{
words.push_back(word);
}
// parse non-empty lines only
if (!words.empty())
{
// skip any comments
if (words[0].find("#") != string::npos) continue;
// parse model name
if (words[0] == ".model")
{
for (unsigned i = 1; i < words.size(); ++i)
name += words[i];
}
// parse primary inputs
else if (words[0] == ".inputs")
{
for (unsigned i = 1; i < words.size(); ++i)
{
Node* inNode = createNode(words[i]);
// node already exists
if (inNode == NULL)
{
cout << "ERROR in readBLIF() (inputs) - node " << words[i] << " already exists.";
return -1;
}
inNode->type = PRIMARY_INPUT;
}
}
// parse primary outputs
else if (words[0] == ".outputs")
{
for (unsigned i = 1; i < words.size(); ++i)
{
Node* outNode = createNode(words[i]);
// node already exists
if (outNode == NULL)
{
cout << "ERROR in readBLIF() (outputs) - node " << words[i] << " already exists.";
return -1;
}
outNode->type = PRIMARY_OUTPUT;
}
}
// parse standard logic node
else if (words[0] == ".names" && words.size() > 2)
{
// find/create output node
Node* outNode = findNode(words[words.size()-1]);
if (outNode == NULL) outNode = createNode(words[words.size()-1]);
else
{
outNode->clearTT();
outNode->clearFanin();
}
// find/create input nodes
for (unsigned i = 1; i < words.size()-1; ++i)
{
Node* inNode = findNode(words[i]);
if (inNode == NULL) inNode = createNode(words[i]);
outNode->addFanin(inNode);
}
outNode->tt.setNumVars(words.size()-2);
// read truth table entries
char c;
do
{
string entry, result;
inFile >> entry >> result;
// cout << entry << " " << result << endl;
if (entry.length() != outNode->getNumFanin())
{
cout << "ERROR in readBLIF() - number of input entries "
<< "does not match number of fanin nodes" << endl;
cout << "entry: " << entry << " ; vs numFanins = " << outNode->getNumFanin() << endl;
return -1;
}
// only parse '1' result
if (result == "1")
{
if (outNode->tt.addEntry(entry))
{
cout << "ERROR in readBLIF() - cannot add entry " << entry
<< " to truth table." << endl;
return -1;
}
}
getline(inFile, result);
c = inFile.peek(); // look at next character
// cout << "c: " << c << endl;
} while (c == '1' || c == '0' || c == '-');
}
// parse logic ZERO and ONE nodes
else if (words[0] == ".names" && words.size() == 2)
{
Node* outNode = findNode(words[1]);
if (outNode == NULL) outNode = createNode(words[1]);
else
{
outNode->clearTT();
outNode->clearFanin();
}
// if next line is '1', then logic 1
if (inFile.peek() == '1')
{
outNode->type = ONE_NODE;
inFile.get(); // throw away '1'
}
else
{
outNode->type = ZERO_NODE;
}
}
// parse end of file
else if (words[0] == ".end") break;
// error
else
{
cout << "ERROR in readBLIF() - invalid line " << words[0] << endl;
cout << "Possibly wrong format?" << endl;
return -1;
}
}
}
cout << "file " << filename << " successfully read." << endl;
inFile.close();
return 0;
}
int Circuit::writeBLIF(const string &filename)
{
ofstream outFile(filename.c_str());
if (!outFile.good())
{
cout << "ERROR in writeBLIF() - cannot open "
<< filename << " for writing." << endl;
return -1;
}
// print model name
outFile << ".model " << name << endl << endl;
// print primary inputs
outFile << ".inputs ";
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == PRIMARY_INPUT)
outFile << it->first << " ";
}
outFile << endl << endl;
// print primary outputs
outFile << ".outputs ";
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == PRIMARY_OUTPUT)
outFile << it->first << " ";
}
outFile << endl << endl;
// print nodes
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
// must explicitly write 0 node
if (it->second->type == ZERO_NODE)
{
outFile << ".names " << it->second->name << endl;
outFile << endl;
}
// must explicitly write 1 node
else if (it->second->type == ONE_NODE)
{
outFile << ".names " << it->second->name << endl;
outFile << "1" << endl << endl;
}
else if (it->second->type != PRIMARY_INPUT)
{
outFile << ".names ";
for (unsigned i = 0; i < it->second->getNumFanin(); ++i)
outFile << it->second->getFanin()[i]->name << " ";
outFile << it->second->name << endl;
const TruthTable &theTT = it->second->tt;
for (unsigned i = 0; i < theTT.logic.size(); ++i)
{
for (unsigned j = 0; j < theTT.logic[i].size(); ++j)
{
switch(theTT.logic[i][j])
{
case ZERO: outFile << "0"; break;
case ONE: outFile << "1"; break;
case DC: outFile << "-"; break;
}
}
outFile << " 1" << endl;
}
outFile << endl;
}
}
outFile << ".end" << endl;
outFile.close();
cout << "File " << filename << " successfully written." << endl;
return 0;
}
int Circuit::clear()
{
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second != NULL)
delete it->second;
}
nodeMap.clear();
return 0;
}
void Circuit::printTopo()
{
vector<Node*> PIs = getPIs(); //primary inputs
map<string, Node*> PImap;
vector<Node*> POs = getPOs(); //primary outputs
map<string, Node*> POmap;
map<string, Node*> InternalsMapBase; //map of internal nodes
map<string, Node*> InternalsMap; //map of internal nodes
vector<Node*> Internals; //generally ordered list of internal nodes
vector<Node*> tempFanIn; //placeholder vector for working vector
//generate the map of PIs
for (int i = 0; i < PIs.size(); i++)
{
PImap.insert(pair<string, Node*>(PIs[i]->getName(), PIs[i]));
}
//generate the map of POs
for (int i = 0; i < POs.size(); i++)
{
POmap.insert(pair<string, Node*>(POs[i]->getName(), POs[i]));
}
//generate the map of internal gates
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == INTERNAL)
{
InternalsMapBase.insert(*it);
}
}
//order the internal gates
bool good = true;
for (mapIter it = InternalsMapBase.begin(); it != InternalsMapBase.end(); )
{
good = true;
tempFanIn = it->second->getFanin();
for (int i = 0; i < tempFanIn.size(); i++) //check all the fanIns
{
if ((PImap.find(tempFanIn[i]->getName()) == PImap.end()) && (InternalsMap.find(tempFanIn[i]->getName()) == PImap.end())) //don't have this fanIn yet, don't add to vector yet
{
good = false;
break;
}
}
if (good) //all fanIns have already been seen, push it into the ordered vector, InternalMap, and delete it from the source map
{
Internals.push_back(it->second);
InternalsMap.insert(pair<string, Node*>(it->first, it->second));
InternalsMapBase.erase(it);
it = InternalsMapBase.begin(); //to make sure I don't reference out of bounds, will eventually get through, despite inefficiency
}
else it++;
if ((it == InternalsMapBase.end()) && (InternalsMapBase.size() > 0)) //if we're at the end of the gates, but there are still more to add, go back to beginning and run again
{
it = InternalsMapBase.begin();
}
}
//output the Topo
cout << "*** Topological order:" << endl;
for (int i = 0; i < PIs.size(); i++) cout << PIs[i]->getName() << ' ';
for (int i = 0; i < Internals.size(); i++) cout << Internals[i]->getName() << ' ';
for (int i = 0; i < POs.size(); i++)
{
cout << POs[i]->getName();
if (i != (POs.size() - 1)) cout << ' ';
else cout << endl;
}
}
void Circuit::simOutputs(string inputFile)
{
vector<Node*> PIs = getPIs(); //primary inputs
map<string, Node*> PImap;
vector<Node*> POs = getPOs(); //primary outputs
map<string, Node*> POmap;
map<string, Node*> InternalsMapBase; //map of internal nodes
map<string, Node*> InternalsMap; //map of internal nodes
vector<Node*> Internals; //generally ordered list of internal nodes
vector<Node*> tempFanIn; //placeholder vector for working vector
ifstream ifs;
string tempName;
string charVal;
int tempVal;
//In this instance, the topological sort is reused to preface the value propagation through the circuit, if nodes are traversed in topological order
//then I don't have to worry about checking if the fanIn nodes have been set yet
//generate the map of PIs
for (int i = 0; i < PIs.size(); i++)
{
PImap.insert(pair<string, Node*>(PIs[i]->getName(), PIs[i]));
}
//generate the map of POs
for (int i = 0; i < POs.size(); i++)
{
POmap.insert(pair<string, Node*>(POs[i]->getName(), POs[i]));
}
//generate the map of internal gates
for (mapIter it = nodeMap.begin(); it != nodeMap.end(); it++)
{
if (it->second->type == INTERNAL)
InternalsMapBase.insert(*it);
}
//order the internal gates
bool good = true;
for (mapIter it = InternalsMapBase.begin(); it != InternalsMapBase.end();)
{
good = true;
tempFanIn = it->second->getFanin();
for (int i = 0; i < tempFanIn.size(); i++) //check all the fanIns
{
if ((PImap.find(tempFanIn[i]->getName()) == PImap.end()) && (InternalsMap.find(tempFanIn[i]->getName()) == PImap.end())) //don't have this fanIn yet, don't add to vector yet
{
good = false;
break;
}
}
if (good) //all fanIns have already been seen, push it into the ordered vector, InternalMap, and delete it from the source map
{
Internals.push_back(it->second);
InternalsMap.insert(pair<string, Node*>(it->first, it->second));
InternalsMapBase.erase(it);
it = InternalsMapBase.begin(); //to make sure I don't reference out of bounds, will eventually get through, despite inefficiency
}
else it++;
if ((it == InternalsMapBase.end()) && (InternalsMapBase.size() > 0)) //if we're at the end of the gates, but there are still more to add, go back to beginning and run again
{
it = InternalsMapBase.begin();
}
}
//initialize the file stream for input
ifs.open(inputFile.c_str());
if (!ifs.good()) cout << "input file is bad, or empty\n";
while (ifs.good()) //initialize all the PrimaryInput nodes with values
{
tempName.clear();
charVal.clear();
getline(ifs, tempName, ' ');
getline(ifs, charVal, '\n');
tempVal = atoi(charVal.c_str());
PImap.find(tempName)->second->setVal(tempVal);
}
for (int i = 0; i < Internals.size(); i++) //propagate the values through the topologically ordered internal nodes
{
Internals[i]->cascade();
}
cout << "*** Outputs:" << endl; //begin output
for (int i = 0; i < POs.size(); i++) //push values through to the primary outputs
{
POs[i]->cascade();
cout << POs[i]->getName() << " = " << POs[i]->getVal(); //output to screen as values are calculated
if (i != (POs.size() - 1)) cout << ", ";
else cout << endl;
}
}