Forum Discussion

Ryry013's avatar
Ryry013
Icon for New Contributor rankNew Contributor
1 year ago
Solved

[Modelsim > Execute tcl (.do) macro] How to get the location of the currently running script

I am trying to use the "Tools > tcl > Execute Macro" option in ModelSim to run a "testbench.do" script to run a simulation. However, I can't find a way to get the location of the script file in the c...
  • Ryry013's avatar
    1 year ago

    I wanted to try to avoid the user having to manually input via text a file path to the script to source it in the console window. The "Tools > tcl > Execute Macro" option was very convenient. It worked IF I did "File > Change Directory", but I wanted a way without making the user have to do that either.

    I found a way to do what I wanted:

    set script_frame [info frame [ expr [info frame] - 1]] 
    set my_cmd [dict get $script_frame cmd]
    set src_dir [ file dirname [ file normalize [ string range $my_cmd 3 end ] ] ]
    cd $src_dir
    cd ../lite_project/simulation/modelsim

    - "Info frame" lists the number of frames (it was 12 for me)

    - "Info frame 11" (the one before the last frame) contained the "do" command for my script

    - So I had to just pull out the path from the "do" command with "string range", and I got my path.

    Now it works right immediately on startup of ModelSim and the script runs perfectly.

    Thanks,

    Ryan