-
Notifications
You must be signed in to change notification settings - Fork 0
/
simu_ckt_ss.cc
306 lines (287 loc) · 7.65 KB
/
simu_ckt_ss.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <climits>
#include <vector>
#include <map>
//#include <unordered_map>
#include <cmath>
#include <cassert>
#include <ctime>
#include <sys/timeb.h>
#include "head/queue.h"
#include "head/call_abc.h"
#include "head/write_func.h"
#include "head/helper.h"
#include "/home/wuyi/usr/CUDD/cudd-2.5.0/cudd/cudd.h"
#include "/home/wuyi/usr/CUDD/cudd-2.5.0/cudd/cuddInt.h"
#include "cudd/bnet.h"
using namespace std;
extern int numPI_ini, numPO_ini;
extern int sample_num;
//Global variables and external variables
void simu_ckt_ss(BnetNetwork *net, map<string, string> &node_signatures, map<string, double> &node_sp, double &real_er, int iIndex, int flag)
{
struct timeb startTime, endTime;
BnetNode *nd, *tmp;
BnetTabline *f;
if(iIndex == 0 && flag == 0)
{
//get node_set: all nodes excluding PI nodes
int num_input = net->npis;
int num_output = 0;
vector<char*> node_set;
nd = net->nodes;
cout << "node_set: " << endl;
while(nd != NULL)
{
if(nd->type == BNET_INPUT_NODE)
{
nd = nd->next;
continue;
}
node_set.push_back(nd->name);
cout << nd->name << " ";
num_output++;
nd = nd->next;
}
cout << endl;
//write ckt_org_simu.blif
string filename = "./blif_files/ckt_org_simu.blif";
ofstream fout;
fout.open(filename.c_str(), iostream::out);
fout << ".model ckt_org_simu" << endl;
fout << ".inputs ";
for(int i = 0; i < net->npis; i++)
fout << "n" << net->inputs[i] << " ";
fout << endl;
fout << ".outputs ";
for(int i = 0; i < node_set.size(); i++)
fout << "n" << node_set[i] << " ";
fout << endl;
nd = net->nodes;
while(nd != NULL)
{
if(nd->type == BNET_INPUT_NODE)
{
nd = nd->next;
continue;
}
fout << ".names ";
for(int i = 0; i < nd->ninp; i++)
fout << "n" << nd->inputs[i] << " ";
fout << "n" << nd->name << endl;
f = nd->f;
while(f != NULL)
{
if (f->values != NULL)
fout << f->values << " " << 1 - nd->polarity << endl;
else
fout << 1 - nd->polarity << endl;
f = f->next;
}
nd = nd->next;
}
//convert ckt_org_simu.blif to ckt_org_simu.v
int res = call_abc(0); //ckt_org_simu.blif -> ckt_org_simu.v
//write testbench file
gen_testbench_org(num_input, num_output, sample_num);
//call vcs to run the simulation
vector<string> rand, simu_res;
rand.reserve(sample_num);
simu_res.reserve(sample_num);
call_vcs_org(rand, simu_res);
//obtain input_signatures
cout << "obtain node_signatures of PI nodes: " << endl;
for(int i = 0; i < net->npis; i++)
{
char *input = net->inputs[i];
string sig;
double num_one = 0;
for(int j = 0; j < rand.size(); j++)
{
string vec = rand[j];
char c = vec[i];
sig.append(1, c);
if(c == '1')
num_one++;
}
double sp = num_one/sample_num;
cout << "input node: " << input << ", sp = " << sp << endl;
string str(input);
node_sp.insert(make_pair(str, sp));
node_signatures.insert(make_pair(str, sig));
}
//obtain node_signatures
cout << "obtain node_signatures of internal nodes: " << endl;
int index = 0;
nd = net->nodes;
while(nd != NULL)
{
if(nd->type == BNET_INPUT_NODE)
{
nd = nd->next;
continue;
}
char *cnode = nd->name;
string str(cnode);
string sig;
double num_one = 0;
for(int j = 0; j < simu_res.size(); j++)
{
string vec = simu_res[j];
char c = vec[index];
sig.append(1, c);
if(c == '1')
num_one++;
}
double sp = num_one/sample_num;
node_sp.insert(make_pair(str, sp));
node_signatures.insert(pair<string, string>(str, sig));
nd = nd->next;
index++;
}
}//if(iIndex == 0 && flag == 0)
else
{
//step1. get node_set: all nodes excluding PI and nodes in ckt_org
int num_input = net->npis;
int num_output = 0;
vector<char*> node_set;
nd = net->nodes;
cout << "node_set: " << endl;
while(nd != NULL)
{
if(nd->type == BNET_INPUT_NODE)
{
nd = nd->next;
continue;
}
string sname(nd->name);
if((sname.find("sim") == string::npos) || nd->type == BNET_OUTPUT_NODE)
{
nd = nd->next;
continue;
}
node_set.push_back(nd->name);
num_output++;
nd = nd->next;
}
char *outnode = net->outputs[0];
st_lookup(net->hash, outnode, &nd);
node_set.push_back(nd->name);
num_output++;
//write ckt_org_sim_simu.blif
string filename = "./blif_files/ckt_org_sim_simu.blif";
ofstream fout;
fout.open(filename.c_str(), iostream::out);
fout << ".model ckt_org_sim_simu" << endl;
fout << ".inputs ";
for(int i = 0; i < net->npis; i++)
fout << "n" << net->inputs[i] << " ";
fout << endl;
fout << ".outputs ";
for(int i = 0; i < node_set.size(); i++)
fout << "n" << node_set[i] << " ";
fout << endl;
nd = net->nodes;
while(nd != NULL)
{
if(nd->type == BNET_INPUT_NODE)
{
nd = nd->next;
continue;
}
fout << ".names ";
for(int i = 0; i < nd->ninp; i++)
fout << "n" << nd->inputs[i] << " ";
fout << "n" << nd->name << endl;
f = nd->f;
while(f != NULL)
{
if (f->values != NULL)
fout << f->values << " " << 1 - nd->polarity << endl;
else
fout << 1 - nd->polarity << endl;
f = f->next;
}
nd = nd->next;
}
//step2.1 convert ckt_org_sim_simu.blif to ckt_org_sim_simu.v
int res = call_abc(1);
//step2.2 write testbench file
string dut = "ckt_org_sim_simu";
gen_testbench_sim(num_input, num_output, sample_num, dut);
//step2.3 call vcs to run the simulation
vector<string> rand, simu_res;
string po_sig;
call_vcs_sim(rand, simu_res, po_sig, dut);
int num_diff = 0;
for(int j = 0; j < po_sig.size(); j++)
if(po_sig[j] == '1')
num_diff++;
cout << "num_diff = " << num_diff << endl;
real_er = num_diff/(double)sample_num;
//step3. obtain node_signatures
cout << "obtain node_signatures of PI nodes: " << endl;
for(int i = 0; i < net->npis; i++)
{
char *input = net->inputs[i];
string snode(input);
string sig;
double num_one = 0;
for(int j = 0; j < rand.size(); j++)
{
string vec = rand[j];
char c = vec[i];
sig.append(1, c);
if(c == '1')
num_one++;
}
double sp = num_one/sample_num;
// cout << "input node: " << input << ", sp = " << sp << endl;
node_sp.insert(make_pair(snode, sp));
node_signatures.insert(make_pair(snode, sig));
}
//step4. obtain node_signatures of internal nodes
cout << "obtain node_signatures of internal nodes: " << endl;
int index = 0;
nd = net->nodes;
while(nd != NULL)
{
if(nd->type == BNET_INPUT_NODE)
{
nd = nd->next;
continue;
}
char *cnode = nd->name;
string snode(cnode);
if((snode.find("sim") == string::npos) || nd->type == BNET_OUTPUT_NODE)
{
nd = nd->next;
continue;
}
string sig;
double num_one = 0;
for(int j = 0; j < simu_res.size(); j++)
{
string vec = simu_res[j];
char c = vec[index];
sig.append(1, c);
if(c == '1')
num_one++;
}
double sp = num_one/sample_num;
// cout << "internal node: " << snode << ", sp = " << sp << endl;
string snode_org;
if(snode.find("sim") != string::npos)
snode_org = snode.substr(0, snode.length()-3);
node_sp.insert(make_pair(snode_org, sp));
node_signatures.insert(make_pair(snode_org, sig));
nd = nd->next;
index++;
}
}
}