-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdesign.cpp
326 lines (291 loc) · 7.01 KB
/
design.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
#include "design.h"
#include <stdexcept>
#include <set>
using namespace std;
Design::Design()
{
desname = "design";
}
// contains name of the design
Design::Design(string n)
{
desname = n;
}
// returns name of device
string Design::name()
{
return desname;
}
void Design::make_name(string n)
{
desname = n;
}
// add an input's name to the list of PIs
void Design::add_pi(string n)
{
pis.push_back(n);
}
// add an output's name to list of POs
void Design::add_po(string n)
{
pos.push_back(n);
}
// returns NULL or pointer to net object
Net* Design::find_net(string net_name)
{
map<string, Net*>::iterator it;
it = design_nets.find(net_name);
if(it == design_nets.end()) { // not found :/
throw range_error("Net not found :(");
}
else { // found :D
return it->second;
}
}
// same as find_net, but with gates
Gate* Design::find_gate(string inst_name)
{
map<string, Gate*>::iterator it;
it = design_gates.find(inst_name);
if(it == design_gates.end()) { // not found :/
throw range_error("Gate not found :(");
}
else { // found :D
return it->second;
}
}
// returns a pointer to the Net object with the given name
// OR if a net with the given name doesn't exist,
// allocates a new one and returns a pointer to it.
Net* Design::add_find_net(string n)
{
map<string, Net*>::iterator it;
it = design_nets.find(n);
if(it != design_nets.end()) { // already exists!!
return it->second;
}
else { // doesn't exist
Net *newNet = new Net(n);
design_nets[n] = newNet;
return newNet;
}
}
// same as add_find_net but for gates
// gtype is the enum value of the gate type
Gate* Design::add_find_gate(int gtype, string n, int d)
{
map<string, Gate*>::iterator it;
it = design_gates.find(n);
if(it != design_gates.end()) { // already exists!!
return it->second;
}
else { // doesn't exist
switch(gtype) {
case AND :
{
And *newGate = new And(n,d);
design_gates[n] = newGate;
return newGate;
}
case OR :
{
Or *newGate = new Or(n,d);
design_gates[n] = newGate;
return newGate;
}
case NAND :
{
Nand *newGate = new Nand(n,d);
design_gates[n] = newGate;
return newGate;
}
case NOR :
{
Nor *newGate = new Nor(n,d);
design_gates[n] = newGate;
return newGate;
}
case XOR :
{
Xor *newGate = new Xor(n,d);
design_gates[n] = newGate;
return newGate;
}
case NOT :
{
Not *newGate = new Not(n,d);
design_gates[n] = newGate;
return newGate;
}
default :
{
ERROR("invalid gate type");
break;
}
}
}
}
// allocates and creates a new vector of pointers to the PI Nets
vector<Net *> Design::get_pi_nets()
{
vector<string>::iterator name;
map<string, Net*>::iterator foundnet;
vector<Net *> PIs;
for(name = pis.begin();name != pis.end();name++)
{
foundnet = design_nets.find(*name);
if(foundnet != design_nets.end()) { // found pi!
PIs.push_back(foundnet->second);
}
else {
//nothing to see here
}
}
return PIs;
}
// same as get_pi_nets
vector<Net *> Design::get_po_nets()
{
vector<string>::iterator name;
map<string, Net*>::iterator foundnet;
vector<Net *> POs;
for(name = pos.begin();name != pos.end();name++)
{
foundnet = design_nets.find(*name);
if(foundnet != design_nets.end()) { // found pi!
POs.push_back(foundnet->second);
}
else {
//nothing to see here
}
}
return POs;
}
vector<Net *> Design::get_wire_nets()
{
// Create a set of inputs and outputs so we can search faster
set<string> pios;
for(vector<string>::iterator it=pos.begin(); it!=pos.end(); it++) {
pios.insert(*it);
}
for(vector<string>::iterator it=pis.begin(); it!=pis.end(); it++) {
pios.insert(*it);
}
// To return
vector<Net *> wire;
// Iterate through all nets in the map
for(map<string, Net *>::iterator it=design_nets.begin(); it!=design_nets.end(); it++) {
// If it is not an input or output
if(pios.find(it->second->name()) == pios.end())
{
wire.push_back(it->second);
} else {
LOG("Skipping " << it->second->name());
}
}
return wire;
}
// returns pointers to all net objects
vector<Net *> Design::all_nets()
{
map<string, Net*>::iterator it;
vector<Net *> all_nets;
for(it = design_nets.begin();it != design_nets.end(); it++)
{
all_nets.push_back(it->second);
}
return all_nets;
}
// similar to all_nets but for gates
vector<Gate *> Design::all_gates()
{
map<string, Gate*>::iterator it;
vector<Gate *> all_gates;
for(it = design_gates.begin();it != design_gates.end(); it++)
{
all_gates.push_back(it->second);
}
return all_gates;
}
// write out the design in Verilog format to the provided ostream
void Design::dump(ostream &os)
{
os << "module " << this->name() << "(";
vector<Net*> pi_nets = this->get_pi_nets();
for(vector<Net*>::iterator it=pi_nets.begin(); it!=pi_nets.end(); it++) {
os << (*it)->name() << ", ";
}
vector<Net*> po_nets = this->get_po_nets();
for(vector<Net*>::iterator it=po_nets.begin(); it!=po_nets.end(); it++) {
os << (*it)->name();
if(it+1 != po_nets.end()) os << ", ";
}
os << ");" << endl;
for(vector<Net*>::iterator it=pi_nets.begin(); it!=pi_nets.end(); it++) {
os << "\tinput " << (*it)->name() << ";" << endl;
}
for(vector<Net*>::iterator it=po_nets.begin(); it!=po_nets.end(); it++) {
os << "\toutput " << (*it)->name() << ";" << endl;
}
vector<Net*> wire_nets = this->get_wire_nets();
for(vector<Net*>::iterator it=wire_nets.begin(); it!=wire_nets.end(); it++) {
os << "\twire " << (*it)->name() << ";" << endl;
}
os << endl;
vector<Gate*> all_gates = this->all_gates();
for(vector<Gate*>::iterator it=all_gates.begin(); it!=all_gates.end(); it++) {
os << "\t";
(*it)->dump(os);
}
os << "endmodule" << endl;
}
void Design::toposort()
{
toposortedList.clear();
// For each node with no outgoing edges
vector<Net*> nodes = get_po_nets();
for(vector<Net*>::iterator it = nodes.begin(); it != nodes.end(); it++)
{
// If N has not been marked as visited
if((*it)->color == WHITE) {
this->DFSvisit(*it);
}
}
}
void tabin(int depth) {
for(int i=0; i<depth; i++) {
cout << "\t";
}
}
Net* Design::DFSvisit(Net* currentNet, int depth)
{
cout << endl;
tabin(depth);
cout << "Visiting " << currentNet->name() << ", which depends on: ";
currentNet->color = GREY;
// For each driver
for(vector<Gate *>::iterator gate = currentNet->getDrivers()->begin(); gate != currentNet->getDrivers()->end(); gate++)
{
cout << (*gate)->name() << endl;
// And each driver's nets
for(vector<Net *>:: iterator net = (*gate)->getInputs()->begin(); net != (*gate)->getInputs()->end(); net++)
{
tabin(depth);
cout << "Net: " << (*net)->name() << " color " << (*net)->color;
if((*net)->color == WHITE) {
this->DFSvisit(*net, depth+1);
}
else if((*net)->color == GREY) {
(*net)->color = BLACK;
}
}
}
if(currentNet->getDrivers()->size() == 0)
cout << endl;
toposortedList.push_back(currentNet );
return currentNet;
}
deque<Net *> Design::get_toposortedList()
{
return toposortedList;
}