-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathopensta_parser.py
executable file
·170 lines (114 loc) · 4.03 KB
/
opensta_parser.py
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
"""
Generates power map
optional arguments:
-h, --help show this help message and exit
--lib Path to the library file
--lef Path to the lef file
--deff Path to the routed def file
--resizer Path to resizer binary files
--clk Clock period of the design"""
import os
import argparse
from pathlib import Path
def read_file(filepath):
try:
with open(filepath, 'r') as f:
return f.readlines()
except IOError:
print('Could not read the following file:',filepath)
def write_file(filepath,line):
try:
with open(filepath, 'a') as f:
f.write(line)
f.close()
except IOError:
print('Could not write in the following file:',filepath)
#Parses arguments from the command line
def ParseArg():
parser = argparse.ArgumentParser(description= 'Generates power map')
parser.add_argument('--lib',type=str,required=True,metavar='', help='Path to the library file')
parser.add_argument('--lef',type=str,required=True, metavar='',help='Path to the lef file')
parser.add_argument('--deff',type=str,required=True, metavar='',help='Path to the routed def file')
parser.add_argument('--resizer',type=str,required=True, metavar='',help='Path to resizer binary files')
parser.add_argument('--clk',type=str,required=True, metavar='',help='Clock period of the design')
return parser.parse_args()
#Writes a script file to be used by resizer
def ScriptTemplate(args):
line='read_liberty '
line='%s %s %s'%(line,args.lib,'\n')
write_file("Script_template",line)
line='read_lef '
line='%s %s %s'%(line,args.lef,'\n')
write_file("Script_template",line)
line='read_def '
line='%s %s %s'%(line,args.deff,'\n')
write_file("Script_template",line)
line='create_clock -name clk -period '
line='%s %s %s %s'%(line,args.clk,'{clk}','\n')
write_file("Script_template",line)
#Parses the def file and returns the coordinates and the list of instances, as well as the associated resizer command
def ParseDef(args):
inst=[]
xcoord=[]
ycoord=[]
listline=read_file(args.deff)
mylines=[]
for line in listline:
mylines.append(line.split())
for i in range(len(mylines)):
if(len(mylines[i])!=0):
if(mylines[i][0]=='COMPONENTS'):
break
start=i+1
for j in range(start,len(mylines)):
if(len(mylines[j])!=0):
if(mylines[j][0]=='END'):
break
end=j
for i in range(start,end):
if(len(mylines[i])>1):
inst.append(mylines[i][1])
xcoord.append(mylines[i][mylines[i].index('(')+1])
ycoord.append(mylines[i][mylines[i].index('(')+2])
for i in range(len(inst)):
inst[i]=inst[i].replace('\\','')
command='report_power -instances "'
for i in range(0,len(inst)):
command='%s %s'%(command,inst[i])
command='%s" > out'%(command)
return command, xcoord,ycoord,inst;
# Runs resizer and parses its output to generate the output file that contains the coordinates and the power of each instance
def RunResizer(args,command,xcoord,ycoord,inst):
stamylines=[]
stainst=[]
stapower=[]
write_file("Script_template",command)
exec_command='%s %s'%(args.resizer,' -exit Script_template')
os.system(exec_command)
stalistline=read_file('out')
for line in stalistline:
stamylines.append(line.split())
for i in range(3,len(stamylines)):
stapower.append(stamylines[i][3])
stainst.append(stamylines[i][4])
for i in range(len(stapower)):
st=stapower[i]
st='%s %s %s \n'%(st,xcoord[inst.index(stainst[i])],ycoord[inst.index(stainst[i])])
write_file("outputhotspot",st)
if __name__ == "__main__":
args=ParseArg()
script_file = Path('Script_template')
if script_file.is_file():
print("Removing script template file")
os.remove('Script_template')
output_file = Path('outputhotspot')
if output_file.is_file():
print("Removing output file")
os.remove('outputhotspot')
ScriptTemplate(args)
print("Parsing DEF file...")
command,xcoord,ycoorrd,inst=ParseDef(args)
print("Running Resizer...")
RunResizer(args,command,xcoord,ycoorrd,inst)
print("Script done. Output file is outputhotspot")
os.rename('Script_template', 'Script_template_saved')