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