# Quartus® II Tcl Example: Automatic Script Execution

By

Beginning with version 4.0 of the Quartus® II software, you can configure scripts to run automatically at various points during compilation. You can use this capability to automatically run scripts that perform custom reporting, make specific assignments, and perform many other tasks.

There are three global assignments that control the automatic execution of scripts. They are listed here, along with when they cause a script to run.

•	PRE_FLOW_SCRIPT_FILE - before a flow starts
•	POST_MODULE_SCRIPT_FILE - after a module finishes
•	POST_FLOW_SCRIPT_FILE - after a flow finishes

The POST_FLOW_SCRIPT_FILE and POST_MODULE_SCRIPT_FILE assignments are supported beginning in version 4.0, and the PRE_FLOW_SCRIPT_FILE assignment is supported beginning in version 4.1.

A module is a Quartus® II executable that performs one step in a flow. For example, two modules are analysis & synthesis (quartus_map) and timing analysis (quartus_tan).

A flow is a series of modules that the Quartus® II software executes with predefined options. For example, compiling a design is a flow that typically consists of the following steps (performed by the indicated module):

1. Analysis and synthesis (quartus_map)
2. Fitter (quartus_fit)
3. Assembler (quartus_asm)
4. Timing Analyzer (quartus_tan)

Other flows are described in the help for the execute_flow Tcl command. Also, most commands in Start (Processing menu) in the Quartus® II GUI correspond to flows.

## Making the Assignment

To make an assignment to automatically run a script, make an assignment with the following form:

set_global_assignment -name <assignment name> <executable>:<script name>

The assignment name is one of the following:

•	PRE_FLOW_SCRIPT_FILE
•	POST_MODULE_SCRIPT_FILE
•	POST_FLOW_SCRIPT_FILE

The executable is the name of a Quartus® II command-line executable that includes a Tcl interpreter.

•	quartus_cdb
•	quartus_sh
•	quartus_sim
•	quartus_stp
•	quartus_tan

The script name is the name of your Tcl script.

## Script Execution

The Quartus® II software executes the scripts as shown here:

<executable> -t <script name> <flow or module name> <project name> <revision name>

The first argument passed in the quartus(args) variable is the name of the flow or module being executed, depending on the assignment you use. The second argument is the name of the project, and the third argument is the name of the revision.

When you use the POST_MODULE_SCRIPT_FILE assignment, the specified script is automatically run after every executable in a flow. You can use a string comparison with the module name (the first argument passed in to the script) to isolate script processing to certain modules.

## Execution Example

This example illustrates how automatic script execution works in a complete flow, assuming you have a project called top with a current revision called rev_1, and you have the following assignments in the Quartus® II Settings File (QSF) for your project:

set_global_assignment -name PRE_FLOW_SCRIPT_FILE quartus_sh:first.tcl
set_global_assignment -name POST_MODULE_SCRIPT_FILE quartus_sh:next.tcl
set_global_assignment -name POST_FLOW_SCRIPT_FILE quartus_sh:last.tcl

When you compile your project, the PRE_FLOW_SCRIPT_FILE assignment causes the following command to be executed before compilation begins:

quartus_sh -t first.tcl compile top rev_1

Next, the Quartus® II software starts compilation with analysis and synthesis, performed by the quartus_map executable. After the analysis and synthesis finishes, the POST_MODULE_SCRIPT_FILE assignment causes the following command to be executed:

quartus_sh -t next.tcl quartus_map top rev_1

Then, the Quartus® II software continues compilation with the fitter, performed by the quartus_fit executable. After the fitter finishes, the POST_MODULE_SCRIPT_FILE assignment causes the following command to be executed:

quartus_sh -t next.tcl quartus_fit top rev_1

Corresponding commands are executed after the other stages of the compilation. Finally, after the compilation is over, the POST_FLOW_SCRIPT_FILE assignment causes the following command to be executed:

quartus_sh -t last.tcl compile top rev_1

## Controlling Processing

The POST_MODULE_SCRIPT_FILE assignment causes a script to be executed after every module. Because it is the same script executed after every module, you may need to include some conditional statements that restrict processing in your script to certain modules.

For example, if you have a script that you want to run only after timing analysis is performed, you should include a conditional test like the one in the following example. It checks the flow or module name passed as the first argument to the script and executes code when the module is quartus_tan.

set module [lindex $quartus(args) 0] if [string match "quartus_tan"$module] {

# Include commands here that are run
# after timing analysis
post_message "Running after timing analysis"
}



## Displaying Messages

Because of the way the Quartus II software automatically runs the scripts, you need to use the post_message command to display messages, instead of the puts command. This requirement applies only to scripts that are run by the three assignments listed at the top of this page.