Reference Card

coro: a coroutine; task: a running coroutine

Assign

dut.mysignal <= 0xFF00

Assign immediately

dut.mysignal.setimmediatevalue(0xFF00)

Assign metavalue

dut.mysignal <= BinaryValue("X")

Read

val = dut.mysignal.value
(mysig = dut.mysignal creates an alias/reference)

Bit slice

mybit = dut.myarray[0].value
mybits = dut.mysignal.value[0]

Convert

val = dut.mysignal.value.integer
val = dut.mysignal.value.binstr

Check

assert dut.mysignal.value == exp, "Not as expected!"

Logging

dut._log.info("Value is", dut.mysignal.value)

Wait time

await cocotb.triggers.Timer(12, "ns")

Generate clock

clk = cocotb.fork(Clock(dut.clk, 12, "ns").start())

Wait for signal edge

await cocotb.triggers.RisingEdge(dut.mysignal)
await cocotb.triggers.FallingEdge(dut.mysignal)
await cocotb.triggers.Edge(dut.mysignal)

Run coros concurrently

task_0 = cocotb.fork(coro_0())
task_1 = cocotb.scheduler.start_soon(coro)
result = await task_0

Resume on Task 0 or 1

await cocotb.triggers.First(task_0, task_1)

Resume on Task 0 and 1

await cocotb.triggers.Combine(task_0, task_1)

Kill coro

task_0.kill()

Queue write

await cocotb.queue.Queue.put(item)
cocotb.queue.Queue.put_nowait(item)

Queue read

item = await cocotb.queue.Queue.get()
item = cocotb.queue.Queue.get_nowait()

Queue attributes

queue.maxsize (None == unlimited)
queue.qsize()
queue.empty()
queue.full()

Specialized queues

.PriorityQueue, .LifoQueue

Set up event

e = cocotb.triggers.Event()

Wake up waiters

e.set()

Wait until set()

await e.wait()

Reset after set()

e.clear()

Timeout

await cocotb.triggers.with_timeout(coro, 1, "us")

Force value

dut.mysignal <= cocotb.handle.Force(0xFF00)

Keep value

dut.mysignal <= cocotb.handle.Freeze()

Release Force/Freeze

dut.mysignal <= cocotb.handle.Release()

Normal assignment with <= is a “deposit” (cocotb.handle.Deposit())

Mark as test

@cocotb.test(skip, expect_fail, timeout_time, timeout_unit)

Normal func → coro

@cocotb.external()

Allow func to block

@cocotb.function()

Set up TestFactory

tf = cocotb.regression.TestFactory(coro)

Add test option

tf.add_option("arg", ["val0", "val1"])

Generate tests

tf.generate_tests()