Reference Card

coro: a coroutine; task: a running coroutine

Assign

dut.mysignal.value = 0xFF00

Assign immediately

dut.mysignal.setimmediatevalue(0xFF00)

Assign metavalue

dut.mysignal.value = Logic("X")
dut.mysignal.value = LogicArray("01XZ")

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.to_unsigned()
val = dut.mysignal.value.to_signed()
val = str(dut.mysignal.value)

Vector length

num_bits = len(dut.mysignal)

Check

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

Access Extended / Escaped Identifiers

dut["\\!Hello!\\"]

Logging

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

Wait time

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

Generate clock

clk = await cocotb.start(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 = await cocotb.start(coro_0()) (start coro now)
task_1 = cocotb.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.value = cocotb.handle.Force(0xFF00)

Keep value

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

Release Force/Freeze

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

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

Mark as test

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

Call non-async function from async

@cocotb.bridge()

Call async function from non-async

@cocotb.resume()

Set up TestFactory

tf = cocotb.regression.TestFactory(coro)

Add test option

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

Generate tests

tf.generate_tests()