Skip to main content

Access Tcl Global Variables from a Sourced File

When you read an SDC file from the command line, it works fine. When you read it using a Tcl procedure, it won’t work, because the global variables cannot be accessed. Here’s how to fix this annoying problem.

Tcl source Works on the Command Line

Suppose you reference the global variable CLK_SKEW in SDC file clk.sdc:

# File clk.sdc, which is a Tcl script
set_clock_uncertainty [get_clocks clk] -setup $CLK_SKEW

You will have no problem when you read the SDC file on the command line using Tcl source or read_sdc:

set CLK_SKEW 15
source clk.sdc
# or alternatively,
read_sdc clk.sdc

This works because on the command line, you are in the global context.

Tcl source Fails in a Tcl Procedure

Now, suppose that you define a Tcl proc to do something fancy, like source multiple files:

proc read_multiple_sdc {sdc_file_list} {
    foreach sdc_file $sdc_file_list {
        source $sdc_file
    }
}

upon execution, this fails:

set CLK_SKEW 15
read_multiple_sdc {"clk.sdc" "design.sdc"}
Error: can't read "CLK_SKEW": no such variable

This is because global variable CLK_SKEW cannot be referenced from within the context of the procedure read_multiple_sdc.

Solutions

There are two solutions.

Declare Global Variables using Tcl global

The best solution is to declare each global variable you will reference as global:

# File clk.sdc 
global CLK_SKEW
set_clock_uncertainty [get_clocks clk] -setup $CLK_SKEW

This approach forces the author of the SDC file to think carefully about each global variable being accessed, while the other global variables remain safely hidden.

Use Tcl uplevel

Another approach is for your procedure to execute read_sdc or source using Tcl uplevel:

proc read_multiple_sdc {sdc_file_list} {
    foreach sdc_file $sdc_file_list {
        uplevel #0 source $sdc_file
    }
}

uplevel #0 means to execute the specified script in context zero, the global context. This gives the Tcl script in the SDC file access to all global variables, but it increases the risk that the SDC file might inadvertently modify a global variable. The advantage is that the SDC file requires no modification.

Background

These techniques should work with any Tcl script. The above discussion is oriented toward reading Standard Design Constraint (SDC) files, which are Tcl scripts used to describe design intent in digital IC design. SDC files are used with logic synthesis or place and route tools like Synopsys Design Compiler, Primetime and IC Compiler, Cadence RTL Compiler and SoC Encounter.

Comments

Popular posts from this blog

Would You Like to Read a Removed Post?

You have been redirected to this page because you were looking for a post that I removed. From time to time I remove a post that I think is outdated, but I could be wrong. If you want to read a deleted post, please tell me by adding a comment to this post. Describe the article by telling me the title, URL or content. Thank you and sorry for the trouble.

The Reef Lounge

When I was about four, we vacationed at the Handerly Hotel & Resort in San Diego (formerly the Stardust Motor Hotel).  That place was heaven on earth as far as I was concerned. I loved the pool. Next to the pool there was a bar, and behind the bar was an underwater theater. It was here that I saw one of the most outlandish scenes I have ever witnessed.

Avoid the Virtual Kidnapping Scam

A relative recently fell victim to the virtual kidnapping scam, where criminals demand ransom for a loved one whom they only claim to have kidnapped.  To make their claim more convincing, the perpetrators make their ransom demand when the purported kidnapping victim cannot be contacted.  They might simply know when the person is isolated and unavailable, or they might force them to go incommunicado by stealing their mobile phone.