Reference Manual
Triggers
Collection of tools for concurrency and synchronization in cocotb.
- class coconext.triggers.Notify
Bases:
objectNotify all waiters of an event.
cocotb.triggers.Eventhas state. It can be in the state whereEvent.is_set()isFalseand callingEvent.wait()will return a Trigger that only fires afterEvent.set()is called. Or it can be in a state whereEvent.is_set()isTrueand callingEvent.wait()will return a Trigger that fires immediately.This object does not have state. It behaves like
Eventif it were only ever in theEvent.is_set()isFalsestate. All calls towait()will block until the next call tonotify()occurs.Added in version 0.1.
- __init__()
- class coconext.triggers.TaskManager
Bases:
objectAn asynchronous context manager which enables the user to run coroutine functions or
awaitawaitables concurrently and wait for them all to finish.awaitables can be added to the
TaskManagerusingstart_soon(). Likewise,fork()can be used to add coroutine functions. Both methods return theTaskwhich is running the concurrent behavior.When control reaches the end of the context block the
TaskManagerblocks theTaskusing it until all childrenTasks of theTaskManagercomplete.async with TaskManager() as tm: @tm.fork async def drive(): ... rising_edge = cocotb.start_soon(RisingEdge(dut.clk)) await drive # wait for drive() to end rising_edge.cancel() # cancel a child Task # Control returns here when all child Tasks have completed
- __init__()
- fork(coro, *args, **kwargs)
Decorate a coroutine function to run it concurrently.
- Parameters:
- Return type:
- Returns:
A
Taskwhich is running coro concurrently.
async with TaskManager() as tm: @tm.fork async def my_func(): # Do stuff ... @tb.fork async def other_func(): # Do other stuff in parallel to my_func ...
- start_soon(aw, *, name=None)
Awaits its argument concurrently to other calls to this function.
- async coconext.triggers.gather(*awaitables, return_exceptions=False)
Awaits on all given awaitables concurrently and returns their results once all have completed.
After the return conditions, based on return_exceptions is met, remaining waiter tasks are cancelled. This does not cancel
Tasks passed as arguments, only the internal tasks awaiting upon their completion.- Parameters:
awaitables (
Awaitable[Any]) – TheAwaitables to concurrentlyawaitupon.return_exceptions (
bool) – IfFalse(default), after the first awaitable results in an exception, cancels the remaining awaitables and re-raises the exception. IfTrue, returns the exception rather than the result value when an awaitable results in an exception.
- Return type:
- Returns:
A tuple of the results of awaiting each awaitable in the same order they were given. The order of the return tuple corresponds to the order of the input.
- async coconext.triggers.select(*awaitables, return_exception=False)
Awaits on all given awaitables concurrently and returns the index and result of the first to complete.
After the first awaitable completes, remaining waiter tasks are cancelled. This does not cancel
Tasks passed as arguments, only the internal tasks awaiting upon their completion.- Parameters:
- Raises:
ValueError – if missing at least one awaitable.
- Return type:
tuple[int,Union[TypeVar(T),BaseException]]- Returns:
A tuple of the index into the argument list (0-based) of the first awaitable to complete and its result.
- async coconext.triggers.wait(*awaitables, return_when)
Awaits on all given awaitables concurrently and blocks until the return_when condition is met.
Every
Awaitablegiven to the function isawaited concurrently in its ownTask. When the return conditions specified by return_when are met, this function returns thoseTasks. Once the return conditions are met, anyTasks which are still running arecancel()ed.The return_when conditions must be one of the following:
"FIRST_COMPLETED": Returns after the first awaitable completes, regardless if that was due to an exception or not."FIRST_EXCEPTION": Returns after all awaitables complete or after the first awaitable that completes due to an exception."ALL_COMPLETED": Returns after all awaitables complete.
- Parameters:
- Return type:
- Returns:
A tuple of waiter
Tasks. The order of the return tuple corresponds to the order of the input.
Simulation Time Utils
Collections of tools for dealing with simulation time in cocotb.
- class coconext.simtime.SimTime
Bases:
objectType for dealing with simulated time.
SimTimeis time quantized to simulator steps. If you can create aSimTime, you have a time that can be represented by the simulator. The type also includes a collection of functionality commonly needed when dealing with time, such as comparison, scaling, and arithmetic.- __init__(time, unit, *, round_mode='error')
Construct a SimTime of time value in unit.
- Parameters:
time (
float) – The time value.unit (
Literal['fs','ps','ns','us','ms','sec','step']) – Unit for time argument.round_mode (
Literal['error','round','ceil','floor']) – How to round the time if it can’t be accurately represented. One of"error","round","ceil", and"floor". Seecocotb.utils.get_sim_steps()for details on these values.
- mul(other, *, round_mode='error')
Scale the time via multiplication and supply a round_mode.
- Return type:
- div(other, *, round_mode='error')
Scale the time via division and supply a round_mode.
- Return type:
Queue
Collection of asynchronous queues for cocotb.
- class coconext.queue.AbstractQueue
Bases:
Generic[T]An asynchronous queue, useful for coordinating producer and consumer tasks.
Queues can have various semantics with respect to the values
put()andget()from them. This class does not enforce any particular semantics, but leaves the implementation and semantics abstract. A concrete queue class can be created by subclassingAbstractQueueand filling in the_size(),_put(),_get(), and_peek()methods.Additionally, there are common implementations included in this module:
Queue(FIFO semantics),LifoQueue(LIFO semantics), andPriorityQueue(min-heap semantics).Added in version 0.2.
- class Lock
Bases:
objectA mutex lock specialized for use by asynchronous Queue.
This class is not intended to be instantiated by the user, but rather be acquired via
AbstractQueue.write_lockorAbstractQueue.read_lock.In addition to all the functionality available on
Lock, these locks are aware of the associated queue’s availability, i.e. whether the read or write side has data or space available to either get or put, respectively.- __init__()
- async acquire()
Acquire the lock.
Waits until the lock is available and unlocked, sets it to locked and returns. Multiple Tasks may attempt to acquire the Lock, but only one will proceed. Tasks are guaranteed to acquire the Lock in the order each attempts to acquire it.
- Return type:
- release()
Release the lock.
Sets the lock to unlocked.
- Raises:
RuntimeError – If called when the lock is unlocked.
- Return type:
- __init__(maxsize=0)
Construct a queue with the given maxsize.
If maxsize is less than or equal to 0, the queue size is infinite. If it is an integer greater than 0, then
put()will block when the queue reaches maxsize, until an item is removed byget().
- property write_lock: Lock
Lock for exclusive write access.
After acquiring this lock, the user has exclusive access to the write-side of the queue until the lock is released. Calling
put()while the write lock is acquired will result in aRuntimeErrorto prevent deadlock.
- property read_lock: Lock
Lock for exclusive read access.
After acquiring this lock, the user has exclusive access to the read-side of the queue until the lock is released. Calling
get()while the read lock is acquired will result in aRuntimeErrorto prevent deadlock.
- full()
Return
Trueif there aremaxsize()items in the queue.Note
If the Queue was initialized with
maxsize=0(the default), thenfull()is neverTrue.- Return type:
- async put(item)
Put an item into the queue.
If the queue is full, wait until a free slot is available before adding the item.
- Return type:
- put_nowait(item)
Put an item into the queue without blocking.
If no free slot is immediately available, raise
QueueFull.- Return type:
- async get()
Remove and return an item from the queue.
If the queue is empty, wait until an item is available.
- Return type:
TypeVar(T)
- get_nowait()
Remove and return an item from the queue.
Returns an item if one is immediately available, otherwise raises
QueueEmpty.- Return type:
TypeVar(T)
- async peek()
Return the next item from the queue without removing it.
If the queue is empty, wait until an item is available.
- Return type:
TypeVar(T)
- peek_nowait()
Return the next item from the queue without removing it.
Returns an item if one is immediately available, otherwise raises
QueueEmpty.- Return type:
TypeVar(T)
- class coconext.queue.Queue
Bases:
AbstractQueue[T]A subclass of
AbstractQueue; retrieves oldest entries first (FIFO).Added in version 2.0.
- __init__(maxsize=0)
Construct a queue with the given maxsize.
If maxsize is less than or equal to 0, the queue size is infinite. If it is an integer greater than 0, then
put()will block when the queue reaches maxsize, until an item is removed byget().
- class coconext.queue.PriorityQueue
Bases:
AbstractQueue[SupportsRichComparisonT]A subclass of
AbstractQueue; retrieves entries in priority order (smallest item first).Entries are typically tuples of the form
(priority number, data).Added in version 2.0.
- __init__(maxsize=0)
Construct a queue with the given maxsize.
If maxsize is less than or equal to 0, the queue size is infinite. If it is an integer greater than 0, then
put()will block when the queue reaches maxsize, until an item is removed byget().
- class coconext.queue.LifoQueue
Bases:
AbstractQueue[T]A subclass of
AbstractQueue; retrieves most recently added entries first (LIFO).Added in version 2.0.
- __init__(maxsize=0)
Construct a queue with the given maxsize.
If maxsize is less than or equal to 0, the queue size is infinite. If it is an integer greater than 0, then
put()will block when the queue reaches maxsize, until an item is removed byget().
Types
Collection of types modelled after those common in HDLs.
- class coconext.types.BitArray
Bases:
AbstractMutableArray[Bit]An Array of Bits.
- __hash__ = None
- __init__(value, range=None)
- self | other
Return self|value.
- Return type:
BitArray|int|LogicArray
- __ror__(other)
Return value|self.
- Return type:
BitArray|int|LogicArray