Table of Contents Previous page Next page Index

ModelSim Documentation Bookcase

Model Technology Inc.


Tcl examples

Example 1

The following Tcl/ModelSim example for UNIX shows how you can access system information and transfer it into VHDL variables or signals and Verilog nets or registers. When a particular HDL source breakpoint occurs, a Tcl function is called that gets the date and time and deposits it into a VHDL signal of type STRING. If a particular environment variable (DO_ECHO) is set, the function also echoes the new date and time to the transcript file by examining the VHDL variable.


Note: In a Windows environment, the Tcl exec command shown below will execute compiled files only, not system commands.

(in VHDL source):

signal datime : string(1 to 28) := "                            ";# 28 spaces 

(on VSIM command line or in macro):

proc set_date {} {
	global env
	set do_the_echo [set env(DO_ECHO)]
	set s [exec date]
	force -deposit datime $s
	if {do_the_echo} {
		echo "New time is [examine -value datime]"
	}
}
bp src/waveadd.vhd 133 {set_date; continue}        
				--sets the breakpoint to call set_date 

This is an example of using the Tcl while loop to copy a list from variable a to variable b, reversing the order of the elements along the way:

set b ""
set i [expr[llength $a]-1]
while {$i >= 0} {
		lappend b [lindex $a $i]
		incr i -1
} 

This example uses the Tcl for command to copy a list from variable a to variable b, reversing the order of the elements along the way:

set b ""
for {set i [expr [llength $a] -1]} {$i >= 0} {incr i -1} {
	lappend b [lindex $a $i]
} 

This example uses the Tcl foreach command to copy a list from variable a to variable b, reversing the order of the elements along the way (the foreach command iterates over all of the elements of a list):

set b ""
foreach i $a {
	set b [linsert $b 0 $i]
} 

This example shows a list reversal as above, this time aborting on a particular element using the Tcl break command:

set b ""
foreach i $a {
	if {$i = "ZZZ"} break
	set b [linsert $b 0 $i]
} 

This example is a list reversal that skips a particular element by using the Tcl continue command:

set b ""
foreach i $a {
	if {$i = "ZZZ"} continue
	set b [linsert $b 0 $i]
} 

The last example is of the Tcl switch command:

switch $x {
	a {incr t1}
	b {incr t2}
	c {incr t3}
} 

Model Technology Inc.
Model Technology Incorporated
Voice: (503) 641-1340
Fax: (503)526-5410
www.model.com
sales@model.com
Table of Contents Previous page Next page Index

ModelSim Documentation Bookcase