How to use the cocotb.utils.get_sim_time function in cocotb

To help you get started, we’ve selected a few cocotb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mciepluc / apbi2c_cocotb_example / tb / test_i2c.py View on Github external
else:
            i2c_op.randomize()
        already_covered.append(
          (i2c_op.direction, i2c_op.repeat_range, i2c_op.divider_range)
        )
        
        #call test sequence
        if i2c_op.direction == "read":
            yield segment_i2c_read_operation(i2c_op)
        else:
            yield segment_i2c_write_operation(i2c_op)
            
        if ENABLE_CHECKPOINTS:
            #if status is OK, add this simulation point to the checkpoints list
            if operations_completed[-1][1]: 
                chkp_name = str(get_sim_time('ns'))
                log.info("Creating a simulation checkpoint: " + chkp_name)
                checkpoints[chkp_name] = (checkpoint(), i2c_op)
                
        #call APB test sequence as long as cover item apb.writeXdelay 
        #coverage level is below 100%
        if apb_cover_item.coverage*100/apb_cover_item.size < 100:
            yield segment_apb_rw(repeat = random.randint(1,5))
                    
        #update the coverage level
        
        cov_op_prev = cov_op
        cov_op = top_cover_item.coverage*100.0/top_cover_item.size
        log.info("Current overall coverage level = %f %%", cov_op)
        
    #print summary
    log.info("Opertions finished succesfully:")
github cocotb / cocotb / tests / test_cases / test_cocotb / test_timing_triggers.py View on Github external
def test_timer_with_rational_units(dut):
    """ Test that rounding errors are not introduced in exact values """
    # now with fractions
    time_fs = get_sim_time(units='fs')
    yield Timer(Fraction(1, int(1e9)), units='sec')
    assert get_sim_time(units='fs') == time_fs + 1000000.0, "Expected a delay of 1 ns"

    # now with decimals
    time_fs = get_sim_time(units='fs')
    yield Timer(Decimal('1e-9'), units='sec')
    assert get_sim_time(units='fs') == time_fs + 1000000.0, "Expected a delay of 1 ns"
github cocotb / cocotb / tests / test_cases / issue_588 / issue_588.py View on Github external
def issue_588_coroutine_list(dut):
    """ Yield a list of triggers and coroutines."""

    # Record simulation time.
    current_time = utils.get_sim_time("ns")

    # Yield a list, containing a RisingEdge trigger and a coroutine.
    yield [sample_coroutine(dut), triggers.Timer(100, "ns")]

    # Make sure that only 5 ns passed, because the sample coroutine
    # terminated first.
    new_time = utils.get_sim_time("ns")
    if int(new_time - current_time) != 5:
        raise result.TestFailure("Did not yield coroutine in list.")
github cocotb / cocotb / tests / test_cases / test_external / test_external.py View on Github external
async def test_time_in_external(dut):
    """
    Test that the simulation time does not advance if the wrapped external
    routine does not call @function
    """
    await Timer(10, units='ns')
    time = get_sim_time('ns')
    dut._log.info("Time at start of test = %d" % time)
    for i in range(100):
        dut._log.info("Loop call %d" % i)
        await external(print_sim_time)(dut, time)

    time_now = get_sim_time('ns')
    await Timer(10, units='ns')

    if time != time_now:
        raise TestFailure("Time has elapsed over external call")
github cocotb / cocotb / tests / test_cases / test_external / test_external.py View on Github external
def print_sim_time(dut, base_time):
    # We are not calling out here so time should not advance
    # And should also remain consistent
    for _ in range(5):
        _t = get_sim_time('ns')
        dut._log.info("Time reported = %d", _t)
        if _t != base_time:
            raise TestFailure("Time reported does not match base_time %f != %f" %
                              (_t, base_time))
    dut._log.info("external function has ended")
github cocotb / cocotb / cocotb / regression.py View on Github external
end = ''
        if want_color_output():
            start = ANSI.COLOR_TEST
            end = ANSI.COLOR_DEFAULT
        # Want this to stand out a little bit
        self.log.info("%sRunning test %d/%d:%s %s" %
                      (start,
                       self.count, self.ntests,
                       end,
                       self._test.__qualname__))

        # start capturing log output
        cocotb.log.addHandler(self._test_task.handler)

        self._test_start_time = time.time()
        self._test_start_sim_time = get_sim_time('ns')
        cocotb.scheduler.add_test(self._test_task)
github cocotb / cocotb / cocotb / regression.py View on Github external
def _log_sim_summary(self) -> None:
        real_time = time.time() - self.start_time
        sim_time_ns = get_sim_time('ns')
        ratio_time = self._safe_divide(sim_time_ns, real_time)

        summary = ""

        summary += "*************************************************************************************\n"
        summary += "**                                 ERRORS : {0:<39}**\n".format(self.failures)
        summary += "*************************************************************************************\n"
        summary += "**                               SIM TIME : {0:<39}**\n".format('{0:.2f} NS'.format(sim_time_ns))
        summary += "**                              REAL TIME : {0:<39}**\n".format('{0:.2f} S'.format(real_time))
        summary += "**                        SIM / REAL TIME : {0:<39}**\n".format('{0:.2f} NS/S'.format(ratio_time))
        summary += "*************************************************************************************\n"

        self.log.info(summary)
github cocotb / cocotb / cocotb / regression.py View on Github external
def handle_result(self, test: RunningTask) -> None:
        """Handle a test completing.

        Dump result to XML and schedule the next test (if any). Entered by the scheduler.

        Args:
            test: The test that completed
        """
        assert test is self._test_task

        real_time = time.time() - self._test_start_time
        sim_time_ns = get_sim_time('ns') - self._test_start_sim_time

        # stop capturing log output
        cocotb.log.removeHandler(test.handler)

        self._record_result(
            test=self._test,
            outcome=self._test_task._outcome,
            wall_time_s=real_time,
            sim_time_ns=sim_time_ns)

        self.execute()
github cocotb / cocotb / cocotb / ipython_support.py View on Github external
def in_prompt_tokens(self, cli=None):
        tokens = super().in_prompt_tokens()
        if self._show_time == self.shell.execution_count:
            tokens = [
                (Token.Comment, "sim time: {}".format(cocotb.utils.get_sim_time())),
                (Token.Text, "\n"),
            ] + tokens
        return tokens