-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPEXSim.py
More file actions
506 lines (408 loc) · 17.7 KB
/
Copy pathPEXSim.py
File metadata and controls
506 lines (408 loc) · 17.7 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import re
def __remove_backslash(e):
x = e.replace(r'\<', '<')
x = x.replace(r'\>', '>')
return x
def __remove_forwardslash(e):
x = e.replace('\\', '')
return x
def __net_related_q(net_name0, net_name1):
return re.match(__conv_net_name(net_name0) + r'_(.+)', net_name1)
def __conv_net_name(netName):
x = netName.split('/')
if len(x) > 1:
x[0:-1] = list(map(lambda x: "X" + x, x[0:-1]))
return '/'.join(x)
def __device_related_q(device0, device1):
return re.match(device0, device1)
def __find_all_related_nets(net_name, nets_set):
res = []
for e in nets_set:
if __net_related_q(net_name, e):
res.append(e)
return res
def __splitDeviceAndTerminalName(terminal_name):
return terminal_name.split(":")
def __find_all_related_terminal(terminal_name, device_set):
[deviceName, terminalName] = __splitDeviceAndTerminalName(terminal_name)
res = []
for e in device_set:
if __device_related_q(deviceName, e):
res.append(e + ":" + terminalName)
return res
def __find_all_related_nets_for_aListOfNets(net_list_to_be_probed, net_set):
res = []
for e in net_list_to_be_probed:
f = __find_all_related_nets(e, net_set)
res += f
net_set -= set(f)
return res
def __find_all_related_terminal_for_aListOfDevices(terminal_list_to_be_probed, device_set):
res = []
for e in terminal_list_to_be_probed:
f = __find_all_related_terminal(e, device_set)
res += f
device_set -= set(f)
return res
def extract_all_nets_name_from_netlist(moduleList, PEXNetlistDirPath):
res = {}
for e in moduleList:
file = __netlistFileName(e, PEXNetlistDirPath)
with open(file, 'r') as fh:
cont = fh.read()
target = re.compile(r'N_(.+?)[\s)]')
res[e] = re.findall(target, cont)
res[e] = set(res[e])
res[e] = set(map(__remove_backslash, res[e]))
res[e] = set(map(__remove_forwardslash, res[e]))
return res
def extract_all_devices_from_netlist(moduleList, PEXNetlistDirPath):
res = {}
for e in moduleList:
file = __netlistFileName(e, PEXNetlistDirPath)
with open(file, 'r') as fh:
cont = fh.read()
target = re.compile(r'X(.+?) \(')
res[e] = target.findall(cont)
res[e] = set(map(lambda x: "X" + x, res[e]))
res[e] = set(map(__remove_backslash, res[e]))
res[e] = set(map(__remove_forwardslash, res[e]))
return res
def __netlistFileName(moduleName, PEXNetlistDirPath):
from os.path import isfile
file = PEXNetlistDirPath + '/' + moduleName
file = file.replace('//', '/')
if isfile(file + '.spectre.pex.netlist'):
return file + '.spectre.pex.netlist'
elif isfile(file + '.pex.netlist'):
return file + '.pex.netlist'
else:
return None
def __removeDupInList(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
def __split_top_module_name(net):
x = net.split('/', 2)
if len(x)>2:
return (x[1], x[2])
else:
return (x[0], x[1])
def __split_top_module_name_forTerminal(term):
x = term.split('/', 1)
return (x[0], x[1])
def __transform_nets_name_into_spectre_netlist_convention(net_name):
replacements = {'/': r'\\/', '<': r'\\<', '>': r'\\>', '+': r'\\+', '-': r'\\-'}
return __multireplace(net_name, replacements)
def __transform_nets_name_into_spectre_netlist_conventionForADEXL(net_name):
replacements = {'/': r'\/', '<': r'\<', '>': r'\>', '+': r'\+', '-': r'\-'}
return __multireplace(net_name, replacements)
# return re.escape(net_name)
def __parse_bus_net_name(bus):
p = re.compile(r'(.+?)<(\d+):(\d+)')
s = p.match(bus)
net_name = s.group(1)
first_num = int(s.group(2))
sec_num = int(s.group(3))
res = []
for i in range(first_num, sec_num+1):
a = net_name + "<" + str(i) + ">"
res.append(a)
return res
def __conv_term_name(term):
termName = {'D': 'd', 'G': 'g', 'S': 's', 'B': 'b'}
x = term.split('/')
if len(x) > 3:
x = [x[1]] + list(map(__conv_term_name_helper, x[2:-1])) + [termName.get(x[-1], x[-1])]
x[1] = "X" + x[1]
return '/'.join(x[:-1]) + ':' + x[-1]
elif len(x) == 3:
return '/' + x[1] + ':' + x[2]
def __conv_term_name_helper(name):
if name.startswith('I'):
return "X" + name
elif name.startswith('T'):
return "M" + name
elif name.startswith('C'):
return "C" + name
elif name.startswith('R'):
return "R" + name
elif name.startswith('t'):
return "D" + name
import fileinput
def modifySaveInOceanScript(vList, iList, oceanScriptFileName):
pTemp = re.compile(r'temp\((.+?)\)')
vList1 = list(map(lambda x: "\"" + x + "\" ", vList))
iList1 = list(map(lambda x: "\"" + x + "\" ", iList))
saveV = "save( \'v " + "\\\n".join(vList1) + ")"
saveI = "save( \'i " + "\\\n".join(iList1) + ")"
ocnOutputV = list(map(lambda x: "ocnxlOutputSignal( " + x + "?plot t ?save t)", vList1))
ocnOutputV = '\n'.join(ocnOutputV) + '\n'
ocnOutputI = list(map(lambda x: "ocnxlOutputTerminal( " + x + "?plot t ?save t)", iList1))
ocnOutputI = '\n'.join(ocnOutputI) + '\n'
with open(oceanScriptFileName, 'r') as fh:
cont = fh.read()
cont = cont.replace('\r', '')
r = re.compile(r'save\((.+?)\)\n' + r'|' + r'ocnxlOutputSignal\((.+?)\)\n' + r'|' + r'ocnxlOutputTerminal\((.+?)\)\n', re.DOTALL)
cont = r.sub('', cont, 0)
cont = cont.split('\n')
contNew = []
for line in cont:
if re.match(pTemp, line):
contNew.append(saveV + '\n' + saveI + '\n' + line + '\n' + ocnOutputV + ocnOutputI)
else:
contNew.append(line)
with open(oceanScriptFileName, 'w') as fh:
fh.write('\n'.join(contNew))
def modifyTestBenchNetlistFileNameInOceanScript(testBenchNetlistFileName, oceanScriptFileName):
for line in fileinput.FileInput(oceanScriptFileName, inplace=1):
if line.startswith('design('):
print('design( \"' + testBenchNetlistFileName + '\" )')
else:
print(line.rstrip())
def __groupNetTermNameAccToInst(net_list):
res = {}
for e in net_list:
if e[0] not in res:
res[e[0]] = [e[1]]
else:
res[e[0]].append(e[1])
return res
def parseParameterFile(fileName):
res = dict()
with open(fileName, 'r') as fh:
cont = fh.read()
cont = cont.replace('\r', '')
p_savescsPath = re.compile(r'savescsPath(\s*){(.*?)}')
p_PEXNetlistDirPath = re.compile(r'PEXNetlistDirPath(\s*){(.*?)}')
p_saveV = re.compile(r'saveV(\s*){(.*?)}', re.DOTALL)
p_saveI = re.compile(r'saveI(\s*){(.*?)}', re.DOTALL)
p_instance_module_mapping = re.compile(r'instance_module_mapping(\s*){(.*?)}', re.DOTALL)
p_testBenchNetlist_pathFileName = re.compile(r'testBenchNetlist_pathFileName(\s*){(.*?)}')
p_oceanScript_toBeModified_pathFileName = re.compile(r'oceanScript_toBeModified_pathFileName(\s*){(.*?)}')
res['savescsPath'] = p_savescsPath.search(cont).group(2)
res['savescsPath'] = ''.join(res['savescsPath'].split())
if p_testBenchNetlist_pathFileName.search(cont): res['testBenchNetlist_pathFileName'] = p_testBenchNetlist_pathFileName.search(cont).group(2).strip()
if p_oceanScript_toBeModified_pathFileName.search(cont): res['oceanScript_toBeModified_pathFileName'] = p_oceanScript_toBeModified_pathFileName.search(cont).group(2).strip()
res['instance_module_mapping'] = ''.join(p_instance_module_mapping.search(cont).group(2).split())
res['instance_module_mapping'] = __parseInstanceModuleMapping(res['instance_module_mapping'])
res['moduleName'] = list(set(res['instance_module_mapping'].values()))
res['PEXNetlistDirPath'] = p_PEXNetlistDirPath.search(cont).group(2).strip()
res['saveV'] = p_saveV.search(cont).group(2)
if res['saveV'].strip() != '':
res['saveV'] = ''.join(res['saveV'].strip(',').split())
res['saveV'] = __parseNetString(res['saveV'])
res['saveV'] = list(map(__split_top_module_name, res['saveV']))
res['saveV'] = __groupNetTermNameAccToInst(res['saveV'])
else:
res['saveV'] = {}
res['saveI'] = p_saveI.search(cont).group(2)
if res['saveI'].strip() != '':
res['saveI'] = ''.join(res['saveI'].strip(',').split())
res['saveI'] = __parseTermString(res['saveI'])
res['saveI'] = list(map(__conv_term_name, res['saveI']))
res['saveI'] = list(map(__split_top_module_name_forTerminal, res['saveI']))
res['saveI'] = __groupNetTermNameAccToInst(res['saveI'])
else:
res['saveI'] = {}
return res
# def parseParameterFile(fileName):
# res = dict()
# with open(fileName, 'r') as fh:
# cont = fh.read()
# cont = cont.replace('\r', '')
#
# p_testBenchNetlist_pathFileName = re.compile(r'testBenchNetlist_pathFileName(\s*){(.*?)}')
# p_oceanScript_toBeModified_pathFileName = re.compile(r'oceanScript_toBeModified_pathFileName(\s*){(.*?)}')
# p_moduleName = re.compile(r'moduleName(\s*){(.*?)}', re.DOTALL)
# p_PEXNetlistDirPath = re.compile(r'PEXNetlistDirPath(\s*){(.*?)}')
# p_saveV = re.compile(r'saveV(\s*){(.*?)}', re.DOTALL)
# p_saveI = re.compile(r'saveI(\s*){(.*?)}', re.DOTALL)
# p_instance_module_mapping = re.compile(r'instance_module_mapping(\s*){(.*?)}', re.DOTALL)
#
# res['testBenchNetlist_pathFileName'] = p_testBenchNetlist_pathFileName.search(cont).group(2).strip()
#
# res['oceanScript_toBeModified_pathFileName'] = p_oceanScript_toBeModified_pathFileName.search(cont).group(2).strip()
#
# res['moduleName'] = p_moduleName.search(cont).group(2)
# res['moduleName'] = ''.join(res['moduleName'].split()).split(',')
#
# res['PEXNetlistDirPath'] = p_PEXNetlistDirPath.search(cont).group(2).strip()
#
# res['instance_module_mapping'] = ''.join(p_instance_module_mapping.search(cont).group(2).split())
# res['instance_module_mapping'] = __parseInstanceModuleMapping(res['instance_module_mapping'])
#
# res['saveV'] = p_saveV.search(cont).group(2)
# if res['saveV'].strip() != '':
# res['saveV'] = ''.join(res['saveV'].strip(',').split())
# res['saveV'] = __parseNetString(res['saveV'])
# res['saveV'] = list(map(__split_top_module_name, res['saveV']))
# res['saveV'] = __groupNetTermNameAccToInst(res['saveV'])
# else:
# res['saveV'] = {}
#
# res['saveI'] = p_saveI.search(cont).group(2)
# if res['saveI'].strip() != '':
# res['saveI'] = ''.join(res['saveI'].strip(',').split())
# res['saveI'] = __parseTermString(res['saveI'])
# res['saveI'] = list(map(__conv_term_name, res['saveI']))
# res['saveI'] = list(map(__split_top_module_name_forTerminal, res['saveI']))
# res['saveI'] = __groupNetTermNameAccToInst(res['saveI'])
# else:
# res['saveI'] = {}
#
# return res
# def parseParameterFileForADEXL(fileName):
# res = dict()
# with open(fileName, 'r') as fh:
# cont = fh.read()
# cont = cont.replace('\r', '')
#
# p_savescsPath = re.compile(r'savescsPath(\s*){(.*?)}')
# p_PEXNetlistDirPath = re.compile(r'PEXNetlistDirPath(\s*){(.*?)}')
# p_saveV = re.compile(r'saveV(\s*){(.*?)}', re.DOTALL)
# p_saveI = re.compile(r'saveI(\s*){(.*?)}', re.DOTALL)
# p_instance_module_mapping = re.compile(r'instance_module_mapping(\s*){(.*?)}', re.DOTALL)
#
# res['savescsPath'] = p_savescsPath.search(cont).group(2)
# res['savescsPath'] = ''.join(res['savescsPath'].split())
#
# res['PEXNetlistDirPath'] = p_PEXNetlistDirPath.search(cont).group(2).strip()
#
# res['instance_module_mapping'] = ''.join(p_instance_module_mapping.search(cont).group(2).split())
# res['instance_module_mapping'] = __parseInstanceModuleMapping(res['instance_module_mapping'])
#
# res['moduleName'] = list(set(res['instance_module_mapping'].values()))
#
# res['saveV'] = p_saveV.search(cont).group(2)
# if res['saveV'].strip() != '':
# res['saveV'] = ''.join(res['saveV'].strip().strip(',').split())
# res['saveV'] = __parseNetString(res['saveV'])
# res['saveV'] = list(map(__split_top_module_name, res['saveV']))
# res['saveV'] = __groupNetTermNameAccToInst(res['saveV'])
# else:
# res['saveV'] = {}
#
# res['saveI'] = p_saveI.search(cont).group(2)
# if res['saveI'].strip() != '':
# res['saveI'] = ''.join(res['saveI'].strip().strip(',').split())
# res['saveI'] = __parseTermString(res['saveI'])
# res['saveI'] = list(map(__conv_term_name, res['saveI']))
# res['saveI'] = list(map(__split_top_module_name_forTerminal, res['saveI']))
# res['saveI'] = __groupNetTermNameAccToInst(res['saveI'])
# else:
# res['saveI'] = {}
#
# return res
def __multireplace(string, replacements):
"""
Given a string and a replacement map, it returns the replaced string.
:param str string: string to execute replacements on
:param dict replacements: replacement dictionary {value to find: value to replace}
:rtype: str
"""
# Place longer ones first to keep shorter substrings from matching where the longer ones should take place
# For instance given the replacements {'ab': 'AB', 'abc': 'ABC'} against the string 'hey abc', it should produce
# 'hey ABC' and not 'hey ABc'
substrs = sorted(replacements, key=len, reverse=True)
# Create a big OR regex that matches any of the substrings to replace
regexp = re.compile('|'.join(map(re.escape, substrs)))
# For each match, look up the new string in the replacements
return regexp.sub(lambda match: replacements[match.group(0)], string)
def __parseNetString(v):
v = v.split(',')
v_ = []
for e in v:
if e.find(":") != -1:
x = __parse_bus_net_name(e)
v_ = v_ + x
else:
v_.append(e)
v_ = __removeDupInList(v_)
return v_
def __parseTermString(i):
i = i.split(',')
i = __removeDupInList(i)
return i
def __parseInstanceModuleMapping(x):
res = {}
x = x.split(',')
for e in x:
a = e.split(':')
res[a[0]] = a[1]
return res
def netsToProbeInSpectreFormat(netsToProbe, allNets, instance_module_mapping):
saveV = {}
for (k,v) in netsToProbe.items():
if k != '':
saveV[k] = __find_all_related_nets_for_aListOfNets(netsToProbe[k], allNets[instance_module_mapping[k]])
saveV[k] = list(map(__transform_nets_name_into_spectre_netlist_convention, saveV[k]))
saveV[k] = list(map(lambda x: k + ".N_"+x, saveV[k]))
else:
saveV[k] = list(map(lambda x: "/" + x, netsToProbe[k]))
vList = []
for (k, v) in saveV.items():
vList = vList + v
return vList
def termsToProbeInSpectreFormat(termsToProbe, allDevices, instance_module_mapping):
saveI = {}
for (k, v) in termsToProbe.items():
if k != '':
saveI[k] = __find_all_related_terminal_for_aListOfDevices(termsToProbe[k], allDevices[instance_module_mapping[k]])
saveI[k] = list(map(__transform_nets_name_into_spectre_netlist_convention, saveI[k]))
saveI[k] = list(map(lambda x: k + "." + x, saveI[k]))
else:
saveI[k] = list(map(lambda x: "/" + x, termsToProbe[k]))
iList = []
for (k, v) in saveI.items():
iList = iList + v
return iList
def netsToProbeInSpectreFormatForADEXL(netsToProbe, allNets, instance_module_mapping):
saveV = {}
for (k,v) in netsToProbe.items():
if k != '':
saveV[k] = __find_all_related_nets_for_aListOfNets(netsToProbe[k], allNets[instance_module_mapping[k]])
saveV[k] = list(map(__transform_nets_name_into_spectre_netlist_conventionForADEXL, saveV[k]))
saveV[k] = list(map(lambda x: k + ".N_"+x, saveV[k]))
else:
saveV[k] = list(map(__transform_nets_name_into_spectre_netlist_conventionForADEXL,netsToProbe[k]))
vList = []
for (k, v) in saveV.items():
vList = vList + v
return vList
def termsToProbeInSpectreFormatForADEXL(termsToProbe, allDevices, instance_module_mapping):
saveI = {}
for (k, v) in termsToProbe.items():
if k != '':
saveI[k] = __find_all_related_terminal_for_aListOfDevices(termsToProbe[k], allDevices[instance_module_mapping[k]])
saveI[k] = list(map(__transform_nets_name_into_spectre_netlist_conventionForADEXL, saveI[k]))
saveI[k] = list(map(lambda x: k + "." + x, saveI[k]))
else:
saveI[k] = list(map(__transform_nets_name_into_spectre_netlist_conventionForADEXL, termsToProbe[k]))
iList = []
for (k, v) in saveI.items():
iList = iList + v
return iList
def modifyTestbenchNetlistFile(PEXNetlistDirPath, moduleName, testBenchNetlist_pathFileName):
includePathName = list(map(lambda x: __netlistFileName(x, PEXNetlistDirPath), moduleName))
for i in range(len(includePathName)):
includePathName[i] = includePathName[i].replace('//', '/')
with open(testBenchNetlist_pathFileName, 'r') as fh:
cont = fh.read()
cont = cont.replace('\r', '')
for i in range(len(includePathName)):
p = re.compile(r'subckt ' + moduleName[i] + r'(.+?)ends ' + moduleName[i] + r'\n', re.DOTALL)
cont = p.sub('include \"' + includePathName[i] + '\"\n', cont,0)
with open(testBenchNetlist_pathFileName, 'w') as fh:
fh.write(cont)
def createSavescs(vList, iList, filePath):
if len(vList) != 0:
x = 'save ' + ' \\\n'.join(vList)
else:
x = ''
if len(iList) != 0:
x = x + '\n' + 'save ' + ' \\\n'.join(iList)
fileName = filePath + '/save.scs'
fileName = fileName.replace('//', '/')
with open(fileName, 'w') as fh:
fh.write(x)