Tim Edwards | 9d3debb | 2020-10-20 20:52:18 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tim Edwards | 55f4d0e | 2020-07-05 15:41:02 -0400 | [diff] [blame] | 2 | # |
| 3 | # fixspice --- |
| 4 | # |
| 5 | # This script fixes problems in the SkyWater SPICE models. This should be |
| 6 | # made obsolete by the forthcoming set of models from the foundry, but the |
| 7 | # script will get the original set working with ngspice. |
| 8 | # |
| 9 | # This script is a filter to be run by setting the name of this script as |
Tim Edwards | ab5bae8 | 2020-07-05 17:40:59 -0400 | [diff] [blame] | 10 | # the value to "filter=" for the model install in the sky130 Makefile. |
Tim Edwards | 55f4d0e | 2020-07-05 15:41:02 -0400 | [diff] [blame] | 11 | |
| 12 | import re |
| 13 | import os |
| 14 | import sys |
| 15 | |
| 16 | def filter(inname, outname): |
| 17 | |
| 18 | # Read input |
| 19 | try: |
| 20 | with open(inname, 'r') as inFile: |
| 21 | spitext = inFile.read() |
| 22 | # (Don't) unwrap continuation lines |
| 23 | # spilines = spitext.replace('\n+', ' ').splitlines() |
| 24 | spilines = spitext.splitlines() |
| 25 | except: |
| 26 | print('fixspice.py: failed to open ' + fnmIn + ' for reading.', file=sys.stderr) |
| 27 | return 1 |
| 28 | |
| 29 | # Process input with regexp |
| 30 | |
| 31 | fixedlines = [] |
| 32 | modified = False |
| 33 | |
| 34 | for line in spilines: |
| 35 | |
| 36 | # Fix 1: ngspice does not understand the syntax used for the dev/gauss lines, |
| 37 | # so remove them. |
| 38 | fixedline = re.sub('dev/gauss[ \t]*=.*$', '', line) |
| 39 | |
| 40 | # Fix 2: Remove references to *_dlc_rotweak |
| 41 | # fixedline = re.sub('\+[ \t]*[^ \t_]+_dlc_rotweak', '', fixedline) |
| 42 | |
| 43 | # Fix 2: Remove references to *_[a,p]junction_mult |
| 44 | # fixedline = re.sub('\*[ \t]*[^ \t_]+_[ap]junction_mult', '', fixedline) |
| 45 | |
| 46 | fixedlines.append(fixedline) |
| 47 | if fixedline != line: |
| 48 | modified = True |
| 49 | |
| 50 | # Write output |
| 51 | if outname == None: |
| 52 | for i in fixedlines: |
| 53 | print(i) |
| 54 | else: |
| 55 | # If the output is a symbolic link but no modifications have been made, |
| 56 | # then leave it alone. If it was modified, then remove the symbolic |
| 57 | # link before writing. |
| 58 | if os.path.islink(outname): |
| 59 | if not modified: |
| 60 | return 0 |
| 61 | else: |
| 62 | os.unlink(outname) |
| 63 | try: |
| 64 | with open(outname, 'w') as outFile: |
| 65 | for i in fixedlines: |
| 66 | print(i, file=outFile) |
| 67 | except: |
| 68 | print('fixspice.py: failed to open ' + outname + ' for writing.', file=sys.stderr) |
| 69 | return 1 |
| 70 | |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | |
| 74 | # This script expects to get one or two arguments. One argument is |
| 75 | # mandatory and is the input file. The other argument is optional and |
| 76 | # is the output file. The output file and input file may be the same |
| 77 | # name, in which case the original input is overwritten. |
| 78 | |
| 79 | options = [] |
| 80 | arguments = [] |
| 81 | for item in sys.argv[1:]: |
| 82 | if item.find('-', 0) == 0: |
| 83 | options.append(item[1:]) |
| 84 | else: |
| 85 | arguments.append(item) |
| 86 | |
| 87 | if len(arguments) > 0: |
| 88 | infilename = arguments[0] |
| 89 | |
| 90 | if len(arguments) > 1: |
| 91 | outfilename = arguments[1] |
| 92 | else: |
| 93 | outfilename = None |
| 94 | |
| 95 | result = filter(infilename, outfilename) |
| 96 | sys.exit(result) |