-
Notifications
You must be signed in to change notification settings - Fork 0
/
program_state.cc
339 lines (306 loc) · 11.7 KB
/
program_state.cc
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
/*
* Copyright (C) 2017-2018 Ruben Undheim <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <cstdio>
#include <cstring>
#include "program_state.h"
#include "input/liberty/liberty.h"
#include "constraints/constraints.h"
#include "constraints/constraint_clock.h"
#define DEBUG 0
ProgramState::ProgramState()
{
top_module = NULL;
has_linked = 0;
missing_library = NULL;
}
ProgramState::~ProgramState()
{
this->cleanup();
}
/* Adds a new Liberty file to the program state
*/
void ProgramState::add_library(Liberty *lib)
{
libraries.push_back(lib);
}
/* Adds a new Verilog file to the program state
*/
void ProgramState::add_design(VerilogDesign *design)
{
designs.push_back(design);
}
/* This method goes over all instances in all modules. At first it
splits any nets wider than 1, and any connections wider than 1. Then
it checks if either there is module definition matching the instantiation,
or a cell definition in a Liberty file matchin the instantiation. All pointers
are then hooked up. If any instantiation does not have a matching definition
somewhere, the method will fail with status == -1.
*/
int ProgramState::connect_modules()
{
std::string *instance_name;
std::string *module_name;
bool add_missing = 1; // FIXME
missing_library = new Liberty();
int done;
for(int l=0;l < designs.size() ;l++){
for(int i=0;i < designs[l]->modules.size() ;i++){
designs[l]->modules[i]->split_nets();
for(int j=0;j<designs[l]->modules[i]->instances.size(); j++) {
done = 0;
instance_name = designs[l]->modules[i]->instances[j]->module_name;
designs[l]->modules[i]->instances[j]->split_connections();
// Check if instance is a module in the design:
for(int m=0;m < designs.size() ;m++){
for(int k=0;k < designs[m]->modules.size() ;k++){
module_name = designs[m]->modules[k]->name;
if(instance_name->compare(*module_name) == 0) {
designs[l]->modules[i]->instances[j]->associated_module = designs[m]->modules[k];
designs[l]->modules[i]->instances[j]->save_memory();
if(DEBUG) std::cout << "Found " << (*designs[l]->modules[i]->instances[j]->module_name) << " is a module instance" << std::endl;
done = 1;
break;
}
}
if(done) break;
}
if(done) continue;
// Check if instance is a cell in the library:
for(int m=0;m < libraries.size() ;m++){
for(int k=0;k < libraries[m]->cells.size() ;k++){
module_name = libraries[m]->cells[k]->name;
if(instance_name->compare(*module_name) == 0) {
designs[l]->modules[i]->instances[j]->associated_cell = libraries[m]->cells[k];
designs[l]->modules[i]->instances[j]->save_memory();
if(DEBUG) std::cout << "Found " << (*designs[l]->modules[i]->instances[j]->module_name) << " is a cell instance" << std::endl;
done = 1;
break;
}
}
if(done) break;
}
if(done) continue;
// FIXME: Temporarily commented out
if(add_missing) {
std::cout << "Warning: Cell or module for " << (*instance_name) << " not found. Have you read all necessary libraries?" << std::endl;
for(int k=0;k<missing_library->cells.size() ; k++) {
module_name = missing_library->cells[k]->name;
if(instance_name->compare(*module_name) == 0) {
designs[l]->modules[i]->instances[j]->associated_cell = missing_library->cells[k];
designs[l]->modules[i]->instances[j]->save_memory();
if(DEBUG) std::cout << "Found " << (*designs[l]->modules[i]->instances[j]->module_name) << " in the automatically generated missing library" << std::endl;
done = 1;
break;
}
}
if(!done) {
LibertyCell *new_missing_cell = new LibertyCell(instance_name);
new_missing_cell->is_missing = 1;
missing_library->cells.push_back(new_missing_cell);
designs[l]->modules[i]->instances[j]->associated_cell = new_missing_cell;
//designs[l]->modules[i]->instances[j]->save_memory(); // Cannot do this, since the string is actually taken from this instance..
std::cout << "Created missing cell definition " << (*designs[l]->modules[i]->instances[j]->module_name) << std::endl;
done = 1;
}
}
else {
std::cout << "Error: Cell or module for " << (*instance_name) << " not found. Have you read all necessary libraries?" << std::endl;
return -1;
}
}
}
}
return 0;
}
/* After the top module has been set, it is possible to link the design.
This will clone the module instances for every instance where they are present.
Afterwards, all connections in all instantiations will be iterated, and pure
pointers hooked up for all signals in the design.
If anything goes wrong, it returns -1
*/
int ProgramState::link_design()
{
bool anyfailing=0;
VerilogInstance *inst;
if(top_module != NULL){
for(int i=0;i<top_module->nets.size();i++) {
top_module->nets[i]->parent = top_module;
}
if(top_module->populate() != 0) anyfailing = 1;
if(top_module->hook_up_ports() != 0) anyfailing = 1;
if(anyfailing == 0) {
std::cout << "Successfully linked design!" << std::endl;
return 0;
}
else {
std::cout << "Failed linking design" << std::endl;
return -1;
}
}
else {
printf("No module set to top module (set_design_top)\n");
return -1;
}
}
/* Add a clock constraint to the program state
Also check if there is a matching net. If not, fail with return value -1.
*/
int ProgramState::create_clock(std::string *name, std::string *signal, float period, float half_period)
{
bool found = 0;
if(has_linked) {
for(int i=0;i<top_module->nets.size();i++) {
if(signal->compare(*top_module->nets[i]->name) == 0) {
ConstraintClock *new_clock = new ConstraintClock(name, signal, period, half_period);
constraints.clocks.push_back(new_clock);
new_clock->net = top_module->nets[i];
std::cout << "Found input " << *top_module->nets[i] << ", and connected the clock to it" << std::endl;
found = 1;
return 0;
}
}
}
if(!found) {
std::cout << "Could not find specified net" << std::endl;
return -1;
}
}
LibertyCell * ProgramState::get_cell_liberty(std::string path)
{
char tmpstr[256];
if(!(has_linked)) {
std::cout << "Design must be linked first" << std::endl;
return NULL;
}
const char *cellname = path.c_str();
VerilogModule *mod = top_module;;
LibertyCell *cell = NULL;
const char *end2;
while(cellname != NULL) {
end2 = strchr(cellname, '/');
if(end2 != NULL) {
int ncopy = (end2-cellname > 255) ? 255 : end2-cellname;
strncpy(tmpstr, cellname, ncopy);
tmpstr[ncopy] = 0;
cellname = end2+1;
}
else {
strcpy(tmpstr, cellname);
cellname = NULL;
}
bool found = 0;
for(int i=0;i<mod->instances.size() ;i++) {
if(mod->instances[i]->associated_module != NULL && mod->instances[i]->name->compare(tmpstr) == 0) {
mod = mod->instances[i]->associated_module;
found = 1;
break;
}
else if(mod->instances[i]->associated_cell != NULL && mod->instances[i]->name->compare(tmpstr) == 0) {
cell = mod->instances[i]->associated_cell;
return cell;
}
}
if(!found) return NULL;
}
return NULL;
}
LibertyPin * ProgramState::get_pin_liberty(const std::string &path)
{
char tmpstr[256];
if(!(has_linked)) {
std::cout << "Design must be linked first" << std::endl;
return NULL;
}
const char *cellname = path.c_str();
VerilogModule *mod = top_module;;
LibertyCell *cell = NULL;
const char *end2;
bool current_is_cell = 0;
while(cellname != NULL) {
end2 = strchr(cellname, '/');
if(end2 != NULL) {
int ncopy = (end2-cellname > 255) ? 255 : end2-cellname;
strncpy(tmpstr, cellname, ncopy);
tmpstr[ncopy] = 0;
cellname = end2+1;
}
else {
strcpy(tmpstr, cellname);
cellname = NULL;
}
if(!current_is_cell) {
bool found = 0;
for(int i=0;i<mod->instances.size() ;i++) {
if(mod->instances[i]->associated_module != NULL && mod->instances[i]->name->compare(tmpstr) == 0) {
mod = mod->instances[i]->associated_module;
found = 1;
break;
}
else if(mod->instances[i]->associated_cell != NULL && mod->instances[i]->name->compare(tmpstr) == 0) {
cell = mod->instances[i]->associated_cell;
found = 1;
current_is_cell = 1;
break;
}
}
if(!found) return NULL;
}
else {
bool found = 0;
for(int i=0;i<cell->pins.size() ; i++) {
if(cell->pins[i]->name->compare(tmpstr) == 0) {
return cell->pins[i];
}
}
}
}
return NULL;
}
VerilogNet * ProgramState::get_pin_verilog(std::string path)
{
if(!(has_linked)) {
std::cout << "Design must be linked first" << std::endl;
return NULL;
}
}
VerilogModule * ProgramState::get_cell_verilog(std::string path)
{
if(!(has_linked)) {
std::cout << "Design must be linked first" << std::endl;
return NULL;
}
}
void ProgramState::cleanup()
{
for(int i=0;i<designs.size();i++){
designs[i]->cleanup();
delete designs[i];
}
for(int i=0;i<libraries.size();i++){
libraries[i]->cleanup();
delete libraries[i];
}
designs.clear();
libraries.clear();
top_module = NULL;
has_linked = 0;
if(missing_library != NULL) {
delete missing_library;
missing_library = NULL;
}
}