10.6 System Panel 10.8 Script branches
API Reference Manual  /  10 Python API  / 

10.7 Testing

10.7.1 stest

Utilities for testing simics.

fail

Synopsis
fail(msg)

Description
Signal a failure in a test.

Takes a string describing the failure as its single argument. If called in collect failures mode this will collect the failure for later checking with check_failures. If called outside collect failures mode it will immediately raise a TestFailure exception instead.

collect_failures

Synopsis
collect_failures()

Description
Change the behavior of stest to collect failures instead of terminating on the first failure detected.

If you use this you have to call check_failures at the end of your test script to check if it signaled any failures.

check_failures

Synopsis
check_failures()

Description
Check if any failures have occurred and leave collect failures mode.

If any failures have occurred this function will report the failures and raise a TestFailure exception. If called outside collect failures mode this function will fail.

trap_log

Synopsis
trap_log(logtype, obj = None)

Description
Configure Simics to trap a type of log messages, and cause Simics to quit with a test failure on any such log message.

By default the stest trap 'error' and 'spec-viol' log messages.

trap_log without an object sets the default handling for the given log type. Object specific settings always take precedence over the default handling.

Arguments:

logtype
the type of log messages to trap, one of 'info', 'error', 'undef', 'spec-viol', and 'unimpl'
obj
the configuration object whose log messages to trap, defaults to None, which means that all log messages of the right type are trapped

untrap_log

Synopsis
untrap_log(logtype, obj = None)

Description
Explicitly allow messages of a given log type. Can be used to undo a previous trap_log or to filter objects from a default trap.

untrap_log without an object sets the default behavior for the given log type. Object specific settings always take precedence over the default handling.

Arguments:

logtype
the type of log messages to untrap, one of 'info', 'error', 'undef', 'spec-viol', and 'unimpl'
obj
the configuration object whose log messages to untrap, defaults to None, which means that all log messages of the right type are untrapped

expect_true

Synopsis
expect_true(cond, msg = 'expectation failed')

Description
Check if a condition is true and cause a test failure if it is not.

Arguments:

cond
the condition to test
msg
a message explaining the failure

expect_false

Synopsis
expect_false(cond, msg = 'expectation failed')

Description
Check if a condition is false and cause a test failure if it is not.

Arguments:

cond
the condition to test
msg
a message explaining the failure

expect_equal

Synopsis
expect_equal(got, expected, msg = 'expectation failed')

Description
Checks if a value is equal to an expectation and cause a test failure if it is not.

Arguments:

got
the value to check
expected
what to compare the value with
msg
a message explaining the failure

expect_different

Synopsis
expect_different(got, unexpected, msg = 'expectation failed')

Description
Checks if a value is different to an expectation and cause a test failure if it is not.

Arguments:

got
the value to check
unexpected
what to compare the value with
msg
a message explaining the failure

expect_log

Synopsis
expect_log(fun, args = [], obj = None, log_type = 'error', msg = None, regex = '', with_log_level = None)

Description
Call a function and verify that a particular log message is emitted. Returns the called function's return value. Arguments:
fun
the function to be called
args
the arguments with which to call fun
All other arguments are identical to expect_log_mgr

expect_log_mgr

Synopsis
expect_log_mgr(*args, **kwds)

Description
Context manager, verifying that, on exit from the with-statement, a particular log message has been emitted. Arguments:
obj
optional object from which a log is expected, default is None which accepts any object
log_type
optional log type which the emitted log must belong to, one of "error" (default), "unimpl", "info", "spec-viol" or "undefined"
msg
optional message emitted on failure
regex
optional regular expression object or a string containing a regular expression suitable for use by re.search(), which the emitted log must match
with_log_level
optional log level to apply inside the context

Example usage:

with stest.expect_log_mgr(log_type="spec-viol",
                          msg="Check warning on read-only fields"):
    reg_compute_units.write(0xffff_ffff_ffff_ffff)
 

expect_exception

Synopsis
expect_exception(fun, args, exc)

Description
Call function fun with arguments args, and verify that it raises an exception of type exc. If fun does not raise exc, cause a failure.

expect_exception_mgr

Synopsis
expect_exception_mgr(*args, **kwds)

Description
Context manager verifying that the body of with-statement throws an exception of the specified type.

Arguments:

exc
exception type

Example usage:

with stest.expect_exception_mgr(simics.SimExc_AttrNotWritable):
    dev.read_only_attribute = False
 

TestFailure

Superclasses
builtins.Exception
Description
Exception class used to signal failures in Simics tests.

Use the fail function instead of raising this exception yourself.

10.7.2 dev_util

A Python module for writing device model tests. Has three major parts: classes for accessing device registers, classes for writing device stubs, and classes for handling simulated memory and data structures in it. It also contains some auxiliary utilities.

It uses the Simics API and can only be used by Python code running in Simics.

bank_regs

Synopsis
bank_regs(bank, inquiry = False, prefix = '')

Description
Given a bank object, return a structure containing objects to access its registers. The returned structure can be a hierarchy of objects; e.g., if a register instance is named "g[2][3].r[1]", and the returned structure is o, then the corresponding register object can be obtained as o.g[2][3].g[1]. The set of registers is extracted using the register_view interface, and uses offsets and bitfields as returned by that interface. The inquiry argument is deprecated and should not be used. If prefix is given, then the returned structure will only contain registers whose full name matches this prefix.

The returned register objects have methods read() and write() that work like their AbstractRegister counterparts, reading and writing the register with side-effects. The register value can be read and written without side-effects objects by getting and setting a property .val, or by calling a method set(). The latter accepts keyword arguments for fields just like the write() method; bits not covered by fields are retrieved by reading .val. The positional argument of the write() method is required and may be either an integer, or READ to denote that previous values are retrieved using read(), or VAL to denote that they are retrieved using .val.

Objects that represent the fields of a register can be retrieved as reg.field.FIELDNAME; e.g., if a register reg has a field named "a.b[2]" then the field object is accessed as reg.field.a.b[2]. The field object has one method read() for performing a full-register read with side-effects and returning the bits corresponding to the field, and a property .val that allows side-effect free access to the field's bits. Field objects do not have a write method; writes must be done from the register object; this is to ensure that remaining bits are explicitly specified.

iface

Synopsis
iface(name)

Description
Deprecated.

value_to_tuple_be

Synopsis
value_to_tuple_be(val, bytes)

Description
Deprecated, use tuple(val.to_bytes(bytes, 'big')) instead.

value_to_tuple_le

Synopsis
value_to_tuple_le(val, bytes)

Description
Deprecated, use tuple(val.to_bytes(bytes, 'little')) instead.

tuple_to_value_be

Synopsis
tuple_to_value_be(t)

Description
Deprecated, use int.from_bytes(t, 'big') instead.

tuple_to_value_le

Synopsis
tuple_to_value_le(t)

Description
Deprecated, use int.from_bytes(t, 'little') instead.

Error

Superclasses
builtins.Exception
Description
Baseclass of all exceptions specified in this module.

InvalidBitfieldException

Superclasses
dev_util.Error
Description
Signals that a bitfield's parameters are invalid.

RangeError

Superclasses
dev_util.Error
Constructor
RangeError(msg, re = None)

Description
Signals that a value is out of range for a bitfield.

MemoryError

Superclasses
dev_util.Error
Description
Signals that a memory operation failed.

Bitfield

Superclasses
builtins.object
Constructor
Bitfield(fields = None, ones = 0, little_endian = True, bits = None, **kwargs)

Description
Utility for bitfield manipulations. Constructor arguments:
fields
a dict on the following format, where sub-field is of type fields:
 {'field-name' : bit-number,
  'field-name' : (start-bit, stop-bit),
  'field-name' : (start-bit, sub-field)
 }
 

ones
a bitmask that will be OR:ed in the complete bitfield value
little-endian
set to True for a little-endian bitorder field and False for big-endian bitorder fields
bits
the total size (in bits) of the fields; required by, as well as only allowed for, big-endian fields
**kwargs
as fields, but specified using keyword arguments

fields

Synopsis
obj.fields(value)

Description
Returns a dict consisting of all fields and their values, taken from the value argument.

mask

Synopsis
obj.mask(**dict)

Description
Returns a mask from the fields in dict.

mk_bitfield_map

Synopsis
obj.mk_bitfield_map(m, prefix = '', oset = 0)

Description
Convert a nested layout type to a map suitable for construction of a dev_util.Bitfield object.

value

Synopsis
obj.value(*args, **kwargs)

Description
Returns the value of the fields given by **kwargs. For example, pass foo=5 to return the value when field "foo" is 5. If the value is a list or dict rather than an integer, then a field array is assumed; e.g. foo=[1,2] is a shorthand for setting field "foo[0]" to 1 and field "foo[1]" to 2, and foo={1:4} is a shorthand for setting field "foo[1]" to 4.

An optional positional argument can be supplied, to provide the values of bits not covered by the given fields.

Bitfield_LE

Superclasses
dev_util.Bitfield
Constructor
Bitfield_LE(fields = None, ones = 0, **kwargs)

Description
Constructor arguments:
fields
a dict on the following format, where sub-field is of type fields:
 {'field-name' : bit-number,
  'field-name' : (start-bit, stop-bit),
  'field-name' : (start-bit, sub-field)
 }
 

ones
a bitmask that will be OR:ed in the complete bitfield value
**kwargs
as fields, but specified using keyword arguments

Bitfield_BE

Superclasses
dev_util.Bitfield
Constructor
Bitfield_BE(fields = None, ones = 0, bits = None, **kwargs)

Description
Bit-endian bitfield. Constructor arguments:
fields
a dict on the following format, where sub-field is of type fields:
 {'field-name' : bit-number,
  'field-name' : (start-bit, stop-bit),
  'field-name' : (start-bit, sub-field)
 }
 

ones
a bitmask that will be OR:ed in the complete bitfield value
bits
the total size (in bits) of the fields; required by big-endian fields
**kwargs
as fields, but specified using keyword arguments

AbstractRegister

Superclasses
builtins.object
Constructor
AbstractRegister(size = 4, bitfield = None, little_endian = True)

Description
Abstract register class.

This class handles generic register stuff, like bitfield mapping and value construction/deconstruction. It depends on a subclass to implement the backend for data storage.

Subclasses are expected to implement two functions:

raw_read(self)
should return a byte string from storage
raw_write(self, bytes)
should write the bytes byte string to storage

Optionally, a bitfield can be applied to a register. For such a register, a specific field can be read through reg.field.FIELDNAME.read(). The write() method also has support for bitfields. Please see the documentation for write() for more information.

There is an alternative deprecated shorthand for accessing fields: reg.FIELDNAME triggers a read that filters out the given field, and similarly, reg.FIELDNAME = value reads the field, adjusts the given field, and writes back the value. This syntax is deprecated, because it is not consistent with the API exposed by register objects that both support accesses with and without side-effects.

Constructor arguments:

size
the size in bytes of the register
bitfield
optional, if set should be an instance of Bitfield
little_endian
should be set to True for a little-endian byte-order register, or False for a big-endian register

fields

Synopsis
obj.fields()

Description
Shortcut to read the bitfield representation of a device register.

read

Synopsis
obj.read()

Description
Read value.

write

Synopsis
obj.write(*args, **fields)

Description
Write value. The value will be truncated to the register size.

You can either pass a single integer, or pass the fields as arguments, i.e., write(field0=1, field1=23). For field arrays, the syntax write(field=[1,23]) or write(field={0: 1, 1: 23}) can be used to set the fields named field[0] and field[1].

If only a subset of fields is given, a positional arg should also be passed for the base value, which defines what to write to remaining bits. The value READ denotes that the read() method is called to retrieve the base value.

Register

Superclasses
dev_util.AbstractRegister
Constructor
Register(*args, **kwargs)

Description
This class allows you to easily access a device register. The bank argument is normally the bank object. However, for backward compatibility it can also be a tuple (obj, bank, ofs); in this case the offset argument should be left out. The tuple denotes:
obj
the (simics) object implementing the register
bank
the port-name or function number of the bank containing the register (function 0 if omitted).
offset
the register offset in said bank

The tuple syntax was useful in old Simics versions where banks were not separate objects. It should be avoided in new versions.

The initiator argument denotes the initiator of transactions accessing the register. See the AbstractRegister documentation for information about the rest of the parameters.

raw_read

Synopsis
obj.raw_read()

Description
Read raw data from the register.

raw_write

Synopsis
obj.raw_write(val)

Description
Write raw data to the register.

Register_LE

Superclasses
dev_util.Register
Constructor
Register_LE(*args, **kwargs)

Description
Little-endian device register. All arguments have the same semantics as in dev_util.Register.

Register_BE

Superclasses
dev_util.Register
Constructor
Register_BE(*args, **kwargs)

Description
Big-endian device register. All arguments have the same semantics as in dev_util.Register.

GRegister

Superclasses
dev_util.AbstractRegister
Constructor
GRegister(size = 4, bitfield = None, init = 0)

Description
This class allows provides a standalone register.

raw_read

Synopsis
obj.raw_read()

Description
Reads the raw contents of the register.

raw_write

Synopsis
obj.raw_write(value)

Description
Write the raw contents of the register.

IRegister

Superclasses
dev_util.AbstractRegister
Constructor
IRegister(data, size = None, bitfield = None)

Description
This class allows you to easily access registers through the int_register interface. If the object obj does not implement the processor_info you have to specify the size in size.

raw_read

Synopsis
obj.raw_read()

Description
Reads the raw contents of the register.

raw_write

Synopsis
obj.raw_write(value)

Description
Write the raw contents of the register.

Dev

Superclasses
builtins.object
Constructor
Dev(iface_list = [], create_sim_obj = True, name = None)

Description
A Simics class with a single instance. Implements zero or more interfaces based on instances of Iface. Presents zero or more ports, implementing one or more interfaces each.

The obj attribute contains the instance of the Simics class, and the cls_name attribute contains the name of the Simics class. Constructor arguments:

iface_list
a list of interface classes or pairs of (port, interface-class)
create_sim_obj
if False, no Simics object will be created, but the class will be registered along with all the interfaces, useful e.g. when loading a checkpoint containing fake objects
name
if given, specifies the name to use for the fake class and the fake object; otherwise a name will be guessed based on the implemented interfaces

Each interface will be instantiated and can be accessed through dev.iface-name, or dev.port.iface-name.

Example:

 dev = Dev([SimpleInterrupt,
            ('sreset', Signal),
            ('hreset', Signal)])

 dev.simple_interrupt
 dev.sreset.signal
 dev.hreset.signal
 

configure_pre_object

Synopsis
obj.configure_pre_object(pre_obj)

Description
Called before SIM_add_configuration. Override to e.g. set attributes.

finalize

Synopsis
obj.finalize()

Description
Called after the Simics object has been instantiated.

register_simics_class

Synopsis
obj.register_simics_class()

Description
Register the Simics class created by this object.

Iface

Superclasses
dev_util_internal.BasicIface
Description
Base class for fake interfaces. To create an interface, inherit this class, define a class variable iface to the interface name, and define all methods that the interface defines.

fail

Synopsis
obj.fail(msg)

Description
Signal a failure when running an interface method.

Called by the default method stubs.

Memory

Superclasses
builtins.object
Constructor
Memory()

Description
Deprecated. Legacy wrapper around an instance of the sparse-memory class. Provides an interface compatible with the deprecated Memory class.

The obj attribute contains an instance of memory-space, mapping to the memory. The real_obj attribute contains an instance of the 'sparse-memory' class. The mem attribute is an alias to the mem attribute of real_obj.

clear

Synopsis
obj.clear(*args)

Description
Clear the contents of the memory.

is_range_touched

Synopsis
obj.is_range_touched(start, length)

Description
Return True if any of this memory's slots in the range contain data.

read

Synopsis
obj.read(addr, n)

Description
Read bytes from this memory.

Arguments:

addr
the start address of the range to read
n
length in bytes of the range to read

Throws an exception if any byte in the read range is empty.

write

Synopsis
obj.write(addr, data)

Description
Write bytes to this memory.

Arguments:

addr
the start address of the range to write
data
the bytes to write

Fills in empty slots in the memory and overwrites already existing data.

Layout

Superclasses
builtins.object
Constructor
Layout(mem, ofs, regs, little_endian)

Description
This class implements a memory layout.

Testing devices that does DMA transfers usually requires setting up DMA descriptors. These can often be seen as register sets located in memory. This class allows you to define the layout of those registers, including bitfields within the registers.

Registers without bitfields can be accessed through:

layout.reg-name

while registers with bitfields can be accessed through:

layout.reg-name.field-name

or

 layout.reg-name.read(),
 layout.reg-name.write()
 

Layouts are mapped ontop of Memory, i.e. NOT ontop of normal Simics RAM. Constructor arguments:

mem
An object that is a valid "map target"
ofs
the byte offset (i.e. the location of the layout)
little_endian
determines the byte order of the registers in the layout
regs
a dictionary on the following form:
 {'reg-name' : (offset, size),
  'reg-name' : (offset, size, bitfield)}
 

offset
the register offset into the layout
size
the size in bytes of the register
bitfield
optional and should be an instance of Bitfield

clear

Synopsis
obj.clear()

Description
Set all of the fields in this layout to 0.

read

Synopsis
obj.read(ofs, size)

Description
Returns the value at ofs.

write

Synopsis
obj.write(ofs, value, size)

Description
Writes value to ofs.

Layout_LE

Superclasses
dev_util.Layout
Constructor
Layout_LE(mem, ofs, regs)

Description
Little-endian layout.

Layout_BE

Superclasses
dev_util.Layout
Constructor
Layout_BE(mem, ofs, regs)

Description
Big-endian layout.

10.7.3 dev_util_internal

Internal and/or legacy classes removed from dev_util.py

iface

Synopsis
iface(name)

Description
Returns an Iface subclass which implements the interface name.

All the interface methods in the class will raise an exception when called. Convenient when creating a fake device that is required to implement an interface, but you know that the interface should be unused in the given test.

Dev

Superclasses
builtins.object
Constructor
Dev(iface_list = [], create_sim_obj = True, name = None)

Description
A Simics class with a single instance. Implements zero or more interfaces based on instances of Iface. Presents zero or more ports, implementing one or more interfaces each.

The obj attribute contains the instance of the Simics class, and the cls_name attribute contains the name of the Simics class. Constructor arguments:

iface_list
a list of interface classes or pairs of (port, interface-class)
create_sim_obj
if False, no Simics object will be created, but the class will be registered along with all the interfaces, useful e.g. when loading a checkpoint containing fake objects
name
if given, specifies the name to use for the fake class and the fake object; otherwise a name will be guessed based on the implemented interfaces

Each interface will be instantiated and can be accessed through dev.iface-name, or dev.port.iface-name.

Example:

 dev = Dev([SimpleInterrupt,
            ('sreset', Signal),
            ('hreset', Signal)])

 dev.simple_interrupt
 dev.sreset.signal
 dev.hreset.signal
 

configure_pre_object

Synopsis
obj.configure_pre_object(pre_obj)

Description
Called before SIM_add_configuration. Override to e.g. set attributes.

finalize

Synopsis
obj.finalize()

Description
Called after the Simics object has been instantiated.

register_simics_class

Synopsis
obj.register_simics_class()

Description
Register the Simics class created by this object.

Iface

Superclasses
dev_util_internal.BasicIface
Description
Base class for fake interfaces. To create an interface, inherit this class, define a class variable iface to the interface name, and define all methods that the interface defines.

fail

Synopsis
obj.fail(msg)

Description
Signal a failure when running an interface method.

Called by the default method stubs.

Memory

Superclasses
dev_util_internal.Dev
Constructor
Memory(test = False)

Description
A Simics memory space in which every slot can contain a byte, or be empty.

Each byte sized slot in this memory can either contain a byte of data or be empty. Empty slots cannot be read.

The obj attribute contains the object implementing the Simics interface to the memory. It implements the memory_space interface. Constructor arguments:

test
set to True to not create any Simics objects, optional, defaults to False

clear

Synopsis
obj.clear()

Description
Clear the contents of the memory.

is_range_touched

Synopsis
obj.is_range_touched(start, length)

Description
Return True if any of this memory's slots in the range contain data.

read

Synopsis
obj.read(addr, n)

Description
Read bytes from this memory.

Arguments:

addr
the start address of the range to read
n
length in bytes of the range to read

This method throws an exception if any byte in the read range is empty.

write

Synopsis
obj.write(addr, bytes)

Description
Write bytes to this memory.

Arguments:

addr
the start address of the range to write
n
the bytes to write

Fills in empty slots in the memory and overwrites already existing data.

10.6 System Panel 10.8 Script branches