Tclug 2007 TCL Command

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

dc_shell> set buf_name lsi_10k/B1I

This command sets the variable buf_name to the value lsi_10k/B1I.


dc_shell> source load_vpna.tcl
This command causes dc_shell to process the script file load_vpna.tcl.
The syntax for a command is command ?argument_list?
The command syntax becomes command cmd_options ?argument_list?
Commands are terminated by new-line characters or semicolons. For example,
set ReportFile netAttr.rpt; set Clean 0; set NumBins 20
When you enter a long command, you can split it across more than one line by using the
backslash character (\). For example,
set physical_library \
/remote/olympia/psyn/db/pdb/physical.pdf
You use the * character to match any sequence of characters in an object. For example, u*
indicates all objects that begin with the letter u, and u*z indicates all objects that begin with
the letter u and end in the letter z.
You use the ? character to match any single character. For example, u? indicates all object
names exactly two characters in length that begin with the letter u.
For example, the following command lists the last five executed commands:
dc_shell> history info 5
To reexecute the last command, enter
dc_shell> !!
To reexecute the command whose event number is 6, enter
dc_shell> !6
dc_shell> echo -help
dc_shell> set total_cells 0 =>0
dc_shell> incr total_cells =>1
You can redirect this output by using the redirection operator (>)
dc_shell> incr total_cells > file_name
To both redirect the incremented command status to a file and display it, enter
dc_shell> redirect -tee file_name {incr total_cells} =>3
dc_shell> echo "Have a good day." => Have a good day
The following example shows how to use puts in its simplest form:
dc_shell> puts "Have a good day." => Have a good day.
Command substitution: You can use the result of a command in another command (nested
commands) by enclosing the nested command in square brackets ([]).
dc_shell> set a [expr 24 * 2]
Variable substitution: You can use variable values in commands by using the dollar sign
character ($) to reference the value of the variable.
dc_shell> set a 24 24
dc_shell> set b [expr $a * 2] 48
Backslash (\) substitution: You use backslash substitution to insert special characters, such
as a new line, into text.
dc_shell> echo "This is line 1.\nThis is line 2."
This is line 1.
This is line 2.
You can also use backslash substitution to disable special characters when weak quoting is
used
You use quoting to disable the interpretation of special characters (for example, [], $, and
;). You disable command substitution and variable substitution by enclosing the
arguments in braces ({}); you disable word and line separators by enclosing the
arguments in double quotation marks ("").
dc_shell> echo {[expr $b - $a]} evaluates to [expr $b - $a]
[expr $b - $a] evaluates to 5
Double quotation marks specify weak quoting. Weak quoting disables word and line separators
while allowing command, variable, and backslash substitution.
dc_shell> echo "A is $A; B is $B.\nNet is [expr $A - $B]."
A is 10; B is 4.
Net is 6.
Tcl Special Characters
$ Used to access the value of a variable.
( ) Used to group expressions.
[ ] Denotes a nested command.
"" Denotes weak quoting. Nested commands and variable substitutions still occur.
{ } Denotes rigid quoting. There are no substitutions
; Ends a command.
# Begins a comment.
In Tcl, all variables are strings. However, Tcl does recognize variables whose values
represent integer and real numbers
dc_shell> set c1 U1; set c2 U2 U2
dc_shell> append c1 " " $c2 U1 U2
dc_shell> incr b 11
dc_shell> incr b -6 5
The default increment value for incr is 1. An error message is displayed if you try to
increment a variable that is not an integer.
to see whether the variable total_cells exists, enter
dc_shell> info exists total_cells
The info exists command returns 1 if the variable exists, and it returns 0 otherwise.
To reference the value of an environment variable, use $env(ENV_VAR_NAME). For
example, you can view the value of the HOME environment variable by entering
dc_shell> echo $env(HOME)
dc_shell> source mysession.tcl
dc_shell> source -echo -verbose myrun.tcl
You can also save the execution results to an output file by using the pipe operator (>). For
dc_shell> source -echo -verbose myrun.tcl > myrun.out
A string is a sequence of characters. Tcl treats command arguments as strings and returns
command results as strings.
For example, to compare two strings you use the compare option as follows:
string compare string1 string2
To convert a string to all uppercase characters, you use the toupper option as follows:
string toupper string
Tcl Commands to Use With Strings
format Formats a string.
regexp Searches for a regular expression within a string.
regsub Performs substitutions based on a regular expression.
scan Assigns fields in the string to variables.
string Provides a set of string manipulation functions.
subst Performs substitutions.
A list is an ordered group of elements; each element can be a string or another list. You can
create a simple list by enclosing the list elements in double quotation marks ("") or braces ({}) or
by using the Tcl list . You must delimit list elements with spaces— do not use commas.
set D_pins "I1/FF3/D I1/FF4/D I1/FF5/D"
set D_pins {I1/FF3/D I1/FF4/D I1/FF5/D}
set D_pins [list I1/FF3/D I1/FF4/D I1/FF5/D]
set compound_list [list {x y} {1 2.5 3.75 4} {red green blue}]
To access a specific element in a simple or compound list, you use the lindex command.
dc_shell> echo [lindex $compound_list 1] 1 2.5 3.75 4
Note that lindex is zero based.
dc_shell> set b {c d $a [list $a z]} c d $a [list $a z]
dc_shell> set b [list c d $a [list $a z]] c 5 {5 z}
Tcl Commands to Use With Lists
concat Concatenates lists and returns a new list.
join Joins elements of a list into a string.
lappend Appends elements to a list.
lindex Returns a specific element from a list.
linsert Inserts elements into a list.
list Returns a list formed from its arguments.
llength Returns the number of elements in a list.
lrange Extracts elements from a list.
lreplace Replaces a specified range of elements in a list.
lsearch Searches a list for a regular expression.
lsort Sorts a list.
split Splits a string into a list.
Tcl uses associative arrays. This type of array uses arbitrary strings, which can include
numbers, as its indices. The associative array is composed of a group of elements where each
element is a variable with its own name and value
dc_shell> set vio_rpt_ext(ir_drop) .volt .volt
dc_shell> array size vio_rpt_ext 3
dc_shell> array names vio_rpt_ext curr curr_dens ir_drop
dc_shell> set p 5
dc_shell> set a [expr (12*$p)] 60
Tcl Operators

Syntax Description Operand types

-a Negative of a int, real


!a Logical NOT: 1 if a is zero, 0 otherwise int, real
~a Bitwise complement of a int

a*b Multiply a and b int, real


a/b Divide a by b int, real
a%b Remainder after dividing a by b int

a+b Add a and b int, real


a-b Subtract b from a int, real

a<<b Left-shift a by b bits int


a>>b Right-shift a by b bits int

Syntax Description Operand types

a<b 1 if a is less than b, 0 otherwise int, real, string


a>b 1 if a is greater than b, 0 otherwise int, real, string
a<=b 1 if a is less than or equal to b, 0 otherwise int, real, string
a>=b 1 if a is greater than or equal to b, 0 otherwise int, real, string
a==b 1 if a is equal to b, 0 otherwise int, real, string
a!=b 1 if a is not equal to b, 0 otherwise int, real, string

a&b Bitwise AND of a and b int


a^b Bitwise exclusive OR of a and b int
a|b Bitwise OR of a and b int

a&&b Logical AND of a and b int, real


a||b Logical OR of a and b int, real

a?b:c If a is nonzero, then b, else c a: int, real


b, c: int, real,
string

An if command requires two arguments; in addition, it can be extended to contain elseif


and else arguments.
if {$x == 0} {
echo "Equal"
} elseif {$x > 0} {
echo "Greater"
} else { echo
"Less"
}
The while command has the following form: while {expression} { script }
set p 0
while {$p <= 10} {
echo "$p squared is: [expr $p * $p]"; incr p
}
For loop The for command has the following form:
for {initial_expr} {termination_expr} {reinit_expr}{ script }
for {set p 0} {$p <= 10} {incr p} {
echo "$p squared is: [expr $p * $p]"
}
The foreach command executes a script for each element in the specified list. Before
executing the script, foreach sets var to the value of the next element in the list.
foreach var $somelist{ script }
dc_shell> set mylist {I1/FF3/D I1/FF4/D I1/FF5/D}
I1/FF3/D I1/FF4/D I1/FF5/D
The break and continue commands change the flow of the while, for, and foreach
commands. The break command causes the innermost loop to terminate. The continue
command causes the current iteration of the innermost loop to terminate.
foreach f [which {VDD.ave GND.tech p4mvn2mb.idm}]
{
echo -n "File $f is "
if { [file isdirectory $f] == 0 } {
echo "NOT a directory"
} else {
echo "a directory"
break
}
}
set p 0
while {$p <= 10} {
if {$p % 2} {
incr p
continue
}
echo "$p squared is: [expr $p * $p]"; incr p
}

The switch command has the following form:


switch $s {pat1 {scripta} pat2 {scriptb} pat3 {scriptc}...}
# Tabulates files by their type
set fnames [glob *.em *.volt *.current]
set curr_ct 0; set em_ct 0; set volt_ct 0
foreach f $fnames {
set f_ext [file extension $f]
switch $f_ext {
.current {incr curr_ct}
.em {incr em_ct}
.volt {incr volt_ct}
}
}
echo "There are $curr_ct current files."
The switch command also has three options, -exact, -glob, and -regexp, that affect
how pattern matching is handled.
The cd and pwd commands are equivalent to the operating system commands with the same
name. You use the cd command to change the current working directory and the pwd
command to print the full path name of the current working directory
You use the file and glob commands to retrieve information about file names and to
generate lists of file names.
File Command Option Samples

File command and option Description

file dirname fname Returns the directory name part of a file name.

file exists fname Returns 1 if the file name exists, 0 otherwise.

file extension fname Returns the extension part of a file name.

file isdirectory fname Returns 1 if the file name is a directory, 0 otherwise.

file isfile fname Returns 1 if the file name is a file, 0 otherwise.

file readable fname Returns 1 if the file is readable, 0 otherwise.

file rootname fname Returns the name part of a file name.

file size fname Returns the size, in bytes, of a file.

file tail fname Returns the file name from a file path string.

file writable fname Returns 1 if the file is writable, 0 otherwise.

You use the glob command to generate a list of file names that match one or more patterns. The
glob command has the following form: glob pattern1 pattern2 pattern3 ...
The following example generates a list of .em and .volt files located in the current directory:
set flist [glob *.em *.volt]
You use the open, close, and flush commands to set up file access.
set f [open VDD.em w+]
close $f
You use the flush command to force buffered output to be written to a file.
flush $fid
You use the gets command to read a single line from a file and the puts commands to write
a single line to a file.
# Write out a line of text, then read it back and print
it set fname "mytext.txt"
# Open file, then write to it
set fid [open $fname w+]
puts $fid "This is my line of text."
close $fid
# Open file, then read from it
set fid [open $fname r]
set data_in [gets $fid]
close $fid
# Print out data read
echo $data_in
You can use the seek, tell, and eof commands to manage nonsequential file access.
The simplest form of the seek command is seek $fid offset
The $fid argument is the file ID of the file that was obtained from an open command; the
offset argument is the number of bytes to move the access position.
You use the tell command to obtain the current access position of a file. The basic form of
the command is tell $fid
You use the eof command to test whether the access position of a file is at the end of the file.
The command returns 1 if true, 0 otherwise.
The following is a procedure example:
# procedure max returns the greater of two values
proc max {a b} {
if {$a > $b} {
return $a
}
return $b
}
You invoke this procedure as follows:
dc_shell> max 10 5
In this example, you can invoke max with two or fewer arguments. If an argument is missing,
its value is set to the specified default, 0 in this case.
#procedure max returns the greater of two values
proc max {{a 0} {b 0}} {
if {$a > $b} {
return $a
}
return $b
}
Additional arguments are placed into args as a list. The following example shows how
to use a varying number of arguments:
# print the square of at least one
proc squares {num args} {
set nlist $num
append nlist " "
append nlist $args
foreach n $nlist {
echo "Square of $n is [expr $n*$n]"
}
}
A collection is a set of design objects such as libraries, ports, and cells. You create collections
with the Synopsys get_* and all_* commands. The following example creates a collection
of all ports in a design:
dc_shell> get_ports
dc_shell> set myports [get_ports]
dc_shell> query_objects [get_ports in*] {in0 in1 in2}
dc_shell> set hc [get_cells -filter is_hierarchical==true]
The result of filter_collection is a new collection, or if no objects match the criteria, an
empty string. For example,
dc_shell> filter_collection [get_cells] is_hierarchical==true
dc_shell> add_to_collection $reports [get_ports CLOCK]
dc_shell> set cPorts [remove_from_collection [all_inputs] CLOCK] {in1}
compare_collections command to compare the contents of two collections. If the two
collections are the same, the compare_collections command returns zero.
dc_shell> compare_collection [get_cells *] [get_cells *]
To iterate over a collection, use the foreach_in_collection command
dc_shell> foreach_in_collection itr [get_cells*] \
{if {[get_attribute $itr is_hierarchical] == “true”}\
{remove_wire_load_model $itr}}
copy_collection command duplicates a collection, resulting in a new collection.
index_collection command creates a collection of one object that is the nth object in
another collection. The objects in a collection are numbers 0 through n-1.
dc_shell> query_objects [index_collection $c1 0] {u1}
The DC_rpt_cell procedure lists all cells in a design and reports if a cell has the following
properties: Is a black box (unknown), Has a don’t touch attribute, Has a DesignWare attribute,
Is hierarchical, Is combinational, Is a test cell
proc DC_rpt_cell args {
suppress_message UID-101
echo " "
echo [format "%-32s %-14s %5s %11s" "Cell" "Reference" "Area" "Attributes"] echo
"-----------------------------------------------------------------"
} elseif {[string match -t* $option]} {
set option "-total_only"
echo ""
set cd [current_design]
echo "Performing cell count on [get_object $cd] . .
." echo " "
} elseif {[string match -h* $option]} {
set option "h"; # hierarchical only
echo ""
set cd [current_design]
echo "Performing hierarchical cell report on [get_object $cd] . . ."
echo " "
echo [format "%-36s %-14s %11s" "Cell" "Reference" "Attributes"]
echo "-----------------------------------------------------------------"
} else { echo
" "
echo " Message: Option Required (Version - December 2002)"
echo " Usage: DC_rpt_cell \[-all_cells\] \[-hier_only\] \[-total_only\]"
echo " "
return
}
# create a collection of all cell
objects set all_cells [get_cells -h *]

foreach_in_collection cell $all_cells {


incr total_cells

set cell_name [get_attribute $cell full_name] set


dt [get_attribute $cell dont_touch]
}

You might also like