Advertisement
Guest User

Untitled

a guest
Nov 14th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. os.system("fgrep output\ reg *.v  | grep = > init.txt")
  4. f = open("init.txt","r")
  5.  
  6. lines = {}
  7.  
  8. def getSize(line):
  9.     if line[0] == "[":
  10.         sizeStr = line[1:line.find("]")]
  11.         sarray = sizeStr.split(":")
  12.         return int(sarray[0])- int(sarray[1]) + 1
  13.     else:
  14.         return 1
  15.        
  16. def insertContents(fileName, contents):
  17.     f2 = open(fileName, "r")
  18.     original = f2.readlines()
  19.     f2.close()
  20.  
  21.     newlines = []
  22.     for line in original:
  23.         if line.startswith("output reg"):
  24.             eqPos = line.find(" =")
  25.             cmPos = line.find(",",eqPos)
  26.             line = line[:eqPos] + line[cmPos:]
  27.         newlines.append(line)
  28.  
  29.     original = newlines
  30.    
  31.     for line in original:
  32.         if line.startswith("initial"):
  33.             return
  34.  
  35.     index = 0
  36.     for line in original:
  37.         if line.startswith("endmodule"):
  38.             original.insert(index, contents)
  39.             f2 = open(fileName, "w")
  40.             contents = "".join(original)
  41.             f2.write(contents)
  42.             f2.close()
  43.             return
  44.         index=index+1
  45.  
  46. for line in f:
  47.    
  48.     splitPos = line.find(":")
  49.     fileName = line[:splitPos]
  50.     lineData = line[splitPos+1:]
  51.    
  52.     if not fileName in lines:
  53.         lines[fileName] = []
  54.  
  55.     lines[fileName].append(lineData.strip())
  56.  
  57.  
  58. for fileName in lines.keys():
  59.  
  60.     print fileName
  61.     contents  = "initial begin\n\n"
  62.     for line in lines[fileName]:
  63.         l = line.replace("output reg  ","")
  64.         l = l.replace(",",";")
  65.         size = getSize(l)
  66.         l = l.replace(" 'd", " %i'd" % (size,))
  67.        
  68.         if size > 1:
  69.             l = l[l.find("]")+1:]
  70.         contents  = contents+"\t"+l.strip()+"\n"
  71.     contents  = contents+"\n\nend\n"
  72.     insertContents(fileName, contents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement