import sys
import os
“”"OOCDFixupSVF.py a simple python script to fixup (in my case Actel) SVF files to
work with OpenOCD v0.2-0.4.0"“”
“”“by Jason Morgan - jason dot morgan at vpnsolutions dot uk dot com”“”
“”"OpenOCD now contains an SVF player, but is’s implementation is not very solid
e.g. it interprets LF as a line end as well as a semicolon, meaning
lines can’t be split.
It also requires a space between TDI, TDO, MASK and SMASK, and its paramater in
brackets even though that is not required by the specification.
e.g. TDI(123) and TDI(123) should both work, but they do not.
This program is a quick hack to reformat SVF scripts into the correct form.
“”"
infile=sys.argv[1]
outfile=sys.argv[2]
if len(sys.argv)!=3:
print “Usage OOCDfixupxvf ”
sys.exit(1)
if os.path.exists(infile):
fi=open(infile,“r”)
else:
print “Input file %s not found”%infile
sys.exit(1)
if infile==outfile:
print “Can’t use the same file name for input and output”
sys.exit(1)
fo=open(outfile,“w”)
kwds=[“TDI”,“TDO”,“ASK”]
linecount=0
l=fi.readline()
linecount+=1
while(l!=“”):
li=l.strip()
##If the line is a comment or is empty write it out
if len(li)==0 or li[0]==“!”:
fo.writelines(li+“\n”)
else:
#if the end of the line is not semicolon append another line
while(li[-1]!=“;”):
print “Joining line \n%s\n”%repr(li)
#Join lines, if the end of a line is a bracket, then add a space
#otherwise don’t add a space
if li[-1]==“)”:
pad=" "
else:
pad=“”
li=li+pad+fi.readline().strip()
linecount+=1
#Now we have a complete, terminated line, replace the keywords
for kw in kwds:
li=li.upper().replace(“%s(”%kw,“%s (”%kw)
#replace tabs with spaces
li=li.replace(“\t”," ")
#replace multiple spaces with single spaces
len1=1
len2=0
while(len1!=len2):
len1=len(li)
li=li.replace(" “,” ")
len2=len(li)
if len2!=len1:print “Removed %d extra spaces”%(len2-len1)
#then write out the line, insert \n after each semicolon for readability
lo=li.replace(“;”,“;\n”)
fo.write(lo)
l=fi.readline()
linecount+=1
if(linecount%100==0):print “.”,
print “\ndone”
print “in : %s”%infile
print “out: %s”%outfile
print “Processed %d lines”%linecount