7.2.3 Decorator functions

MyHDL defines a number of decorator functions, that make it easier to create generators from local generator functions.

instance( )
The instance decorator is the most general decorator. It automatically creates a generator by calling the decorated generator function.

It is used as follows:

def top(...):
    ...
    @instance
    def inst():
        <generator body>
    ...
    return inst, ...

This is equivalent to:

def top(...):
    ...
    def _gen_func():
        <generator body>
    ...
    inst = _gen_func()
    ...
    return inst, ...

always( arg [, *args])

The always decorator is a specialized decorator that targets a widely used coding pattern. It is used as follows:

def top(...):
    ...
    @always(event1, event2, ...)
    def inst()
        <body>
    ...
    return inst, ...

This is equivalent to the following:

def top(...):
    ...
    def _func():
        <body>

    def _gen_func()
        while True:
            yield event1, event2, ... 
            _func()
    ...
    inst = _gen_func()
    ...
    return inst, ...

The argument list of the decorator corresponds to the sensitivity list. Only signals, edge specifiers, or delay objects are allowed. The decorated function should be a classic function.

always_comb( )

The always_comb decorator is used to describe combinatorial logic.

def top(...):
    ...
    @always_comb
    def comb_inst():
        <combinatorial body>
    ...
    return comb_inst, ...

The always_comb decorator infers the inputs of the combinatorial logic and the corresponding sensitivity list automatically. The decorated function should be a classic function.

About this document