-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthesis.py
187 lines (150 loc) · 4.57 KB
/
synthesis.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import shutil
import subprocess
import time
from autohdl import toolchain
from autohdl.hdlGlobals import synthesisPath
import logging
from autohdl.hdlLogger import log_call
import sys
log = logging.getLogger(__name__)
@log_call
def getIncludePath(iPrj):
incl = (iPrj.get('include_path', ".") or '.')
return ';'.join(incl)
@log_call
def setParams(iPrj):
device = iPrj.get('device')
part = 'xc' + device[:-5]
package = device[-5:]
family = iPrj.get('family')
technology = family.split()[1] # e.g. 'Xilinx11x SPARTAN3E'
iPrj['scriptContent'] = iPrj['scriptContent'].format(
device=device,
part=part,
package=package,
technology=technology,
topModule=iPrj['top'],
netlist=iPrj['top']+'.edf',
src_files=iPrj['srcFiles'],
includePath=getIncludePath(iPrj))
f = open(iPrj['pathScript'], 'w')
f.write(iPrj['scriptContent'])
f.close()
@log_call
def setSrc(iPrj):
srcMain = iPrj['mainSrc']
srcDep = iPrj['depSrc'] or []
ucf = [iPrj['ucf']] or []
#TODO: got duplicates, guess some bugs in structure
srcFiles = set([i.replace('\\', '/') for i in srcMain + srcDep + ucf])
iPrj['srcFiles'] = '\n'.join(['add_file "{0}"'.format(i) for i in srcFiles])
@log_call
def extend(prj):
#TODO: convert all to abspath
prj['pathTool'] = toolchain.Tool().get(prj['mode'])
prj['pathSynthesis'] = synthesisPath
prj['pathScript'] = prj['pathSynthesis']+ '/synthesis.prj'
prj['pathLog'] = prj['top'] + '.srr'
prj['pathWas'] = os.getcwd().replace('\\','/')
try:
if os.path.exists(prj['pathSynthesis']):
shutil.rmtree(prj['pathSynthesis'])
except OSError as e:
log.warning(e)
for i in range(5):
if os.path.exists(prj['pathSynthesis']):
break
else:
time.sleep(0.5)
try:
os.makedirs(prj['pathSynthesis'])
except OSError as e:
log.warning(e)
synthesis_template = os.path.join(os.path.dirname(__file__), 'data', 'template_synplify_prj')
f = open(synthesis_template, 'r')
prj['scriptContent'] = f.read()
f.close()
setSrc(prj)
setParams(prj)
return prj
@log_call
def parseLog(iPrj, iSilent = False):
logFile = os.path.abspath(iPrj['pathLog'])
if os.path.exists(logFile):
f = open(logFile, 'r')
res = f.read()
errors = res.count('@E:')
warningsAll = res.count('@W:')
ignoreWarnings = res.count('Initial value is not supported on state machine state')
warnings = warningsAll - ignoreWarnings
time.sleep(1)
log.info('See logfile: '+logFile)
if errors:
log.error('Synthesis ended with errors! Num: '+str(errors))
sys.exit(1)
elif warnings:
log.warning('Synthesis ended with warnings! Num: '+str(warnings))
else:
log.info('Errors: '+str(errors)+'; Warnings: '+ str(warnings))
if errors or (not iSilent and warnings):
subprocess.Popen(['notepad', logFile])
return logFile
import xilinx_env
@log_call
def run_synplify_batch(iPrj):
run = '"%s" %s %s %s %s' % (iPrj['pathTool'],
'-product synplify_premier',
'-licensetype synplifypremier',
'-batch','synthesis.prj')
subprocess.Popen(run, env = xilinx_env.get())
loge = ''
for i in range(13):
time.sleep(3)
try:
loge = open(iPrj['pathLog'], 'r')
except IOError as e:
log.debug(e)
continue
if loge:
break
if not loge:
log.error('Cant start synthesis see '+synthesisPath+'/stdout.log')
return
done = False
while True:
where = loge.tell()
res = loge.readline()
if res.find('Mapper successful!') != -1:
done = True
if '@E' in res:
done = True
if res.endswith('\n'):
print res
elif done:
break
else:
loge.seek(where)
time.sleep(1)
loge.close()
@log_call
def run_synplify_gui(iPrj):
subprocess.check_call([iPrj['pathTool'], iPrj['pathScript']], env = xilinx_env.get())
@log_call
def runTool(iPrj, iSilent):
"""
Runs external tool for synthesis
"""
os.chdir(iPrj['pathSynthesis'])
if iPrj['mode'] == 'synplify_batch':
run_synplify_batch(iPrj = iPrj)
parseLog(iPrj = iPrj, iSilent = iSilent)
elif iPrj['mode'] == 'synplify_gui':
run_synplify_gui(iPrj=iPrj)
os.chdir(iPrj['pathWas'])
@log_call
def run(config):
logging.info('Synthesis stage...')
# changing current location to synthesis directory
runTool(extend(config), iSilent = True)
log.info('Synthesis done!')