One scripting language without which it will be very difficult to survive in VLSI Industry, that would definitely be TCL (Tool Command Language). TCL is widely used everywhere in the VLSI industry because many tools are based on the tcl. We can directly interact with the tool using tcl CLI (Command Line Interpreter).
It has been observed that many beginners initially hesitate to start the TCL scripting. Most of the cases user know the basic tcl commands but how to connect all them and create a script is the only problem. I would say even you know some basic tcl commands you can start writing your own script for your day to day requirements. Most important this is BEGIN the process of writing the script. Here I am presenting 20 common tcl syntax which I use frequently in my tcl scripting and hope it will help you to break the ice. Improvement and prefection will come as you practice but start is the most important. These 20 syntax will definitely help you lot to start and improve your tcl scripting a lot.
1. foreach loop
Use:
Where we have to iterate on each element on a list of elements and have to perform some operation on each element.
Syntax:
foreach var $Var_list {
//operations for each $var
}
Example:
Supposed we want to print all the macros instance name, reference name and total count of macro in a block.
set macros [dbGet [dbGet top.insts.cell.baseClass block -p2].name]set i 0
foreach macro $macros {
set refName [dbGet top.insts.name $macro -p].cell.name
puts “$macro – $refName “
incr i
}
puts total macro count = $i
2. Nested foreach loop
Use:
Syntax:
foreach i $list1 {
//list2 is derived based on $i
foreach j $list2 {
//operations on $j
}
}
Example:
Suppose we have to find the list of feedthrough pins and the total numbers of feedthrough pins in each edge of a rectilinear block. A script can be written like this for that.
foreach edge $edges {
set j 0
foreach pin [dbget [dbget top.terms.edge $edge -p].name ft* ] {
puts “$edge $pin”
}
puts “$edge : total ft pin count = $j”
incr i
puts “Toal edge of block = $i”
3. for loop
4. Nested for loop
Same as above example only syntax change and one loop in running inside another loop.
Syntax:
for {initialization}{condition}{increment} {
for {initialization}{condition}{increment} {
statements
}
}
5. while loop
Use:
Syntax:
while {condition} {
//statements
}
Example:
Suppose we need to read all the lines of a file one by one and store is a variable dynamically.
if {[regexp “VIOLATED” $data]} {
//desired statement for operation
}
}
6. if-else conditions
Use:
When we want to do something if certain condition is true. We can either put else statement or skip it.
Syntax:
if {bolean_condition} {
//statements
} else {
//Statements
}
Example:
Suppose we need to convert SVT cell to ULVT cell.
if {[regexp SVT $cell_ref]} {
set eco_ref [regsub {SVT} $cell_ref “ULVT”]
}
7. if-elseif… else condition
Use:
if {bolean_condition} {
//statements
} elseif {bolean_condition} {
//Statements
} else {
//statement
Example:
if {[regexp “Weak Driver” $data]} {
set inst [lindex $data 7] set cell [lindex $data 8] if {[regexp D1BWP $cell]} {
regsub “D1BWP” $cell “D3BWP” newCell
} elseif {[regexp D2BWP $cell]} {
regsub “D2BWP” $cell “D4BWP” newCell
} elseif {[regexp D3BWP $cell]} {
regsub “D3BWP” $cell “D5BWP” newCell
if { [ info exists newCell ]} {
puts $fp1 “ecoChangeCell -inst {$inst} -cell $newCell”
}
}
close $fp
close $fp1
8. Arithmetic operations
Use:
When we need to add/subract/multiply/devide some numbers
set s [expr $a + $b + $c]set d [expr $a – $b ]set m [expr $a * $b]set d1 [expr $a / $b ]
If we need to know, how much space will take a 4X, a 8X and a 16X decap cell together.
set cell4X [dbget [dbget head.libCells.name $decap4 -p].size_x -u]set cell8X [dbget [dbget head.libCells.name $decap8 -p].size_x -u]set cell16X [dbget [dbget head.libCells.name $decap16 -p].size_x -u]set distX [expr $cell16X + $cell16X + $cell4X]puts $distX
Note: Be careful like in the following cases
like:
>expr 3/2
>1
>1.5
9. regexp
Use:
To match the regular expression. Regular expression has a wide list, we will see only few which we use mostly.
Syntax :
regexp {pattern} $string
Example:
if {[regexp {D1BWP240H11.*PDSVT} $clock_cell_ref]} {
regsub {D1BWP240H11} $clock_cell_ref {D2BWP240H8} new_clock_cell_ref
regsub {PDSVT} $new_clock_cell_ref {PDLVT} new_clock_cell_ref
puts $fp_w “ecoChangeCell -inst $clock_cell -cell $new_clock_cell_ref”
}
10. regsub
Use:
To substitute the regular expression
Syntax:
regsub {old_pattern} $string {new_pattern} new_string_name
Example:
As explained in the regexp section
11. Reading a file
Use:
Many times we have to read a report file inside the tcl scripting to make some fixes based on the violation reported. We read the file line by line and store the data in a file pointer variable dynamically.
Syntax:
set fp [open existing_file_name r]
we can read a file line by line in a variable like this.
set fp1 [open $old r] while {[gets $fp1 data] >= 0 } {
close $fp1
12. Writing a file
We can write a file and close that as follow.
close $fp2
13. proc
Use:
Syntax:
proc proc_name {} {
// lines of code
}
proc_name
puts “Block name: [dbget top.name]”
puts “All Layers: [dbget head.layers.name]”
puts “Block Area: [dbget top.fPlan.area]”
puts “Box size: [dbget top.fPlan.box_size]”
puts “Boxes: [dbget top.fPlan.boxes]”
puts “Toatl pins: [dbget top.numTerms], Inputs – [dbget top.numInputs], Outputs – [llength [dbget top.terms.isOutput 1 -p]]”
puts “Macro Count: [llength [dbget top.insts.cell.baseClass block -p2]]”
// Many more parameters can be added
}
We just need to source above file and call the proc blockInfo proc, it will display all the above info of block.
14. proc with arguments
Use:
Syntax:
proc proc_name { arg1 arg2 arg3 …} {
// lines of code
}
proc_name arg1 arg2 arg3
Suppose we want to write a general proc in which if we pass the net_name, it should return the net_length of that particular net. We can write that as follow.
set net_length 0
set net_wires_length [dbget [dbget top.nets.name $net_name -p].wires.length] foreach i $net_wires_length {
set net_length [expr $net_length + $i] }
puts $net_length
}
Note:
Syntax:
proc proc_name {{arg1 10} {arg2 20}} {
set a $arg1
set b $arg2
// More statements
}
So if we call the proc like
proc_name
proc_name 50
proc_name 50 45
15. exec
Use:
Syntax:
exec date
set timestamp_prefix [exec date +%m%d_%H_%M]
16. dbGet/dbSet/dbQuery
Use:
These are the innovus tool-specific commands, and widely used in innovus tool related scripting.
Syntax and Examples will be discussed in a separate article.
17. list operations
Use:
Syntax:
llenght, lappend, lindex, lreplace, lset, lsort etc.
18. alias
19. grep
Use:
Syntax:
exec grep “pattern” $file_name
20. sed
Use:
Syntax:
exec sed
Example 2:
exec sed -i “/DEL*/d” $file_name