Table of Contents Previous page Next page Index

ModelSim Documentation Bookcase

Model Technology Inc.


when

The when command allows you to instruct ModelSim to perform actions when the specified conditions are met. For example, you can use the when command to break on a signal value or at a specific simulator time (see "Time-based breakpoints" ) . Conditions can include the following HDL items: VHDL signals, and Verilog nets and registers. Use the nowhen command to deactivate when commands.

The when command uses a when_condition_expression to determine whether or not to perform the action. The when_condition_expression uses a simple restricted language (that is not related to Tcl), which permits only four operators and operands that may be either HDL item names, signame'event, or constants. ModelSim evaluates the condition every time any item in the condition changes, hence the restrictions.

With no arguments, when will list the currently active when statements and their labels (explicit or implicit).


Note: Virtual signals, functions, regions, types, etc. cannot be used in the when command.

Syntax

when

[[-label <label>] [-id <id#>] {<when_condition_expression>} {<command>}]

Arguments

-label <label>

Used to identify individual when commands. Optional.

-id <id#>

Attempts to assign this id number to the when statement. Optional. If the id number you specify is already used, ModelSim will return an error.


Note: Ids for when statements are assigned from the same pool as those used for the bp command. So, even if you haven't used an id number for a when statement, it's possible it is used for a breakpoint.

{<when_condition_expression>}

Specifies the conditions to be met for the specified <command> to be executed. Required. The condition is evaluated in the simulator kernal and can be an item name, in which case the curly braces can be omitted. The command will be executed when the item changes value. The condition can be an expression with these operators:

Name
Operator
equals
==, =
not equal
!=, /=
AND
&&, AND
OR
||, OR

The operands may be item names, signame'event, or constants. Subexpressions in parentheses are permitted. The command will be executed when the expression is evaluated as TRUE or 1.

The formal BNF syntax is:

condition ::= Name | { expression }

expression ::= expression AND relation
              | expression OR relation
              | relation 
relation ::= Name = Literal
           | Name /= Literal
           | Name ' EVENT
           | ( expression ) 
Literal ::= '<char>' | "<bitstring>" | <bitstring> 

The "=" operator can occur only between a Name and a Literal. This means that you cannot compare the value of two signals, i.e., Name = Name is not possible.

{<command>}

The command(s) for this argument are evaluated by the Tcl interpreter within the ModelSim GUI. Any ModelSim or Tcl command or series of commands are valid with one exception-the run command cannot be used with the when command. Required. The command sequence usually contains a stop command that sets a flag to break the simulation run after the command sequence is completed. Multiple-line commands can be used.


Note: If you want to stop the simulation using a when command, you must use a stop command within your when statement. DO NOT use an exit command or a quit command. The stop command acts like a breakpoint at the time it is evaluated. See "Ending the simulation with the stop command" for an example.

Examples

The when command below instructs the simulator to display the value of item c in binary format when there is a clock event, the clock is 1, and the value of b is 01100111, and then to stop.

when -label when1 {clk'event and clk='1' and b = "01100111"} {
	echo "Signal c is [exa -bin c]"
	stop}
 

The when command below is labeled "a" and will cause ModelSim to echo the message "b changed" whenever the value of the item b changes.

when -label a b {echo "b changed"} 

The multi-line when command below does not use a label and has two conditions. When the conditions are met, an echo and a stop command will be executed.

when {b = 1
	 and c /= 0 } {
	 echo "b is 1 and c is not 0"
	 stop
} 

In the example below, for the declaration "wire [15:0] a;", the when command will activate when the selected bits match a 7:

when {a(3:1) = 3'h7} {echo "matched at time" $now} 

If you encounter a vectored net caused by compiling with -fast, use the 'event qualifier to prevent the command from falsely evaluating when unrelated bits of 'a' change:

when {a(3:1) = 3'h7 and a(3:1)'event} {echo "matched at time" $now} 

Ending the simulation with the stop command

Batch mode simulations (see "How to use checkpoint/restore" ) are often structured as "run until condition X is true," rather than "run for X time" simulations. The multi-line when command below sets a done condition and executes an echo and a stop command when the condition is reached.

The simulation will not stop (even if a quit -f command is used) unless a stop command is executed. To exit the simulation and quit ModelSim, use an approach like the following:

onbreak {resume}
when {/done_condition == '1'} 
     {echo "End condition reached"
     if [batch_mode] {
          set DoneConditionReached 1
          stop 
     }
}
run 1000 us
if {$DoneConditionReached == 1} {
     quit -f
} 

Time-based breakpoints

You can build time-based breakpoints into a when statement with the following syntax.

For absolute time (indicated by @) use:

when {$now = @1750ns} {stop} 

You can also use:

when {errorFlag = '1' OR $now = 2ms} {stop} 

This example adds 2ms to the simulation time at which the when statement is first evaluated, then stops.

You can also use variables, as shown in the following example:

set time 1000
when "\$now = $time" {stop} 

The quotes instruct Tcl to expand the variables before calling the command. So, the when command sees:

when "$now = 1000" stop 

Note that "$now" has the $ escaped. This prevents Tcl from expanding the variable, because if it did, you would get:

when "0 = 1000" stop 

See also

bp, disablebp, enablebp, nowhen


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