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 | |
| 15 | searchpath = ['sky130A/libs.tech/ngspice/parameters/critical.spice', |
| 16 | 'sky130A/libs.tech/ngspice/parameters/montecarlo.spice'] |
| 17 | |
| 18 | if len(sys.argv) > 1: |
| 19 | searchpath = sys.argv[1] |
| 20 | |
| 21 | # Flag all parameters that have the same name as devices. |
| 22 | # These parameters are unused and must be deleted. |
| 23 | |
| 24 | excludelist = [ |
| 25 | 'sky130_fd_pr__nfet_20v0_nvt_iso', |
| 26 | 'sky130_fd_pr__nfet_20v0_nvt', |
| 27 | 'sky130_fd_pr__nfet_20v0_iso', |
| 28 | 'sky130_fd_pr__nfet_20v0_zvt', |
| 29 | 'sky130_fd_pr__nfet_20v0', |
| 30 | 'sky130_fd_pr__pfet_20v0', |
| 31 | 'sky130_fd_pr__nfet_01v8_lvt', |
| 32 | 'sky130_fd_pr__pfet_01v8_mvt', |
| 33 | 'sky130_fd_pr__pfet_01v8', |
| 34 | 'sky130_fd_pr__nfet_g5v0d16v0', |
| 35 | 'sky130_fd_pr__pfet_g5v0d16v0', |
| 36 | 'sky130_fd_pr__special_nfet_pass_lvt', |
| 37 | 'sky130_fd_pr__pnp_05v5_W0p68L0p68'] |
| 38 | |
| 39 | # For each entry in the exclude list, create a regular expression |
| 40 | # for detecting that entry and excluding other similar cases that |
| 41 | # aren't the parameter. Items with preceding "/" are the device |
| 42 | # filename, and items with additional text are other parameters. |
| 43 | |
| 44 | rexpat = {} |
| 45 | rexdict = {} |
| 46 | |
| 47 | for item in excludelist: |
| 48 | rexpat[item] = '([^/])' + item + '(?=[^_])' |
| 49 | rexdict[item] = re.compile('([^/])' + item + '(?=[^_])') |
| 50 | |
| 51 | #-------------------------------------------------------------------- |
| 52 | |
| 53 | for infile_name in searchpath: |
| 54 | filepath = os.path.split(infile_name)[0] |
| 55 | outfile_name = os.path.join(filepath, 'temp') |
| 56 | |
| 57 | infile = open(infile_name, 'r') |
| 58 | outfile = open(outfile_name, 'w') |
| 59 | |
| 60 | line_number = 0 |
| 61 | replaced_something = False |
| 62 | for line in infile: |
| 63 | line_number += 1 |
| 64 | |
| 65 | newline = line |
| 66 | for device in excludelist: |
| 67 | rmatch = rexdict[device].search(line) |
| 68 | if rmatch: |
| 69 | replacement = device[14:] |
| 70 | newline = re.sub(rexpat[device], '\g<1>' + replacement, newline) |
| 71 | replaced_something = True |
| 72 | |
| 73 | outfile.write(newline) |
| 74 | |
| 75 | infile.close() |
| 76 | outfile.close() |
| 77 | if replaced_something: |
| 78 | print("Something was replaced in '{}'".format(infile_name)) |
| 79 | os.rename(outfile_name, infile_name) |
| 80 | else: |
| 81 | print("Nothing was replaced in '{}'.".format(infile_name)) |
| 82 | os.remove(outfile_name) |
| 83 | |