Home › Practice › Design Verification › cocotb first steps 2: Test a D flip-flop with reset

cocotb first steps 2: Test a D flip-flop with reset

EasycocotbPythonDV

A flip-flop needs a clock. cocotb can make one for you and wait for its edges.

cocotb cheat sheet
cocotb.start_soon(Clock(dut.clk, 10, units="ns").start())   # start a clock
await RisingEdge(dut.clk)        # wait for the next rising clock edge
await FallingEdge(dut.clk)       # wait for the next falling edge
await Timer(1, units="ns")       # wait a little after an edge

Worked example. Start the clock, hold reset for one rising edge, check the output is 0:

import cocotb
from cocotb.clock import Clock
from cocotb.triggers import RisingEdge, Timer

@cocotb.test()
async def test_reset(dut):
    cocotb.start_soon(Clock(dut.clk, 10, units="ns").start())
    dut.rst.value = 1
    dut.d.value = 0
    await RisingEdge(dut.clk)
    await Timer(1, units="ns")
    assert int(dut.q.value) == 0

Your task. Test module dff_r (input clk, rst, d, output reg q). On each rising edge, q takes d; if rst is 1, q becomes 0. Check that q follows d one clock later, and that reset clears q even when d is 1. Catch two hidden buggy flip-flops.

Solve in your browser →

No install and no sign-up needed. You write your answer in the editor and Nishkam checks it.

More in this track

← cocotb first steps 1: Test an AND gatecocotb first steps 3: Test a 4-bit counter with enable →