-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_reader_v2
More file actions
391 lines (295 loc) · 21.2 KB
/
bench_reader_v2
File metadata and controls
391 lines (295 loc) · 21.2 KB
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
import re
network_name = []
benchmark_name = ["b18"] # "b01", "b02", "b03", "b04", "b05", "b06", "b07", "b08", "b09", "b10",
# "b11", "b12", "b13", "b14", "b15", "b17", , "b19", "b20", "b21", "b22"
benchmark_type = [""] # , "_opt", "_opt_r"
for b_n in benchmark_name:
for b_t in benchmark_type:
name = 'i99t/' + b_n + "/" + b_n + b_t + '.bench'
bp_name = 'i99t_blueprints/'+ b_n + b_t + '.txt'
print(bp_name)
with open(name, 'r') as reader:
line = reader.readline()
# print(reader.readline)
# while line != '': # The EOF char is an empty string
# if not(line[0] == "#"):
# print(line, end='')
# line = reader.readline()
full_text = reader.readlines()
network_name = re.split('\.|/', str(reader.name))
with open(bp_name, 'w') as writer:
# Alternatively you could use
# writer.writelines(reversed(dog_breeds))
writer.write("template <typename Ntk>\n")
writer.write("mockturtle::names_view<Ntk> {}()\n".format(network_name[2]))
writer.write("{\n")
writer.write(" mockturtle::names_view<Ntk> ntk{};\n")
# Write the dog breeds to the file in reversed order
register_storage = []
bridge_storage = []
output_storage = []
gate_string_storage = []
placed_gates_storage = []
required_gates_storage = []
place_this_gate = []
bridge_counter = 1
for new_line in full_text:
if not(new_line[0] == "#"):
new_line = new_line.replace(' ', '')
res = re.split('=|\)|\(|,', new_line)
if("INPUT" in new_line):
# writer.write(new_line)
#split the string to get the name of the INPUT
#INPUT name = res[1]
#write the corresponding blueprint line
# e.g. const auto a = ntk.create_pi("a");
writer.write(" const auto {} = ntk.create_pi(\"{}\");".format(res[1], res[1]))
placed_gates_storage.append(res[1])
writer.write("\n")
elif("OUTPUT" in new_line):
#write the corresponding blueprint line
# e.g. ntk.create_po(m, "f");
# e.g. const auto bridge_out_neg_1 = ntk.create_not(OUTP_REG);
if("REG" in res[1]):
Bridge_Appendix = []
Bridge_Appendix.append(" const auto bridge_out_neg_{} = ntk.create_not({});\n".format(bridge_counter, res[1]))
Bridge_Appendix.append(" const auto bridge_out_{} = ntk.create_not(bridge_out_neg_{});\n".format(bridge_counter, bridge_counter))
bridge_storage.append(Bridge_Appendix)
output_storage.append(" ntk.create_po(bridge_out_{}, \"{}\");\n".format(bridge_counter, res[1]))
bridge_counter += 1
else:
output_storage.append(" ntk.create_po({}, \"{}\");\n".format(res[1], res[1]))
elif("REG" in res[0]):
#write the corresponding blueprint line for RO
# e.g. const auto r1_o = ntk.create_ro();
writer.write(" const auto {} = ntk.create_ro();\n".format(res[0]))
placed_gates_storage.append(res[0])
#safe the corresponding blueprint line for RI and write later
# e.g. ntk.create_ri(xo1);
ri_creator = " ntk.create_ri({});\n".format(res[2])
register_storage.append(ri_creator)
elif(len(res) == 4):
if("NOT" in res[1]):
#res[2] is input
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
# writer.write(" const auto {} = ntk.create_not({});\n".format(res[0], res[2]))
Appendix = []
Appendix.append(" const auto {} = ntk.create_not({});\n".format(res[0], res[2]))
gate_string_storage.append(Appendix)
required_gates_storage.append([res[2]])
place_this_gate.append(res[0])
elif(len(res) == 5):
if("NAND" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {} = ntk.create_not({}_h);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("AND" in res[1]):
Appendix = []
Appendix.append(" const auto {} = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
gate_string_storage.append(Appendix)
elif("NOR" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {} = ntk.create_not({}_h);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("OR" in res[1]):
Appendix = []
Appendix.append(" const auto {} = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
gate_string_storage.append(Appendix)
required_gates_storage.append([res[2], res[3]])
place_this_gate.append(res[0])
elif(len(res) == 6):
if("NAND" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_and({}_h1, {});\n".format(res[0], res[0], res[4]))
Appendix.append(" const auto {} = ntk.create_not({}_h2);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("AND" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {} = ntk.create_and({}_h1, {});\n".format(res[0], res[0], res[4]))
gate_string_storage.append(Appendix)
elif("NOR" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_or({}_h1, {});\n".format(res[0], res[0], res[4]))
Appendix.append(" const auto {} = ntk.create_not({}_h2);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("OR" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {} = ntk.create_or({}_h1, {});\n".format(res[0], res[0], res[4]))
gate_string_storage.append(Appendix)
required_gates_storage.append([res[2], res[3], res[4]])
place_this_gate.append(res[0])
elif(len(res) == 7):
if("NAND" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_and({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h3 = ntk.create_and({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_not({}_h3);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("AND" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_and({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {} = ntk.create_and({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
gate_string_storage.append(Appendix)
elif("NOR" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_or({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h3 = ntk.create_or({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_not({}_h3);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("OR" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_or({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {} = ntk.create_or({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
gate_string_storage.append(Appendix)
required_gates_storage.append([res[2], res[3], res[4], res[5]])
place_this_gate.append(res[0])
elif(len(res) == 8):
if("NAND" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_and({}_h1, {});\n".format(res[0], res[0], res[4]))
Appendix.append(" const auto {}_h3 = ntk.create_and({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h4 = ntk.create_and({}_h2, {}_h3);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_not({}_h4);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("AND" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_and({}_h1, {});\n".format(res[0], res[0], res[4]))
Appendix.append(" const auto {}_h3 = ntk.create_and({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {} = ntk.create_and({}_h2, {}_h3);\n".format(res[0], res[0], res[0]))
gate_string_storage.append(Appendix)
elif("NOR" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_or({}_h1, {});\n".format(res[0], res[0], res[4]))
Appendix.append(" const auto {}_h3 = ntk.create_or({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h4 = ntk.create_or({}_h2, {}_h3);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_not({}_h4);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("OR" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_or({}_h1, {});\n".format(res[0], res[0], res[4]))
Appendix.append(" const auto {}_h3 = ntk.create_or({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {} = ntk.create_or({}_h2, {}_h3);\n".format(res[0], res[0], res[0]))
gate_string_storage.append(Appendix)
required_gates_storage.append([res[2], res[3], res[4], res[5]])
place_this_gate.append(res[0])
elif(len(res) == 9):
if("NAND" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_and({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h3 = ntk.create_and({}, {});\n".format(res[0], res[6], res[7]))
Appendix.append(" const auto {}_h4 = ntk.create_and({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {}_h5 = ntk.create_and({}_h3, {}_h4);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_not({}_h5);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("AND" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_and({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_and({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h3 = ntk.create_and({}, {});\n".format(res[0], res[6], res[7]))
Appendix.append(" const auto {}_h4 = ntk.create_and({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_and({}_h3, {}_h4);\n".format(res[0], res[0], res[0]))
gate_string_storage.append(Appendix)
elif("NOR" in res[1]):
#res[2] and res[3] are the two inputs
#write the corresponding blueprint line for RO
# e.g. const auto f = ntk.create_and(a, n);
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_or({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h3 = ntk.create_or({}, {});\n".format(res[0], res[6], res[7]))
Appendix.append(" const auto {}_h4 = ntk.create_or({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {}_h5 = ntk.create_or({}_h3, {}_h4);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_not({}_h5);\n".format(res[0], res[0]))
gate_string_storage.append(Appendix)
elif("OR" in res[1]):
Appendix = []
Appendix.append(" const auto {}_h1 = ntk.create_or({}, {});\n".format(res[0], res[2], res[3]))
Appendix.append(" const auto {}_h2 = ntk.create_or({}, {});\n".format(res[0], res[4], res[5]))
Appendix.append(" const auto {}_h3 = ntk.create_or({}, {});\n".format(res[0], res[6], res[7]))
Appendix.append(" const auto {}_h4 = ntk.create_or({}_h1, {}_h2);\n".format(res[0], res[0], res[0]))
Appendix.append(" const auto {} = ntk.create_or({}_h3, {}_h4);\n".format(res[0], res[0], res[0]))
gate_string_storage.append(Appendix)
required_gates_storage.append([res[2], res[3], res[4], res[5]])
place_this_gate.append(res[0])
else:
print(res)
go_ahead = False
while(go_ahead == False):
go_ahead = True
iterator = 0
for write in required_gates_storage:
# print(write)
place = True
if not (place_this_gate[iterator] in placed_gates_storage):
for current_gate in write:
if not(current_gate in placed_gates_storage):
place = False
go_ahead = False
# print("NOT PLACED" + current_gate)
if(place):
placed_gates_storage.append(place_this_gate[iterator])
for g in gate_string_storage[iterator]:
writer.write(g)
del gate_string_storage[iterator]
del place_this_gate[iterator]
del required_gates_storage[iterator]
# writer.write(gate_string_storage[iterator])
# print(iterator)
iterator +=1
# print(iterator)
writer.write("\n")
for bridge in bridge_storage:
for b in bridge:
writer.write(b)
writer.write("\n")
for po in output_storage:
writer.write(po)
writer.write("\n")
for ri in register_storage:
writer.write(ri)
writer.write("\n")
writer.write(" return ntk;\n")
writer.write("}")