Tuesday, December 15, 2015

TCL example scripts

In this post, I will upload sample scripts to refer : (Under cons)

EXAMPLE : 1
For the engineers who are familiar with static timing analysis, as a programmer I want to calculate arrival time, required time, slack at each node using the given circuit. The example script will be able to demonstrate some of the tcl functions. The following circuit 10 nodes excluding (Out node), delay of each node is represented in the brackets. The objective of this program is to write a TCL script to analyse the slack at each and every node using graph based slack analysis.

Brief introduction about the graph based STA :
We need to break the entire program into three sections :
1. Arrival time calculation (AT)
2. Required time calculation (RT)
3. Slack calculation. 

First we need to calculate AT at each node, Let's do hand calculation of arrival time at few nodes. Arrival time at node 'A' is 10 as delay of node  'A' is 10 and then it passes through node 'D' whose delay is '8',  so the arrival time at node 'D' is 18 adding the delay of traversed path. But we can reach D by traversing IN->B->D accounting the delay of 13 (5+8), so we need to choose bigger one of the two arrival times (18,13) and assign that  arrival time to node 'D' in this case it is '18'. We need to repeat the same for all the nodes.


In the second section we need to calculate the required time starting from out node 'O'. In general we might be given required time by the clock specification. But in this case we take RT as the max of all the arrival times.









The above circuit can be represented using the adjacency matrix. Please refer to following link to know more about adjacency matrix. https://en.wikipedia.org/wiki/Adjacency_matrix


set ifile [open sta_inputs.txt r]
set i 0
while { [gets $ifile line] != -1 } {
set j 0
foreach val $line {
set orig($i,$j) $val
incr j
}
incr i
}
close $ifile
#set arrival time,required time matrices
array set atmat [array get orig]
array set rtmat [array get orig]
#procedures used
proc getmaxcol { mat itr } {
upvar $mat mat1
set ctr [expr int (sqrt ([llength [array names mat1]]))]
set max $mat1($itr,$itr)
for { set i 0 } { $i < $ctr } { incr i } {
if { $max < $mat1($i,$itr) } { set max $mat1($i,$itr) }
}
return $max
}
proc getminrow { mat itr } {
upvar $mat mat1
set ctr [expr int (sqrt ([llength [array names mat1]]))]
set min $mat1($itr,$itr)
for { set i 0 } { $i < $ctr } { incr i } {
if { $min > $mat1($itr,$i) } { set min $mat1($itr,$i) }
}
return $min
}
#arrival time calculation
set ctr [expr int (sqrt ([llength [array names orig]]))]
for { set i 0 } { $i < $ctr } { incr i } {
set max [getmaxcol atmat $i]
for {set j 0 } { $j < $ctr } { incr j } {
if { $atmat($i,$j) != 0 } {
set atmat($i,$j) [expr $atmat($i,$j)+$max]
}
}
}
set rt [getmaxcol atmat 0]
for { set i 0 } { $i < $ctr } { incr i } {
set arrival($i) [getmaxcol atmat $i]
if { $rt < $arrival($i) } { set rt $arrival($i) }
}
#required time calculation
set n [expr $ctr - 1]
#set rt [getmaxcol atmat $n]
for { set i $n } { $i >= 0 } { incr i -1 } {
for { set j $n } { $j >= 0 } { incr j -1 } {
if { $rtmat($i,$j) != 0 } {
set rtmat($i,$j) [expr [getminrow rtmat $j] - $rtmat($i,$j)]
} else {
set rtmat($i,$j) $rt;
}
}
}
for { set i 0 } { $i < $ctr } { incr i } {
set required($i) [getminrow rtmat $i]
}
for {set i 0 } { $i < $ctr } { incr i } {
set slack($i) [ expr $required($i) - $arrival($i)]
}
puts "Node :: arrival_time :: reqd_time :: slack"
for {set i 0 } { $i < $ctr } { incr i } {
puts "$i :: $arrival($i) :: $required($i) :: $slack($i)"
}
view raw sta.tcl hosted with ❤ by GitHub
0 10 5 2 0 0 0 0 0 0
0 0 0 0 8 0 0 0 0 0
0 0 0 0 8 7 0 0 0 0
0 0 0 0 0 7 0 0 0 0
0 0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 4 0 0 0
0 0 0 0 0 0 0 6 3 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 0 0
view raw sta_inputs.txt hosted with ❤ by GitHub


Sunday, December 13, 2015

physical design

Please visit this blog to learn/discuss all about physical design tips, techniques, fundamentals.

Monday, December 7, 2015

regex

Regex,regsub  :


A regular expression is simply a description of a pattern that describes a set of possible characters in an input string.

Example :



#I want to replace dog with cat in the following statement
set exp1 "someone let the dog out of bag"
#First check whether dog is present in bag or not
set val [regexp (dog) $exp1 sub1 sub2 sub3]
puts $sub1
if { $val } {
regsub dog $exp1 cat exp2
}
puts "original expression : $exp1"
puts "modified expression : $exp2"
set exp3 "I have one black dog, one white dog and one brown dog in my bag"
#replace all dogs with corresponding cats
regsub -all dog $exp3 cat exp4
puts $exp4
#When you don't want to include particular pattern in sub-pattern use ?:
set exp5 "Sasken LSI TI Mirafra"
regexp "(Sasken|Smartplay).*(?:LSI|Avago).*(Mirafra)" $exp5 sub1 sub2 sub3
#Output :
#% puts $sub1
#Sasken LSI TI Mirafra
#% puts $sub2
#Sasken
#% puts $sub3
#Mirafra
set exp6 "Ooh La La Tu Hai Meri Fantasy "
regexp -nocase (oo)? $exp6 sub1
puts $sub1
#If I want to search starting with ^O and ending with y
regexp "^O.*y$" $exp6
#Loop through all the words in the exp6 and put the words which doesn't start with vowels
foreach var $exp6 {
if { [regexp -nocase {^[^aeiou]} $var ] } {
puts $var
}
}
#Output
#La
#La
#Tu
#Hai
#Meri
#Fantasy
view raw regex_demo.tcl hosted with ❤ by GitHub

strings

Strings

  • A string is a sequence of letters, it always need not be enclosed in the quotes until you use some white spaces in between the words. Tcl is a string based language it provides rich set of operations on strings.
Examples :
set str1 one
set str2 "two three"

set str3 I\ need\ not\ enclose\ this\ in\ quotes

  • Following operations can be executed on strings:
    • string compare
    • string equal
    • string length
    • string match #global expression
    • string toupper
    • string tolower
    • string totitle #it will convert the starting letter of string to uppercase
    • string index
    • string range
    • string reverse
Example :
set str1 Tcl
set str2 "Tcl programmer"
set str3 tcl
#The string compare command compares strings character by character. If it finds that the first characters of both strings are equal,
#it continues with the second character, until the end. It returns 0 if the strings are equal and -1
#if a non-matching character in the first string is located in the ASCII table before the character of the second string.
#The 1 number is returned if the non-matching character of the first string is located after the character of the second string.
puts [string compare $str1 $str2 ] ; #output is -1
puts [string compare -length 3 $str3 $str2 ] ; #output is 1
puts [string compare -length 3 $str1 $str2 ] ; #output is 0, it checks first 3 letters in the string
#string equal can be used to compare strings, it returns 1 if they are true else 0 for false. It checks for case
puts [string equal $str2 $str3 ] ; #output is 0
puts [string equal -length 3 $str2 $str3 ] ; #output is 1, it checks first 3 letters in the string
puts [string length $str1] ; #output is 3
puts [string match Tcl* $str2] ; #global expression
puts [string toupper $str1] ; #Output : TCL
puts [string tolower $str1]; #Output : tcl
#string totitle #it will convert the starting letter of string to uppercase
puts [string totitle $str3]; # Output : Tcl
#To get the last letter of the string
puts [string index $str1 end]
#to get range of letters
puts [string range $str2 2 5]; #output : l pr
puts [string reverse $str2]
#Output:remmargorp lcT
view raw strings.tcl hosted with ❤ by GitHub




lists


  • Lists
    • Lists are equivalent to arrays in the conventional programming language like C.
    • Lists can be expanded and collapsed on the fly.

  • Example 1:
    • set list1 "a  b c d"

File handling

File Handling :

File names could be specified in Unix format.
In order to work with a file, first one has to open a channel to the file...this is also called handle
Once the channel is opened, TCL retruns a channelId which is used to work with the files
Typical steps to be followed while working with files:
Step 1: Check whether the file specified exists or not
Step 2: Open a channel pointing to the file...TCL returns an ID
Step 3: Use the ID to work with the file
Step 4: Close the channel after use

File Open :

"open" could be used to open a channel to a file. This command returns the file-handle (channelID) which could be used to work with the files.
Syntax:
open fileName ?access mode?
where
fileName - is the name of the file to be opened for use
access mode - either r, r+, w, w+, a or a+. These are the access modes (similar to C language)
"open" opens a channel to the file specified by the fileName with the access permissions specified by the access mode. Default is "r".
A channel once opened, remains open until specifically closed.

File Close :

"close" is used to close a channel previously opened using the "open" command.
Syntax:
close fileID
where
fileID - is the channel ID of the file opened


Example :

set ofile [open data.txt w]
for {set i 0 } { $i < 10 } { incr i } {
puts $ofile "This is line number : $i "
}
close $ofile
set ifile [open data.txt r]
while { [gets $ifile line] != -1 } {
puts $line
}
close $ifile



some of the useful file operations are: (no need to open a channel for these operations)
file exists      --> checks if the specifie file exists or not
file dirname     --> returns the parent directory of the specified file
file extension   --> returns the extension (suffix) of the specified file (incl. dot)
file isfile      --> returns 1 if “name” is a normal file
file isdirectory --> returns 1 if “name” is a directory
file executable  --> returns 1 if “name” has executable permissions
file readable    --> returns 1 if “name” has read permissions
file writable    --> returns 1 if “name” has write permissions
file tail        --> returns the last pathname component of “name”

file rootname    --> returns all but the extension of the “name”

procs

procedures are supported by TCL :

Procedures return to the calling program through "return" statement
Every procedure must have a corresponding return statement. This however is not strictly required.
Once they are created, procedures could be used just as any other TCL command
Procedures are declared with the help of keyword "proc"

Each procedure may have a set of formal parameters and some variables - local and global
Syntax:
proc proc_name { formal parameters } {
  statement1
  statement2
  :
  return
}
Example:
proc plus { a b } {
  expr $a + $b
  return
}

Syntax:
proc name argList body
where,
name - is the procedure name
argList - List of arguments. If a procedure takes more than one argument, they should be specified within braces.
body - body of the procedure
Example:

% proc proc_a { a b c } { body of procedure }
% proc proc_b b { body of procedure }
Once created, a procedure could be invoked/ called just by its name
Example: proc_a { $a $b $c }, proc_b { $b } w.r.t. The above example
Every procedure has its own set of local variables.
Any two procedures may have variables with the same names. The values of the variables do not get affected since they are not visible outside the procedure block.

"global" could be used to declare the scope of some variables as global
"global" should be used inside a procedure
Syntax:
global varName ?varName ...?
where
varName - is the name of variable whose scope has to be
extended beyond a procedure. More than one variable could be 
specified.
"global" binds variable names "varName"... to global variables. References to these names will refer to global variables instead of local variables for the duration of the current procedure.




Example 1:
proc proc_a { } {
global globvar
puts "Variable globvar is associated to $globvar"
set globvar "Variable updated inside proc_a"
puts $globvar
}
proc proc_b { } {
set globvar "Variable updated inside proc_b"
puts $globvar
}
set globvar "outside proc"
proc_a
puts "Value of globvar now after proc_a execution: $globvar"
proc_b
puts "Value of globvar now after proc_b execution: $globvar"
view raw proc_global.tcl hosted with ❤ by GitHub

Example 2: Simple tree traversal iterative procedure.

set child(a) "b c"
set child(b) "d e"
set child(c) "f g h"
set child(d) "i j"
set child(e) "k l"
set child(f) "m"
set child(g) "n o p"
set child(h) "q"
set child(i) r
set child(j) s
set child(k) "t u"
proc iter_tree { a } {
global child;
if [info exists child($a)] {
foreach aa $child($a) {
puts "$aa is child of $a"
iter_tree $aa
}
}
}
iter_tree a
Example 3 :

#Procedure whose default variable is set 1
#This script will increment the variable with the given amount of increment if provided.
#if optional increment value is not provided, it will increment by 1
proc increment { var {increment 1} } {
return [expr $var+$increment]
}
view raw proc_incr.tcl hosted with ❤ by GitHub

Sunday, December 6, 2015

continue and break


  • Continue and Break statements
  • Continue statement will terminate the current execution of the loop and start the next iteration.
  • Break statement will terminate the execution of the loop and comes out of the loop.
  • Continue and Break statements are very important in real life implementation.


  •  For example, you might run placer or some extraction which is running for like a very long time (say 48 hours) and you do not want to lose your database with all the fixes you can use break and come out of the loop. Another example is if you do not want to execute the timer update on every iteration, but want to execute in every other iteration.

 Example 1:





for { set i 0 } { i < 10 } { incr i } {
puts "Hello executing command1, value of i is $i"
if { $i%2 == 0 } {
continue
}
puts "Hello executing command2, value of i is $i"
if { $i%5 == 0}
break
}
puts "Hello executing command3, value of i is $i"
}

Control Structures

Control Structures : (Under construction)

In general execution of the program would be sequential, if the programmer needs to change the flow of execution, he can very well do using certain tcl constructs. We will discuss about them in this section. Following are the different constructs which are available in TCL:

  • if - else & if - elseif - else structure
  • while structure
  • foreach  structure
  • for structure
  • switch





  • if - else & if - elseif - else structure
     If the condition is evaluated true, command1 and command2 will be executed.
     Syntax :
       if { condition } {
       command1
       command2
       }


     You can use if-else structure as well. i.e. if the condition is true do this and if it is false do that.
    Syntax :
     if { condition } {
       command1
       command2
      } else {
        command3
        command4
     }

You can use if-elseif-else and even nested if within each group.

If condition1 is evaluated to true, command_m would be executed, else if condition2 is evaluated to true command_n. If neither the condition evaluates to true, command_p would be executed



   if { condition1 } {
  command_m  
 } elseif { condition2 } { 
  command_n    
} else {  
command_p  
  }
Example 1:
set i 100
if { $i < 20 } {
puts "I have less than 20 rupees"
} elseif { $i >= 20} {
puts "I have greater than 20 rupees in my pocket"
} else {
puts "I will never get into this condition"
}



While loop :

  • While the condition evaluates to true iterate over the loop again and again till it evaluates to false. Once it evaluates to false come out of the loop.



while { condition } {
statement1;statement2;updation; }


Example 2:
set i 100
while { $i <= 100 } {
puts "Value of i is $i"
incr i -1
}


For loop :
It is similar to the while loop except that you can have a flexibility to  initialize the loop iterator and also update the iterator in the same line.


for { initiliaze } { condition } { updation } {
command1;command2;..}

Example 3:
for { set i 10 } { $i < 10 } { incr i } {
puts "I love my country"
}


foreach control structure:
It loops over a set of variables in the list and executes operations on each variable. It is the most commonly used control structure in the TCL.


foreach var <list> {

execute operation on var;
statement1;
statement2;
.
.
}

Example 4:
set pins "inst1/a
inst2/b
inst3/a
inst4/a
mpu1/a
mpu2/b
"
set mpu_viopins ""
foreach pin $pins {
puts "pin name is $pin"
if { [regexp mpu $pin] } {
lappend mpu_viopins $pin
}
}
puts "Number of violating mpu pins : [llength $mpu_viopins]"


TCL arrays

TCL array: (Under construction)

  • Array is a collection of variables.
  • Associative  -> abstract data type composed of a collection of key value pairs. 
  • Key value pairs are not ordered.
  • Each variable can hold any value or an array.

How to create an array in tcl?
  • We can create an array using set or "array set"
Array operations :
Different operations associated with array


array exists
array get
array names
array set
array size
array unset
array startsearch
array nextelement
array anymore
array donesearch
array statistics


set days(mon) Monday
set days(tue) Tuesday
set days(wed) Wednesday
set days(thu) Thursday
set days(fri) Friday
set days(sat) Saturday
set days(sun) Sunday
foreach key [array names days] {
puts "$key and $days($key) are same"
}
view raw arrayset1.tcl hosted with ❤ by GitHub

Output: 
sat and Saturday are same
wed and Wednesday are same
fri and Friday are same
thu and Thursday are same
sun and Sunday are same
tue and Tuesday are same
mon and Monday are same

Note: Observe that key and value pairs are not ordered.

Example:2

#delete existing array
array unset days
array set days {
1 Mon
2 Tue
3 Wed
4 Thu
5 Fri
6 Sat
7 Sun
}
foreach key [array names days] {
puts "$key is associated to $days($key) "
}
view raw arrayset2.tcl hosted with ❤ by GitHub
Output:
4 is associated to  Thu
5 is associated to  Fri 
1 is associated to  Mon 
6 is associated to  Sat 
2 is associated to  Tue 
7 is associated to  Sun 
3 is associated to  Wed

All array operations are listed in this example
Example:3

array set days {
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
8 Jollyday
}
if [array exists days] { puts "Array days exists"}
unset days(8)
puts "size of array is [array size days]"
set start [array startsearch days]
while {[array anymore days $start]} {
set key [array nextelement days $start]
puts $days($key)
}
array donesearch days $start
puts "printing array using foreach loop"
foreach key [array names days] {
puts "$key is associated with $days($key)"
}
set i 1
puts "printing array using while loop"
while { $i <= [array size days] } {
puts "$i is associated with $days($i)"
incr i
}
array statistics days
array unset days
view raw arrayops.tcl hosted with ❤ by GitHub

Example:4

proc addname {first last} {
global name
incr name(ID)
set id $name(ID)
set name($id,first) $first
set name($id,last) $last
}
#Initialization
set name(ID) 0
addname Teja Konduri
addname Mark Zuckerberg
addname Steve Jobs
addname Kate Twinslet
addname Deepika Padukone
addname Katrina Kaif
view raw addname.tcl hosted with ❤ by GitHub

TCL upvar command usage

upvar, a built-in Tcl command, links a variable to some other variable at the specified level of evaluation.

i.e If you want to change the value of variable from with in the procedure we can use upvar to do that.

Example:
proc upvar_demo {var1 var2} {
upvar $var1 procvar1
upvar $var2 procvar2

set procvar1 TCL
set procvar2 Script

}

set outvar1 Hello
set outvar2 World

puts "Values of variables before executing the procedure"
puts "outvar1 : $outvar1"
puts "outvar2 : $outvar2"

upvar_demo outvar1 outvar2
puts "Values of variables after executing the upvar procedure"
puts "outvar1 : $outvar1"
puts "outvar2 : $outvar2"

#Output :
Values of variables before executing the procedure
outvar1 : Hello
outvar2 : World

Values of variables after executing the upvar procedure
outvar1 : TCL


outvar2 : Script