Vous êtes sur la page 1sur 5

11/2/13

Top 10 Tips for efficient Perl Scripting for Chip Designers


About D&R | D&R Enterprise Platform | dr-embedded.com | Login | Subscribe to D&R SoC New s Alert

SEARCH IP

NEWS

INDUSTRY ARTICLES

BLOGS

VIDEOS

IP-SOC EVENTS

Search Industry Articles

Top 10 Tips for efficient Perl Scripting for Chip Designers


By Umang Mistry, Sibridge Technologies 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 designers 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 uninitialized variables etc., which may cause unpredictable behavior of the script. e.g. # !/ u s r / b i n / p e r l w u s es t r i c t ; u s ew a r n i n g s ; 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. Lets see the following example, where we want to print the data in hexadecimal format: e.g. I f( $ d a t a= =1 ) p r i n t f( d a t ar e c e i v e d=% x , $ r c v 1 _ d a t a ) ; e l s i f( $ d a t a= =2 ) p r i n t f( d a t ar e c e i v e d=% x , $ r c v 2 _ d a t a ) ; e l s e p r i n t f( E R R O R:I l l e g a li n p u t% x , $ d a t a ) ; 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. # S u b r o u t i n et oc h e c kd a t av a l u e
Share 1 0 Tw eet 0

SEARCH SILICON IP
12,000 IP Cores from 400 Vendors

Enter Keywords....

RELATED ARTICLES
Top 10 Tips for Success with Formal Analysis - Part 3 Top 10 Tips for Success with Formal Analysis - Part 2 Top 10 Tips for Success with Formal Analysis - Part 1 10 software tips for hardware engineers Top 10 methods for ASIC power minimization -- Part 2

NEW ARTICLES
How to verify control loop design Optimizing high-performance CPUs, GPUs and DSPs? Use logic and memory IP - Part I Application of Option in Semiconductor IP Business Strategy Get ready for M-PCIe testing Meticom: Bridging FPGAs & MIPIEnabled Devices

See New Articles >>

MOST POPULAR
1. 2. 3. 4. 5. Dynamic Memory Allocation and Fragmentation in C and C++ NAND Flash memory in embedded systems MIPI MPHY - An introduction How to calculate CPU utilization CORTEX-R versus CORTEX-M

www.design-reuse.com/articles/20613/perl-scripting-chip-design.html

1/5

11/2/13

Top 10 Tips for efficient Perl Scripting for Chip Designers


See the Top 20 >>

s u bd a t a _ c h k ( ) { m y $ d a t a=s h i f t ; m y$ r c v 1 _ d a t a=s h i f t ; m y$ r c v 2 _ d a t a=s h i f t ; I f( $ d a t a= =1 ) p r i n t f( d a t ar e c e i v e d=% x , $ r c v 1 _ d a t a ) ; e l s i f( $ d a t a= =2 ) p r i n t f( d a t ar e c e i v e d=% x , $ r c v 2 _ d a t a ) ; e l s e p r i n t f( E R R O R:I l l e g a li n p u t% d , $ d a t a ) ; } #S u b r o u t i n eu s a g e & d a t a _ c h k( $ d a t a ,$ r e c 1 _ d a t a ,$ r e c 2 _ d a t a ) ; & d a t a _ c h k( $ d a t a _ n e w ,$ r e c 1 _ d a t a ,$ r e c 2 _ d a t a ) ; 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. @ a r r a y=( m o d u l e , t e s t _ n a m e , t e s t _ n u m b e r ) ; @ c o n t e n t _ a r r a y=( M u l t i p l e x e r , s a n i t y _ c h e c k , t e s t _ 1 ) ; #A c c e s st h en a m e f o r( $ i=0 ;$ i< =$ # a r r a y ;$ i+ + ){ i f( $ a r r a y [ $ i ]e q m o d u l e ){ $ m o d u l e=$ c o n t e n t _ a r r a y[ $ 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. % h a s h=( o p e r a t o r = >[ + , ] , r _ v a l u e = > a , l _ v a l u e = > b ) ; $ i=0 ; f o r e a c h$ k e y( k e y s% h a s h ){ p r i n t c=.$ h a s h { r _ v a l u e }.. $ h a s h { o p e r a t o r } > [ $ i ]..$ h a s h { l _ v a l u e } ; $ i + + ; } After execution, this code will produce both the combinations of the equation as an output: O u t p u t : c=a+b ; c=ab ; 4. Use OOP for modularity in the code OOP (Object Oriented Programming) methodology is of great help when it comes to reusability. Perl provides Object Oriented support by means of package. Designers can define their own package and a set of code which they can use whenever needed. But do remember to create

E - mail T his A rtic le

P rinter- Friendly P age

www.design-reuse.com/articles/20613/perl-scripting-chip-design.html

2/5

11/2/13

Top 10 Tips for efficient Perl Scripting for Chip Designers

a constructor (new subroutine in Perl) for each package. Check the following example how it can be used to improve code efficiency: e.g. p a c k a g eg e n _ r a n d o m ; s u bn e w{ m y$ s e e d=s h i f t ; i f( u n d e f$ s e e d ){ $ s e e d=t i m e ( ) ; } } s u br a n d o m _ d a t a{ m y$ m a x _ v a l=s h i f t ; m y$ d a t a=$ r a n d ( $ m a x _ v a l ) ; r e t u r n$ d a t a ; } s u bd i s t _ r a n d o m _ d a t a{ } s u bw e i g h t e d _ r a n d o m _ d a t a{ . . } 1 ; Above code can be used as follows : e . g . m y$ r a n d _ o b j 1=g e n _ r a n d o m > n e w ( 1 ) ; m y$ r a n d _ o b j 2=g e n _ r a n d o m > n e w ( 2 ) ; m y$ d a t a=$ r a n d _ o b j 1 > r a n d o m _ d a t a ( 1 0 ) ; m y$ d a t a 1=$ r a n d _ o b j 1 > d i s t _ r a n d o m _ d a t a ( 2 0 ) ; m y$ d a t a 2=$ r a n d _ o b j 2 > w e i g h t e d _ r a n d o m _ d a t a ( 1 5 ) ; 5. Use a common place to define all the hardcoded values for easy accessibility Most of the time in Chip industry, scripts are used for regression. This kind of script always requires different paths, constant values etc. to be defined. Most of the designer defines these quantities at the place where they are used. This practice is fine as far as script is small, but when dealing with larger scripts, this code will take lot of debugging time. So, the solution is to use the use constant directive at the start of the script. This will act as a constant parameter and allow easy modification and debugging. Also, usage of system environment variables wherever required allows the script to run on different machines with different users. Check the following code: e.g. u s ec o n s t a n tS I M _ P A T H= / p r o j e c t / m y _ a r e a / $ E N V { U S E R } / s i m ; u s ec o n s t a n tM A X _ I T R=1 0 ; f o r( $ i=0;$ i< =M A X _ I T R ;$ i + + ){ $ c m d= c d.S I M _ P A T H. ;m a k et e s t ; s y s t e m( $ c m d ) ; } This code allows the designer to easily modify the constant arguments with ease of accessibility for each user. 6. Use shift instead of @_ for subroutine arguments to avoid unexpected results Typically, the designer uses @_to get argument values in subroutine, where the @ refers to an array comprising of all the arguments passed. Review the following example: e.g. s u bm y _ s u b{ p r i n t F i r s ta r g u m e n t=$ _ [ 0 ] ; p r i n t S e c o n da r g u m e n t=$ _ [ 1 ] ; p r i n t T h i r da r g u m e n t=$ _ [ 2 ] ; } & m y _ s u b( f i r s t , s e c o n d , t h i r d ) ; For this sample code, the designer has to remember the index of the argument array for all the arguments used. This can cause error prone code, when it comes to multiple arguments. To reduce this risk, the designer can use shift, which will make sure that the subroutine has all the required arguments and the designer can access it with meaningful names. Examine the following code for argument passing with shift:

www.design-reuse.com/articles/20613/perl-scripting-chip-design.html

3/5

11/2/13
e.g.

Top 10 Tips for efficient Perl Scripting for Chip Designers

s u bm y _ s u b{ m y$ f i r s t=s h i f t ; m y$ s e c o n d=s h i f t ; m y$ t h i r d=s h i f t ; p r i n t F i r s ta r g u m e n t=$ f i r s t ; p r i n t S e c o n da r g u m e n t=$ s e c o n d ; ; p r i n t T h i r da r g u m e n t=$ t h i r d ; } & m y _ s u b( f i r s t , s e c o n d , t h i r d ) ; 7. Use Regular Expressions instead of Split to increase performance Using Split is a common practice when it comes to getting a specified pattern from a line or bigger pattern. Following is the typical usage of Split function: e.g. $ l i n e= Ia mad e s i g n e r ; @ a r r a y=s p l i t( ,$ l i n e ) ;#s p l i tl i n ew i t h s p a c e $ l a s t _ w o r d=$ a r r a y [ 3 ] ;#w i l ls t o r ew o r d d e s i g n e r i n $ l a s t _ w o r d As shown in above code, Split function uses arrays to store the patterns generated after breaking the whole sentence or word or any big pattern. As it uses arrays, it definitely grabs memory to store the elements. If you have 25-30 such arrays with big patterns, it will certainly degrade your PCs performance. To solve this, the best solution is to use Regular Expressions. Regular Expressions are expressions which let designers search for required patterns. The following code shows how you can use Regular expression instead of Split. e.g. $ l i n e= Ia mad e s i g n e r ; $ l a s t _ w o r d=$ 1i f( $ l i n e= ~/ . * ( d e s i g n e r ) / ) ; #$ 1s t o r e s t h ep a t t e r n d e s i g n e r or $ l a s t _ w o r d= $ l i n e ;#c o p i e s Ia mad e s i g n e r t o $ l a s t _ w o r d $ l a s t _ w o r d= ~s / Ia ma/ / ;#T h i sw i l lr e m o v e Ia ma s t r i n gf r o m$ l a s t _ w o r d . 8. Use Comments to accelerate debugging process Meaningful comments are the heart of any code. It will give the designer or debugger some familiarity about the code and more importantly its intent. Quite often, scripts developed by one designer, are used (or reused) by another. In this scenario, if any error or unexpected behavior occurs and if comments are not proper or missing, it will be very difficult for the debugger to trace the cause of failure. So, meaningful comments definitely save time and increase efficiency of the script debugging process. 9. Use proper Error messaging to identify system level issues Proper error messaging is a great way to improve efficiency of debugging. If you know the correct message generated by script, then your half task is done. Following are a few examples of useful error messages: e.g. $ F P T R > o p e n ( > f i l e . t x t )| |d i e( $ 0:E R R O R 1:C a n n o t o p e nf i l e< f i l e . t x t >f o rw r i t i n g .S y s t e mE r r o r:$ ! ) ; #I s s u e se r r o rw i t hS y s t e me r r o rm e s s a g e i f( !( eP A T H / $ f i l e ) ){ p r i n t $ 0:E R R O R 2:< $ f i l e >d o e s n te x i s t sa t.P A T H ; e x i t1 ; }#I s s u e se r r o ra n de x i tt h es c r i p tw i t hr e t u r nc o d e= 1 i f( s y s t e m( c p$ f i l e 1$ f i l e 2 ) ){ d i e( $ 0:E R R O R 3:C a n n o tc o p y< $ f i l e 1 >t o< $ f i l e 2 > , S y s t e mE r r o r:$ ! ) ; }

www.design-reuse.com/articles/20613/perl-scripting-chip-design.html

4/5

11/2/13

Top 10 Tips for efficient Perl Scripting for Chip Designers

10. Use ready to use modules to avoid reinventing the wheel This is the most important point to note. While developing scripts, one can come across so many situations where one needs to develop new code/package for the script. For Chip industry, examples such as Random data generation, Case statements, File management, generation of report etc., are very common. For all these functionalities, many designers develop their own packages or subroutines which consumes needless time. The solution to this is CPAN: CPAN is the Perl archive network where you can get a solution to most of your problems. Designers can download almost all the required packages/modules which can save valuable time. By using some or all of the above techniques, chip designers can markedly improve the efficiency of their scripts. As I mentioned earlier, in this competitive world where Time-to-Market is very critical, a small change in the traditional way of script-writing can make a huge difference in performance and efficiency. Author: Umang Mistry Member, Technical Staff Sibridge Technologies, Ahmedabad References: Perl in 21 days book by David Till www.cpan.org : Website with Perl Archive Network Perl Cookbook book by By Tom Christiansen & Nathan Torkington, O-Reilly Media

Give us your feedback


Was this page helpful? Ask us a question or get help

Partner with us
Suppliers, list your IPs for free.

Design-Reuse.com
About us D&R Partner Program Advertise with Us Privacy Policy

2013 Design And Reuse


All Rights Reserved. No portion of this site may be copied, retransmitted, reposted, duplicated or otherwise used without the express written permission of Design And Reuse.

Talk to us

Add your Products

www.design-reuse.com/articles/20613/perl-scripting-chip-design.html

5/5

Vous aimerez peut-être aussi