[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 code. I want to be able to reference other files around the script file to compile them. Right now my solution is to do "File > Change Directory" and then I can use the script using the "pwd" variable. I don't want to hardcode in directory structures because I want this testbench setup to be able to work on any machine right out of the box.
I've tried reading posts about finding the location of a tcl script, like here ( https://stackoverflow.com/questions/23285360/how-to-get-path-of-current-script ), but they're not helpful. For example:
puts [ info script ] # returns nothing puts [ file normalize [ info script ] ] # returns nothing file dirname [ file normalize [ info script ] ] # "." puts $argv0 # "C:/mtitcl/vsim/vsim" file dirname [ file normalize $argv0 ] # "C:/mtitcl/vsim" file dirname [ file normalize [ info nameofexecutable ] ] # "C:/intelFPGA_lite/20.1/modelsim_ase/win32aloem"
Is there a way to get the location of my "testbench.do" file? I want to do things like `vcom -2008 -work work "$filepath/top_level_design.vhd"`. Unless I manually do "Change Directory", then something like "./top_level_design.vhd" works because it thinks the "pwd" is the ModelSim exectuable directory.
Thank you
(Using
ModelSim - INTEL FPGA STARTER EDITION 2020.1
Revision: 2020.02)
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