| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from typing import Any, Dict, TypeVar |
| 5 |
|
| 6 |
import IPython |
| 7 |
from IPython.terminal.ipapp import load_default_config |
| 8 |
from IPython.terminal.prompts import Prompts |
| 9 |
from pygments.token import Token |
| 10 |
|
| 11 |
import cocotb |
| 12 |
from cocotb.task import bridge |
| 13 |
from cocotb.utils import get_sim_time |
| 14 |
|
| 15 |
T = TypeVar("T") |
| 16 |
|
| 17 |
|
| 18 |
class SimTimePrompt(Prompts): |
| 19 |
"""custom prompt that shows the sim time after a trigger fires""" |
| 20 |
|
| 21 |
_show_time = 1 |
| 22 |
|
| 23 |
def in_prompt_tokens(self): |
| 24 |
tokens = super().in_prompt_tokens() |
| 25 |
if self._show_time == self.shell.execution_count: |
| 26 |
tokens = [ |
| 27 |
(Token.Comment, f"sim time: {get_sim_time()}"), |
| 28 |
(Token.Text, "\n"), |
| 29 |
*tokens, |
| 30 |
] |
| 31 |
return tokens |
| 32 |
|
| 33 |
|
| 34 |
async def embed(user_ns: Dict[str, Any] = {}) -> None: |
| 35 |
""" |
| 36 |
Start an IPython shell in the current coroutine. |
| 37 |
|
| 38 |
Unlike using :func:`IPython.embed` directly, the :keyword:`await` keyword |
| 39 |
can be used directly from the shell to wait for triggers. |
| 40 |
The :keyword:`yield` keyword from the legacy :ref:`yield-syntax` is not supported. |
| 41 |
|
| 42 |
This coroutine will complete only when the user exits the interactive session. |
| 43 |
|
| 44 |
Args: |
| 45 |
user_ns: |
| 46 |
The variables to have made available in the shell. |
| 47 |
Passing ``locals()`` is often a good idea. |
| 48 |
``cocotb`` will automatically be included. |
| 49 |
|
| 50 |
.. note:: |
| 51 |
If your simulator does not provide an appropriate ``stdin``, you may |
| 52 |
find you cannot type in the resulting shell. Using simulators in batch |
| 53 |
or non-GUI mode may resolve this. This feature is experimental, and |
| 54 |
not all simulators are supported. |
| 55 |
""" |
| 56 |
|
| 57 |
default_ns = {"cocotb": cocotb} |
| 58 |
default_ns.update(user_ns) |
| 59 |
|
| 60 |
def _runner(x): |
| 61 |
"""Handler for async functions""" |
| 62 |
nonlocal shell |
| 63 |
ret = cocotb._scheduler_inst._queue_function(x) |
| 64 |
shell.prompts._show_time = shell.execution_count |
| 65 |
return ret |
| 66 |
|
| 67 |
|
| 68 |
c = load_default_config() |
| 69 |
c.TerminalInteractiveShell.loop_runner = _runner |
| 70 |
c.TerminalInteractiveShell.autoawait = True |
| 71 |
|
| 72 |
|
| 73 |
c.HistoryAccessor.connection_options = {"check_same_thread": False} |
| 74 |
|
| 75 |
shell = IPython.terminal.embed.InteractiveShellEmbed( |
| 76 |
user_ns=default_ns, |
| 77 |
config=c, |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
shell.prompts = SimTimePrompt(shell) |
| 82 |
|
| 83 |
|
| 84 |
@bridge |
| 85 |
def run_shell() -> None: |
| 86 |
shell() |
| 87 |
|
| 88 |
await run_shell() |
| 89 |
|
| 90 |
|
| 91 |
@cocotb.test() |
| 92 |
async def run_ipython(dut: Any) -> None: |
| 93 |
"""A test that launches an interactive Python shell. |
| 94 |
|
| 95 |
Do not call this directly - use this as ``make COCOTB_TEST_MODULES=cocotb.ipython_support``. |
| 96 |
|
| 97 |
Within the shell, a global ``dut`` variable pointing to the design will be present. |
| 98 |
""" |
| 99 |
await embed(user_ns={"dut": dut}) |