Tool Command Language
2. Basics

2.1 Simple Variables
2.2 Arithmetic Expressions
2.3 Lists
2.4 Loops
2.5 Arrays
2.6 Conditional Branching
2.7 Logical Operators
2.8 Assignment

Objectives

All examples discussed in this module are part of the GettingStarted project, which can be found in the directory Applications_Library/GettingStarted/tcl/tcl_basics.

Click to view the master file gtclsh_tcl.cmd.

2.1 Simple Variables

In Tcl, variables do not need to be declared before being used and they do not have a type. This means that the difference between an integer, a floating-point variable, and a string is only how the variable is used:

set i  3
set q  1.6e-19
set W  "Hello World"

The dollar sign ($) is used to access the value of a variable. (The puts command writes to standard output, that is, to the screen.)

puts "The value of i is $i"
#-> The value of i is 3

puts "The elementary charge is $q C"
#-> The elementary charge is 1.6e-19 C

puts "The string W contains >$W<"
#-> The string W contains >Hello World<

In some cases, it is unclear where the variable name ends and a string starts. In this case, use {} to indicate where the variable name ends:

puts "The ${i}rd value"
#-> The 3rd value

2.2 Arithmetic Expressions

Use the Tcl function expr to execute an arithmetic expression:

set j   [expr $i+5]
puts "$i + 5 is $j"
#-> 3 + 5 is 8

set pi [expr 2.0*asin(1.0)]
puts "pi = $pi"
#-> pi = 3.141592653589793

set SIN [expr sin($pi/4.0)]
puts "sin(pi/4.0) is $SIN"
#-> sin(pi/4.0) is 0.7071067811865475

Like other programming languages, note the different ways of handling integer and floating-point operations:

puts [expr 1/2]
#-> 0

puts [expr 1/2.0]
#-> 0.5

Therefore, in most cases, you should append a dot (.) to any integer when doing arithmetics.

2.3 Lists

A very basic type of Tcl is a list. A list consists of elements that are separated by whitespace. The first element of a list is associated with the index 0.

2.4 Loops

This section describes different loop operations that can be performed using Tcl.

2.4.1 The foreach Loop

The foreach loop operates on lists:

foreach NUM $NUMList CHAR $ABCList {
  puts "The ${NUM} letter of the alphabet is $CHAR"
}
#-> The 1 letter of the alphabet is a
#-> The 2 letter of the alphabet is b
#-> The 3 letter of the alphabet is c
#-> The 4 letter of the alphabet is d
#-> The 5 letter of the alphabet is e
#-> The 6 letter of the alphabet is f   

A foreach loop steps through the given lists, here NUMList and ABCList, and executes the body, here a simple puts for each element of the list.

The current elements of the list are stored in the variables NUM and CHAR. All lists given to the foreach loop should have the same number of elements. Otherwise, they are padded with empty elements {}.

2.4.2 The for Loop

The for loop uses a counter that is incremented in each pass. In the following for loop, the variable i takes the values 0, 1, 2, ..., 10:

for { set i 0 } { $i <= 10 } { incr i } {
  puts -nonewline " i=$i "
}
#-> i=0  i=1  i=2  i=3  i=4  i=5  i=6  i=7  i=8  i=9  i=10 

The first argument of the for loop initializes the counter, here i is set to zero. The second argument defines the end condition, here, the loop is executed until the variable i is no longer less than or equal to 10. The third argument defines the increment of the counter after each pass, here, the counter is incremented by 1.

The spaces at the locations marked by * are required:

for*{ set i 0 }*{ $i <= 10 }*{ incr i }*{...}

2.4.3 The while Loop

The while loop offers more flexibility than the for loop. The end condition can be anything.

set f 1.0
set x 0.0
while { $f > 0.0 } {
   set x [expr $x + 0.01]
   set f [expr 1.0 - $x*$x]   
}
puts "Zero crossing is at x= $x"
#-> Zero crossing is at x= 1.0 

Here, a while loop is used to find the zero crossing of the function f = 1 – x2. In each pass, x increases, and the function f is evaluated. The process stops when f is no longer positive.

2.4.4 The break and continue Keywords

The loop flow can be influenced by using the following keywords:

for {set i 0} {$i<=100} {incr i} {
  if {[expr fmod($i, 2)] == 0} {
    continue
  } elseif {$i > 6} {
    break
  } 
  puts $i
}
#-> 1
#-> 3
#-> 5

2.5 Arrays

An array contains elements that can be addressed using an identifier.

set model(1) Fermi
set model(2) Constant
set model(3) 3Stream
set model(4) 5Stream

for { set i 1 } { $i <= 4 } { incr i } {
  puts "Model #$i is $model($i)"
}
#-> Model #1 is Fermi
#-> Model #2 is Constant
#-> Model #3 is 3Stream
#-> Model #4 is 5Stream

Here, the identifier is an integer index. However, in Tcl, the array identifier does not have to be an integer or a number:

set Identifiers [list first second third fourth]
set model(first)  Fermi
set model(second) Constant
set model(third)  3Stream
set model(fourth) 5Stream

foreach i $Identifiers {
  puts "The $i model is $model($i)"
}
#-> The first model is Fermi
#-> The second model is Constant
#-> The third model is 3Stream
#-> The fourth model is 5Stream

If a variable is used as an array, it can no longer be used as a normal variable unless the array is deleted using array unset <variable name>.

2.6 Conditional Branching

The if blocks execute conditional code:

set val 1
if { $val == 0 } {
   puts "val is 0"
} elseif { $val > 2 } {
   puts "val is larger than 2"
} else {
   puts "val is negative or one"
}
#-> val is negative or one

The additional elseif test condition and the default else are optional.

The main comparators are:

The switch blocks are more compact than the if blocks, but they are restricted to tests for equality:

set MODEL Fermi      
switch $MODEL {
  Constant { puts "Use constant diffusion model" }
  Fermi    { puts "Use Fermi diffusion model"}
}
#-> Use Fermi diffusion model

2.7 Logical Operators

Use logical operators to create more complex conditions:

set Vd 1.5
set Device "pMOS"
set Simulation "IV"

if { $Vd < 0.1 && $Device == "nMOS"  &&  $Simulation != "BV" } {
   puts "Simulate nMOS IdVg in linear regime"
} elseif { $Vd > 0.1 && $Device == "pMOS"  &&  ! ($Simulation == "BV") } {
   set Vd [expr -1.0*$Vd]
   puts "Simulate pMOS IdVg for Vd=$Vd"
} elseif { $Simulation == "BV" || $Device == "BJT" } {
   puts "Simulate BJT or MOS breakdown"
} else {
   puts "None of the specified conditions are met..."
}
#-> Simulate pMOS IdVg for Vd=-1.5

Using the NOT operator ! directly before () will confuse the Sentaurus Workbench preprocessor, as it is misinterpreted as the start of a Tcl preprocessing block. To avoid this, insert a space between the exclamation mark and the opening parenthesis: ! ().

2.8 Assignment

The following Tcl lists contain a vector of frequency points and corresponding current gain values (h21). In this assignment:

Use the following data:

set freqList [list 1.00e5  2.15e5  4.64e5  1.00e6  2.15e6  4.64e6  \
                   1.00e7  2.15e7  4.64e7  1.00e8  2.15e8  4.64e8  \
                   1.00e9  2.15e9  4.64e9  1.00e10 2.15e10 4.64e10 \
                   1.00e11 2.15e11 4.64e11 1e+12 ]

set h21List  [list 33.62   33.62   33.62   33.62   33.62   33.62   \
                   33.62   33.61   33.59   33.46   32.86   30.48   \
                   23.74   14.12    7.06    3.34   1.57    0.75    \
                    0.38    0.19    0.20    0.23 ]

Click to view a solution in the master file gtclsh_tcl.cmd. Search for ### 2.8.

main menu    |   module menu    |   << previous section    |   next section >>