Guest User

Untitled

a guest
Jun 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.51 KB | None | 0 0
  1.  
  2. #Constants
  3. N = 32 #number of mask gates we are going to insert
  4. TB = "ben_tb.v" #test bench file
  5. CKT = "ben_scan.v" #circuit file
  6. CPR = "critpaths.report"
  7. CLKTOD = /^(?<dstart>DFF\_\d+\_Q\_reg)\/CLK\s\(SDFFX1\).*(?<dend>DFF\_\d+\_Q\_reg)\/D\s\(SDFFX1\)/ #for finding paths in critpaths.report
  8. KEY = ["0"]*N
  9. TRN = ["0"]*N
  10.  
  11.  
  12. #read in the first file
  13. ckt = File.read(CKT).split(/\n/) #read in circuit
  14.  
  15. #flatten the verilog so that it is easier to work with
  16. a = "\n" #initialize, don't know why inject wasn't working
  17. ckt.each do |b|
  18.     if (a[-1] != "\n") #if last line wasn't complete
  19.         a.concat(b.sub(/^\s*/,"")) #add this line with trailing whitespace removed
  20.     else #if last line is already complete
  21.         a.concat(b) #add this line just as it was before
  22.     end
  23.     (b =~ /\;$/) ? a.concat("\n") : nil #if command terminated, add newline
  24. end
  25.  
  26. #we now have a as a flatter version of the input file.  
  27. a = a.split(/\n/)
  28. #we need to add key and trn signature/inputs to the verilog file.
  29. a = a.map{|l| (l =~ /^\s*module/) ? (l.sub(" );",(0...N).inject(""){|a,b| a.concat(", key#{b}, trn#{b}")}.concat(");"))) : l}
  30. a = a.map{|l| (l =~ /^\s*input/) ? (l.sub(";",(0...N).inject(""){|a,b| a.concat(", key#{b}, trn#{b}")}.concat(";"))) : l}
  31.  
  32. #need to find the paths that we are going to insert the xor gates onto
  33. paths = File.read('critpaths.report').split(/\n/).reverse.map{|e| (e.to_s =~ CLKTOD) ? $2 : nil}.select{|e| e != nil}.take(N) #takes lowest 32 paths matching regex
  34. gates = paths.map{|e| a.select{|f| f =~ Regexp.new(Regexp.escape(e))}} #finds the gates terminating those paths
  35. nets = gates.map{|e| (e.to_s =~ /\.D\((?<gate>.*?)\)/) ? $1 : nil} #finds the nets feeding into the gates terminating those paths
  36.  
  37. #create the masks
  38. (0...N).each do |e|
  39.     s = "  XOR3X1 MSK#{e.to_s} ( .IN1(key#{e.to_s}), .IN2(trn#{e.to_s}), .IN3(#{nets[e]}), .Q(xgt#{e.to_s}));"
  40.     a.insert(-2,s)
  41.     a.map!{|f| (f =~ Regexp.new(Regexp.escape(paths[e]))) ? (f=f.sub(nets[e],"xgt#{e.to_s}")) : f}
  42. end
  43.  
  44. #write the file
  45. a = a.inject(""){|a,l| a.concat(l.concat("\n"))} #compresses a back into a single string
  46. File.open("scan_edit.v",'w+') {|f| f.write(a)}
  47.  
  48. #read in testbench, edit file.  a little hackier than above but should usually still work.
  49. tbn = File.read(TB).split(/\n/)
  50. ins = (0...N).reduce(""){|f,g| f.concat(",\n      .key#{g}(1'b#{KEY[g]}),\n      .trn#{g}(1'b#{TRN[g]})")}
  51. tbn.map!{|e| (e =~ /\.test\_se\(test_se\)/) ? (e.insert(-6,ins)): e}
  52. tbn = tbn.reduce(""){|a,l| a.concat(l.concat("\n"))}
  53. File.open("tb_edit.v",'w+') {|f| f.write(tbn)}
Add Comment
Please, Sign In to add comment