| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """A clock class.""" |
|
|
| import itertools |
| from decimal import Decimal |
| from numbers import Real |
| from typing import Union |
|
|
| from cocotb._py_compat import cached_property |
| from cocotb.log import SimLog |
| from cocotb.triggers import Timer |
| from cocotb.utils import get_sim_steps, get_time_from_sim_steps |
|
|
|
|
| class Clock: |
| r"""Simple 50:50 duty cycle clock driver. |
| |
| Instances of this class should call its :meth:`start` method |
| and pass the coroutine object to one of the functions in :ref:`task-management`. |
| |
| This will create a clocking task that drives the signal at the |
| desired period/frequency. |
| |
| Example: |
| |
| .. code-block:: python |
| |
| c = Clock(dut.clk, 10, 'ns') |
| await cocotb.start(c.start()) |
| |
| Args: |
| signal: The clock pin/signal to be driven. |
| period (int): The clock period. Must convert to an even number of |
| timesteps. |
| units (str, optional): One of |
| ``'step'``, ``'fs'``, ``'ps'``, ``'ns'``, ``'us'``, ``'ms'``, ``'sec'``. |
| When *units* is ``'step'``, |
| the timestep is determined by the simulator (see :make:var:`COCOTB_HDL_TIMEPRECISION`). |
| |
| If you need more features like a phase shift and an asymmetric duty cycle, |
| it is simple to create your own clock generator (that you then :func:`~cocotb.start`): |
| |
| .. code-block:: python |
| |
| async def custom_clock(): |
| # pre-construct triggers for performance |
| high_time = Timer(high_delay, units="ns") |
| low_time = Timer(low_delay, units="ns") |
| await Timer(initial_delay, units="ns") |
| while True: |
| dut.clk.value = 1 |
| await high_time |
| dut.clk.value = 0 |
| await low_time |
| |
| If you also want to change the timing during simulation, |
| use this slightly more inefficient example instead where |
| the :class:`Timer`\ s inside the while loop are created with |
| current delay values: |
| |
| .. code-block:: python |
| |
| async def custom_clock(): |
| while True: |
| dut.clk.value = 1 |
| await Timer(high_delay, units="ns") |
| dut.clk.value = 0 |
| await Timer(low_delay, units="ns") |
| |
| high_delay = low_delay = 100 |
| await cocotb.start(custom_clock()) |
| await Timer(1000, units="ns") |
| high_delay = low_delay = 10 # change the clock speed |
| await Timer(1000, units="ns") |
| |
| .. versionchanged:: 1.5 |
| Support ``'step'`` as the *units* argument to mean "simulator time step". |
| |
| .. versionchanged:: 2.0 |
| Passing ``None`` as the *units* argument was removed, use ``'step'`` instead. |
| """ |
|
|
| def __init__( |
| self, signal, period: Union[float, Real, Decimal], units: str = "step" |
| ): |
| self.signal = signal |
| self.period = get_sim_steps(period, units) |
| self.half_period = get_sim_steps(period / 2, units) |
| self.frequency = 1 / get_time_from_sim_steps(self.period, units="us") |
| self.hdl = None |
| self.signal = signal |
| self.coro = None |
| self.mcoro = None |
|
|
| async def start(self, cycles=None, start_high=True): |
| r"""Clocking coroutine. Start driving your clock by :func:`cocotb.start`\ ing a |
| call to this. |
| |
| Args: |
| cycles (int, optional): Cycle the clock *cycles* number of times, |
| or if ``None`` then cycle the clock forever. |
| Note: ``0`` is not the same as ``None``, as ``0`` will cycle no times. |
| start_high (bool, optional): Whether to start the clock with a ``1`` |
| for the first half of the period. |
| Default is ``True``. |
| |
| .. versionadded:: 1.3 |
| """ |
| t = Timer(self.half_period) |
| if cycles is None: |
| it = itertools.count() |
| else: |
| it = range(cycles) |
|
|
| |
| if start_high: |
| for _ in it: |
| self.signal.value = 1 |
| await t |
| self.signal.value = 0 |
| await t |
| else: |
| for _ in it: |
| self.signal.value = 0 |
| await t |
| self.signal.value = 1 |
| await t |
|
|
| def __str__(self): |
| return type(self).__qualname__ + "(%3.1f MHz)" % self.frequency |
|
|
| @cached_property |
| def log(self): |
| return SimLog(f"cocotb.{type(self).__qualname__}.{self.signal._name}") |
|
|