Saturday 27 June 2015

Insertion of Pragmas in Source Code using Cshell Script

Their was a requirement to develop a script to add pragma to the code in order to encrypt the source code.We need to add `protect begin and `protect end between module and end module of .v files.



Original .v file:

 module chip( MINUS,PLUS );

  output wreal PLUS;
  input wreal MINUS;

  parameter elite  = 0;
   parameter coffee = 1;
   parameter club   = 1;

  always@(*) 
   begin 
    $display("elite  = %d" , elite );
    $display("coffee = %d" , coffee);
    $display("club   = %d" , club  );
   end

endmodule 
// comments 
// parameter

Output .v file:

 module chip( MINUS,PLUS );

  output wreal PLUS;
  input wreal MINUS;

  parameter elite  = 0;
   parameter coffee = 1;
   parameter club   = 1; 

  `protect begin

  always@(*) 
   begin 

    $display("elite  = %d" , elite );
    $display("coffee = %d" , coffee);
    $display("club   = %d" , club  );
   end

 `protect end

endmodule
// comments 
// parameter



The script was written in cshell as stated below

#! /bin/csh -fx

setenv RUN_DIR $PWD

set filename = $RUN_DIR/test_list.txt  # test_list.txt gives list of all .v files
set temp = temp                        # temp variable
set file = file                        # file variable

foreach filelocal (`cat $filename`)    # foreach is reading each file in test_list

  echo $filelocal

  # Removing Tempfiles
  rm -rf $file$temp                    # Removing temporary in between generated files
  rm -rf $file

  # Removing comments in the file
  cat $filelocal | sed 's/\/\/.*$//' > $file  # Making sure to remove all comment " // " lines from the orginal .v file ($filelocal) and piping to a temporary file ($file)

  #Counting total lines of the file
  set count = `cat $file | wc -l`             # counting the entire lines of the file ($file)

  # Finding the last line for the grep item
  set final = `grep -n "input\|output\|parameter\|inout" $file | sed 's/:/ /g' | awk '{print $1}' | tail -n 1 `  # Here we are greping the various fields as stated , here up to parameter and finding the line number


  echo $final

  head -$final $file > $file$temp # Then we are sending the contents of the lines up to the parameter to a new file

  echo \`protect begin >> $file$temp # Then printing the `protect begin to the new file

  # Finding the last line for the grep item
  set end = `grep -n "endmodule" $file | sed 's/:/ /g' | awk '{print $1}' | tail -n 1 ` # Finding the endmodule line here
  echo $end

  # printing between Lines
  set final = `expr $final + 1`
  set end = `expr $end - 1`

  sed -n $final,{$end}p $file >> $file$temp # Printing all the lines between after parameter last line to endmoule before line

  echo \`protect end >> $file$temp # Printing the `protect end
  echo endmodule >> $file$temp # Printing endmodule

   #printing end lines
  set end = `grep -n "endmodule" $file | sed 's/:/ /g' | awk '{print $1}' | tail -n 1 `
  set rem = `expr $count - $end`
  tail -$rem $file >> $file$temp # Printing the ending lines after endmdule

  # Copying the local file to orginal file
  cp -rf $file$temp $filelocal  # Copying the temporary file back to original file

end

Thanks for reading this post ..!

No comments:

Post a Comment