Article

 

 

 

 


Top 10 Tips for efficient Perl Scripting for Chip Designers

Bringing automation to ASIC design typically includes the use of “Scripts”. In the new competitive generation of chip designing where Time-to-Market is so critical, you need a way to finish your automation tasks in smarter ways. The Perl scripting language is the designer’s best friend in meeting milestone targets. However, at times, scripts are so bulky, messy or complicated, that they will require more time in debugging and fixing the scripting issues rather than using to increase efficiency.

There are several pitfalls which may lead to inefficiency in Perl scripts. One major factor is that many chip design engineers are not good software programmers. Most of them work with HDLs, HVLs or C/C++ which gives them limited exposure to scripts. So, they tend to write scripts like they write “C” programs or any other RTL. But for ASIC engineers, knowledge of basic concepts of Perl helps to significantly increase the efficiency of Scripts. This inspired me to share the following “Top 10 tips” to write efficient Perl scripts in the world of ASIC.

1. Use “strict” and “warnings” to identify common problems at an early stage

Perl scripting is very flexible when it comes to syntax checking and that is the reason why it is like a double edged sword. Flexibility in syntax checking allows the designer to write hassle free code but on the other hand it is more error prone due to human limitations.
To avoid this, use of “use strict;” and “use warnings;” semantics or “-w” option in the script is advantageous. This helps to identify common mistakes, like same name variables declaration, undefined or un-initialized variables etc., which may cause unpredictable behavior of the script.
e.g.
#! /usr/bin/perl –w
use strict;
use warnings;

2. Use Subroutines for reusability and generalized code

Many new designers write code in a similar way as they think, which results in bulky repetitive code in the script.
Let’s see the following example, where we want to print the data in hexadecimal format:
e.g.
If ($data == 1)
printf (“data received = %x”,$rcv1_data);
elsif ($data == 2)
printf (“data received = %x”,$rcv2_data);
else
printf (“ERROR : Illegal input %x”,$data);

Now, if the above code is replicated 10-15 times to check the different “$data” value and to print output, then it will add about 60-90 additional lines. This is enough to make a script bulky. A simple solution is to move the code to a subroutine and to use the subroutine whenever required. Check the following code for code optimization with subroutine:
e.g.
#Subroutine to check data value
sub data_chk(){
  my  $data = shift;
  my $rcv1_data = shift;
  my $rcv2_data = shift;
  If ($data == 1)
  printf (“data received = %x”,$rcv1_data);
  elsif ($data == 2)
  printf (“data received = %x”,$rcv2_data);
  else
 printf (“ERROR : Illegal input %d”,$data);
}

# Subroutine usage
&data_chk ($data, $rec1_data, $rec2_data);
&data_chk ($data_new, $rec1_data, $rec2_data);

As we can see in the above example, subroutines provide the ability to define generalized and reusable code. This will not only reduce the number of lines but also increase the efficiency of the designer during debug and upgrade process.

3. Use Hashes for optimization

Most ASIC designers are comfortable working with arrays. So, they prefer to use an array in place of a “Hash” for certain Perl functionalities such as looking for required patterns or contents from the given set of data. Implementation of such tasks with array requires some sort of code which traverses through the entire array and gets the required data. This definitely takes more processing time and reduces the efficiency of the code by unnecessarily accessing all the indices. 
The following example shows one commonly used method to access data through arrays:
e.g.
       @array = (“module”, “test_name”, “test_number”);
       @content_array = (“Multiplexer”, “sanity_check”, “test_1”);
       # Access the name
       for ($i = 0; $i <= $#array; $i ++) {
           if ($array[$i] eq “module”) {
              $module = $content_array [$i];
           }
       }

As we can see from the code, we need at least two arrays and a bunch of code to find value of the “module”. This could be easily replaced by a “Hash” as follows:
e.g.
%hash = (“module” => “Multiplexer”, “test_name” => “sanity_check”, “test_number” => “test_1”);
$module = $hash {“module”};

Thus, Hash helps the designer reduce code in a significant way. Hash also helps to add more generalized code which is really helpful in tasks like creating a number of different combinations. Check the following example:
e.g.
%hash = (“operator” => [“+”, “-”], “r_value” => “a”, “l_value” => “b”);
$i = 0;
foreach $key (keys %hash) {
   print “c = ” . $hash{“r_value”} . “ ” . $hash{“operator”}->[$i] . “ ” . $hash{“l_value”};
  $i++;
}
After execution, this code will produce both the combinations of the equation as an output:
Output:
      c = a + b;
      c = a – b; 

 


  1 | 2 | 3 | Next Page>>

  Silicon Design | Design & Verf. IP | Embedded Design | About Us | Partners | Contact