blob: 943920c53d6c995bc25f4e4d6396c2f920f90108 [file] [log] [blame]
Tim Edwards04d1f832021-02-17 09:54:38 -05001#!/usr/bin/env python3
Tim Edwardsb71e5f82020-12-29 16:15:26 -05002# SPDX-FileCopyrightText: 2020 Efabless Corporation
3#
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.
15# SPDX-License-Identifier: Apache-2.0
16
17#
18# generate_fill.py ---
19#
20# Run the fill generation on a layout top level.
21#
22
23import sys
24import os
25import re
Tim Edwards04d1f832021-02-17 09:54:38 -050026import glob
Tim Edwardsb71e5f82020-12-29 16:15:26 -050027import subprocess
Tim Edwards04d1f832021-02-17 09:54:38 -050028import multiprocessing
Tim Edwardsb71e5f82020-12-29 16:15:26 -050029
30def usage():
31 print("Usage:")
Tim Edwards04d1f832021-02-17 09:54:38 -050032 print("generate_fill.py <layout_name> [-keep] [-test] [-dist]")
Tim Edwardsb71e5f82020-12-29 16:15:26 -050033 print("")
34 print("where:")
35 print(" <layout_name> is the path to the .mag file to be filled.")
36 print("")
37 print(" If '-keep' is specified, then keep the generation script.")
38 print(" If '-test' is specified, then create but do not run the generation script.")
Tim Edwards04d1f832021-02-17 09:54:38 -050039 print(" If '-dist' is specified, then run distributed (multi-processing).")
Tim Edwardsb71e5f82020-12-29 16:15:26 -050040 return 0
41
Tim Edwards04d1f832021-02-17 09:54:38 -050042def makegds(file):
43 # Procedure for multiprocessing only: Run the distributed processing
44 # script to load a .mag file of one flattened square area of the layout,
45 # and run the fill generator to produce a .gds file output from it.
46
47 magpath = os.path.split(file)[0]
48 filename = os.path.split(file)[1]
49
50 myenv = os.environ.copy()
51 myenv['MAGTYPE'] = 'mag'
52
53 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
54 '-rcfile', rcfile, magpath + '/generate_fill_dist.tcl',
55 filename],
56 stdin = subprocess.DEVNULL,
57 stdout = subprocess.PIPE,
58 stderr = subprocess.PIPE,
59 cwd = magpath,
60 env = myenv,
61 universal_newlines = True)
62 if mproc.stdout:
63 for line in mproc.stdout.splitlines():
64 print(line)
65 if mproc.stderr:
66 print('Error message output from magic:')
67 for line in mproc.stderr.splitlines():
68 print(line)
69 if mproc.returncode != 0:
70 print('ERROR: Magic exited with status ' + str(mproc.returncode))
71
72
Tim Edwardsb71e5f82020-12-29 16:15:26 -050073if __name__ == '__main__':
74
75 optionlist = []
76 arguments = []
77
78 debugmode = False
79 keepmode = False
80 testmode = False
Tim Edwards04d1f832021-02-17 09:54:38 -050081 distmode = False
Tim Edwardsb71e5f82020-12-29 16:15:26 -050082
83 for option in sys.argv[1:]:
84 if option.find('-', 0) == 0:
85 optionlist.append(option)
86 else:
87 arguments.append(option)
88
89 if len(arguments) != 1:
90 print("Wrong number of arguments given to generate_fill.py.")
91 usage()
92 sys.exit(1)
93
94 user_project_path = arguments[0]
95
96 magpath = os.path.split(user_project_path)[0]
97 if magpath == '':
98 magpath = os.getcwd()
99
100 if os.path.splitext(user_project_path)[1] != '.mag':
101 if os.path.splitext(user_project_path)[1] == '':
102 user_project_path += '.mag'
103 else:
104 print('Error: Project is not a magic database .mag file!')
105 sys.exit(1)
106
107 if not os.path.isfile(user_project_path):
108 print('Error: Project "' + user_project_path + '" does not exist or is not readable.')
109 sys.exit(1)
110
111 if '-debug' in optionlist:
112 debugmode = True
113 if '-keep' in optionlist:
114 keepmode = True
115 if '-test' in optionlist:
116 testmode = True
Tim Edwards04d1f832021-02-17 09:54:38 -0500117 if '-dist' in optionlist:
118 distmode = True
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500119
120 rcfile = magpath + '/.magicrc'
121 if not os.path.isfile(rcfile):
122 rcfile = None
123
124 project = os.path.splitext(os.path.split(user_project_path)[1])[0]
125
126 topdir = os.path.split(magpath)[0]
127 gdsdir = topdir + '/gds'
128 hasgdsdir = True if os.path.isdir(gdsdir) else False
129
Tim Edwards04d1f832021-02-17 09:54:38 -0500130 ofile = open(magpath + '/generate_fill.tcl', 'w')
131
132 print('#!/usr/bin/env wish', file=ofile)
133 print('drc off', file=ofile)
134 print('tech unlock *', file=ofile)
135 print('snap internal', file=ofile)
136 print('box values 0 0 0 0', file=ofile)
137 print('box size 700um 700um', file=ofile)
138 print('set stepbox [box values]', file=ofile)
139 print('set stepwidth [lindex $stepbox 2]', file=ofile)
140 print('set stepheight [lindex $stepbox 3]', file=ofile)
141 print('', file=ofile)
142 print('set starttime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
143 print('puts stdout "Started: $starttime"', file=ofile)
144 print('', file=ofile)
145 print('load ' + project + ' -dereference', file=ofile)
146 print('select top cell', file=ofile)
147 print('expand', file=ofile)
148 if not distmode:
149 print('cif ostyle wafflefill(tiled)', file=ofile)
150 print('', file=ofile)
151 print('set fullbox [box values]', file=ofile)
152 print('set xmax [lindex $fullbox 2]', file=ofile)
153 print('set xmin [lindex $fullbox 0]', file=ofile)
154 print('set fullwidth [expr {$xmax - $xmin}]', file=ofile)
155 print('set xtiles [expr {int(ceil(($fullwidth + 0.0) / $stepwidth))}]', file=ofile)
156 print('set ymax [lindex $fullbox 3]', file=ofile)
157 print('set ymin [lindex $fullbox 1]', file=ofile)
158 print('set fullheight [expr {$ymax - $ymin}]', file=ofile)
159 print('set ytiles [expr {int(ceil(($fullheight + 0.0) / $stepheight))}]', file=ofile)
160 print('box size $stepwidth $stepheight', file=ofile)
161 print('set xbase [lindex $fullbox 0]', file=ofile)
162 print('set ybase [lindex $fullbox 1]', file=ofile)
163 print('', file=ofile)
164
165 # Break layout into tiles and process each separately
166 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
167 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
168 print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
169 print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile)
170 print(' set xhi [expr $xlo + $stepwidth]', file=ofile)
171 print(' set yhi [expr $ylo + $stepheight]', file=ofile)
172 print(' if {$xhi > $fullwidth} {set xhi $fullwidth}', file=ofile)
173 print(' if {$yhi > $fullheight} {set yhi $fullheight}', file=ofile)
174 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
175 # The flattened area must be larger than the fill tile by >1.5um
176 print(' box grow c 1.6um', file=ofile)
177
178 # Flatten into a cell with a new name
179 print(' puts stdout "Flattening layout of tile x=$x y=$y. . . "', file=ofile)
180 print(' flush stdout', file=ofile)
181 print(' update idletasks', file=ofile)
182 print(' flatten -dobox -nolabels ' + project + '_fill_pattern_${x}_$y', file=ofile)
183 print(' load ' + project + '_fill_pattern_${x}_$y', file=ofile)
184
185 # Remove any GDS_FILE reference (there should not be any?)
186 print(' property GDS_FILE ""', file=ofile)
187 # Set boundary using comment layer, to the size of the step box
188 # This corresponds to the "topbox" rule in the wafflefill(tiled) style
189 print(' select top cell', file=ofile)
190 print(' erase comment', file=ofile)
191 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
192 print(' paint comment', file=ofile)
193
194 if not distmode:
195 print(' puts stdout "Writing GDS. . . "', file=ofile)
196
197 print(' flush stdout', file=ofile)
198 print(' update idletasks', file=ofile)
199
200 if distmode:
201 print(' writeall force ' + project + '_fill_pattern_${x}_$y', file=ofile)
202 else:
203 print(' gds write ' + project + '_fill_pattern_${x}_$y.gds', file=ofile)
204
205 # Reload project top
206 print(' load ' + project, file=ofile)
207
208 # Remove last generated cell to save memory
209 print(' cellname delete ' + project + '_fill_pattern_${x}_$y', file=ofile)
210
211 print(' }', file=ofile)
212 print('}', file=ofile)
213
214 if distmode:
215 print('set ofile [open fill_gen_info.txt w]', file=ofile)
216 print('puts $ofile "$stepwidth"', file=ofile)
217 print('puts $ofile "$stepheight"', file=ofile)
218 print('puts $ofile "$xtiles"', file=ofile)
219 print('puts $ofile "$ytiles"', file=ofile)
220 print('puts $ofile "$xbase"', file=ofile)
221 print('puts $ofile "$ybase"', file=ofile)
222 print('close $ofile', file=ofile)
223 print('quit -noprompt', file=ofile)
224 ofile.close()
225
226 with open(magpath + '/generate_fill_dist.tcl', 'w') as ofile:
227 print('#!/usr/bin/env wish', file=ofile)
228 print('drc off', file=ofile)
229 print('tech unlock *', file=ofile)
230 print('snap internal', file=ofile)
231 print('box values 0 0 0 0', file=ofile)
232 print('set filename [file root [lindex $argv $argc-1]]', file=ofile)
233 print('load $filename', file=ofile)
234 print('cif ostyle wafflefill(tiled)', file=ofile)
235 print('gds write [file root $filename].gds', file=ofile)
236 print('quit -noprompt', file=ofile)
237
238 ofile = open(magpath + '/generate_fill_final.tcl', 'w')
239 print('#!/usr/bin/env wish', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500240 print('drc off', file=ofile)
241 print('tech unlock *', file=ofile)
242 print('snap internal', file=ofile)
243 print('box values 0 0 0 0', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500244
Tim Edwards04d1f832021-02-17 09:54:38 -0500245 print('set ifile [open fill_gen_info.txt r]', file=ofile)
246 print('gets $ifile stepwidth', file=ofile)
247 print('gets $ifile stepheight', file=ofile)
248 print('gets $ifile xtiles', file=ofile)
249 print('gets $ifile ytiles', file=ofile)
250 print('gets $ifile xbase', file=ofile)
251 print('gets $ifile ybase', file=ofile)
252 print('close $ifile', file=ofile)
Tim Edwards10aaba32021-02-17 10:20:12 -0500253 print('cif ostyle wafflefill(tiled)', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500254
Tim Edwards04d1f832021-02-17 09:54:38 -0500255 # Now create simple "fake" views of all the tiles.
256 print('gds readonly true', file=ofile)
257 print('gds rescale false', file=ofile)
258 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
259 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
260 print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
261 print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile)
262 print(' set xhi [expr $xlo + $stepwidth]', file=ofile)
263 print(' set yhi [expr $ylo + $stepheight]', file=ofile)
264 print(' load ' + project + '_fill_pattern_${x}_$y -quiet', file=ofile)
265 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
266 print(' paint comment', file=ofile)
267 print(' property FIXED_BBOX "$xlo $ylo $xhi $yhi"', file=ofile)
268 print(' property GDS_FILE ' + project + '_fill_pattern_${x}_${y}.gds', file=ofile)
269 print(' property GDS_START 0', file=ofile)
270 print(' }', file=ofile)
271 print('}', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500272
Tim Edwards04d1f832021-02-17 09:54:38 -0500273 # Now tile everything back together
274 print('load ' + project + '_fill_pattern -quiet', file=ofile)
275 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
276 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
277 print(' box values 0 0 0 0', file=ofile)
278 print(' getcell ' + project + '_fill_pattern_${x}_$y child 0 0', file=ofile)
279 print(' }', file=ofile)
280 print('}', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500281
Tim Edwards04d1f832021-02-17 09:54:38 -0500282 # And write final GDS
283 print('puts stdout "Writing final GDS"', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500284
Tim Edwards04d1f832021-02-17 09:54:38 -0500285 print('cif *hier write disable', file=ofile)
286 print('cif *array write disable', file=ofile)
287 if hasgdsdir:
288 print('gds write ../gds/' + project + '_fill_pattern.gds', file=ofile)
289 else:
290 print('gds write ' + project + '_fill_pattern.gds', file=ofile)
291 print('set endtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
292 print('puts stdout "Ended: $endtime"', file=ofile)
293 print('quit -noprompt', file=ofile)
294 ofile.close()
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500295
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500296 myenv = os.environ.copy()
297 myenv['MAGTYPE'] = 'mag'
298
299 if not testmode:
300 # Diagnostic
301 # print('This script will generate file ' + project + '_fill_pattern.gds')
302 print('This script will generate files ' + project + '_fill_pattern_x_y.gds')
303 print('Now generating fill patterns. This may take. . . quite. . . a while.', flush=True)
304 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
305 '-rcfile', rcfile, magpath + '/generate_fill.tcl'],
306 stdin = subprocess.DEVNULL,
307 stdout = subprocess.PIPE,
308 stderr = subprocess.PIPE,
309 cwd = magpath,
310 env = myenv,
311 universal_newlines = True)
312 if mproc.stdout:
313 for line in mproc.stdout.splitlines():
314 print(line)
315 if mproc.stderr:
316 print('Error message output from magic:')
317 for line in mproc.stderr.splitlines():
318 print(line)
319 if mproc.returncode != 0:
320 print('ERROR: Magic exited with status ' + str(mproc.returncode))
321
Tim Edwards04d1f832021-02-17 09:54:38 -0500322 if distmode:
323 # If using distributed mode, then run magic on each of the generated
324 # layout files
325 pool = multiprocessing.Pool()
326 magfiles = glob.glob(magpath + '/' + project + '_fill_pattern_*.mag')
327 # NOTE: Adding 'x' to the end of each filename, or else magic will
328 # try to read it from the command line as well as passing it as an
329 # argument to the script. We only want it passed as an argument.
330 magxfiles = list(item + 'x' for item in magfiles)
331 pool.map(makegds, magxfiles)
332
333 # If using distributed mode, then remove all of the temporary .mag files
334 # and then run the final generation script.
335 for file in magfiles:
336 os.remove(file)
337
338 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
339 '-rcfile', rcfile, magpath + '/generate_fill_final.tcl'],
340 stdin = subprocess.DEVNULL,
341 stdout = subprocess.PIPE,
342 stderr = subprocess.PIPE,
343 cwd = magpath,
344 env = myenv,
345 universal_newlines = True)
346 if mproc.stdout:
347 for line in mproc.stdout.splitlines():
348 print(line)
349 if mproc.stderr:
350 print('Error message output from magic:')
351 for line in mproc.stderr.splitlines():
352 print(line)
353 if mproc.returncode != 0:
354 print('ERROR: Magic exited with status ' + str(mproc.returncode))
355
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500356 if not keepmode:
357 # Remove fill generation script
358 os.remove(magpath + '/generate_fill.tcl')
359 # Remove all individual fill tiles, leaving only the composite GDS.
360 filelist = os.listdir(magpath)
361 for file in filelist:
362 if os.path.splitext(magpath + '/' + file)[1] == '.gds':
Tim Edwardsa0cdd342020-12-29 16:26:58 -0500363 if file.startswith(project + '_fill_pattern_'):
364 os.remove(magpath + '/' + file)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500365
Tim Edwards04d1f832021-02-17 09:54:38 -0500366 if distmode:
367 os.remove(magpath + '/generate_fill_dist.tcl')
368 os.remove(magpath + '/generate_fill_final.tcl')
369 os.remove(magpath + '/fill_gen_info.txt')
370 if testmode:
371 magfiles = glob.glob(magpath + '/' + project + '_fill_pattern_*.mag')
372 for file in magfiles:
373 os.remove(file)
374
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500375 print('Done!')
376 exit(0)