blob: 2e2d0f5c7b5930fa2dcabd2ed1c3b39984c35e37 [file] [log] [blame]
Jeff DiCorpo963296c2021-02-19 01:36:54 -08001#!/usr/bin/env python3
agorararmard6c766a82020-12-10 18:13:12 +02002# SPDX-FileCopyrightText: 2020 Efabless Corporation
agorararmarde5780bf2020-12-09 21:27:56 +00003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
agorararmardafa96ea2020-12-09 23:37:31 +020015# SPDX-License-Identifier: Apache-2.0
agorararmarde5780bf2020-12-09 21:27:56 +000016
Tim Edwardsafa7a412020-12-07 16:10:02 -050017#
18# generate_fill.py ---
19#
Jeff DiCorpo963296c2021-02-19 01:36:54 -080020# Run the fill generation on a layout top level.
Tim Edwardsafa7a412020-12-07 16:10:02 -050021#
22
23import sys
24import os
25import re
Jeff DiCorpo963296c2021-02-19 01:36:54 -080026import glob
Tim Edwardsafa7a412020-12-07 16:10:02 -050027import subprocess
Jeff DiCorpo963296c2021-02-19 01:36:54 -080028import multiprocessing
Tim Edwardsafa7a412020-12-07 16:10:02 -050029
30def usage():
Jeff DiCorpo963296c2021-02-19 01:36:54 -080031 print("Usage:")
32 print("generate_fill.py [<path_to_project>] [-keep] [-test] [-dist]")
33 print("")
34 print("where:")
35 print(" <path_to_project> is the path to the project top level directory.")
36 print("")
37 print(" If <path_to_project> is not given, then it is assumed to be the cwd.")
38 print(" If '-keep' is specified, then keep the generation script.")
39 print(" If '-test' is specified, then create but do not run the generation script.")
40 print(" If '-dist' is specified, then run distributed (multi-processing).")
41
Tim Edwardsafa7a412020-12-07 16:10:02 -050042 return 0
43
Jeff DiCorpo963296c2021-02-19 01:36:54 -080044def makegds(file):
45 # Procedure for multiprocessing run only: Run the distributed processing
46 # script to load a .mag file of one flattened square area of the layout,
47 # and run the fill generator to produce a .gds file output from it.
Tim Edwardsafa7a412020-12-07 16:10:02 -050048
Jeff DiCorpo963296c2021-02-19 01:36:54 -080049 magpath = os.path.split(file)[0]
50 filename = os.path.split(file)[1]
Tim Edwardsafa7a412020-12-07 16:10:02 -050051
52 myenv = os.environ.copy()
53 myenv['MAGTYPE'] = 'mag'
54
55 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
Jeff DiCorpo963296c2021-02-19 01:36:54 -080056 '-rcfile', rcfile, magpath + '/generate_fill_dist.tcl',
57 filename],
Tim Edwardsafa7a412020-12-07 16:10:02 -050058 stdin = subprocess.DEVNULL,
59 stdout = subprocess.PIPE,
60 stderr = subprocess.PIPE,
Jeff DiCorpo963296c2021-02-19 01:36:54 -080061 cwd = magpath,
Tim Edwardsafa7a412020-12-07 16:10:02 -050062 env = myenv,
63 universal_newlines = True)
64 if mproc.stdout:
65 for line in mproc.stdout.splitlines():
66 print(line)
67 if mproc.stderr:
68 print('Error message output from magic:')
69 for line in mproc.stderr.splitlines():
70 print(line)
71 if mproc.returncode != 0:
72 print('ERROR: Magic exited with status ' + str(mproc.returncode))
73
Tim Edwardsafa7a412020-12-07 16:10:02 -050074
Jeff DiCorpo963296c2021-02-19 01:36:54 -080075if __name__ == '__main__':
76
77 optionlist = []
78 arguments = []
79
80 debugmode = False
81 keepmode = False
82 testmode = False
83 distmode = False
84
85 for option in sys.argv[1:]:
86 if option.find('-', 0) == 0:
87 optionlist.append(option)
88 else:
89 arguments.append(option)
90
91 if len(arguments) > 1:
92 print("Wrong number of arguments given to generate_fill.py.")
93 usage()
94 sys.exit(1)
95
96 if len(arguments) == 1:
97 user_project_path = arguments[0]
98 else:
99 user_project_path = os.getcwd()
100
101 if not os.path.isdir(user_project_path):
102 print('Error: Project path "' + user_project_path + '" does not exist or is not readable.')
103 sys.exit(1)
104
105 # Check for valid user ID
106 user_id_value = None
107 if os.path.isfile(user_project_path + '/info.yaml'):
108 with open(user_project_path + '/info.yaml', 'r') as ifile:
109 infolines = ifile.read().splitlines()
110 for line in infolines:
111 kvpair = line.split(':')
112 if len(kvpair) == 2:
113 key = kvpair[0].strip()
114 value = kvpair[1].strip()
115 if key == 'project_id':
116 user_id_value = value.strip('"\'')
117 break
118
119 project = 'caravel'
120 if user_id_value:
121 project_with_id = project + '_' + user_id_value
122 else:
123 print('Error: No project_id found in info.yaml file.')
124 sys.exit(1)
125
126 if '-debug' in optionlist:
127 debugmode = True
128 if '-keep' in optionlist:
129 keepmode = True
130 if '-test' in optionlist:
131 testmode = True
132 if '-dist' in optionlist:
133 distmode = True
134
135 magpath = user_project_path + '/mag'
136 rcfile = magpath + '/.magicrc'
137
138 if not os.path.isfile(rcfile):
139 rcfile = None
140
141 topdir = user_project_path
142 gdsdir = topdir + '/gds'
143 hasgdsdir = True if os.path.isdir(gdsdir) else False
144
145 ofile = open(magpath + '/generate_fill.tcl', 'w')
146
147 print('#!/bin/env wish', file=ofile)
148 print('drc off', file=ofile)
149 print('tech unlock *', file=ofile)
150 print('snap internal', file=ofile)
151 print('box values 0 0 0 0', file=ofile)
152 print('box size 700um 700um', file=ofile)
153 print('set stepbox [box values]', file=ofile)
154 print('set stepwidth [lindex $stepbox 2]', file=ofile)
155 print('set stepheight [lindex $stepbox 3]', file=ofile)
156 print('', file=ofile)
157 print('set starttime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
158 print('puts stdout "Started: $starttime"', file=ofile)
159 print('', file=ofile)
160 # Read the user project from GDS, as there is not necessarily a magic database file
161 # to go along with this.
162 # print('gds read ../gds/user_project_wrapper', file=ofile)
163 # Now read the full caravel project
164 # print('load ' + project + ' -dereference', file=ofile)
165 print('gds readonly true', file=ofile)
166 print('gds rescale false', file=ofile)
167 print('gds read ../gds/caravel', file=ofile)
168 print('select top cell', file=ofile)
169 print('expand', file=ofile)
170 if not distmode:
171 print('cif ostyle wafflefill(tiled)', file=ofile)
172 print('', file=ofile)
173 print('set fullbox [box values]', file=ofile)
174 print('set xmax [lindex $fullbox 2]', file=ofile)
175 print('set xmin [lindex $fullbox 0]', file=ofile)
176 print('set fullwidth [expr {$xmax - $xmin}]', file=ofile)
177 print('set xtiles [expr {int(ceil(($fullwidth + 0.0) / $stepwidth))}]', file=ofile)
178 print('set ymax [lindex $fullbox 3]', file=ofile)
179 print('set ymin [lindex $fullbox 1]', file=ofile)
180 print('set fullheight [expr {$ymax - $ymin}]', file=ofile)
181 print('set ytiles [expr {int(ceil(($fullheight + 0.0) / $stepheight))}]', file=ofile)
182 print('box size $stepwidth $stepheight', file=ofile)
183 print('set xbase [lindex $fullbox 0]', file=ofile)
184 print('set ybase [lindex $fullbox 1]', file=ofile)
185 print('', file=ofile)
186
187 # Break layout into tiles and process each separately
188 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
189 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
190 print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
191 print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile)
192 print(' set xhi [expr $xlo + $stepwidth]', file=ofile)
193 print(' set yhi [expr $ylo + $stepheight]', file=ofile)
194 print(' if {$xhi > $fullwidth} {set xhi $fullwidth}', file=ofile)
195 print(' if {$yhi > $fullheight} {set yhi $fullheight}', file=ofile)
196 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
197 # The flattened area must be larger than the fill tile by >1.5um
198 print(' box grow c 1.6um', file=ofile)
199
200 # Flatten into a cell with a new name
201 print(' puts stdout "Flattening layout of tile x=$x y=$y. . . "', file=ofile)
202 print(' flush stdout', file=ofile)
203 print(' update idletasks', file=ofile)
204 print(' flatten -dobox -nolabels ' + project_with_id + '_fill_pattern_${x}_$y', file=ofile)
205 print(' load ' + project_with_id + '_fill_pattern_${x}_$y', file=ofile)
206 # Remove any GDS_FILE reference (there should not be any?)
207 print(' property GDS_FILE ""', file=ofile)
208 # Set boundary using comment layer, to the size of the step box
209 # This corresponds to the "topbox" rule in the wafflefill(tiled) style
210 print(' select top cell', file=ofile)
211 print(' erase comment', file=ofile)
212 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
213 print(' paint comment', file=ofile)
214
215 if not distmode:
216 print(' puts stdout "Writing GDS. . . "', file=ofile)
217
218 print(' flush stdout', file=ofile)
219 print(' update idletasks', file=ofile)
220
221 if distmode:
222 print(' writeall force ' + project_with_id + '_fill_pattern_${x}_$y', file=ofile)
223 else:
224 print(' gds write ' + project_with_id + '_fill_pattern_${x}_$y.gds', file=ofile)
225 # Reload project top
226 print(' load ' + project, file=ofile)
227
228 # Remove last generated cell to save memory
229 print(' cellname delete ' + project_with_id + '_fill_pattern_${x}_$y', file=ofile)
230
231 print(' }', file=ofile)
232 print('}', file=ofile)
233
234 if distmode:
235 print('set ofile [open fill_gen_info.txt w]', file=ofile)
236 print('puts $ofile "$stepwidth"', file=ofile)
237 print('puts $ofile "$stepheight"', file=ofile)
238 print('puts $ofile "$xtiles"', file=ofile)
239 print('puts $ofile "$ytiles"', file=ofile)
240 print('puts $ofile "$xbase"', file=ofile)
241 print('puts $ofile "$ybase"', file=ofile)
242 print('close $ofile', file=ofile)
243 print('quit -noprompt', file=ofile)
244 ofile.close()
245
246 with open(magpath + '/generate_fill_dist.tcl', 'w') as ofile:
247 print('#!/bin/env wish', file=ofile)
248 print('drc off', file=ofile)
249 print('tech unlock *', file=ofile)
250 print('snap internal', file=ofile)
251 print('box values 0 0 0 0', file=ofile)
252 print('set filename [file root [lindex $argv $argc-1]]', file=ofile)
253 print('load $filename', file=ofile)
254 print('cif ostyle wafflefill(tiled)', file=ofile)
255 print('gds write [file root $filename].gds', file=ofile)
256 print('quit -noprompt', file=ofile)
257
258 ofile = open(magpath + '/generate_fill_final.tcl', 'w')
259 print('#!/bin/env wish', file=ofile)
260 print('drc off', file=ofile)
261 print('tech unlock *', file=ofile)
262 print('snap internal', file=ofile)
263 print('box values 0 0 0 0', file=ofile)
264
265 print('set ifile [open fill_gen_info.txt r]', file=ofile)
266 print('gets $ifile stepwidth', file=ofile)
267 print('gets $ifile stepheight', file=ofile)
268 print('gets $ifile xtiles', file=ofile)
269 print('gets $ifile ytiles', file=ofile)
270 print('gets $ifile xbase', file=ofile)
271 print('gets $ifile ybase', file=ofile)
272 print('close $ifile', file=ofile)
273
274 # Now create simple "fake" views of all the tiles.
275 print('gds readonly true', file=ofile)
276 print('gds rescale false', file=ofile)
277 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
278 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
279 print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
280 print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile)
281 print(' set xhi [expr $xlo + $stepwidth]', file=ofile)
282 print(' set yhi [expr $ylo + $stepheight]', file=ofile)
283 print(' load ' + project_with_id + '_fill_pattern_${x}_$y -quiet', file=ofile)
284 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
285 print(' paint comment', file=ofile)
286 print(' property FIXED_BBOX "$xlo $ylo $xhi $yhi"', file=ofile)
287 print(' property GDS_FILE ' + project_with_id + '_fill_pattern_${x}_${y}.gds', file=ofile)
288 print(' property GDS_START 0', file=ofile)
289 print(' }', file=ofile)
290 print('}', file=ofile)
291
292 # Now tile everything back together
293 print('load ' + project_with_id + '_fill_pattern -quiet', file=ofile)
294 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
295 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
296 print(' box values 0 0 0 0', file=ofile)
297 print(' getcell ' + project_with_id + '_fill_pattern_${x}_$y child 0 0', file=ofile)
298 print(' }', file=ofile)
299 print('}', file=ofile)
300
301 # And write final GDS
302 print('puts stdout "Writing final GDS"', file=ofile)
303
304 print('cif *hier write disable', file=ofile)
305 print('cif *array write disable', file=ofile)
306 if hasgdsdir:
307 print('gds write ../gds/' + project_with_id + '_fill_pattern.gds', file=ofile)
308 else:
309 print('gds write ' + project_with_id + '_fill_pattern.gds', file=ofile)
310 print('set endtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
311 print('puts stdout "Ended: $endtime"', file=ofile)
312 print('quit -noprompt', file=ofile)
313 ofile.close()
314
315 myenv = os.environ.copy()
316 myenv['MAGTYPE'] = 'mag'
317
318 if not testmode:
319 # Diagnostic
320 # print('This script will generate file ' + project_with_id + '_fill_pattern.gds')
321 print('This script will generate files ' + project_with_id + '_fill_pattern_x_y.gds')
322 print('Now generating fill patterns. This may take. . . quite. . . a while.', flush=True)
323 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
324 '-rcfile', rcfile, magpath + '/generate_fill.tcl'],
325 stdin = subprocess.DEVNULL,
326 stdout = subprocess.PIPE,
327 stderr = subprocess.PIPE,
328 cwd = magpath,
329 env = myenv,
330 universal_newlines = True)
331 if mproc.stdout:
332 for line in mproc.stdout.splitlines():
333 print(line)
334 if mproc.stderr:
335 print('Error message output from magic:')
336 for line in mproc.stderr.splitlines():
337 print(line)
338 if mproc.returncode != 0:
339 print('ERROR: Magic exited with status ' + str(mproc.returncode))
340
341 if distmode:
342 # If using distributed mode, then run magic on each of the generated
343 # layout files
344 pool = multiprocessing.Pool()
345 magfiles = glob.glob(magpath + '/' + project_with_id + '_fill_pattern_*.mag')
346 # NOTE: Adding 'x' to the end of each filename, or else magic will
347 # try to read it from the command line as well as passing it as an
348 # argument to the script. We only want it passed as an argument.
349 magxfiles = list(item + 'x' for item in magfiles)
350 pool.map(makegds, magxfiles)
351
352 # If using distributed mode, then remove all of the temporary .mag files
353 # and then run the final generation script.
354 for file in magfiles:
355 os.remove(file)
356
357 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
358 '-rcfile', rcfile, magpath + '/generate_fill_final.tcl'],
359 stdin = subprocess.DEVNULL,
360 stdout = subprocess.PIPE,
361 stderr = subprocess.PIPE,
362 cwd = magpath,
363 env = myenv,
364 universal_newlines = True)
365 if mproc.stdout:
366 for line in mproc.stdout.splitlines():
367 print(line)
368 if mproc.stderr:
369 print('Error message output from magic:')
370 for line in mproc.stderr.splitlines():
371 print(line)
372 if mproc.returncode != 0:
373 print('ERROR: Magic exited with status ' + str(mproc.returncode))
374
375 if not keepmode:
376 # Remove fill generation script
377 os.remove(magpath + '/generate_fill.tcl')
378 # Remove all individual fill tiles, leaving only the composite GDS.
379 filelist = os.listdir(magpath)
380 for file in filelist:
381 if os.path.splitext(magpath + '/' + file)[1] == '.gds':
382 if file.startswith(project + '_fill_pattern_'):
383 os.remove(magpath + '/' + file)
384
385 if distmode:
386 os.remove(magpath + '/generate_fill_dist.tcl')
387 os.remove(magpath + '/generate_fill_final.tcl')
388 os.remove(magpath + '/fill_gen_info.txt')
389 if testmode:
390 magfiles = glob.glob(magpath + '/' + project_with_id + '_fill_pattern_*.mag')
391 for file in magfiles:
392 os.remove(file)
393
394 print('Done!')
Tim Edwardsafa7a412020-12-07 16:10:02 -0500395 exit(0)