Running Python Scripts

There are three methods in which Python scrips can be executed inside TEMU. The first option is to use the script-run command. The second option (which is experimental at time of writing) is to prefix a line with an exclamation mark '!'. This tells the command interpreter to execute the line as a Python script. The third option is to run temu in non-interactive mode by adding the flags –run-script "foo.py" when starting TEMU.

Python scripts are especially convenient for writing tests of device models (and you could also implement a device model in Python, but at present you need to manually wrap functions using the ctypes library, which is not convenient).

The Python wrappers are defined in /opt/temu/{temu-version}}/share/temu/wrappers/Python, this directory is automatically added to the PYTHON_PATH by TEMU. It defines a number of packages based on ctypes, under the temu.c umbrella package. The current API is a simple wrapper of the TEMU C-API (but stripping the temu_ prefix of all functions as these become redundant with python packages), note that not all APIs are wrapped at present.

For example:

from temu.c.support.objsys import *
from temu.c.support.cpu import *
from temu.c.support.assembler import *
assembleToMemory(objectForName("mem0"), "st %g1, [%g2]", 0x40000000)
cpuSetPc(objectForName("cpu0"), 0x40000000)
# Set %g2 (used for address of store)
cpuSetReg(objectForName("cpu0"), 2, 0x80001000)
cpuStep(objectForName("cpu0"), 1) # Cpu will write data to device register
print "%x" % (getValueU32(objectForName("mydevice"), "myreg", 0))