can I run unix shell command in the ModelSim shell?

can I run unix shell command in the ModelSim shell?

Post by clinton__b » Sun, 20 Feb 2005 03:31:55

Just wonder if I can run unix shell command in the Modelsim shell,
expecially those time/date command?


can I run unix shell command in the ModelSim shell?

Post by Tim Hubber » Sun, 20 Feb 2005 04:36:37

ModelSim uses Tcl as its scripting language so (almost) anything you can
do from Tcl can be done in ModelSim. Tcl has a variety of shell and file
interaction commands available. Of course, if you want to execute Unix
commands, you need to be running on a Unix (or Cygwin) system. Note that
Tcl commands are NOT directly accessible from VHDL.

If all you want is time and date, this Tcl command will return it in a
human-readable format:

clock format [clock seconds]

I suggest you go to
Tim Hubberstey, P.Eng. . . . . . Hardware/Software Consulting Engineer
Marmot Engineering . . . . . . . VHDL, ASICs, FPGAs, embedded systems
Vancouver, BC, Canada . . . . . . . . . . .


can I run unix shell command in the ModelSim shell?

Post by Jonathan B » Sun, 20 Feb 2005 07:04:30

Tim Hubberstey's advice is good - Tcl provides most of what
you are likely to need, and it's platform-independent.

However, if you really must execute native operating-system
commands from within your ModelSim window, find out about
the Tcl "exec" command. Very much simplified (because
"exec" has a vast range of options relating to file
redirection, process control and so on), here's the deal:

1) If you want to execute a system command from
the ModelSim Tcl environment:

VSIM> exec some_command argument1 argument2

Tcl will ask the operating system to execute "some_command"
just as if you had typed it, and its arguments, at the
OS command prompt. The big difference, however, is that
any output from this command that would have gone to
the standard output channel (the terminal) is instead
captured, and the whole of this captured output is then
returned to Tcl as the result of the exec command. So,
for example, if you want to find out the contents of
a file, you could do the following...

VSIM> set file_contents [exec cat somefile.txt]

And now the whole of the file is available in the
Tcl variable called "file_contents".

(Please don't do this - it's a really stupid way
to read a file from Tcl!)

2) But there's a short cut...

If you simply type any old operating system command
at the ModelSim Tcl command prompt...

VSIM> cat junk.vhd

then it will work!!!!

It's very far from obvious why this is so, but it's
extremely convenient. What's happening is this...

- you type the Unix command
- Tcl detects that it's not a built-in Tcl or ModelSim
- First, Tcl checks if it's a unique abbreviation of
a built-in command (for example, "vsi" would match
the "vsim" command). If so, it assumes that you
meant the appropriate command, and does that.
- If this abbreviation check fails, then Tcl tries
instead to get the operating system to execute it.
- Finally, if all this fails, Tcl throws an error.

Consequently, if you type the "cat" command, Tcl will
ultimately get the OS to run "cat" for you.

Note, though, that this is not a very robust mechanism.
First, it works ONLY at the interactive command line,
and NOT from within a Tcl script. Second, it can
easily be subverted by existing Tcl commands. For
example, if you try to do "ls", Tcl will recognise
that it is an *ambiguous* abbreviation of several
different Tcl commands such as lsort, lset, lsearch
and it will throw an error. And if you type an OS
command which is also a Tcl command - for example
"time" - then the Tcl command will of course take

So, the m *** of the story is:
- learn some Tcl - it's easy, convenient and powerful;
- learn about the Tcl exec command so that you can
reliably invoke OS commands on the rare occasions
that you need them.

Jonathan Bromley