Python Scripting

TEMU embeds a Python 3 interpreter. Python scripts can create and configure objects, run commands, drive the simulation and react to notifications, which makes them a convenient way to write tests for your device models.

Running Scripts

There are three ways to run a Python script:

  • Pass it on the command line with --run-script, or simply as a file ending in .py:

    $ temu --project=temu-project.yaml --run-script scripts/python/mmio-test.py
  • Run it from the command line interface or a .temu script with the script-run command:

    temu> script-run file=scripts/python/mmio-test.py
  • Run a single statement with script-run script=…​.

Without -i (--interactive), TEMU exits when the scripts given on the command line have run. If a script raises an exception, TEMU prints the traceback and the script counts as failed, so a failing assert fails the run.

The tutorial project puts scripts/python on the Python search path, so modules placed there can be imported by your scripts.

The temu Module

The temu package is installed in share/temu/wrappers/Python under the TEMU installation, and TEMU adds it to the Python search path automatically.

The script below repeats the session from Memory Mapped Registers, as a test of the model. Save it as scripts/python/mmio-test.py in the tutorial project, build the MMIO plug-in from that chapter as libmmio.so, and run it as shown above.

temu.get_object() returns an object by name:

import temu

temu.cmd("import", "mmio")
temu.command("MyModel.new name=foo")

foo = temu.get_object("foo")
cpu = temu.get_object("board0-cpu")
sched = temu.get_object("sched")

Properties are attributes of the object. Reading or writing one goes through the property accessors of the model, so the foo : updating a log messages from the MMIO chapter are printed here too:

foo.a = 7
assert foo.a == 7
foo.a = 0

Commands are functions. Object commands such as board0-cpu.assemble are methods of the object, and global commands such as step are functions in the temu module. Options are keyword arguments. A command name containing a hyphen is spelled with an underscore, for example cpu-enable-trap-events becomes temu.cpu_enable_trap_events(). When an option cannot be passed from Python (interface references such as iface=foo:MemAccessIface at present), or for class commands such as MyModel.new, temu.command() runs a complete command line instead:

temu.command("board0-mem.map iface=foo:MemAccessIface addr=0x80001000 length=256")

cpu.assemble(inst="st %g1, [%g2 + 0]", pa=0)
cpu.assemble(inst="ld [%g2 + 0], %g3", pa=4)
cpu.setReg(reg="%g2", value=0x80001000)
cpu.setReg(reg="%g1", value=42)

sched.start(workers=1)
temu.step(cpuidx=0, steps=2)

A command that fails raises temu.CommandError. Finally, the test checks that the store reached the model:

assert foo.a == 42, f"a is {foo.a}, expected 42"
print("mmio-test: register a =", foo.a)

The functions of the C API are available too, under temu.c, for example temu.c.support.cpu.cpuGetPc(). They follow the C names without the temu_ prefix, and accept the objects returned by temu.get_object().

Notifications

The Listening for Notifications chapter subscribed to the trap entry and trap exit notifications from a plug-in. The same notifications can be handled by Python functions. A handler is called with the object that raised the notification and the notification information:

def on_trap(source, info):
    print(f"{source.name}: trap 0x{info.TrapId:x} taken at 0x{info.PC:08x}")


def on_rett(source, info):
    print(f"{source.name}: rett at 0x{info.PC:08x}")

For the CPU trap notifications the information is decoded into a TrapEventInfo with TrapId, PC and nPC, provided temu.c.target.cpu has been imported. Other notifications that carry information which the temu module does not know how to decode pass it on as the raw address.

The CPU only raises trap notifications once they are enabled, here with the cpu-enable-trap-events command. subscribe() on an object listens to notifications from that object only; temu.subscribe(name, handler) listens to every object that raises the notification:

temu.cpu_enable_trap_events(cpu=cpu)
entry = cpu.subscribe("temu.cpuTrapEntry", on_trap)
exit_ = cpu.subscribe("temu.cpuTrapExit", on_rett)

subscribe() returns a subscription, which ends when it is unsubscribed:

entry.unsubscribe()
exit_.unsubscribe()

The complete example sets up a small program that takes a software trap (ta 5) and returns from it, then steps through it:

# Listening for notifications from Python: the trap entry and trap exit
# notifications of the "Listening for Notifications" chapter, with Python
# functions as handlers.
#
# Run from the tutorial project directory:
#   temu --project=temu-project.yaml --run-script scripts/python/trap-notifications.py

import temu
import temu.c.target.cpu  # declares temu_TrapEventInfo for the decoded info

cpu = temu.get_object("board0-cpu")

# A program that takes a software trap and returns from it. 'ta 5' is trap
# type 0x85, so with the trap table at 0x40001000 the handler sits at
# 0x40001000 + 0x85 * 16. It returns to the instruction after the 'ta'.
TBR = 0x40001000
HANDLER = TBR + 0x85 * 16
cpu.assemble(pa=0x40000000, inst="ta %g0 + 5")
cpu.assemble(pa=0x40000004, inst="ba 0x40000004")
cpu.assemble(pa=0x40000008, inst="nop")
cpu.assemble(pa=HANDLER, inst="jmpl %l2 + %g0, %g0")
cpu.assemble(pa=HANDLER + 4, inst="rett %l2 + 4")
cpu.setReg(reg="%tbr", value=TBR)
cpu.setReg(reg="%wim", value=0)
cpu.setReg(reg="%psr", value=0xe0)  # supervisor, traps enabled, CWP 0
cpu.setPC(pc=0x40000000)

# tag::handlers[]
def on_trap(source, info):
    print(f"{source.name}: trap 0x{info.TrapId:x} taken at 0x{info.PC:08x}")


def on_rett(source, info):
    print(f"{source.name}: rett at 0x{info.PC:08x}")
# end::handlers[]


# tag::subscribe[]
temu.cpu_enable_trap_events(cpu=cpu)
entry = cpu.subscribe("temu.cpuTrapEntry", on_trap)
exit_ = cpu.subscribe("temu.cpuTrapExit", on_rett)
# end::subscribe[]

temu.get_object("sched").start(workers=1)
temu.step(cpuidx=0, steps=4)

# tag::unsubscribe[]
entry.unsubscribe()
exit_.unsubscribe()
# end::unsubscribe[]

Running it prints:

board0-cpu: trap 0x85 taken at 0x40000000
board0-cpu: rett at 0x40001854

On the trap exit notification, SPARC CPUs report the address of the rett instruction and no trap number.