-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.cpp
547 lines (452 loc) · 19.9 KB
/
circuit.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
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
#include "circuit.h"
int Circuit::gen_temp_gate_count = 0;
int Circuit::gen_temp_wire_count = 0;
int Circuit::gen_submodule_temp_wire_count = 0;
int Circuit::gen_submodule_temp_gate_count = 0;
std::map<std::string, int> Circuit::submoduleCounts;
void Circuit::addEdge(CircuitElement & element1, CircuitElement & element2)
{
if (std::find(elements.begin(), elements.end(), element1) == elements.end()) {
elements.push_back(element1);
adjacencyList.push_back(std::make_pair(element1, std::vector<CircuitElement>()));
}
if (std::find(elements.begin(), elements.end(), element2) == elements.end()) {
elements.push_back(element2);
adjacencyList.push_back(std::make_pair(element2, std::vector<CircuitElement>()));
}
for (size_t i = 0; i < adjacencyList.size(); i++) {
if (adjacencyList[i].first == element1) {
//if(std::find(adjacencyList[i].second.begin(), adjacencyList[i].second.end(), element2) == adjacencyList[i].second.end()) {
adjacencyList[i].second.push_back(element2);
//}
}
}
}
void Circuit::print()
{
std::cout << "Name: " << circuitName << std::endl;
std::map<CircuitElementType, std::string> mapOfGates = {
{CircuitElementType::AND,"AND"},
{CircuitElementType::OR,"OR"},
{CircuitElementType::NAND,"NAND"},
{CircuitElementType::NOR,"NOR"},
{CircuitElementType::XOR,"XOR"},
{CircuitElementType::XNOR,"XNOR"},
{CircuitElementType::NOT,"NOT"},
{CircuitElementType::BUF,"BUF"},
{CircuitElementType::OAI3,"OAI3"},
};
for (auto& adj : adjacencyList){
if (adj.first.elementType == CircuitElementType::INPUT) {
std::cout << "INPUT " << adj.first.elementName << " is connected to: ";
for (auto& connected : adj.second) {
std::cout << connected.elementName << " ";
}
std::cout << std::endl;
}
else if (adj.first.elementType == CircuitElementType::OUTPUT) {
std::cout << "OUTPUT " << adj.first.elementName << " is the result of: ";
for (auto& a : adjacencyList) {
for (auto& s : a.second) {
if (s.elementName == adj.first.elementName) {
std::cout << a.first.elementName << " " ;
}
}
}
std::cout << std::endl;
}
else if (adj.first.elementType == CircuitElementType::WIRE) {
std::cout << "WIRE " << adj.first.elementName << " is the result of: ";
for (auto& a : adjacencyList) {
for (auto& s : a.second) {
if (s.elementName == adj.first.elementName) {
std::cout << a.first.elementName << " ";
}
}
}
std::cout << "also becomes an input to ";
for (auto& connected : adj.second) {
std::cout << connected.elementName << " ";
}
std::cout << std::endl;
}
else {
std::cout << mapOfGates[adj.first.elementType] << " gate " << adj.first.elementName << " get inputs of: ";
for (auto& a : adjacencyList) {
for (auto& s : a.second) {
if (s.elementName == adj.first.elementName) {
std::cout << a.first.elementName << " ";
}
}
}
std::cout << "and outputs: ";
for (auto& a : adjacencyList) {
if (a.first.elementName == adj.first.elementName) {
for (auto& s : a.second) {
std::cout << s.elementName << " ";
}
}
}
std::cout << std::endl;
}
}
}
bool Circuit::fillFromVerilogFile(std::string fileName)
{
auto start = std::chrono::steady_clock::now();
inputs.clear();
outputs.clear();
std::ifstream inFile;
inFile.open(fileName);
if (!inFile) {
std::cout << fileName << std::endl;
std::cerr << "Unable to open file"<<std::endl;
return false;
}
std::string line;
std::map<std::string, CircuitElementType> isInputOutputOrWire;
std::vector<std::string> fileLines = Utils::getLinesFromVerilogFile(fileName);
for (auto line : fileLines) {
// Handle Windows / DOS style line endings.
if (line.back() == '\r') {
line.pop_back();
}
if(line.length() == 0){
continue;
}
if(line.rfind("//", 0) == 0){
continue;
}
line.erase(std::remove_if(line.begin(), line.end(), [](char chr) { return chr == ' (' || chr == ' )' || chr == ';'; }), line.end());
std::replace(line.begin(), line.end(), ',', ' ');
std::replace(line.begin(), line.end(), '(', ' ');
std::replace(line.begin(), line.end(), ')', ' ');
std::istringstream stm(line);
std::string word;
std::vector<std::string> tokens;
while (stm >> word) {
tokens.push_back(word);
}
if (tokens.front() == "module") {
circuitName = tokens.at(1);
for(size_t i = 2; i < tokens.size(); i++){
callOrder.push_back(tokens.at(i));
}
}
else if (tokens.front() == "input") {
for (size_t i = 1; i < tokens.size(); i++) {
isInputOutputOrWire[tokens.at(i)] = CircuitElementType::INPUT;
inputs.push_back(tokens.at(i));
}
sortInputsWithCallOrder();
}
else if (tokens.front() == "output") {
for (size_t i = 1; i < tokens.size(); i++) {
isInputOutputOrWire[tokens.at(i)] = CircuitElementType::OUTPUT;
outputs.push_back(tokens.at(i));
}
sortOutputsWithCallOrder();
}
else if (tokens.front() == "wire") {
for (size_t i = 1; i < tokens.size(); i++) {
isInputOutputOrWire[tokens.at(i)] = CircuitElementType::WIRE;
}
}
else if(tokens.front() == "'include"){
std::string moduleFilename = "";
for(size_t i = 1; i < tokens.size(); i++){
moduleFilename.append(tokens.at(i));
if(i != tokens.size() - 1){
moduleFilename.append(" ");
}
}
moduleFilename.erase(std::remove(moduleFilename.begin(), moduleFilename.end(), '"'), moduleFilename.end());
if(moduleFilename.find('/') == std::string::npos){
std::string parentDirectoryPath = fileName;
int endIndex = parentDirectoryPath.find_last_of('/');
moduleFilename = parentDirectoryPath.substr(0,endIndex) + "/" + moduleFilename;
}
Circuit submodule;
submodule.fillFromVerilogFile(moduleFilename);
submodules.push_back(submodule);
}
else if(tokens.front() == "dff"){
CircuitElement gateNode{ tokens.at(1), CircuitElementType::DFF};
CircuitElement outputNode{ tokens.at(3), isInputOutputOrWire[tokens.at(3)] };
addEdge(gateNode, outputNode);
for (size_t i = 2; i < 5; i+=2) {
CircuitElement inputNode{ tokens.at(i) , isInputOutputOrWire[tokens.at(i)] };
addEdge(inputNode, gateNode);
}
}
else if (mapOfNodeTypes.find(tokens.front()) != mapOfNodeTypes.end()) {
CircuitElement gateNode{ tokens.at(1), mapOfNodeTypes[tokens.front()]};
//This might not be the case for all the cell libraries, the gate submodules might have different input/output ordering
//such as the case in the dff. A way to understand has to be added.
//Also we are getting a cell library but we do not actually understand the contents of it, we just think it is like the Lib45NanGate
//library which is also a problem. We need to find a way to fix that
CircuitElement outputNode{ tokens.at(2), isInputOutputOrWire[tokens.at(2)] };
addEdge(gateNode, outputNode);
for (size_t i = 3; i < tokens.size(); i++) {
CircuitElement inputNode{ tokens.at(i) , isInputOutputOrWire[tokens.at(i)] };
addEdge(inputNode, gateNode);
}
}
else{
if(tokens.front()!= "endmodule"){
bool exists = false;
for(Circuit& c: submodules){
if(c.circuitName == tokens.front()){
exists = true;
}
}
Circuit submodule;
if(exists){
for(auto & s: submodules){
if(s.circuitName == tokens.front()){
submodule = s;
}
}
}
else{
std::string parentDirectoryPath = fileName;
int endIndex = parentDirectoryPath.find_last_of('/');
std::string moduleFilename = tokens.front() + ".v";
moduleFilename = parentDirectoryPath.substr(0,endIndex) + "/" + moduleFilename;
submodule.submoduleConnectionMethod = this->submoduleConnectionMethod;
submodule.fillFromVerilogFile(moduleFilename);
for(const Circuit& module: submodule.submodules){
submodules.push_back(module);
}
submodules.push_back(submodule);
}
Circuit copyOfSubmodule = submodule;
if(submoduleConnectionMethod == SubmoduleConnectionMethod::graphJoiningMethod){
std::vector <std::string> parameterList;
for(size_t i = 2; i < tokens.size(); i++){
parameterList.push_back(tokens.at(i));
}
connectSubmoduleByGraphJoining(©OfSubmodule, parameterList, isInputOutputOrWire);
}
else{
CircuitElement subcircuitNode{ tokens.at(1), CircuitElementType::SUBCIRCUIT,tokens.at(0)};
for(size_t i = 2; i < tokens.size(); i++){
if(std::find(submodule.inputs.begin(), submodule.inputs.end(), submodule.callOrder.at(i-2)) != submodule.inputs.end()){
subcircuitNode.addToInputOrder(tokens.at(i));
}
else if(std::find(submodule.outputs.begin(), submodule.outputs.end(), submodule.callOrder.at(i-2)) != submodule.outputs.end()){
subcircuitNode.addToOutputOrder(tokens.at(i));
}
}
for(size_t i = 2; i < tokens.size(); i++){
if(std::find(submodule.inputs.begin(), submodule.inputs.end(), submodule.callOrder.at(i-2)) != submodule.inputs.end()){
std::string inputToken = tokens.at(i);
CircuitElement inputNode{ inputToken , isInputOutputOrWire[inputToken] };
addEdge(inputNode, subcircuitNode);
}
else if(std::find(submodule.outputs.begin(), submodule.outputs.end(), submodule.callOrder.at(i-2)) != submodule.outputs.end()){
std::string outputToken = tokens.at(i);
CircuitElement outputNode{ outputToken , isInputOutputOrWire[outputToken] };
addEdge(outputNode, subcircuitNode);
}
}
}
}
}
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_time = end-start;
std::cout << circuitName << " - CONVERSION TIME : " << elapsed_time.count() << std::endl;
return true;
}
Circuit::Circuit (const Circuit & circuit){
for(auto& input: circuit.inputs){
inputs.push_back(input);
}
for(auto& output: circuit.outputs){
outputs.push_back(output);
}
for(auto& adjListElement: circuit.adjacencyList){
adjacencyList.push_back(adjListElement);
}
for(auto& element : circuit.elements){
elements.push_back(element);
}
for(auto& submodule : circuit.submodules){
submodules.push_back(submodule);
}
for(auto& order : circuit.callOrder){
callOrder.push_back(order);
}
circuitName = circuit.circuitName;
}
bool Circuit::doesElementWithNameExist(std::string name) {
for(auto & mainCircuitElement : adjacencyList){
if(name == mainCircuitElement.first.elementName){
return true;
}
}
return false;
}
void Circuit::sortOutputsWithCallOrder(){
std::vector<std::pair<std::string,int>> outputCallOrder;
for(size_t i = 0; i < outputs.size(); i++){
for(size_t j = 0; j < callOrder.size(); j++){
if(outputs.at(i) == callOrder.at(j)){
outputCallOrder.push_back(std::make_pair(outputs.at(i), j));
}
}
}
sort(outputCallOrder.begin(), outputCallOrder.end(), [=](std::pair<std::string, int>& a, std::pair<std::string, int>& b)
{
return a.second < b.second;
});
outputs.clear();
for(size_t i = 0; i < outputCallOrder.size(); i++){
outputs.push_back(outputCallOrder.at(i).first);
}
}
void Circuit::sortInputsWithCallOrder(){
std::vector<std::pair<std::string,int>> inputCallOrder;
for(size_t i = 0; i < inputs.size(); i++){
for(size_t j = 0; j < callOrder.size(); j++){
if(inputs.at(i) == callOrder.at(j)){
inputCallOrder.push_back(std::make_pair(inputs.at(i), j));
}
}
}
sort(inputCallOrder.begin(), inputCallOrder.end(), [=](std::pair<std::string, int>& a, std::pair<std::string, int>& b)
{
return a.second < b.second;
});
inputs.clear();
for(size_t i = 0; i < inputCallOrder.size(); i++){
inputs.push_back(inputCallOrder.at(i).first);
}
}
void Circuit::connectSubmoduleByGraphJoining(Circuit * submodule, std::vector<std::string> parameters, std::map<std::string, CircuitElementType> isInputOutputOrWire){
if (submoduleCounts.find(submodule->circuitName) != submoduleCounts.end()){
submoduleCounts[submodule->circuitName]++;
}
else{
submoduleCounts.insert(std::make_pair(submodule->circuitName, 0));
}
int currentSubmoduleCount = submoduleCounts[submodule->circuitName];
// Rename non input/output elements.
for (auto& element : submodule->elements){
if (element.elementType != INPUT && element.elementType != OUTPUT){
if (element.elementType == WIRE){
std::string generatedWireName = "SM_" + submodule->circuitName + "_" + std::to_string(currentSubmoduleCount)+ "_WIRE_" + std::to_string(gen_submodule_temp_wire_count);
submodule->changeElementName(element.elementName, generatedWireName);
gen_submodule_temp_wire_count++;
}
else{
std::string generatedGateName = "SM_" + submodule->circuitName + "_" + std::to_string(currentSubmoduleCount)+ "_GATE_" + std::to_string(gen_submodule_temp_gate_count);
submodule->changeElementName(element.elementName, generatedGateName);
gen_submodule_temp_gate_count++;
}
}
}
// Find corresponding input / outputs from parent to child.
for (unsigned long i = 0; i < submodule->callOrder.size(); i++){
submodule->changeElementName(submodule->callOrder[i], parameters[i]);
// Also, change submodule input/output's type to corresponding element type in parent.
submodule->changeElementType(parameters[i], isInputOutputOrWire[parameters[i]]);
}
// Merge subcircuit to this circuit.
connectOtherCircuit(submodule);
}
void Circuit::changeElementName(std::string previousName, std::string newName){
// Rename in elements list.
for (auto& element : elements){
if (element.elementName == previousName){
element.elementName = newName;
}
}
// Rename in adjacency list.
for (auto& node : adjacencyList){
for (auto& connectedNode : node.second){
if (connectedNode.elementName == previousName){
connectedNode.elementName = newName;
}
}
if (node.first.elementName == previousName){
node.first.elementName = newName;
}
}
// Rename in inputs list (if it's an input).
for (auto& input: inputs){
if (input == previousName){
input = newName;
}
}
// Rename in outputs list (if it's an output).
for (auto& output: outputs){
if (output == previousName){
output = newName;
}
}
// Rename in call order (if it's an in/output).
for (auto& callOrderElement : callOrder){
if (callOrderElement == previousName){
callOrderElement = newName;
}
}
}
void Circuit::changeElementType(std::string elementName, CircuitElementType newType){
// Change type in elements list.
for (auto& element : elements){
if (element.elementName == elementName){
element.elementType = newType;
}
}
// Change type in adjacency list.
for (auto& node :adjacencyList){
for (auto& connectedNode : node.second){
if (connectedNode.elementName == elementName){
connectedNode.elementType = newType;
}
}
if (node.first.elementName == elementName){
node.first.elementType = newType;
}
}
}
void Circuit::connectOtherCircuit(Circuit * circuit){
// For each element of the given (sub)circuit, find corresponding element in this circuit.
// If there are no matches, create a new element.
for (auto& node : circuit->adjacencyList){
// Search for a node with same name.
// If it's found, append adjacents of connected node to this circuit.
bool nodeWithSameNameFound = false;
for (auto& myNode : this->adjacencyList){
if (node.first == myNode.first){
nodeWithSameNameFound = true;
for (auto& adjacentNode : node.second){
myNode.second.push_back(adjacentNode);
}
}
}
// If this circuit element is not in my circuit, crete a new element in my circuit.
if (!nodeWithSameNameFound){
this->adjacencyList.push_back(std::make_pair(node.first, std::vector<CircuitElement>()));
for (auto& adjacentNode : node.second){
// Get latest added element (the one we created 2 lines above)
// And push all adjacent nodes to it, simply making a copy from given circuit to my circuit.
this->adjacencyList.back().second.push_back(adjacentNode);
}
}
}
// Also add elements to element list if their name is unique.
for (auto& element : circuit->elements){
bool elementWithSameNameFound = false;
for (auto& myElement : this->elements){
if (element.elementName == myElement.elementName){
elementWithSameNameFound = true;
}
}
if (!elementWithSameNameFound){
this->elements.push_back(element);
}
}
}