-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlepath.cpp
221 lines (183 loc) · 6.71 KB
/
singlepath.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
#include "singlepath.h"
SinglePath::SinglePath(std::vector<std::string> pathStr)
{
path = pathStr;
}
void SinglePath::InsertWireLine(std::ifstream& inVerilogFile, std::ofstream& outVerilogFile)
{
std::string input = path[0];
std::string output = path[path.size() - 1];
std::string line;
//For only writing wire line
std::unordered_set<std::string> wireSet;
while(getline(inVerilogFile, line))
{
std::replace(line.begin(), 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;
std::vector<std::string> pathGates;
while (stm >> word)
{
if(path.end() != std::find(path.begin(), path.end(), word))
{
pathGates.push_back(word);
}
tokens.push_back(word);
}
if(tokens.size() > 0 && isalpha(tokens.front().at(0)))
{
std::string gate = tokens.front();
for(size_t i = 0; i < gate.size(); ++i)
{
gate[i] = toupper(gate[i]);
}
if(cirElements.end() != std::find(cirElements.begin(), cirElements.end(), gate))
{
bool insidePath = false;
for(std::string& a : tokens)
{
if(path.end() != std::find(path.begin(), path.end(), a))
{
insidePath = true;
}
}
if(insidePath)
{
std::string gateOutput = tokens.at(2);
if(path.end() == std::find(path.begin(), path.end(), gateOutput))
{
if(gateOutput != input && gateOutput != output)
{
wireSet.insert(gateOutput);
}
}
for(size_t i = 0; i < pathGates.size(); ++i)
{
if(pathGates[i] != input && pathGates[i] != output)
{
wireSet.insert(pathGates[i]);
}
}
}
}
}
}
//Wire components colelcted
//Insert wire line
outVerilogFile << "wire ";
for(auto& a : wireSet)
{
outVerilogFile << a <<", ";
}
outVerilogFile << "Vcc, gnd;" << std::endl;
inVerilogFile.clear();
inVerilogFile.seekg(0);
}
int SinglePath::ConvertToSinglePathVerilog(std::string inputVerilogLoc, std::string outputVerilogLoc)
{
std::ifstream inVerilogFile;
std::ofstream outVerilogFile;
inVerilogFile.open(inputVerilogLoc);
outVerilogFile.open(outputVerilogLoc, std::ofstream::trunc);
outVerilogFile.clear();
if (!inVerilogFile) {
std::cout << inputVerilogLoc << std::endl;
std::cerr << "Unable to open file"<<std::endl;
return -1;
}
std::string input = path[0];
std::string output = path[path.size() - 1];
outVerilogFile << "module singlepath (" << input << ", " << output << ");" << std::endl;
outVerilogFile << "input " << input << ";" << std::endl;
outVerilogFile << "output " << output << ";" << std::endl;
std::string line;
InsertWireLine(inVerilogFile, outVerilogFile);
while(getline(inVerilogFile, line))
{
std::replace(line.begin(), 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;
std::vector<std::string> pathGates;
while (stm >> word)
{
if(path.end() != std::find(path.begin(), path.end(), word))
{
pathGates.push_back(word);
}
tokens.push_back(word);
}
if(tokens.size() > 0 && isalpha(tokens.front().at(0)))
{
std::string gate = tokens.front();
std::string gateLower = tokens.front();
for(size_t i = 0; i < gate.size(); ++i)
{
gate[i] = toupper(gate[i]);
gateLower[i] = tolower(gate[i]);
}
if(cirElements.end() != std::find(cirElements.begin(), cirElements.end(), gate))
{
bool insidePath = false;
size_t sizeArguments;
size_t countArguments = 0;
for(std::string& a : tokens)
{
if(path.end() != std::find(path.begin(), path.end(), a))
{
insidePath = true;
}
}
if(insidePath)
{
sizeArguments = tokens.size() - 2;
std::string gateOutput = tokens.at(2);
outVerilogFile << gateLower << " " << tokens.at(1) << "(" ;
if(path.end() == std::find(path.begin(), path.end(), gateOutput))
{
outVerilogFile << gateOutput << ", ";
++countArguments;
}
for(size_t i = 0; i < pathGates.size(); ++i)
{
outVerilogFile << pathGates[i];
++countArguments;
if(i < pathGates.size() - 1)
{
outVerilogFile << ", ";
}
else if(i == pathGates.size() - 1)
{
std::string sideinput;
if(gate == "AND" || gate == "NAND")
{
sideinput = "Vcc";
}
else
{
sideinput = "gnd";
}
while(countArguments < sizeArguments)
{
outVerilogFile << ", " << sideinput;
++countArguments;
}
outVerilogFile << ");" << std::endl;;
}
}
}
}
}
}
outVerilogFile << std::endl << "endmodule" << std::endl;
inVerilogFile.close();
outVerilogFile.close();
return 0;
}