Module 57 min

Procedures and Reuse

Wrapping logic into proc

A procedure (proc) is a reusable command you define yourself. Procedures are how a messy script becomes a clean, repeatable flow.

Defining and calling

tcl
proc report_slack {endpoint} {
  set slack [get_attribute [get_timing_paths -to $endpoint] slack]
  if {$slack < 0} {
    puts "FAIL $endpoint : $slack"
  } else {
    puts "PASS $endpoint : $slack"
  }
  return $slack
}

# Call it like any command
report_slack [get_pins core/data_reg*/D]

Arguments and defaults

tcl
proc setup_clock {name period {uncertainty 0.05}} {
  create_clock -name $name -period $period [get_ports $name]
  set_clock_uncertainty $uncertainty [get_clocks $name]
}
setup_clock clk 1.25          ;# uses default uncertainty
setup_clock refclk 20.0 0.2   ;# overrides it

Why it matters in flows

  • Write a constraint or report routine once, call it for every clock or block.
  • Fix a bug in one place instead of in ten copied-and-pasted lines.
  • Build a small library of helpers you reuse across projects.
Pro tip

variables inside a proc are local by default; they do not leak out, and the proc cannot see outer variables unless you pass them in or use global / upvar. That isolation is a feature: it keeps procedures self-contained and predictable.