2

I just started using cocotb and I have a problem with a small piece of code that should use a coroutine.

import cocotb
from cocotb.triggers import Timer

@cocotb.coroutine
def test(dut):
    dut.a <= 1
    dut.b <= 2
    cocotb.log.info('test')
    yield Timer(1, unit='ns')

@cocotb.test()
def add_corner(dut):
    dut.uop <= 0b0
    dut.src <= 0b01
    test(dut)
    yield Timer(1, units='ns')
    dut._log.info('done')

The simulation is created and runs through but the coroutine is never called. Neither the log message nor the assignments are executed.

I use python 3.8 and I tested a few of the examples contained in the repo. The axi_slave test works fine so I assume my setup is working.

Does anybody have a guess on what could cause the problem?

cmarqu
  • 493
  • 3
  • 10
mdxg
  • 45
  • 4

2 Answers2

2

You need to yield your coroutine, not directly call it.

cmarqu
  • 493
  • 3
  • 10
2

Note that if you use the new await syntax, you'll get a warning if you make that mistake:

import cocotb
from cocotb.triggers import Timer

# note: no coroutine decorator needed (or wanted)
async def test(dut):
    dut.a <= 1
    dut.b <= 2
    cocotb.log.info('test')
    await Timer(1, unit='ns')

@cocotb.test()
async def add_corner(dut):
    dut.uop <= 0b0
    dut.src <= 0b01
    test(dut)  # whoops - missing await
    await Timer(1, units='ns')
    dut._log.info('done')

gives

RuntimeWarning: coroutine 'test' was never awaited
Eric
  • 95,302
  • 53
  • 242
  • 374