Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global alias for DUT parameter #2134

Merged
merged 1 commit into from Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions cocotb/__init__.py
Expand Up @@ -129,6 +129,16 @@ def _reopen_stream_with_buffering(stream_name):
_library_coverage = None
""" used for cocotb library coverage """

top = None # type: cocotb.handle.SimHandleBase
r"""
A handle to the :envvar:`TOPLEVEL` entity/module.

This is equivalent to the :term:`DUT` parameter given to cocotb tests, so it can be used wherever that variable can be used.
It is particularly useful for extracting information about the :term:`DUT` in module-level class and function definitions;
and in parameters to :class:`.TestFactory`\ s.
``None`` if :mod:`cocotb` was not loaded from a simulator.
"""


def fork(coro: Union[RunningTask, Coroutine]) -> RunningTask:
""" Schedule a coroutine to be run concurrently. See :ref:`coroutines` for details on its use. """
Expand Down Expand Up @@ -239,11 +249,12 @@ def _initialise_testbench(argv_):
if not handle:
raise RuntimeError("Can not find root handle ({})".format(root_name))

dut = cocotb.handle.SimHandle(handle)
global top
top = cocotb.handle.SimHandle(handle)

# start Regression Manager
global regression_manager
regression_manager = RegressionManager.from_discovery(dut)
regression_manager = RegressionManager.from_discovery(top)
regression_manager.execute()

_rlock.release()
Expand Down
3 changes: 3 additions & 0 deletions documentation/source/building.rst
Expand Up @@ -45,6 +45,9 @@ Cocotb
Use this to indicate the instance in the hierarchy to use as the :term:`DUT`.
If this isn't defined then the first root instance is used.

The DUT is available in cocotb tests as a Python object at :data:`cocotb.top`;
and is also passed to all cocotb tests as the :ref:`first and only parameter <quickstart_creating_a_test>`.

.. envvar:: RANDOM_SEED

Seed the Python random module to recreate a previous test stimulus.
Expand Down
2 changes: 2 additions & 0 deletions documentation/source/library_reference.rst
Expand Up @@ -369,6 +369,8 @@ Other Runtime Information

.. autodata:: cocotb.LANGUAGE

.. autodata:: cocotb.top


Signal Tracer for WaveDrom
--------------------------
Expand Down
1 change: 1 addition & 0 deletions documentation/source/newsfragments/2134.feature.rst
@@ -0,0 +1 @@
The handle to :envvar:`TOPLEVEL`, typically seen as the first argument to a cocotb test function, is now available globally as :data:`cocotb.top`.
2 changes: 2 additions & 0 deletions documentation/source/quickstart.rst
Expand Up @@ -78,6 +78,8 @@ Python test script to load.
We would then create a file called ``test_my_design.py`` containing our tests.


.. _quickstart_creating_a_test:

Creating a test
---------------

Expand Down