Tcl Automation For Synplicity® Tools

Elías Todorovich, School of Engineering, Universidad Autónoma de Madrid

Tcl (Tool Command Language) is the de-facto standard for EDA tool automation. Tcl is a scripting language designed to be extended and integrated along with other host applications such as EDA tools. It is managed as an open source project and is released for multiple platforms as free software. Tcl syntax is minimal and consequently easy to learn and use although powerful enough to write applications from basic data manipulation up to complex automation tasks [1]. Tcl adds a convenient skill for better and more productive designers. This article shows how to use Tcl to automate synthesis tasks with Synplicity tools.

The Tcl Language

In the following examples, the commands are on the left and the results after evaluating these commands on the right. These examples can be written at the console provided by any tool that has a Tcl interpreter.

Syntax and Substitution Rules

A Tcl script is simply a sequence of commands. These commands are separated by a line break or a semicolon.

A Tcl command consists of one or more words separated by a blank space. The first word is the command name, and the rest of the words are the command arguments. Every command returns a string. For example, Hello World can be written as:

puts "Hello World!"

As in all modern programming languages, functions and procedures enable modular design. However, in Tcl there are just commands, so procedures are defined using the proc command.

proc factorial x {
if $x<=1 {return 1}
expr $x*[factorial [expr $x-1]] }

Now fac is available like other native commands:

factorial 4         24

The above paragraphs summarized the basic Tcl syntax. Next, we’ll cover the substitution mechanism for Tcl variables. The syntax to substitute variables is: $varName. The set command is used to assign values to Tcl variables:

set b 56            56
set a b             b

To assign the value of b to variable a, the dollar sign must be included:

set a $b            56

The substitution can be done in any place within a word, taking into account the lexical rules in Tcl:

set a $b+$b+$b      56+56+56
set a $b.3          56.3
set a $b4           no such variable (b4)

The next step to learn Tcl is to understand the command substitution mechanism. The syntax is: [script]. The interpreter first evaluates the script, and then substitutes [script] by the evaluation result:

set b 8             8
set a [expr $b+2]   10

As with variable substitution, command substitution can be done in any place within a word. Note that as in other scripting languages (e.g. UNIX’s sh, csh, ksh, and Perl), Tcl does not have types: all the variables are strings. A specific command must be executed, expr, to evaluate an expression as a mathematical one.

Finally, quoting mechanisms need to be studied. Words are separated with blanks or semicolons except when the following quoting rules are applicable:

1. Quotation marks: avoid word separation. The Tcl interpreter considers the text between quotation marks as a single word. However, variable and command substitution is done in the first place:

set x 10                                10
set a "x is $x; y is [expr 10+10]"      x is 10; y is 20

2. Curly braces: avoid both word separation and substitution. The Tcl interpreter considers the text between curly braces as a single parameter, and defer the substitution for a later stage:

set a {[expr $b*$c]}                    a = [expr $b*$c]

3. Backslashes escape special characters like blanks:

set a word\ with\ \$\ and\ spaces       word with $ and spaces

Backslashes also escape line breaks. This is useful for continuing long commands.

Commands

Commands can be studied as they are needed and by examples. Tcl syntax is so simple because the control structures are also commands. For example, if you need to study the while command, you can customize the following loop that inverts the list a and assigns the result to the list b:

set b [list]
set i [expr [llength $a] - 1]
while {$i >= 0} {
  lappend b [lindex $a $i]
incr i -1 }

The main control commands are: if, for, switch, break, foreach, while, eval, continue,and source. Nevertheless, there are a large number of native Tcl commands for different sorts of functionalities. For example, Tcl supports lists, arrays, files, sockets, etc. The source command is essential: It evaluates a file as a Tcl script. For example you can type in a Tcl console:

source tcl_script_file.tcl

Synthesis Automation

Synplicity has integrated Tcl support in their synthesis tools. Tcl scripts can be used to control the synthesis process and run it iteratively with different options and technologies. Moreover, Tcl is used for several additional purposes: Constraint files (*.sdc), where timing constraints and synthesis directives are Tcl commands; the Synplify project file is a Tcl script; Synplify forward annotates constraint files to the place and route programs, for Altera this is a Tcl file. In this way it is possible to chain scripts to control different EDA tools passing the information by means of intermediate files. Within Tcl project files, source files, device options, and constraint files are specified..

There is an interactive Tcl window within the Project window to enter single commands or scripts. In addition, for every action in the Project window, the corresponding Tcl command is displayed in the Tcl window. This is useful to write the first version of a script. Furthermore, it is possible to record commands from the current session into a file with the recording command.

On the other hand, if you have a floating license and are running in batch mode, you can run scripts from the command line. If you have a project set up with the implementation options, type the following:

synplify -batch project_file_name.prj

With a floating licnese you can run Tcl script files in batch mode:

synplify_pro -batch tcl_script.tcl

Finally, Synplicity has a sophisticated mechanism to customize the design flow: The predefined hook (synhooks) procedures that are automatically executed as the design is synthesized. For example, user settings can be assigned to projects with syn_on_set_project_template, when a synthesis run is finished syn_on_end_run is executed, etc. For example, syn_on_end_run can send an e-mail to the user when a long run finishes.

Example

Typically, a synthesis script can run multiple implementations with different target technologies, clock frequencies, or other sets of options. For example, Xilinx Spartan3 FPGAs have four parts that use the FG456 package: XC3S400, XC3S1000, XC3S1500, and XC3S2000. It is useful to know the smallest and least expensive part the current design fits into:

set try_these { XC3S400 XC3S1000 XC3S1500 XC3S2000}
foreach part $try_these {
  set_option -part $part
  project -log_file $part.srr
  project -run
  open_file -edit_file $part.srr
}

With this information, the cheapest PCB can be produced with flexibility to allow future extended implementations as an additional benefit.

If the previous design can be specified with optional components, for example an embedded microprocessor with an optional parallel multiplier, it could be synthesized with different options. In VHDL, a constant use_mult can be defined in a package (def_pkg.vhd) in order to control this feature. These implementations can be run using nested loops:

set try_these { XC3S400 XC3S1000 XC3S1500 XC3S2000}

foreach part $try_these {
  set_option -part $part
  foreach use_mult {true false} {
    project -log_file $part$use_mult.srr
    file copy -force base_pkg.vhd def_pkg.vhd
    set outf [open def_pkg.vhd {RDWR APPEND}]
       puts $outf ""
    puts $outf "  constant use_mult: boolean := $use_mult ;"
       puts $outf "end my_package;"
    close $outf
    project -run
  }
}

References

[1] B. Welch, Practical Programming in Tcl and Tk, 4th ed. Prentice Hall, 2003.

From The Syndicated Q2, 2007, published quarterly by Synplicity, Inc., www.synplicity.com.
Copyright © 2007 Synplicity, Inc. All rights reserved.