TCL Ece720spr13 - 18

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

ECE 720 – ESL & Physical Design

Lecture 18:
Tcl Scripting
Spring 2013
W. Rhett Davis
NC State University

© W. Rhett Davis NC State University Slide 1 ECE 720 Spring 2013


Announcements

 Homework #6 Due in 1 Week

© W. Rhett Davis NC State University Slide 2 ECE 720 Spring 2013


Today’s Lecture

 Tcl Scripting

 Using Tcl with Synopsys PrimeTime

© W. Rhett Davis NC State University Slide 3 ECE 720 Spring 2013


Tcl History
 Tcl – Tool Command Language
 (1987) created by John Ousterhout of UC Berkeley
(creator of Magic, IRsim)
 Intended as a generic command languages for tools like
Magic, Irsim
 Distributed under a BSD-style open-source license
(similar to Magic, BSD-Unix, SPICE)
 Syntax derived from UNIX shells
 (1999) Tcl 8.1 released (first open-source release)
 Around that time, Synopsys Design Compiler started
supporting a "Tcl mode". Most other EDA products
soon followed.

© W. Rhett Davis NC State University Slide 4 ECE 720 Spring 2013


Tcl Introduction
 To start interactively:
» tclsh
» type "exit" to exit
 To run a script:
» tclsh myscript.tcl
 General command format is "command [arg] …"
 Commands are newline-terminated
 Comments indicated by “#” at beginning of line
 Line continuation indicated by “\” at end of line
 Tcl/Tk is also very popular for graphical applications
» start with "wish" instead of "tclsh"
» not used in this class

© W. Rhett Davis NC State University Slide 5 ECE 720 Spring 2013


Variables, Output, & Math
 Create variables or set their value
with the set command

% set buf Hello


 Print to stdout with puts command Hello
% puts $buf
Hello
 Reference variables with "$"
% puts World!
World!
 Evaluate expressions with expr % set a 1
1
command
% set b 2
2
 Note that Tcl, like Perl, is "weakly % expr $a + $b
typed," unlike Python, SKILL 3

© W. Rhett Davis NC State University Slide 6 ECE 720 Spring 2013


Grouping Arguments with ""
 Arguments can be grouped between
double-quotes ""
 Variables are evaluated
 Special string characters are also
evaluated % set buf Hello
 Use backslash \ Hello
% puts "$buf \nWorld!"
to escape the Hello
$ and " operators World!
% puts "\$buf = \"$buf\""
$buf = "Hello"

© W. Rhett Davis NC State University Slide 7 ECE 720 Spring 2013


Grouping Arguments with {}
 Arguments can also be grouped between curly-
braces {}
 Variables, special % set buf Hello
Hello
characters are not % puts {$buf \nWorld!}
evaluated $buf \nWorld!
% puts {Hello World!}
 Newline is captured Hello World!
as part of the group % puts {Hello
World!}
Hello
World!
ERROR! % puts Hello World!
sending two arguments can not find channel
when only one is intended! named "Hello"

© W. Rhett Davis NC State University Slide 8 ECE 720 Spring 2013


Grouping Arguments with [ ]
 Arguments can also be
% puts "1 + 2" grouped between
1 + 2 square-brackets [ ]
% puts [expr 1 + 2]
3  Command is executed,
% puts [ and the result is
set a 1
set b 2 substituted for the
expr $a + $b argument group
]
3
NOTE: Newline is captured,
meaning that you can execute
multiple commands,
but this is not commonly done
© W. Rhett Davis NC State University Slide 9 ECE 720 Spring 2013
Nesting Argument Groups
 Arguments can be nested within each other
without limit
 No evaluation is performed within a { } group
 "" and [ ] groups are evaluated within each other
» Remember that you can avoid this behavior with \[, \",
and \$
% puts {[expr 1 + 2]}
[expr 1 + 2]
% puts "[expr 1 + 2]"
3
% puts "[expr [expr 1 + 2] + 2]"
5

© W. Rhett Davis NC State University Slide 10 ECE 720 Spring 2013


Control Structures
 { } Groups are sometimes interpreted as blocks of code
 In this case, the first
% set b 2
argument to the "if"
2
command is an % if {$b > 1} then {
expression that will puts Greater
be evaluated with the puts "b = $b"
expr command }
Greater
 The second argument b = 2
must be "then"
 The third argument is a list of commands
 NOTE: We could also use "" for grouping, but this would
be very confusing
 See also: switch, while, for (tutorial sections 10-13)
© W. Rhett Davis NC State University Slide 11 ECE 720 Spring 2013
Lists in Tcl
 Argument groups can be interpreted as lists
 Elements must be separated by a white-space
 Use llength
command to % set list1 {a b c d}
a b c d
get length % llength $list1
 Use lindex 4
% set list2 {{x1 y1} {x2 y2}}
command {x1 y1} {x2 y2}
to index % lindex $list2 1
{x2 y2}
elements % lindex [lindex $list2 1] 0
 Note that they x2
can be nested
© W. Rhett Davis NC State University Slide 12 ECE 720 Spring 2013
More List Commands
 list – compose a list by evaluating elements
 split – compose a list by splitting a string
 concat – compose a list by concatenating two lists
 lappend – add an element to the end of a list
 linsert, lreplace, lset – modify list elements
 lsearch – search for a list element
 lsort – sort the list
 lrange – return a subset of a list
 See sections 17-19 of the Tcl Tutorial for more
details
© W. Rhett Davis NC State University Slide 13 ECE 720 Spring 2013
foreach
 foreach command is one of the most useful
» allows iteration over all elements in a list
% set days {Sun Mon Tue Wed Thu Fri Sat}
Sun Mon Tue Wed Thu Fri Sat
% foreach day $days {puts $day}
Sun
Mon
Tue
Wed
Thu
Fri
Sat

© W. Rhett Davis NC State University Slide 14 ECE 720 Spring 2013


UNIX Commands in Tcl
 Because Tcl borrows syntax from UNIX, many
UNIX commands are borrowed as well
» source, echo, ls, more, etc.
» Output redirection to a file
 NOTE: Not all Tcl interpreters support all
commands
% echo Hello World! > README.txt
% ls
README.txt
% more README.txt
Hello World!

© W. Rhett Davis NC State University Slide 15 ECE 720 Spring 2013


Defining Procedures
 Define your own procedures with the proc command
 I don't recommend defining a lot of procedures, because
exception handling in Tcl is very poor
» when an error occurs, you usually get no feedback in your program
output
» Extremely difficult to debug
 Note: Without exit command, script execution would simply
stop without exiting when finished
proc.tcl Execution
proc doit {a b} {
puts "Get $a and $b!" % tclsh proc.tcl
} Get on down and boogie!
doit {on down} boogie Get up and go!
doit up go
exit

© W. Rhett Davis NC State University Slide 16 ECE 720 Spring 2013


info (1)
 info command very helpful for exploring the
state of the interpreter
» info tclversion – get version number
» info vars – See what variables are defined
» info commands – See what commands exist
 See sections 33-35 of Tcl Tutorial
$ info tclversion
8.4
% info vars arg*
argc argv argv0
% info commands l*
list lrange lsearch lappend llength linsert lreplace
lset load lindex lsort

© W. Rhett Davis NC State University Slide 17 ECE 720 Spring 2013


info (2)
% info procs auto*
auto_load_index auto_load auto_execok auto_qualify
auto_import
% info args auto_load
cmd namespace
% info body auto_load
global auto_index auto_oldpath auto_path
. . .

 info can be used to help when documentation is


incomplete or out-of-date
» info procs – list procedures
» info args – list arguments
» info body – dump the body of a procedure
 Doesn't work with commands (use "help" in Cadence
tools, "man" in Synopsys tools)
© W. Rhett Davis NC State University Slide 18 ECE 720 Spring 2013
Arrays & Dictionaries
 Arrays are like Dictionaries in Python
» once defined, can use the array command to querry & manipulate

% set numdays(March) 31
31
% set numdays(February) 28
28
% puts "February has $numdays(February) days"
February has 28 days
% array names numdays
February March

 Dictionaries are another type of complex variable that is


similar, but rarely used
 Arrays and Dictionaries are about the only time you will
ever see parentheses ( ) used in Tcl
 See tutorial sections 27-29 for more info
© W. Rhett Davis NC State University Slide 19 ECE 720 Spring 2013
File I/O
 Open & close files with open, close commands
» File descriptors are called "channels"

 puts, gets commands used for I/O


» first argument is assumed to be channel, if two
arguments given, stdin & stdout used, otherwise

 File output is often necessary, but I personally


avoid file input in Tcl
» Much easier to use Python to format a Tcl list, then just
use "source filename" in Tcl
© W. Rhett Davis NC State University Slide 20 ECE 720 Spring 2013
File I/O & Regular Expressions
 Regular Expression parsing is also possible with regexp
command
 Code below reads module names from a Verilog file,
writes to another

set src [open LMS_pipe.v r]


set dest [open modnames w]
while {[gets $src line] >= 0} {
if [regexp {^module\s+(\w+)} $line expr name] {
puts $dest $name
}
}
close $src
close $dest

© W. Rhett Davis NC State University Slide 21 ECE 720 Spring 2013


Child Interpreters (1)
 Tcl makes it very easy to build your own command
interpreters for C source code programs
» See Tcl Library documentation for API reference
 Below is a simple program to execute a Tcl script
 Also very simple to define a set of custom commands
and offer them in a command loop
 This is essentially the reason why Tcl is popular
#include <stdio.h>
#include <stdlib.h>
#include <tcl.h>

main(int argc, char *argv[]) {


Tcl_Interp *interp;
int code;
© W. Rhett Davis NC State University Slide 22 ECE 720 Spring 2013
Child Interpreters (2)
if (argc != 2) {
fprintf(stderr, "Wrong # arguments: ");
fprintf(stderr, "should be \"%s fileName\"\n", argv[0]);
exit(1);
}

interp = Tcl_CreateInterp();
code = Tcl_EvalFile(interp, argv[1]);
if (code != TCL_OK) {
printf("%s\n", Tcl_GetStringResult(interp));
exit(1);
}

exit(0);
}

© W. Rhett Davis NC State University Slide 23 ECE 720 Spring 2013


For More Information
 see http://www.tcl.tk → Documentation
» Tcl 8.5
– Tcl/Tk Applications – for command-line options of
the tclsh executable
– Tcl Commands – command reference
– Tcl Library – C API reference
– Tcl Tutorial – 49 section tutorial, each section is
about 1 page
» Tcl 8.5 Tutorial – another link to the tutorial
 J. Ousterhout's book Tcl and the Tk
Toolkit (mostly useful for the C interface)
© W. Rhett Davis NC State University Slide 24 ECE 720 Spring 2013
Today’s Lecture

 Tcl Scripting

 Using Tcl with Synopsys PrimeTime

© W. Rhett Davis NC State University Slide 25 ECE 720 Spring 2013


PrimeTime Introduction
 Synopsys PrimeTime is a gate-level static timing and
power analysis tool
 Analyses are accurate enough for Design Signoff, i.e.
final verification or "SPICE-Like accuracy"
 Command syntax is similar to Design Compiler, but
without Synthesis
 Where we use Tcl scripts most often at NCSU is for
static delay and power analysis of final designs
 NOTE: Cadence tools are much better integrated with
Encounter, but we're less familiar with them and don't
know how to get timing/power values we want
» I don't necessarily recommend our approach to everyone

© W. Rhett Davis NC State University Slide 26 ECE 720 Spring 2013


Collections
 Because TCL does not support complex data
types, Synopsys tools use "collections" to represent
objects or groups of objects
 Use find command to create a collection
» some commands also return collections
 Use get_attribute command to query object
attributes
» Different object classes have different attributes
» Common Object Classes: port, net, cell, pin, clock,
timing_path
 The collection variable in Tcl is merely an identifier
© W. Rhett Davis NC State University Slide 27 ECE 720 Spring 2013
get_attribute Examples
 Code below returns the min & max transition times for a
pin & port
» Note that nothing is annotated for the port
pt_shell> set pin [find pin u_logic_I1h3z4_reg/SN]
{"u_logic_I1h3z4_reg/SN"}
pt_shell> get_attribute $pin actual_rise_transition_max
0.952798
pt_shell> get_attribute $pin actual_rise_transition_min
0.915528
pt_shell> set port [find port HRESETn]
{"HRESETn"}
pt_shell> get_attribute $port actual_rise_transition_max
0.000000
pt_shell> get_attribute $port actual_rise_transition_min
0.000000

© W. Rhett Davis NC State University Slide 28 ECE 720 Spring 2013


get_timing_paths
 get_timing_paths command functions like
report_timing command, but returns a
collection of timing_path objects
 "arrival" attribute gives the arrival time (delay) at
the end of the path
 "path_type" attribute gives the type (max or min)
pt_shell> set path [get_timing_paths –from HRESETn]
_sel24
pt_shell> get_attribute $path arrival
1.084861
pt_shell> get_attribute $path path_type
max

© W. Rhett Davis NC State University Slide 29 ECE 720 Spring 2013


report_attribute
 report_attribute command
lists all attributes & values
» -application argument is needed, or you'll see nothing
pt_shell> report_attribute –application $path
...
Design Object Type Attribute Name Value
-----------------------------------------------------------
CORTEXM0DS path string object_class timing_path
CORTEXM0DS path float slack 39.695076
CORTEXM0DS path float arrival 1.084861
CORTEXM0DS path float required 40.779938
CORTEXM0DS path collection startpoint HRESETn
CORTEXM0DS path collection endpoint
u_logic_I1h 3z4_reg/SN
...

© W. Rhett Davis NC State University Slide 30 ECE 720 Spring 2013


Collection Iterators
 Collections are not Tcl lists, foreach and llength won't work
 foreach_in_collection used in place of foreach
 sizeof_collection used in place of llength
pt_shell> set path [get_timing_paths –from HRESETn –nworst 5]
_sel37
pt_shell> sizeof_collection $path
5
pt_shell> foreach_in_collection p $path {
puts [get_attribute $p arrival]
}
1.084861
1.021985
1.020240
1.019076
1.017272

© W. Rhett Davis NC State University Slide 31 ECE 720 Spring 2013

You might also like