simics> $foo = "some text" simics> $foo some text simics> echo $not_used_before 0
There is also support for indexed variables (arrays). This is useful in loops for example.
simics> $foo[0] = 10 simics> $foo[1] = 20 simics> echo $foo[0] + $foo[1] 30
CLI also has support for local variables, described later in this chapter.
simics> $address = 0
simics> set $address 20
simics> echo "The Value at address " + $address + " is " + (get $address)
The Value at address 0 is 20
simics> $id = 0
simics> ("cpu" + $id).print-time
processor                 steps             cycles    time [s]
cpu0                          0                  0         0.0
simics> $cpu = cpu0
simics> $cpu.print-time
processor                 steps             cycles    time [s]
cpu0                          0                  0         0.0
Parenthesis can also be used to enter a multi-line command, making it easier to read scripts with nested command invocations. In the text console, the prompt will change to ....... for code spanning more than one line.
simics> (echo 10 ....... + (20 - 5) ....... + (max 4 7)) ....... 32
simics> $value = 10
simics> if $value > 5 { echo "Larger than five!" }
Larger than five!
The if statement has a return value:
simics> $num_cpus = 2
simics> (if $num_cpus > 1 { "multi" } else { "single" }) + "-pro"
multi-pro
It is also possible to have else followed by another if statement.
simics> if $b == 1 {
.......     echo 10
....... } else if $b == 0 {
.......     echo 20
....... } else {
.......     echo 30
....... }
.......
20
Loops can be written with the while command.
simics> $loop = 3
simics> while $loop {
.......     echo $loop
.......     $loop -= 1
....... }
.......
3
2
1
In if and while statements, it can be useful to have variables that are local to the scope and thus do not collide with the names of global variables. By adding local before the first assignment of a variable, the variable is made local.
simics> $global = 10
simics> if 1 > 0 {
.......     local $global = 20
.......     echo $global
....... }
....... echo $global
20
10
simics> phys_mem.set 0 0xffffffff 4 simics> phys_mem.get 0 4 4294967295 simics> signed32 (phys_mem.get 0 4) -1
simics> echo "Will switch cpu every " + (sim->cpu_switch_time) + " cycles" Will switch cpu every 1000000 cycles
A simple example from a Simics script:
script-branch {
    echo "This is a script branch test - going to sleep."
    cpu0.wait-for-step 10
    echo "Processor registers after 10 steps:"
    cpu0.pregs
}
The example above will execute the first echo command at once, and then go to sleep waiting until the first 10 instructions (steps) have run. When the step counter for cpu0 has reached 10, the branch will wake up and run the next two commands, echo and pregs.
Some commands can not be run while Simics is executing. One example is the write-configuration command. To issue such commands from a script branch, it is possible to stop the execution, issue the command and then resume the simulation. The following is an example that writes a checkpoint when the simulation reaches a login prompt, and then continues running. It assumes that a text-console called con0 is used.
script-branch {
    con0.wait-for-string login
    stop
    write-configuration login.conf
    run
}
script-branch {
    wait-for-hap Core_Exception info
    echo "Processor " + $info[0] + " got exception " + $info[1]
}
When a script branch is started (using script-branch), it begins executing immediately, and runs until a wait-for-, command is issued. Execution is then resumed in the main script; i.e., there is never any concurrent activity. When a hap, or some other activity, occurs that a script branch is waiting for, the branch continues executing once the currently simulated instruction is ready.
The following example
script-branch {
    $foo = 20
    cpu0.wait-for-step 10
    echo "foo is " + $foo
}
$foo = 15
run
will produce the output foo is 15 while the following script will
print foo is 20.
script-branch {
    local $foo = 20
    cpu0.wait-for-step 10
    echo "foo is " + $foo
}
$foo = 15
run
$id = (script-branch {
    wait-for-variable trigger
})
...
simics> interrupt-script-branch $id
Command 'wait-for-variable' interrupted.
Script branch 1 interrupted.
There are some limitations to script branches. The first two in the list are enforced by Simics: