Abstract Syntax Tree¶
- torii.hdl.ast.AnyConst(shape, *, src_loc_at: int = 0) AnyValue ¶
- Return type:
AnyValue
Todo
Document Me
- torii.hdl.ast.AnySeq(shape, *, src_loc_at: int = 0) AnyValue ¶
- Return type:
AnyValue
Todo
Document Me
- class torii.hdl.ast.Array(iterable: Sequence[Value] = ()) None ¶
Addressable multiplexer.
An array is similar to a
list
that can also be indexed by aValue
; indexing by an integer or a slice works the same as for Python lists, but indexing by aValue
results in a proxy.The array proxy can be used as an ordinary
Value
, i.e. participate in calculations and assignments, provided that all elements of the array are values. The array proxy also supports attribute access and further indexing, each returning another array proxy; this means that the results of indexing into arrays, arrays of records, and arrays of arrays can all be used as first-class values.It is an error to change an array or any of its elements after an array proxy was created. Changing the array directly will raise an exception. However, it is not possible to detect the elements being modified; if an element’s attribute or element is modified after the proxy for it has been created, the proxy will refer to stale data.
Examples
Simple array:
gpios = Array(Signal() for _ in range(10)) with m.If(bus.we): m.d.sync += gpios[bus.addr].eq(bus.w_data) with m.Else(): m.d.sync += bus.r_data.eq(gpios[bus.addr])
Multidimensional array:
Array of records:
layout = [ ("r_data", 16), ("r_en", 1), ] buses = Array(Record(layout) for busno in range(4)) master = Record(layout) m.d.comb += [ buses[sel].r_en.eq(master.r_en), master.r_data.eq(buses[sel].r_data), ]
- insert(index: SupportsIndex, value: Value) None ¶
Todo
Document Me
- class torii.hdl.ast.ArrayProxy(elems, index: Value | int | bool | EnumType | ValueCastable | ValueLike, *, src_loc_at: int = 0) None ¶
Todo
Document Me
- torii.hdl.ast.Assert(test: Value | int | bool | EnumType | ValueCastable | ValueLike, *, name: str | None = None, src_loc_at: int = 0) Property ¶
- Return type:
Todo
Document Me
- class torii.hdl.ast.Assign(lhs: Value | int | bool | EnumType | ValueCastable | ValueLike, rhs: Value | int | bool | EnumType | ValueCastable | ValueLike, *, src_loc_at: int = 0) None ¶
Todo
Document Me
- torii.hdl.ast.Assume(test: Value | int | bool | EnumType | ValueCastable | ValueLike, *, name: str | None = None, src_loc_at: int = 0) Property ¶
- Return type:
Todo
Document Me
- class torii.hdl.ast.Cat(*args: ValueCastT | Iterable[ValueCastT], src_loc_at: int = 0) None ¶
Concatenate values.
Form a compound
Value
from several smaller ones by concatenation. The first argument occupies the lower bits of the result. The return value can be used on either side of an assignment, that is, the concatenated value can be used as an argument on the RHS or as a target on the LHS. If it is used on the LHS, it must solely consist ofSignal
s, slices ofSignal
s, and other concatenations meeting these properties. The bit length of the return value is the sum of the bit lengths of the arguments:- Parameters:
*args (Values or iterables of Values, inout) –
Value
s to be concatenated.- Returns:
Value – Resulting
Value
obtained by concatenation.
- class torii.hdl.ast.ClockSignal(domain: str = 'sync', *, src_loc_at: int = 0) None ¶
Clock signal for a clock domain.
Any
ClockSignal
is equivalent tocd.clk
for a clock domain with the corresponding name. All of these signals ultimately refer to the same signal, but they can be manipulated independently of the clock domain, even before the clock domain is created.- Parameters:
domain (str) – Clock domain to obtain a clock signal for. Defaults to
sync
.
- class torii.hdl.ast.Const(value: int, shape: Shape | int | range | type | ShapeCastable | None = None, *, src_loc_at: int = 0) None ¶
A constant, literal integer value.
- Parameters:
- Attributes:
width (int)
signed (bool)
- static cast(obj: Value | int | bool | EnumType | ValueCastable | ValueLike) Const ¶
Converts
obj
to an Torii constant.First,
obj
is converted to a value usingValue.cast()
. If it is a constant, it is returned. If it is a constant-castable expression, it is evaluated and returned. Otherwise,TypeError
is raised.- Return type:
- torii.hdl.ast.Cover(test: Value | int | bool | EnumType | ValueCastable | ValueLike, *, name: str | None = None, src_loc_at: int = 0) Property ¶
- Return type:
Todo
Document Me
- torii.hdl.ast.Edge(expr: Value | int | bool | EnumType | ValueCastable | ValueLike, clocks: int = 0, domain: str | None = None) Operator ¶
Check if the given Signal rose or fell in the past
clocks + 1
clock cycles.- Parameters:
- Return type:
- Returns:
Operator – If the sample rose or fell between
clocks
andclocks + 1
- torii.hdl.ast.Fell(expr: Value | int | bool | EnumType | ValueCastable | ValueLike, clocks: int = 0, domain: str | None = None) Operator ¶
Check if the given Signal fell in the past
clocks + 1
clock cycles.- Parameters:
- Return type:
- Returns:
Operator – If the sample fell between
clocks
andclocks + 1
- class torii.hdl.ast.Initial(*, src_loc_at: int = 0) None ¶
Start indicator, for model checking.
An
Initial
signal is1
at the first cycle of model checking, and0
at any other.
- torii.hdl.ast.Mux(sel: Value | int | bool | EnumType | ValueCastable | ValueLike, val1: Value | int | bool | EnumType | ValueCastable | ValueLike, val0: Value | int | bool | EnumType | ValueCastable | ValueLike) Operator ¶
Choose between two values.
- class torii.hdl.ast.Operator(operator: Literal['+', '~', '-', 'b', 'r|', 'r&', 'r^', 'u', 's', '*', '//', '%', '<', '<=', '==', '!=', '>', '>=', '&', '^', '|', '<<', '>>', 'm'], operands: Sequence[Value | int | bool | EnumType | ValueCastable | ValueLike], *, src_loc_at: int = 0) None ¶
Todo
Document Me
- class torii.hdl.ast.Part(value: Value | int | bool | EnumType | ValueCastable | ValueLike, offset: Value | int | bool | EnumType | ValueCastable | ValueLike, width: int, stride: int = 1, *, src_loc_at: int = 0) None ¶
Todo
Document Me
- torii.hdl.ast.Past(expr: Value | int | bool | EnumType | ValueCastable | ValueLike, clocks: int = 1, domain: str | None = None) Value ¶
Get a value from the past.
This function is effectively just an alias for constructing a
Sample
object.Important
The sample value is likely one extra clock cycle in the past that you would expect, meaning when sampling with
1
clock, 2 clock cycles will have passed, the diagram below shows an example with differentclocks
values on the sample.- Parameters:
- Return type:
- Returns:
Sample – The sample value.
- class torii.hdl.ast.Property(kind: str, test: Value | int | bool | EnumType | ValueCastable | ValueLike, *, _check: Signal | None = None, _en: Signal | None = None, name: str | None = None, src_loc_at: int = 0) None ¶
Todo
Document Me
- class Kind(value)¶
Todo
Document Me
- class torii.hdl.ast.ResetSignal(domain: str = 'sync', allow_reset_less: bool = False, *, src_loc_at: int = 0) None ¶
Reset signal for a clock domain.
Any
ResetSignal
is equivalent tocd.rst
for a clock domain with the corresponding name. All of these signals ultimately refer to the same signal, but they can be manipulated independently of the clock domain, even before the clock domain is created.- Parameters:
- torii.hdl.ast.Rose(expr: Value | int | bool | EnumType | ValueCastable | ValueLike, clocks: int = 0, domain: str | None = None) Operator ¶
Check if the given Signal rose in the past
clocks + 1
clock cycles.- Parameters:
- Return type:
- Returns:
Operator – If the sample rose between
clocks
andclocks + 1
- class torii.hdl.ast.Sample(expr: Value | int | bool | EnumType | ValueCastable | ValueLike, clocks: int, domain: str | None, *, src_loc_at: int = 0) None ¶
Value from the past.
A
Sample
of an expression is equal to the value of the expressionclocks
clock edges of thedomain
clock back. If that moment is before the beginning of time, it is equal to the value of the expression calculated as if each signal had its reset value.Important
The sample value is likely one extra clock cycle in the past that you would expect, meaning when sampling with
1
clock, 2 clock cycles will have passed, the diagram below shows an example with differentclocks
values on the sample.- Parameters:
- Attributes:
value (Value) – The Value of the input expression.
clocks (int) – The number of clock cycles in the past to sample from.
domain (str) – The domain the sample is taken on.
- class torii.hdl.ast.Shape(width: int = 1, signed: bool = False) None ¶
Bit width and signedness of a value.
A
Shape
can be constructed using:explicit bit width and signedness;
aliases
signed()
andunsigned()
;casting from a variety of objects.
A
Shape
can be cast from:an integer, where the integer specifies the bit width;
- a range, where the result is wide enough to represent any element of the range, and is
signed if any element of the range is signed;
- an
Enum
with all integer members orIntEnum
, where the result is wide enough to represent any member of the enumeration, and is signed if any member of the enumeration is signed.
- an
- Parameters:
- class torii.hdl.ast.ShapeCastable¶
Interface of user-defined objects that can be cast to a
Shape
.An object deriving from
ShapeCastable
is automatically converted to aShape
when it is used in a context where aShape
is expected. Such objects can contain a richer description of the shape than what is supported by the core Torii language, yet still be transparently used with it.
- class torii.hdl.ast.ShapeLike(*args, **kwargs)¶
An abstract class representing all objects that can be cast to a
Shape
.issubclass(cls, ShapeLike)
returnsTrue
for:ShapeCastable
and its subclassesint
and its subclassesrange
and its subclassesenum.EnumMeta
and its subclassesShapeLike
itself
isinstance(obj, ShapeLike)
returnsTrue
for:Shape
instancesShapeCastable
instancesnon-negative
int
valuesrange
instancesenum.Enum
subclasses where all values are value-like
This class is only usable for the above checks — no instances and no (non-virtual) subclasses can be created.
- class torii.hdl.ast.Signal(shape: ShapeCastT | None = None, *, name: str | None = None, reset: int | EnumMeta | None = None, reset_less: bool = False, attrs: SignalAttrs | None = None, decoder: SignalDecoder | None = None, src_loc_at: int = 0) None ¶
A varying integer value.
- Parameters:
shape (
Shape
-castable object or None) – Specification for the number of bits in thisSignal
and its signedness (whether it can represent negative values). SeeShape.cast
for details. If not specified,shape
defaults to 1-bit and non-signed.name (str) – Name hint for this signal. If
None
(default) the name is inferred from the variable name thisSignal
is assigned to.reset (int or integral Enum) – Reset (synchronous) or default (combinatorial) value. When this
Signal
is assigned to in synchronous context and the corresponding clock domain is reset, theSignal
assumes the given value. When thisSignal
is unassigned in combinatorial context (due to conditional assignments not being taken), theSignal
assumes itsreset
value. Defaults to 0.reset_less (bool) – If
True
, do not generate reset logic for thisSignal
in synchronous statements. Thereset
value is only used as a combinatorial default or as the initial value. Defaults toFalse
.attrs (dict) – Dictionary of synthesis attributes.
decoder (function or Enum) – A function converting integer signal values to human-readable strings (e.g. FSM state names). If an
Enum
subclass is passed, it is concisely decoded using format string"{0.name:}/{0.value:}"
, or a number if the signal value is not a member of the enumeration.
- Attributes:
width (int)
signed (bool)
name (str)
reset (int)
reset_less (bool)
attrs (dict)
decoder (function)
- class torii.hdl.ast.SignalDict(pairs: tuple[tuple[Key, Val], ...] = ()) None ¶
Mapping of Signal objects to arbitrary value types
- class torii.hdl.ast.SignalKey(signal: Signal | ClockSignal | ResetSignal) None ¶
Allow aliasing of the same internal signal in a design from multiple object instances.
This allows you to map them as the same key in the resolution collections.
Doing so allows for multiple instances of, say, a
ClockSignal
with the same domain to refer internally to the same signal upon design flatting.Meaning that wherever there is a
ClockSignal('sync')
they all refer to the same internal signal ID.
- class torii.hdl.ast.SignalSet(elements: Iterable[Key] = ()) None ¶
Collection of unique Signal objects
- class torii.hdl.ast.Slice(value: Value | int | bool | EnumType | ValueCastable | ValueLike, start: int, stop: int, *, src_loc_at: int = 0) None ¶
Todo
Document Me
- torii.hdl.ast.Stable(expr: Value | int | bool | EnumType | ValueCastable | ValueLike, clocks: int = 0, domain: str | None = None) Operator ¶
Check if a Signal is stable over the given
clocks + 1
cycles in the past.- Parameters:
- Return type:
- Returns:
Operator – If the sample is stable over
clocks + 1
samples.
- class torii.hdl.ast.Switch(test: Value | int | bool | EnumType | ValueCastable | ValueLike, cases: Mapping[str | int | Enum | None | tuple[str | int | Enum | None, ...], Iterable[object] | _StatementList], *, src_loc: tuple[str, int] | None = None, src_loc_at: int = 0, case_src_locs: dict[str | int | Enum | None | tuple[str | int | Enum | None, ...], tuple[str, int]] = {}) None ¶
Todo
Document Me
- class torii.hdl.ast.Value(*, src_loc_at: int = 0) None ¶
Todo
Document Me
- static cast(obj: Value | int | bool | EnumType | ValueCastable | ValueLike) Value ¶
Converts
obj
to an Torii value.Booleans and integers are wrapped into a
Const
. Enumerations whose members are all integers are converted to aConst
with a shape that fits every member.ValueCastable
objects are recursively cast to an Torii value.- Return type:
- as_unsigned() Operator ¶
Conversion to unsigned.
- Return type:
- Returns:
Operator – This
Value
reinterpreted as a unsigned integer.
- as_signed() Operator ¶
Conversion to signed.
- Return type:
- Returns:
Operator – This
Value
reinterpreted as a signed integer.
- bool() Operator ¶
Conversion to boolean.
- Return type:
- Returns:
Operator –
1
if any bits are set,0
otherwise.
- any() Operator ¶
Check if any bits are
1
.- Return type:
- Returns:
Operator –
1
if any bits are set,0
otherwise.
- all() Operator ¶
Check if all bits are
1
.- Return type:
- Returns:
Operator –
1
if all bits are set,0
otherwise.
- xor() Operator ¶
Compute pairwise exclusive-or of every bit.
- Return type:
- Returns:
Operator –
1
if an odd number of bits are set,0
if an even number of bits are set.
- implies(conclusion: Value | int | bool | EnumType | ValueCastable | ValueLike) Operator ¶
Implication.
- Return type:
- Returns:
Operator –
0
ifpremise
is true andconclusion
is not,1
otherwise.
- bit_select(offset: Value | int, width: int) Value ¶
Part-select with bit granularity.
Selects a constant width but variable offset part of a
Value
, such that successive parts overlap by all but 1 bit.
- word_select(offset: Value | int, width: int) Value ¶
Part-select with word granularity.
Selects a constant width but variable offset part of a
Value
, such that successive parts do not overlap.
- matches(*patterns: int | str | EnumType) Value ¶
Pattern matching.
Matches against a set of patterns, which may be integers or bit strings, recognizing the same grammar as
Case()
.
- replicate(count: int) Value ¶
Replication.
A
Value
is replicated (repeated) several times to be used on the RHS of assignments:
- inc(value: Value | int | bool | EnumType | ValueCastable | ValueLike = 1) Assign ¶
Increment value.
This is shorthand for
a.eq(a + n)
- class torii.hdl.ast.ValueCastable¶
Interface of user-defined objects that can be cast to
Value
s.An object deriving from
ValueCastable`
is automatically converted to aValue
when it is used in a context where aValue`
is expected. Such objects can implement different or richer semantics than what is supported by the core Torii language, yet still be transparently used with it as long as the final underlying representation is a single ToriiValue
. These objects also need not commit to a specific representation until they are converted to a concrete Torii value.Note that it is necessary to ensure that Torii’s view of representation of all values stays internally consistent. The class deriving from
ValueCastable`
must decorate theas_value()
method with thelowermethod()
decorator, which ensures that all calls toas_value()
return the sameValue
representation. If the class deriving fromValueCastable
is mutable, it is up to the user to ensure that it is not mutated in a way that changes its representation after the first call toas_value()
.- static lowermethod(func)¶
Decorator to memoize lowering methods.
Ensures the decorated method is called only once, with subsequent method calls returning the object returned by the first first method call.
This decorator is required to decorate the
as_value
method ofValueCastable
subclasses. This is to ensure that Torii’s view of representation of all values stays internally consistent.
- class torii.hdl.ast.ValueDict(pairs: tuple[tuple[Key, Val], ...] = ()) None ¶
Mapping of Value objects to arbitrary value types
- class torii.hdl.ast.ValueKey(value: Value | int | bool | EnumType | ValueCastable | ValueLike) None ¶
Todo
Document Me
- class torii.hdl.ast.ValueLike(*args, **kwargs)¶
Todo
Document Me