Checking Validity of Variables - 2023.1 English

Vivado Design Suite User Guide: Using Tcl Scripting (UG894)

Document ID
UG894
Release Date
2023-06-14
Version
2023.1 English

When developing Tcl scripts, it is recommended to always check for corner cases and for conditions where the code could fail. By doing the proper checks, it is possible to inform the user of issues and/or incorrect usage of the script. If the checks are not correctly performed, then the script could stop without informing the user about what went wrong and therefore what should be corrected.

Example 1: Check when a file is opened for read/write (unsafe version).

if {[file exists $filename]} {
  set FH [open $filename r]
  if {$FH != {}} {
    # The file is opened, do something
    # …
      close $FH
  } else {
    puts " File $filename could not be opened"
  }
} else {
  puts " File $filename does not exist"
}

Although the above script seems algorithmically correct, it would not work properly as the open command generates a low-level Tcl error (TCL_ERROR) that would stop the execution of the script in the event the file could not be opened. Later, in Example 3 we will see how this script can be improved.

Example 2: Check that the Vivado objects are valid after using the get_* commands.

proc get_pin_dir { pinName } {
 if {$pinName == {}} {
   puts " Error - no pin name provided"
   return {}
 }
 set pin [get_pins $pinName]
 if {$pin == {}} {
   puts " Error - pin $pinName does not exist"
   return {}
 }
 set direction [get_property DIRECTION $pin]
 return $direction
}

It is especially important to check that Vivado objects do exist after using the get_* commands when those objects are used inside other commands (filter, get_*, …).