Home › Practice › Design Verification › cocotb 2: Verify a PWM controller

cocotb 2: Verify a PWM controller

MediumcocotbPythonDVClockEarly

Concept first. For clocked circuits, start a clock with cocotb.start_soon(Clock(dut.clk, 10, units="ns").start()), step cycles with await ClockCycles(dut.clk, n) or await RisingEdge(dut.clk), and sample after flip-flops update (e.g. await FallingEdge(dut.clk)).

Verify this 3-bit PWM generator. It counts 0..7 (8-cycle frame) and drives out = (cnt < duty) on each rising edge of clk. Synchronous active-high rst clears both cnt and out to 0.

module pwm3 (
  input        clk, rst,
  input  [2:0] duty,
  output reg   out
);

Write cocotb tests that check reset behavior and measure the high-cycle count across duty settings (including duty = 0).

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 1: Test a 2-bit ALUcocotb 3: FIFO monitor & scoreboard →