Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 1 | #!/bin/env python3 |
| 2 | # 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 | |
| 23 | import sys |
| 24 | import os |
| 25 | import re |
| 26 | import subprocess |
| 27 | |
| 28 | def usage(): |
| 29 | print("Usage:") |
| 30 | print("generate_fill.py <layout_name> [-keep] [-test]") |
| 31 | print("") |
| 32 | print("where:") |
| 33 | print(" <layout_name> is the path to the .mag file to be filled.") |
| 34 | print("") |
| 35 | print(" If '-keep' is specified, then keep the generation script.") |
| 36 | print(" If '-test' is specified, then create but do not run the generation script.") |
| 37 | return 0 |
| 38 | |
| 39 | if __name__ == '__main__': |
| 40 | |
| 41 | optionlist = [] |
| 42 | arguments = [] |
| 43 | |
| 44 | debugmode = False |
| 45 | keepmode = False |
| 46 | testmode = False |
| 47 | |
| 48 | for option in sys.argv[1:]: |
| 49 | if option.find('-', 0) == 0: |
| 50 | optionlist.append(option) |
| 51 | else: |
| 52 | arguments.append(option) |
| 53 | |
| 54 | if len(arguments) != 1: |
| 55 | print("Wrong number of arguments given to generate_fill.py.") |
| 56 | usage() |
| 57 | sys.exit(1) |
| 58 | |
| 59 | user_project_path = arguments[0] |
| 60 | |
| 61 | magpath = os.path.split(user_project_path)[0] |
| 62 | if magpath == '': |
| 63 | magpath = os.getcwd() |
| 64 | |
| 65 | if os.path.splitext(user_project_path)[1] != '.mag': |
| 66 | if os.path.splitext(user_project_path)[1] == '': |
| 67 | user_project_path += '.mag' |
| 68 | else: |
| 69 | print('Error: Project is not a magic database .mag file!') |
| 70 | sys.exit(1) |
| 71 | |
| 72 | if not os.path.isfile(user_project_path): |
| 73 | print('Error: Project "' + user_project_path + '" does not exist or is not readable.') |
| 74 | sys.exit(1) |
| 75 | |
| 76 | if '-debug' in optionlist: |
| 77 | debugmode = True |
| 78 | if '-keep' in optionlist: |
| 79 | keepmode = True |
| 80 | if '-test' in optionlist: |
| 81 | testmode = True |
| 82 | |
| 83 | rcfile = magpath + '/.magicrc' |
| 84 | if not os.path.isfile(rcfile): |
| 85 | rcfile = None |
| 86 | |
| 87 | project = os.path.splitext(os.path.split(user_project_path)[1])[0] |
| 88 | |
| 89 | topdir = os.path.split(magpath)[0] |
| 90 | gdsdir = topdir + '/gds' |
| 91 | hasgdsdir = True if os.path.isdir(gdsdir) else False |
| 92 | |
| 93 | with open(magpath + '/generate_fill.tcl', 'w') as ofile: |
| 94 | print('#!/bin/env wish', file=ofile) |
| 95 | print('drc off', file=ofile) |
| 96 | print('tech unlock *', file=ofile) |
| 97 | print('snap internal', file=ofile) |
| 98 | print('box values 0 0 0 0', file=ofile) |
| 99 | print('box size 700um 700um', file=ofile) |
| 100 | print('set stepbox [box values]', file=ofile) |
| 101 | print('set stepwidth [lindex $stepbox 2]', file=ofile) |
| 102 | print('set stepheight [lindex $stepbox 3]', file=ofile) |
| 103 | print('', file=ofile) |
| 104 | print('set starttime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile) |
| 105 | print('puts stdout "Started: $starttime"', file=ofile) |
| 106 | print('', file=ofile) |
| 107 | print('load ' + project + ' -dereference', file=ofile) |
| 108 | print('select top cell', file=ofile) |
| 109 | print('expand', file=ofile) |
| 110 | print('cif ostyle wafflefill(tiled)', file=ofile) |
| 111 | print('', file=ofile) |
| 112 | print('set fullbox [box values]', file=ofile) |
| 113 | print('set xmax [lindex $fullbox 2]', file=ofile) |
| 114 | print('set xmin [lindex $fullbox 0]', file=ofile) |
| 115 | print('set fullwidth [expr {$xmax - $xmin}]', file=ofile) |
Tim Edwards | 113b083 | 2021-01-13 11:27:52 -0500 | [diff] [blame] | 116 | print('set xtiles [expr {int(ceil(($fullwidth + 0.0) / $stepwidth))}]', file=ofile) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 117 | print('set ymax [lindex $fullbox 3]', file=ofile) |
| 118 | print('set ymin [lindex $fullbox 1]', file=ofile) |
| 119 | print('set fullheight [expr {$ymax - $ymin}]', file=ofile) |
Tim Edwards | 113b083 | 2021-01-13 11:27:52 -0500 | [diff] [blame] | 120 | print('set ytiles [expr {int(ceil(($fullheight + 0.0) / $stepheight))}]', file=ofile) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 121 | print('box size $stepwidth $stepheight', file=ofile) |
| 122 | print('set xbase [lindex $fullbox 0]', file=ofile) |
| 123 | print('set ybase [lindex $fullbox 1]', file=ofile) |
| 124 | print('', file=ofile) |
| 125 | |
| 126 | # Break layout into tiles and process each separately |
| 127 | print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile) |
| 128 | print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile) |
| 129 | print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile) |
| 130 | print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile) |
| 131 | print(' set xhi [expr $xlo + $stepwidth]', file=ofile) |
| 132 | print(' set yhi [expr $ylo + $stepheight]', file=ofile) |
Tim Edwards | 113b083 | 2021-01-13 11:27:52 -0500 | [diff] [blame] | 133 | print(' if {$xhi > $fullwidth} {set xhi $fullwidth}', file=ofile) |
| 134 | print(' if {$yhi > $fullheight} {set yhi $fullheight}', file=ofile) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 135 | print(' box values $xlo $ylo $xhi $yhi', file=ofile) |
| 136 | # The flattened area must be larger than the fill tile by >1.5um |
| 137 | print(' box grow c 1.6um', file=ofile) |
| 138 | |
| 139 | # Flatten into a cell with a new name |
| 140 | print(' puts stdout "Flattening layout of tile x=$x y=$y. . . "', file=ofile) |
| 141 | print(' flush stdout', file=ofile) |
| 142 | print(' update idletasks', file=ofile) |
| 143 | print(' flatten -dobox -nolabels ' + project + '_fill_pattern_${x}_$y', file=ofile) |
| 144 | print(' load ' + project + '_fill_pattern_${x}_$y', file=ofile) |
| 145 | |
Tim Edwards | ca85a19 | 2020-12-30 10:58:52 -0500 | [diff] [blame] | 146 | # Remove any GDS_FILE reference (there should not be any?) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 147 | print(' property GDS_FILE ""', file=ofile) |
| 148 | # Set boundary using comment layer, to the size of the step box |
Tim Edwards | ca85a19 | 2020-12-30 10:58:52 -0500 | [diff] [blame] | 149 | # This corresponds to the "topbox" rule in the wafflefill(tiled) style |
Tim Edwards | 113b083 | 2021-01-13 11:27:52 -0500 | [diff] [blame] | 150 | print(' select top cell', file=ofile) |
| 151 | print(' erase comment', file=ofile) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 152 | print(' box values $xlo $ylo $xhi $yhi', file=ofile) |
| 153 | print(' paint comment', file=ofile) |
| 154 | print(' puts stdout "Writing GDS. . . "', file=ofile) |
| 155 | print(' flush stdout', file=ofile) |
| 156 | print(' update idletasks', file=ofile) |
| 157 | print(' gds write ' + project + '_fill_pattern_${x}_$y.gds', file=ofile) |
| 158 | |
| 159 | # Reload project top |
| 160 | print(' load ' + project, file=ofile) |
| 161 | |
| 162 | # Remove last generated cell to save memory |
| 163 | print(' cellname delete ' + project + '_fill_pattern_${x}_$y', file=ofile) |
| 164 | |
| 165 | print(' }', file=ofile) |
| 166 | print('}', file=ofile) |
| 167 | |
| 168 | # Now create simple "fake" views of all the tiles. |
Tim Edwards | ca85a19 | 2020-12-30 10:58:52 -0500 | [diff] [blame] | 169 | print('gds readonly true', file=ofile) |
| 170 | print('gds rescale false', file=ofile) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 171 | print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile) |
| 172 | print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile) |
| 173 | print(' set xlo [expr $xbase + $x * $stepwidth]', file=ofile) |
| 174 | print(' set ylo [expr $ybase + $y * $stepheight]', file=ofile) |
| 175 | print(' set xhi [expr $xlo + $stepwidth]', file=ofile) |
| 176 | print(' set yhi [expr $ylo + $stepheight]', file=ofile) |
Tim Edwards | ca85a19 | 2020-12-30 10:58:52 -0500 | [diff] [blame] | 177 | print(' load ' + project + '_fill_pattern_${x}_$y -quiet', file=ofile) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 178 | print(' box values $xlo $ylo $xhi $yhi', file=ofile) |
| 179 | print(' paint comment', file=ofile) |
| 180 | print(' property FIXED_BBOX "$xlo $ylo $xhi $yhi"', file=ofile) |
| 181 | print(' property GDS_FILE ' + project + '_fill_pattern_${x}_${y}.gds', file=ofile) |
| 182 | print(' property GDS_START 0', file=ofile) |
| 183 | print(' }', file=ofile) |
| 184 | print('}', file=ofile) |
| 185 | |
| 186 | # Now tile everything back together |
Tim Edwards | ca85a19 | 2020-12-30 10:58:52 -0500 | [diff] [blame] | 187 | print('load ' + project + '_fill_pattern -quiet', file=ofile) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 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(' box values 0 0 0 0', file=ofile) |
| 191 | print(' getcell ' + project + '_fill_pattern_${x}_$y child 0 0', file=ofile) |
| 192 | print(' }', file=ofile) |
| 193 | print('}', file=ofile) |
| 194 | |
| 195 | # And write final GDS |
| 196 | print('puts stdout "Writing final GDS"', file=ofile) |
| 197 | |
| 198 | print('cif *hier write disable', file=ofile) |
| 199 | print('cif *array write disable', file=ofile) |
| 200 | if hasgdsdir: |
| 201 | print('gds write ../gds/' + project + '_fill_pattern.gds', file=ofile) |
| 202 | else: |
| 203 | print('gds write ' + project + '_fill_pattern.gds', file=ofile) |
| 204 | print('set endtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile) |
| 205 | print('puts stdout "Ended: $endtime"', file=ofile) |
| 206 | print('quit -noprompt', file=ofile) |
| 207 | myenv = os.environ.copy() |
| 208 | myenv['MAGTYPE'] = 'mag' |
| 209 | |
| 210 | if not testmode: |
| 211 | # Diagnostic |
| 212 | # print('This script will generate file ' + project + '_fill_pattern.gds') |
| 213 | print('This script will generate files ' + project + '_fill_pattern_x_y.gds') |
| 214 | print('Now generating fill patterns. This may take. . . quite. . . a while.', flush=True) |
| 215 | mproc = subprocess.run(['magic', '-dnull', '-noconsole', |
| 216 | '-rcfile', rcfile, magpath + '/generate_fill.tcl'], |
| 217 | stdin = subprocess.DEVNULL, |
| 218 | stdout = subprocess.PIPE, |
| 219 | stderr = subprocess.PIPE, |
| 220 | cwd = magpath, |
| 221 | env = myenv, |
| 222 | universal_newlines = True) |
| 223 | if mproc.stdout: |
| 224 | for line in mproc.stdout.splitlines(): |
| 225 | print(line) |
| 226 | if mproc.stderr: |
| 227 | print('Error message output from magic:') |
| 228 | for line in mproc.stderr.splitlines(): |
| 229 | print(line) |
| 230 | if mproc.returncode != 0: |
| 231 | print('ERROR: Magic exited with status ' + str(mproc.returncode)) |
| 232 | |
| 233 | if not keepmode: |
| 234 | # Remove fill generation script |
| 235 | os.remove(magpath + '/generate_fill.tcl') |
| 236 | # Remove all individual fill tiles, leaving only the composite GDS. |
| 237 | filelist = os.listdir(magpath) |
| 238 | for file in filelist: |
| 239 | if os.path.splitext(magpath + '/' + file)[1] == '.gds': |
Tim Edwards | a0cdd34 | 2020-12-29 16:26:58 -0500 | [diff] [blame] | 240 | if file.startswith(project + '_fill_pattern_'): |
| 241 | os.remove(magpath + '/' + file) |
Tim Edwards | b71e5f8 | 2020-12-29 16:15:26 -0500 | [diff] [blame] | 242 | |
| 243 | print('Done!') |
| 244 | exit(0) |