blob: 08209b3a95c40f3a2c6a4ea45d6531a7bb6622fe [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
emayecsc6b8a8c2021-07-03 13:51:48 -040096 magpath = os.getcwd()+'/'+os.path.split(user_project_path)[0]
Tim Edwardsb71e5f82020-12-29 16:15:26 -050097 if magpath == '':
98 magpath = os.getcwd()
emayecsc6b8a8c2021-07-03 13:51:48 -040099
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500100 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
emayecsc6b8a8c2021-07-03 13:51:48 -0400120 # Searching for rcfile
121
122 rcfile_paths=[magpath+'/.magicrc','/$PDK_PATH/libs.tech/magic/sky130A.magicrc','/usr/share/pdk/sky130A/libs.tech/magic/sky130A.magicrc']
123
124 rcfile=''
125
126 for rc_path in rcfile_paths:
127 if os.path.isfile(rc_path):
128 rcfile=rc_path
129 break
130
131 if rcfile=='':
132 print('Error: .magicrc file not found.')
133 sys.exit(1)
134
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500135 project = os.path.splitext(os.path.split(user_project_path)[1])[0]
136
137 topdir = os.path.split(magpath)[0]
138 gdsdir = topdir + '/gds'
139 hasgdsdir = True if os.path.isdir(gdsdir) else False
emayecsc6b8a8c2021-07-03 13:51:48 -0400140
Tim Edwards04d1f832021-02-17 09:54:38 -0500141 ofile = open(magpath + '/generate_fill.tcl', 'w')
emayecsc6b8a8c2021-07-03 13:51:48 -0400142
Tim Edwards04d1f832021-02-17 09:54:38 -0500143 print('#!/usr/bin/env wish', file=ofile)
144 print('drc off', file=ofile)
145 print('tech unlock *', file=ofile)
146 print('snap internal', file=ofile)
147 print('box values 0 0 0 0', file=ofile)
148 print('box size 700um 700um', file=ofile)
149 print('set stepbox [box values]', file=ofile)
150 print('set stepwidth [lindex $stepbox 2]', file=ofile)
151 print('set stepheight [lindex $stepbox 3]', file=ofile)
152 print('', file=ofile)
153 print('set starttime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
154 print('puts stdout "Started: $starttime"', file=ofile)
155 print('', file=ofile)
156 print('load ' + project + ' -dereference', file=ofile)
157 print('select top cell', file=ofile)
158 print('expand', file=ofile)
159 if not distmode:
160 print('cif ostyle wafflefill(tiled)', file=ofile)
161 print('', file=ofile)
162 print('set fullbox [box values]', file=ofile)
163 print('set xmax [lindex $fullbox 2]', file=ofile)
164 print('set xmin [lindex $fullbox 0]', file=ofile)
165 print('set fullwidth [expr {$xmax - $xmin}]', file=ofile)
166 print('set xtiles [expr {int(ceil(($fullwidth + 0.0) / $stepwidth))}]', file=ofile)
167 print('set ymax [lindex $fullbox 3]', file=ofile)
168 print('set ymin [lindex $fullbox 1]', file=ofile)
169 print('set fullheight [expr {$ymax - $ymin}]', file=ofile)
170 print('set ytiles [expr {int(ceil(($fullheight + 0.0) / $stepheight))}]', file=ofile)
171 print('box size $stepwidth $stepheight', file=ofile)
172 print('set xbase [lindex $fullbox 0]', file=ofile)
173 print('set ybase [lindex $fullbox 1]', file=ofile)
174 print('', file=ofile)
175
176 # Break layout into tiles and process each separately
177 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
178 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
179 print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
180 print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile)
181 print(' set xhi [expr $xlo + $stepwidth]', file=ofile)
182 print(' set yhi [expr $ylo + $stepheight]', file=ofile)
183 print(' if {$xhi > $fullwidth} {set xhi $fullwidth}', file=ofile)
184 print(' if {$yhi > $fullheight} {set yhi $fullheight}', file=ofile)
185 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
186 # The flattened area must be larger than the fill tile by >1.5um
187 print(' box grow c 1.6um', file=ofile)
188
189 # Flatten into a cell with a new name
190 print(' puts stdout "Flattening layout of tile x=$x y=$y. . . "', file=ofile)
191 print(' flush stdout', file=ofile)
192 print(' update idletasks', file=ofile)
193 print(' flatten -dobox -nolabels ' + project + '_fill_pattern_${x}_$y', file=ofile)
194 print(' load ' + project + '_fill_pattern_${x}_$y', file=ofile)
195
196 # Remove any GDS_FILE reference (there should not be any?)
197 print(' property GDS_FILE ""', file=ofile)
198 # Set boundary using comment layer, to the size of the step box
199 # This corresponds to the "topbox" rule in the wafflefill(tiled) style
200 print(' select top cell', file=ofile)
201 print(' erase comment', file=ofile)
202 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
203 print(' paint comment', file=ofile)
204
205 if not distmode:
206 print(' puts stdout "Writing GDS. . . "', file=ofile)
207
208 print(' flush stdout', file=ofile)
209 print(' update idletasks', file=ofile)
210
211 if distmode:
212 print(' writeall force ' + project + '_fill_pattern_${x}_$y', file=ofile)
213 else:
214 print(' gds write ' + project + '_fill_pattern_${x}_$y.gds', file=ofile)
215
216 # Reload project top
217 print(' load ' + project, file=ofile)
218
219 # Remove last generated cell to save memory
220 print(' cellname delete ' + project + '_fill_pattern_${x}_$y', file=ofile)
221
222 print(' }', file=ofile)
223 print('}', file=ofile)
224
225 if distmode:
226 print('set ofile [open fill_gen_info.txt w]', file=ofile)
227 print('puts $ofile "$stepwidth"', file=ofile)
228 print('puts $ofile "$stepheight"', file=ofile)
229 print('puts $ofile "$xtiles"', file=ofile)
230 print('puts $ofile "$ytiles"', file=ofile)
231 print('puts $ofile "$xbase"', file=ofile)
232 print('puts $ofile "$ybase"', file=ofile)
233 print('close $ofile', file=ofile)
234 print('quit -noprompt', file=ofile)
235 ofile.close()
236
237 with open(magpath + '/generate_fill_dist.tcl', 'w') as ofile:
238 print('#!/usr/bin/env wish', file=ofile)
239 print('drc off', file=ofile)
240 print('tech unlock *', file=ofile)
241 print('snap internal', file=ofile)
242 print('box values 0 0 0 0', file=ofile)
243 print('set filename [file root [lindex $argv $argc-1]]', file=ofile)
244 print('load $filename', file=ofile)
245 print('cif ostyle wafflefill(tiled)', file=ofile)
246 print('gds write [file root $filename].gds', file=ofile)
247 print('quit -noprompt', file=ofile)
248
249 ofile = open(magpath + '/generate_fill_final.tcl', 'w')
250 print('#!/usr/bin/env wish', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500251 print('drc off', file=ofile)
252 print('tech unlock *', file=ofile)
253 print('snap internal', file=ofile)
254 print('box values 0 0 0 0', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500255
Tim Edwards04d1f832021-02-17 09:54:38 -0500256 print('set ifile [open fill_gen_info.txt r]', file=ofile)
257 print('gets $ifile stepwidth', file=ofile)
258 print('gets $ifile stepheight', file=ofile)
259 print('gets $ifile xtiles', file=ofile)
260 print('gets $ifile ytiles', file=ofile)
261 print('gets $ifile xbase', file=ofile)
262 print('gets $ifile ybase', file=ofile)
263 print('close $ifile', file=ofile)
Tim Edwards10aaba32021-02-17 10:20:12 -0500264 print('cif ostyle wafflefill(tiled)', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500265
Tim Edwards04d1f832021-02-17 09:54:38 -0500266 # Now create simple "fake" views of all the tiles.
267 print('gds readonly true', file=ofile)
268 print('gds rescale false', file=ofile)
269 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
270 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
271 print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile)
272 print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile)
273 print(' set xhi [expr $xlo + $stepwidth]', file=ofile)
274 print(' set yhi [expr $ylo + $stepheight]', file=ofile)
275 print(' load ' + project + '_fill_pattern_${x}_$y -quiet', file=ofile)
276 print(' box values $xlo $ylo $xhi $yhi', file=ofile)
277 print(' paint comment', file=ofile)
278 print(' property FIXED_BBOX "$xlo $ylo $xhi $yhi"', file=ofile)
279 print(' property GDS_FILE ' + project + '_fill_pattern_${x}_${y}.gds', file=ofile)
280 print(' property GDS_START 0', file=ofile)
281 print(' }', file=ofile)
282 print('}', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500283
Tim Edwards04d1f832021-02-17 09:54:38 -0500284 # Now tile everything back together
285 print('load ' + project + '_fill_pattern -quiet', file=ofile)
286 print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
287 print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
288 print(' box values 0 0 0 0', file=ofile)
289 print(' getcell ' + project + '_fill_pattern_${x}_$y child 0 0', file=ofile)
290 print(' }', file=ofile)
291 print('}', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500292
Tim Edwards04d1f832021-02-17 09:54:38 -0500293 # And write final GDS
294 print('puts stdout "Writing final GDS"', file=ofile)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500295
Tim Edwards04d1f832021-02-17 09:54:38 -0500296 print('cif *hier write disable', file=ofile)
297 print('cif *array write disable', file=ofile)
298 if hasgdsdir:
299 print('gds write ../gds/' + project + '_fill_pattern.gds', file=ofile)
300 else:
301 print('gds write ' + project + '_fill_pattern.gds', file=ofile)
302 print('set endtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
303 print('puts stdout "Ended: $endtime"', file=ofile)
304 print('quit -noprompt', file=ofile)
305 ofile.close()
emayecsc6b8a8c2021-07-03 13:51:48 -0400306
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500307 myenv = os.environ.copy()
308 myenv['MAGTYPE'] = 'mag'
309
310 if not testmode:
311 # Diagnostic
312 # print('This script will generate file ' + project + '_fill_pattern.gds')
313 print('This script will generate files ' + project + '_fill_pattern_x_y.gds')
314 print('Now generating fill patterns. This may take. . . quite. . . a while.', flush=True)
emayecsc6b8a8c2021-07-03 13:51:48 -0400315
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500316 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
317 '-rcfile', rcfile, magpath + '/generate_fill.tcl'],
318 stdin = subprocess.DEVNULL,
319 stdout = subprocess.PIPE,
320 stderr = subprocess.PIPE,
321 cwd = magpath,
322 env = myenv,
323 universal_newlines = True)
emayecsc6b8a8c2021-07-03 13:51:48 -0400324
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500325 if mproc.stdout:
326 for line in mproc.stdout.splitlines():
327 print(line)
328 if mproc.stderr:
329 print('Error message output from magic:')
330 for line in mproc.stderr.splitlines():
331 print(line)
332 if mproc.returncode != 0:
333 print('ERROR: Magic exited with status ' + str(mproc.returncode))
emayecsc6b8a8c2021-07-03 13:51:48 -0400334
335
Tim Edwards04d1f832021-02-17 09:54:38 -0500336 if distmode:
337 # If using distributed mode, then run magic on each of the generated
338 # layout files
339 pool = multiprocessing.Pool()
340 magfiles = glob.glob(magpath + '/' + project + '_fill_pattern_*.mag')
341 # NOTE: Adding 'x' to the end of each filename, or else magic will
342 # try to read it from the command line as well as passing it as an
343 # argument to the script. We only want it passed as an argument.
344 magxfiles = list(item + 'x' for item in magfiles)
345 pool.map(makegds, magxfiles)
346
347 # If using distributed mode, then remove all of the temporary .mag files
348 # and then run the final generation script.
349 for file in magfiles:
350 os.remove(file)
351
352 mproc = subprocess.run(['magic', '-dnull', '-noconsole',
353 '-rcfile', rcfile, magpath + '/generate_fill_final.tcl'],
354 stdin = subprocess.DEVNULL,
355 stdout = subprocess.PIPE,
356 stderr = subprocess.PIPE,
357 cwd = magpath,
358 env = myenv,
359 universal_newlines = True)
360 if mproc.stdout:
361 for line in mproc.stdout.splitlines():
362 print(line)
363 if mproc.stderr:
364 print('Error message output from magic:')
365 for line in mproc.stderr.splitlines():
366 print(line)
367 if mproc.returncode != 0:
368 print('ERROR: Magic exited with status ' + str(mproc.returncode))
369
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500370 if not keepmode:
371 # Remove fill generation script
372 os.remove(magpath + '/generate_fill.tcl')
373 # Remove all individual fill tiles, leaving only the composite GDS.
374 filelist = os.listdir(magpath)
375 for file in filelist:
376 if os.path.splitext(magpath + '/' + file)[1] == '.gds':
Tim Edwardsa0cdd342020-12-29 16:26:58 -0500377 if file.startswith(project + '_fill_pattern_'):
378 os.remove(magpath + '/' + file)
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500379
Tim Edwards04d1f832021-02-17 09:54:38 -0500380 if distmode:
381 os.remove(magpath + '/generate_fill_dist.tcl')
382 os.remove(magpath + '/generate_fill_final.tcl')
383 os.remove(magpath + '/fill_gen_info.txt')
384 if testmode:
385 magfiles = glob.glob(magpath + '/' + project + '_fill_pattern_*.mag')
386 for file in magfiles:
387 os.remove(file)
388
Tim Edwardsb71e5f82020-12-29 16:15:26 -0500389 print('Done!')
390 exit(0)