blob: aaa349019b26c980be78b6f9b5a0f9cc31248dc2 [file] [log] [blame]
#!/usr/bin/env python3
# rename_models ---
# This script renames include paths from SPICE files in the models
# ( directory that point to the original ../cells/
# directory, to point to the correct location in libs.ref/sky130_fd_pr/spice/
# This script is a filter to be run by setting the name of this script as
# the value to "filter=" for the model install in the sky130 Makefile.
import re
import os
import sys
def filter(inname, outname, ef_format = True):
if ef_format:
libpath = 'spi/sky130_fd_pr/'
libpath = 'sky130_fd_pr/spice/'
# Read input
with open(inname, 'r') as inFile:
spitext =
spilines = spitext.splitlines()
print(' failed to open ' + inname + ' for reading.', file=sys.stderr)
return 1
# Check specifically for the file "all.spice"
allspice = False
if os.path.split(inname)[1] == 'all.spice':
allspice = True
optionrex = re.compile('option scale=', re.IGNORECASE)
# Process input with regexp
fixedlines = []
modified = False
for line in spilines:
# Fix: Replace "../cells/<name>/" with "../../libs.ref/sky130_fd_pr/spice/"
fixedline = re.sub('\.\./cells/[^/]+/', '../../libs.ref/' + libpath, line)
# This subsitution makes SPICE files compatible with Xyce without
# breaking ngspice compatibility ('$' comments changed to ';')
fixedline = re.sub('(.*[ \t]+)\$([ \t+].*)', '\g<1>;\g<2>', fixedline)
# Add an additional option line for Xyce compatibility. Note that having
# both option lines means that both ngspice and Xyce will complain about
# one of them, but both will run without issues otherwise.
if allspice:
omatch = optionrex.match(fixedline)
if omatch:
fixedlines.append('.options parser scale=1.0u ; for Xyce')
if fixedline != line:
modified = True
# Write output
if outname == None:
for i in fixedlines:
# If the output is a symbolic link but no modifications have been made,
# then leave it alone. If it was modified, then remove the symbolic
# link before writing.
if os.path.islink(outname):
if not modified:
return 0
with open(outname, 'w') as outFile:
for i in fixedlines:
print(i, file=outFile)
print(' failed to open ' + outname + ' for writing.', file=sys.stderr)
return 1
if __name__ == '__main__':
# This script expects to get one or two arguments. One argument is
# mandatory and is the input file. The other argument is optional and
# is the output file. The output file and input file may be the same
# name, in which case the original input is overwritten.
options = []
arguments = []
for item in sys.argv[1:]:
if item.find('-', 0) == 0:
if len(arguments) > 0:
infilename = arguments[0]
print('Usage: <filename> [<outfilename>] [-ef_format]')
if len(arguments) > 1:
outfilename = arguments[1]
outfilename = None
ef_format = False
if len(options) > 0:
if options[0] == 'ef_format':
ef_format = True
result = filter(infilename, outfilename, ef_format)