Tim Edwards | 33ce97a | 2021-05-24 20:51:28 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | #-------------------------------------------------------------------- |
| 4 | # Workaround for the problem that ngspice is unable to accept a |
| 5 | # parameter name that shadows a device name. Parameters in the |
| 6 | # files critical.spice and montecarlo.spice that shadow parameter |
| 7 | # names have the leading sky130_fd_pr__ stripped off to prevent |
| 8 | # this from being a problem. |
| 9 | #-------------------------------------------------------------------- |
| 10 | |
| 11 | import os |
| 12 | import re |
| 13 | import sys |
| 14 | |
Tim Edwards | 7325e44 | 2022-03-03 16:51:52 -0500 | [diff] [blame] | 15 | options = [] |
| 16 | arguments = [] |
| 17 | for item in sys.argv[1:]: |
| 18 | if item.find('-', 0) == 0: |
| 19 | options.append(item[1:]) |
| 20 | else: |
| 21 | arguments.append(item) |
Tim Edwards | 33ce97a | 2021-05-24 20:51:28 -0400 | [diff] [blame] | 22 | |
Tim Edwards | 7325e44 | 2022-03-03 16:51:52 -0500 | [diff] [blame] | 23 | variant = 'sky130A' |
| 24 | |
| 25 | if len(options) > 0: |
| 26 | for option in options: |
| 27 | if option.startswith('variant'): |
| 28 | variant = option.split('=')[1] |
| 29 | searchpath = [variant + '/libs.tech/ngspice/parameters/critical.spice', |
| 30 | variant + '/libs.tech/ngspice/parameters/montecarlo.spice'] |
| 31 | |
| 32 | elif len(arguments) > 0: |
Tim Edwards | 33ce97a | 2021-05-24 20:51:28 -0400 | [diff] [blame] | 33 | searchpath = sys.argv[1] |
| 34 | |
Tim Edwards | 7325e44 | 2022-03-03 16:51:52 -0500 | [diff] [blame] | 35 | |
Tim Edwards | 33ce97a | 2021-05-24 20:51:28 -0400 | [diff] [blame] | 36 | # Flag all parameters that have the same name as devices. |
| 37 | # These parameters are unused and must be deleted. |
| 38 | |
| 39 | excludelist = [ |
| 40 | 'sky130_fd_pr__nfet_20v0_nvt_iso', |
| 41 | 'sky130_fd_pr__nfet_20v0_nvt', |
| 42 | 'sky130_fd_pr__nfet_20v0_iso', |
| 43 | 'sky130_fd_pr__nfet_20v0_zvt', |
| 44 | 'sky130_fd_pr__nfet_20v0', |
| 45 | 'sky130_fd_pr__pfet_20v0', |
| 46 | 'sky130_fd_pr__nfet_01v8_lvt', |
| 47 | 'sky130_fd_pr__pfet_01v8_mvt', |
| 48 | 'sky130_fd_pr__pfet_01v8', |
| 49 | 'sky130_fd_pr__nfet_g5v0d16v0', |
| 50 | 'sky130_fd_pr__pfet_g5v0d16v0', |
| 51 | 'sky130_fd_pr__special_nfet_pass_lvt', |
| 52 | 'sky130_fd_pr__pnp_05v5_W0p68L0p68'] |
| 53 | |
| 54 | # For each entry in the exclude list, create a regular expression |
| 55 | # for detecting that entry and excluding other similar cases that |
| 56 | # aren't the parameter. Items with preceding "/" are the device |
| 57 | # filename, and items with additional text are other parameters. |
| 58 | |
| 59 | rexpat = {} |
| 60 | rexdict = {} |
| 61 | |
| 62 | for item in excludelist: |
| 63 | rexpat[item] = '([^/])' + item + '(?=[^_])' |
| 64 | rexdict[item] = re.compile('([^/])' + item + '(?=[^_])') |
| 65 | |
| 66 | #-------------------------------------------------------------------- |
| 67 | |
| 68 | for infile_name in searchpath: |
| 69 | filepath = os.path.split(infile_name)[0] |
Tim Edwards | 477d9b7 | 2022-02-06 17:01:15 -0500 | [diff] [blame] | 70 | filename = os.path.split(infile_name)[1] |
| 71 | fileroot = os.path.splitext(filename)[0] |
Tim Edwards | 7325e44 | 2022-03-03 16:51:52 -0500 | [diff] [blame] | 72 | outfile_name = os.path.join(filepath, fileroot + '_temp') |
Tim Edwards | 33ce97a | 2021-05-24 20:51:28 -0400 | [diff] [blame] | 73 | |
| 74 | infile = open(infile_name, 'r') |
Tim Edwards | 7325e44 | 2022-03-03 16:51:52 -0500 | [diff] [blame] | 75 | outfile = open(outfile_name, 'w') |
Tim Edwards | 33ce97a | 2021-05-24 20:51:28 -0400 | [diff] [blame] | 76 | |
| 77 | line_number = 0 |
| 78 | replaced_something = False |
| 79 | for line in infile: |
| 80 | line_number += 1 |
| 81 | |
| 82 | newline = line |
| 83 | for device in excludelist: |
| 84 | rmatch = rexdict[device].search(line) |
| 85 | if rmatch: |
| 86 | replacement = device[14:] |
| 87 | newline = re.sub(rexpat[device], '\g<1>' + replacement, newline) |
| 88 | replaced_something = True |
| 89 | |
| 90 | outfile.write(newline) |
| 91 | |
| 92 | infile.close() |
| 93 | outfile.close() |
| 94 | if replaced_something: |
| 95 | print("Something was replaced in '{}'".format(infile_name)) |
| 96 | os.rename(outfile_name, infile_name) |
| 97 | else: |
| 98 | print("Nothing was replaced in '{}'.".format(infile_name)) |
| 99 | os.remove(outfile_name) |
| 100 | |