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