TScript
TScript is the language of the TEMU command line.
Everything typed at the temu> prompt is TScript,
and the same commands can be saved in a .temu file and run as a script.
Scripts are the normal way to set up a machine, load software, and test models reproducibly.
This chapter writes two scripts for the tutorial project: a test of the MMIO model from Memory Mapped Registers, and a script that reacts to notifications. The TScript language reference describes the complete language.
Running Scripts
Give the script file to temu after the project:
$ temu --project=temu-project.yaml scripts/temu/mmio-test.temu
The tutorial project already uses scripts:
temu-project.yaml runs scripts/temu/imports.temu and scripts/temu/startup.temu when the project is opened,
which is how board0 and the scheduler are created.
scripts/temu is on the script search path,
so a script there can also be run from another script, or from the prompt, with exec:
temu> exec file=mmio-test.temu
When scripts are given on the command line, temu exits after running them,
unless -i (--interactive) is given.
End a script with quit to make that explicit.
The exit status tells whether the script succeeded:
a command that fails stops the script, and temu exits with a non-zero status.
This makes scripts usable as tests, for example from CTest or a CI job.
A syntax error is reported as <file>:<line>: error: …, but the commands before and after it still run,
and the exit status is 0.
Check for that message when scripts are run automatically.
|
Commands, Objects and Properties
A line is a command, followed by named arguments:
import mmio
MyModel.new name=foo
board0-mem.map iface=foo:MemAccessIface addr=0x80001000 length=256
MyModel.new is a command of the class MyModel, and board0-mem.map a command of the object board0-mem.
Properties are read and written by name:
temu> foo.a = 7
:info: foo : updating a from 0 to 7
temu> foo.a
:info: foo : reading a as 7
7
Comments start with // or # and run to the end of the line.
Variables and Expressions
Variables are assigned with = and read with $name.
Variables can also be given on the command line with -D NAME=value,
and defined() tells whether a variable exists,
which allows a script to provide a default:
// The value the CPU stores. Override with: temu ... -D VALUE=7 ...
if !defined("VALUE") {
VALUE = 42
}
$ temu --project=temu-project.yaml -D VALUE=7 scripts/temu/mmio-test.temu
Expressions are evaluated in assignments, conditions and named arguments,
and can read properties: foo.a = $i * 10 and if foo.a == 42 both work.
Arguments without a name, such as the words given to echo, are not evaluated:
echo $i + 1 prints 3+1 when i is 3.
Assign the result to a variable first, and echo that.
Strings are not interpolated either: echo "i is $i" prints i is $i.
Pass the variable as an argument of its own instead: echo "i is" $i.
A file name in an argument must be quoted when it is written out,
file="images/app.elf", but not when it comes from a variable, file=${IMAGE}.
In ${…} form, a variable can be joined with text: file=${DIR}/app.elf.
Defining Commands
defcmd defines a new command.
Its arguments are typed, here as int, and are variables inside the body:
defcmd check-a(expected: int) {
if !(foo.a == $expected) {
raise "foo.a does not have the expected value"
}
}
raise makes the command fail with a message.
When a command fails outside try, the script stops,
so check-a works as an assertion.
There is no != operator at present; write !(a == b) instead.
Variables assigned inside a defcmd are local to it.
Control Flow
while repeats a block as long as its condition is true.
The test writes three values to the register through the property and checks each:
i = 1
while $i <= 3 {
foo.a = $i * 10
check-a expected=$i * 10
i = $i + 1
}
if takes an integer condition, with optional elif and else blocks.
try runs a command without stopping the script when it fails,
and gives 1 if it succeeded and 0 if it failed.
It lets the test check that check-a rejects a wrong value:
if try check-a expected=99 {
raise "check-a accepted a wrong value"
}
The failing command still prints its error message.
Finally, the test lets the CPU store the value through the memory system, as in the MMIO chapter, and checks that it arrived:
board0-cpu.assemble inst="st %g1, [%g2 + 0]" pa=0
board0-cpu.assemble inst="ld [%g2 + 0], %g3" pa=4
board0-cpu.setReg reg="%g2" value=0x80001000
board0-cpu.setReg reg="%g1" value=$VALUE
sched.start workers=1
step cpuidx=0 steps=2
check-a expected=$VALUE
The complete script:
// A model test in TScript: the MMIO session from the "Memory Mapped
// Registers" chapter, checked by the script itself.
//
// Run from the tutorial project directory, with libmmio.so built:
// temu --project=temu-project.yaml scripts/temu/mmio-test.temu
// It fails (exit status 255) if a check does not hold.
// tag::value[]
// The value the CPU stores. Override with: temu ... -D VALUE=7 ...
if !defined("VALUE") {
VALUE = 42
}
// end::value[]
// tag::setup[]
import mmio
MyModel.new name=foo
board0-mem.map iface=foo:MemAccessIface addr=0x80001000 length=256
// end::setup[]
// tag::check-a[]
defcmd check-a(expected: int) {
if !(foo.a == $expected) {
raise "foo.a does not have the expected value"
}
}
// end::check-a[]
// tag::properties[]
i = 1
while $i <= 3 {
foo.a = $i * 10
check-a expected=$i * 10
i = $i + 1
}
// end::properties[]
// tag::try[]
if try check-a expected=99 {
raise "check-a accepted a wrong value"
}
// end::try[]
// tag::program[]
board0-cpu.assemble inst="st %g1, [%g2 + 0]" pa=0
board0-cpu.assemble inst="ld [%g2 + 0], %g3" pa=4
board0-cpu.setReg reg="%g2" value=0x80001000
board0-cpu.setReg reg="%g1" value=$VALUE
sched.start workers=1
step cpuidx=0 steps=2
check-a expected=$VALUE
// end::program[]
echo "mmio-test passed"
quit
Notifications
A command defined with defcmd can be called when a notification is raised,
as the plug-in in Listening for Notifications did with C functions.
The command is called with the arguments:
-
notification(string): the notification name. -
sender(obj): the object that raised it, when the subscription is for a specific object. -
info: the notification information, if the command declares it. Its type says how the information is read:int,real,stringorobj. For the trap notifications,info: intis the trap number.
defcmd on-trap(notification: string, sender: obj, info: int) {
echo "trap" $info "taken on" $sender
}
defcmd on-rett(notification: string, sender: obj) {
echo "rett on" $sender
}
notification-subscribe connects a command to a notification.
Without obj=, the command is called for every object that raises the notification,
and it must not declare sender.
The CPU only raises trap notifications once they are enabled with cpu-enable-trap-events:
cpu-enable-trap-events cpu=board0-cpu
notification-subscribe name=temu.cpuTrapEntry cmd=on-trap obj=board0-cpu
notification-subscribe name=temu.cpuTrapExit cmd=on-rett obj=board0-cpu
notification-unsubscribe takes the same arguments and removes the subscription:
notification-unsubscribe name=temu.cpuTrapEntry cmd=on-trap obj=board0-cpu
notification-unsubscribe name=temu.cpuTrapExit cmd=on-rett obj=board0-cpu
The complete script sets up a program that takes a software trap (ta 5) and returns from it:
// Listening for notifications from TScript: the trap entry and trap exit
// notifications, handled by commands defined in the script.
//
// Run from the tutorial project directory:
// temu --project=temu-project.yaml scripts/temu/trap-notifications.temu
// A program that takes a software trap ('ta 5', trap type 0x85) and returns
// from it. The trap table is at 0x40001000, so the handler for 0x85 is at
// 0x40001000 + 0x85 * 16 = 0x40001850.
board0-cpu.assemble pa=0x40000000 inst="ta %g0 + 5"
board0-cpu.assemble pa=0x40000004 inst="ba 0x40000004"
board0-cpu.assemble pa=0x40000008 inst="nop"
board0-cpu.assemble pa=0x40001850 inst="jmpl %l2 + %g0, %g0"
board0-cpu.assemble pa=0x40001854 inst="rett %l2 + 4"
board0-cpu.setReg reg="%tbr" value=0x40001000
board0-cpu.setReg reg="%wim" value=0
board0-cpu.setReg reg="%psr" value=0xe0
board0-cpu.setPC pc=0x40000000
// tag::handlers[]
defcmd on-trap(notification: string, sender: obj, info: int) {
echo "trap" $info "taken on" $sender
}
defcmd on-rett(notification: string, sender: obj) {
echo "rett on" $sender
}
// end::handlers[]
// tag::subscribe[]
cpu-enable-trap-events cpu=board0-cpu
notification-subscribe name=temu.cpuTrapEntry cmd=on-trap obj=board0-cpu
notification-subscribe name=temu.cpuTrapExit cmd=on-rett obj=board0-cpu
// end::subscribe[]
sched.start workers=1
step cpuidx=0 steps=4
// tag::unsubscribe[]
notification-unsubscribe name=temu.cpuTrapEntry cmd=on-trap obj=board0-cpu
notification-unsubscribe name=temu.cpuTrapExit cmd=on-rett obj=board0-cpu
// end::unsubscribe[]
quit
Running it prints the trap number in decimal, 133 being 0x85:
trap 133 taken on board0-cpu
rett on board0-cpu
The Python chapter handles the same notifications from Python.