Cover: Clock multiplication vs division, with a divider checked exactly, a PLL checked by time after lock, and the trap of checking too early
| | | | |

Clock Multiplication vs Division: How Each Works and How to Verify It

In short: A clock divider makes a slower clock by counting reference edges, so every edge it makes lands on a reference edge and its period can be checked exactly: N times the reference period, nothing more, nothing less. A PLL multiplies the reference. Its edges fall between reference edges, it needs time to lock after every change, and in simulation its period carries rounding. So a divided clock gets an exact check, and a multiplied clock gets a timed check with a small tolerance that runs only while the PLL is locked, against the ratio it locked to. My UVM bench caught a divider counting one cycle too many, a divider changing ratio mid-period, and a PLL claiming lock too early. On VCS, the mid-period bug passed all 300 held cycles and failed 73 periods once ratios moved, and the early lock passed both lock assertions while failing 184 PLL periods.

Another bench where an AI-written model was put through the same runs: RISC-V verification with an ISS golden model, where the AI’s model caught a blind spot mine had missed.

Why do clock multiplication and division need different checks?

Most chips run many clocks from one or two crystals. Two ways get you there: divide a fast clock down, or multiply a slow clock up with a phase-locked loop (PLL). Design teams pick between them for jitter, power and the ratios they need.

For a verification engineer the question is different. Both blocks hand you a new clock, and you have to prove it is the right one. A divider and a PLL fail in different ways, so a checker that is right for one gives false alarms or misses bugs on the other.

In this post I explain how each works, then build one UVM bench that drives both from the same 100 MHz reference. I plant three bugs, two in the divider and one in the PLL’s lock detector, and show which check catches which. I also ask an AI tool to write the PLL checker and run its answer against the same design.

The picture: reading a speedometer after you change gear

Counting the stairs you climb is exact. Every step is a whole step, and if you climbed four you can say so with no doubt.

A car’s speed is different. Right after you change gear the needle swings and settles. If you read it in that moment you get a number that is wrong, and if you read it one second after the car tells you “settled” you get the right one, give or take the width of the needle.

A divider is the stairs: count reference edges, check exactly. A PLL is the speedometer: wait for lock, then read the period with a small tolerance. Read it early and you get false alarms. Trust a lock signal that comes early and you miss a real bug.

What will you learn?

By the end of this post you will be able to:

  • Explain how a clock divider and a PLL each make a new clock, and what each one costs.
  • Check a divided clock exactly, period by period, against the ratio that was in force when the period started.
  • Check a multiplied clock by measuring time, with a tolerance sized to your simulator’s time precision.
  • Gate PLL checks on lock correctly, using the ratio the PLL locked to rather than the one on the pin right now.
  • Write stimulus that changes ratios mid-period, since that is where divider bugs hide.
  • Review an AI-written clock checker and find the window where it raises false alarms.

How does clock division work, and where does it break?

A divider is a counter on the reference clock. It counts N reference edges, then starts again, and drives its output high for part of the count and low for the rest. With a 100 MHz reference and N = 4 you get 25 MHz.

Because the output only ever changes on a reference edge, three things follow:

  • Only integer ratios. You get ref/2, ref/3, ref/4 and so on. Fractional dividers exist, but they get there by mixing two integer ratios, so single periods differ from the average.
  • Odd ratios are not 50% duty by default. With N = 5, a counter on one edge can hold the output high for 3 cycles and low for 2. A 50% duty at odd N needs the falling edge too.
  • Changing N is risky. If the new ratio takes effect in the middle of a period, that period comes out short or long, and a short one is a glitch. A glitch-free divider reads the new ratio only when a period starts.

The good news for verification: a divider is fully synchronous to its reference. Every edge it makes is a reference edge, so you know exactly when each edge should come.

How does clock multiplication work, and where does it break?

A PLL multiplies. A phase detector compares the reference with a divided-down copy of the output, a loop filter smooths the error, and a voltage-controlled oscillator (VCO) speeds up or slows down until the two match. With a divide-by-M in the feedback path, the output settles at M times the reference. Put a divider in front of the reference too and you get ratios like M/R.

That loop has costs a divider does not:

  • Lock time. After power-up or a ratio change, the output wanders until the loop settles. A lock detector raises a lock signal when it judges the output stable, and logic downstream waits for it.
  • The lock signal can lie. A lock detector that fires too early lets the design run on a clock that is still off frequency. This is exactly the kind of bug a checker must catch.
  • Edges between reference edges. At M = 4 you get four output periods per reference period, so most edges do not line up with anything else in the testbench.

In practice chips use both: a PLL makes one fast, clean clock, and dividers make the slower clocks from it.

Clock division chain from 100 MHz down to 5 MHz, and a PLL multiplying 25 MHz by 8 to 200 MHz then dividing as needed
Division gives integer ratios below the reference; a PLL can go above it, and dividers then take it down where needed.

What does that mean for a testbench?

A PLL is analog, so in RTL simulation you never simulate the real loop. You use a behavioural model that produces the right output clock after a lock time. The model is what your checker sees, so the checks are about what the digital side depends on: the output period, the duty cycle, and whether the lock signal tells the truth.

Waveforms: divider edges sit on reference edges while PLL edges fall between them; after M changes from 4 to 8 the PLL output keeps the old rate until pll_locked drops, settles while unlocked, and is checked only once locked
The divider can be checked edge by edge. The PLL needs a timed check, and only while it is locked.

The second panel shows the trap. The new ratio appears on pll_m on a falling reference edge. The PLL only sees it on the next rising edge, so for half a reference cycle pll_locked is still high while the output still runs at the old ratio. A checker that reads pll_m live and trusts pll_locked flags those periods as wrong. The fix is to remember the ratio at the moment lock went high.

DividerPLL
Ratiosintegers, below the referenceM, or M/R, above or below
Output edgeson reference edgesbetween reference edges
After a ratio changenext period, if glitch-freeafter lock time
How to check the periodexact: N x reference periodmeasured in time, small tolerance
When to checkevery period after resetonly while locked, against the locked ratio
Typical bugsoff by one, odd-N duty, glitch on changelock too early, wrong ratio, unstable after lock

What does the testbench look like?

One file, tav_clock_mult_div_uvm.sv, holds both designs and the whole bench. A 100 MHz reference clock (10 ns period) drives two clock sources:

  • tav_clk_div, an RTL divider. div_out is the reference divided by div_n (2 to 15). It reads the ratio when a period starts and holds it for that period. The output is high for (N + 1) / 2 reference cycles.
  • tav_pll_model, a behavioural PLL. pll_out is the reference times pll_m (1 to 8). After reset or a change it runs about 25% slow, settles over 8 reference cycles, and then raises pll_locked.
Testbench architecture: a UVM test with a hold phase and a random phase drives div_n and pll_m into tav_clk_div and tav_pll_model; a monitor and scoreboard check the divider exactly and the PLL by time behind a lock gate, with SVA on the lock handshake
One reference, two clocks, two kinds of check.

The bench has these parts:

  • Sequences. Phase 1 (tav_hold_seq) holds N = 4 and M = 4 for 300 reference cycles, the kind of test that “proves the clocks work”. Phase 2 (tav_random_seq) makes 80 random changes. Each holds for 3 to 40 cycles, so divider changes land mid-period, and M changes in about 3 of every 10.
  • Driver. Holds reset for 5 cycles, then applies each setting on the falling reference edge, so nothing races the rising edge the designs use.
  • Monitor. Turns edges into one measurement per full period: its length, its high time and the ratio that applies. For the divider, that is div_n at the rising edge that started the period. For the PLL, it is pll_m at the moment pll_locked rose, and a period is only sent if pll_locked was high at the rising edge before it, at its own start and at its end.
  • Scoreboard. The expected values come from the spec. Divider: exactly N x 10 ns, high for (N + 1) / 2 x 10 ns. PLL: 10 ns / M and a 50% duty, each within 2 ps, because the simulator rounds every delay to 1 ps. It counts mismatches per clock and per phase.
  • Assertions in the interface. A_LOCK_DROP: when pll_m changes, pll_locked is low on the next reference edge. A_LOCK_TIME: lock comes back within 16 reference cycles, unless pll_m changes again first.

The planted bugs, one per run:

OptionWhereBug
(none)the clean design
+BUG=1dividercounts one reference cycle too many, so it divides by N + 1
+BUG=2dividera new ratio takes effect at once instead of at the end of the period
+BUG=3PLLthe lock detector fires 3 reference cycles before the output has settled

How do you check a divided clock and a multiplied clock in UVM?

Here is the whole bench. To run it on EDA Playground, pick Synopsys VCS and UVM 1.2, paste the file into testbench.sv, leave design.sv empty, and set the top module to tav_top. Add +BUG=1, +BUG=2 or +BUG=3 in the run options to plant a bug. The same file is in the tav-verification-basics repo on GitHub.

Three parts carry the lesson. watch_div() in the monitor takes the ratio at the edge that starts a period. watch_pll() gates on lock at three rising edges and uses the ratio saved when lock rose. And the scoreboard’s tolerance is 0.5 ps for the divider, which is exact in practice, but 2 ps for the PLL, because #(half) with half = 10 / 6 ns rounds to the nearest picosecond.

// tav_clock_mult_div_uvm.sv : checking a divided clock and a multiplied clock in one UVM bench (UVM 1.2)
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 25 Nov 2026.
// The Art of Verification | theartofverification.com
//
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// Two clock sources run from the same 100 MHz reference (10 ns period):
//   DIV  an RTL divider: div_out = ref / N, N from 2 to 15
//   PLL  a behavioural PLL model: pll_out = ref x M, M from 1 to 8, with a lock signal
// The bench checks them in two different ways, and that difference is the lesson:
//   DIV  every edge lands on a reference edge, so each period must be exactly N x 10 ns.
//   PLL  edges land between reference edges, so each period is measured in time, checked
//        against 10 ns / M with a small tolerance, and only once the PLL says it is locked.
//
// Run options (one per run):
//   (none)    the clean design
//   +BUG=1    DIV: counts one reference cycle too many, so it divides by N + 1
//   +BUG=2    DIV: a new ratio takes effect at once instead of at the end of the current period
//   +BUG=3    PLL: the lock detector fires 3 reference cycles before the output has settled
//
// Spec:
//   DIV  A period starts on a rising edge of div_out. The ratio N is read at that edge and
//        holds for the whole period. div_out is high for (N + 1) / 2 reference cycles
//        (integer divide) and low for the rest. Ratios below 2 count as 2. The first period
//        starts on the first reference edge after reset.
//   PLL  After reset, or after pll_m changes, pll_locked goes low on the next reference edge
//        and comes back high within 16 reference cycles. While pll_locked is high, every
//        period of pll_out is 10 ns / M and the duty cycle is 50%, both within 2 ps.
//        M = 0 counts as 1.
//
// Phase 1 holds N = 4 and M = 4 for 300 reference cycles. Phase 2 changes the settings
// 80 times at random. The scoreboard prints mismatches for each phase.

`timescale 1ns/1ps
`include "uvm_macros.svh"
import uvm_pkg::*;

// ---------------------------------------------------------------------------
// The designs
// ---------------------------------------------------------------------------
module tav_clk_div (input logic ref_clk, rst_n, input logic [3:0] div_n,
                    output logic div_out, input int bug);
  logic [3:0] cnt, cur_n;
  int nn, use_n;
  always_comb begin
    nn    = (div_n < 2) ? 2 : div_n;
    use_n = (bug == 2) ? nn : cur_n;                  // BUG=2: the new ratio is used at once
  end
  always_ff @(posedge ref_clk or negedge rst_n)
    if (!rst_n) begin cnt <= 15; cur_n <= 2; div_out <= 0; end   // so the first edge starts a period
    else if (cnt >= use_n - 1 + ((bug == 1) ? 1 : 0)) begin   // BUG=1: one extra cycle per period
      cnt <= 0; cur_n <= nn; div_out <= 1;           // a new period starts and reads the ratio
    end else begin
      cnt     <= cnt + 1;
      div_out <= (cnt + 1) < ((use_n + 1) / 2);
    end
endmodule

// A PLL is analog. In a testbench it is a behavioural model like this one: it measures the
// reference period, runs an oscillator at ref / M, starts about 25% slow after a change and
// settles over 8 reference cycles, then raises pll_locked.
module tav_pll_model #(parameter int SETTLE = 8)
                     (input logic ref_clk, rst_n, input logic [3:0] pll_m,
                      output logic pll_out, pll_locked, input int bug);
  int      cur_m, mm, settle;
  realtime last_t = -1, tref = 10.0, half = 5.0;

  initial pll_out = 0;
  always #(half) pll_out = ~pll_out;                 // the oscillator

  always @(posedge ref_clk or negedge rst_n)
    if (!rst_n) begin
      cur_m = 1; settle = SETTLE; pll_locked <= 0;
    end else begin
      if (last_t >= 0) tref = $realtime - last_t;    // measure the reference
      last_t = $realtime;
      mm = (pll_m == 0) ? 1 : pll_m;
      if (mm != cur_m) begin cur_m = mm; settle = SETTLE; end
      else if (settle > 0) settle--;
      pll_locked <= (bug == 3) ? (settle <= 3) : (settle == 0);   // BUG=3: lock fires 3 cycles early
      half = tref / (2.0 * cur_m) * (1.0 + settle / 32.0);
    end
endmodule

// ---------------------------------------------------------------------------
// Interface: the pins, plus the lock handshake rules as assertions
// ---------------------------------------------------------------------------
interface tav_clk_if (input logic ref_clk);
  logic       rst_n;
  logic [3:0] div_n, pll_m;
  logic       div_out, pll_out, pll_locked;

  // Each assertion reports its first 3 fails and then switches itself off.
  int tav_fails [2];   // A_LOCK_DROP, A_LOCK_TIME

  A_LOCK_DROP: assert property (@(posedge ref_clk) disable iff (!rst_n)
                 $changed(pll_m) |=> !pll_locked)
    else begin
      tav_fails[0]++; `uvm_error("SVA", "A_LOCK_DROP: pll_m changed but pll_locked stayed high")
      if (tav_fails[0] == 3) $assertoff(0, A_LOCK_DROP);
    end
  A_LOCK_TIME: assert property (@(posedge ref_clk) disable iff (!rst_n)
                 ($rose(rst_n) || $changed(pll_m)) |-> ##[1:16] (pll_locked || $changed(pll_m)))
    else begin
      tav_fails[1]++; `uvm_error("SVA", "A_LOCK_TIME: no lock within 16 reference cycles")
      if (tav_fails[1] == 3) $assertoff(0, A_LOCK_TIME);
    end
endinterface

// The test sets this so every measurement knows which phase it came from.
int tav_phase = 0;

// ---------------------------------------------------------------------------
// Stimulus item: one setting of both clocks, held for some reference cycles
// ---------------------------------------------------------------------------
class tav_clk_item extends uvm_sequence_item;
  rand bit [3:0] div_n;
  rand bit [3:0] pll_m;
  rand bit       chg_pll;
  rand int       hold;
  constraint c_range { div_n inside {[2:15]}; pll_m inside {[1:8]}; hold inside {[3:40]};
                       chg_pll dist {1 := 3, 0 := 7}; }
  `uvm_object_utils(tav_clk_item)
  function new(string name = "tav_clk_item");
    super.new(name);
  endfunction
endclass

// ---------------------------------------------------------------------------
// Measurement: one full period of one clock
// ---------------------------------------------------------------------------
class tav_clk_meas extends uvm_object;
  bit      is_pll;     // 0 = DIV, 1 = PLL
  realtime t_end, period, high;
  int      ratio;      // N for DIV, M for PLL
  int      phase;
  `uvm_object_utils(tav_clk_meas)
  function new(string name = "tav_clk_meas");
    super.new(name);
  endfunction
endclass

// ---------------------------------------------------------------------------
// Sequences
// ---------------------------------------------------------------------------
class tav_hold_seq extends uvm_sequence #(tav_clk_item);
  `uvm_object_utils(tav_hold_seq)
  function new(string name = "tav_hold_seq");
    super.new(name);
  endfunction
  task body();
    req = tav_clk_item::type_id::create("req");
    start_item(req);
    req.div_n = 4; req.pll_m = 4; req.hold = 300;
    finish_item(req);
  endtask
endclass

class tav_random_seq extends uvm_sequence #(tav_clk_item);
  `uvm_object_utils(tav_random_seq)
  int n_items = 80;
  function new(string name = "tav_random_seq");
    super.new(name);
  endfunction
  task body();
    bit [3:0] prev_m = 4;
    repeat (n_items) begin
      req = tav_clk_item::type_id::create("req");
      start_item(req);
      if (!req.randomize() with { chg_pll -> pll_m != local::prev_m;
                                  !chg_pll -> pll_m == local::prev_m; })
        `uvm_fatal("RAND", "randomize failed")
      prev_m = req.pll_m;
      finish_item(req);
    end
  endtask
endclass

// ---------------------------------------------------------------------------
// Driver: holds reset for 5 cycles, then changes settings on the falling reference edge
// ---------------------------------------------------------------------------
class tav_clk_driver extends uvm_driver #(tav_clk_item);
  `uvm_component_utils(tav_clk_driver)
  virtual tav_clk_if vif;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_clk_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction
  task run_phase(uvm_phase phase);
    vif.rst_n <= 0; vif.div_n <= 4; vif.pll_m <= 4;
    repeat (5) @(negedge vif.ref_clk);
    vif.rst_n <= 1;
    forever begin
      seq_item_port.get_next_item(req);
      @(negedge vif.ref_clk);
      vif.div_n <= req.div_n; vif.pll_m <= req.pll_m;
      repeat (req.hold - 1) @(negedge vif.ref_clk);
      seq_item_port.item_done();
    end
  endtask
endclass

// ---------------------------------------------------------------------------
// Monitor: turns edges into period measurements
// ---------------------------------------------------------------------------
class tav_clk_monitor extends uvm_monitor;
  `uvm_component_utils(tav_clk_monitor)
  virtual tav_clk_if vif;
  uvm_analysis_port #(tav_clk_meas) ap;
  // reach counts
  int div_ratio_changed_inside, pll_skipped_unlocked, lock_events;
  int lock_m;

  function new(string name, uvm_component parent);
    super.new(name, parent);
    ap = new("ap", this);
  endfunction
  function void build_phase(uvm_phase phase);
    if (!uvm_config_db#(virtual tav_clk_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction

  function void send(bit is_pll, realtime t_end, period, high, int ratio);
    tav_clk_meas m = tav_clk_meas::type_id::create("m");
    m.is_pll = is_pll; m.t_end = t_end; m.period = period; m.high = high;
    m.ratio = ratio; m.phase = tav_phase;
    ap.write(m);
  endfunction

  // DIV: the ratio for a period is the one on div_n at the rising edge that starts it
  task watch_div();
    realtime prev_rise = -1, fall_t = -1;
    int prev_n, n_now;
    forever begin
      @(vif.div_out or negedge vif.rst_n);
      if (vif.rst_n !== 1'b1) begin prev_rise = -1; fall_t = -1; end
      else if (vif.div_out === 1'b1) begin
        n_now = (vif.div_n < 2) ? 2 : vif.div_n;
        if (prev_rise >= 0 && fall_t > prev_rise) begin
          send(0, $realtime, $realtime - prev_rise, fall_t - prev_rise, prev_n);
          if (n_now != prev_n) div_ratio_changed_inside++;
        end
        prev_rise = $realtime; prev_n = n_now;
      end else fall_t = $realtime;
    end
  endtask

  // PLL: check a period only if pll_locked was high at the rising edge before it, at its
  // own start and at its end. That keeps out every period the oscillator was still settling in.
  task watch_pll();
    realtime r0 = -1, r1 = -1, fall_t = -1;
    bit l0, l1, l2;
    fork
      forever begin @(negedge vif.pll_out); fall_t = $realtime; end
      forever begin @(posedge vif.pll_locked); lock_m = (vif.pll_m == 0) ? 1 : vif.pll_m; lock_events++; end
    join_none
    forever begin
      @(posedge vif.pll_out);
      l2 = (vif.pll_locked === 1'b1);
      if (r0 >= 0) begin
        if (l0 && l1 && l2 && fall_t > r1) send(1, $realtime, $realtime - r1, fall_t - r1, lock_m);
        else pll_skipped_unlocked++;
      end
      r0 = r1; l0 = l1; r1 = $realtime; l1 = l2;
    end
  endtask

  task run_phase(uvm_phase phase);
    fork
      watch_div();
      watch_pll();
    join
  endtask
endclass

// ---------------------------------------------------------------------------
// Scoreboard: the expected period and high time, from the spec
// ---------------------------------------------------------------------------
class tav_clk_scoreboard extends uvm_scoreboard;
  `uvm_component_utils(tav_clk_scoreboard)
  uvm_analysis_imp #(tav_clk_meas, tav_clk_scoreboard) imp;
  realtime TREF = 10.0;    // the reference period in the spec
  realtime TOL  = 0.002;   // 2 ps
  int checks[2], mism[2], shown[2];
  int mism_ph[2][3];       // [DIV/PLL][phase]
  realtime worst_pll_err;  // largest |error| on a passing PLL period
  bit div_seen[16], pll_seen[9];

  function new(string name, uvm_component parent);
    super.new(name, parent);
    imp = new("imp", this);
  endfunction

  function realtime tav_abs(realtime x);
    return (x < 0) ? -x : x;
  endfunction

  function void write(tav_clk_meas m);
    realtime exp_p, exp_h, tol;
    string nm = m.is_pll ? "PLL" : "DIV";
    if (m.is_pll) begin
      exp_p = TREF / m.ratio; exp_h = exp_p / 2.0; tol = TOL;
      pll_seen[m.ratio] = 1;
    end else begin
      exp_p = TREF * m.ratio; exp_h = TREF * ((m.ratio + 1) / 2); tol = 0.0005;   // exact
      div_seen[m.ratio] = 1;
    end
    checks[m.is_pll]++;
    if (tav_abs(m.period - exp_p) > tol || tav_abs(m.high - exp_h) > tol) begin
      mism[m.is_pll]++; mism_ph[m.is_pll][m.phase]++;
      if (shown[m.is_pll] < 3) begin
        shown[m.is_pll]++;
        `uvm_error("MISMATCH", $sformatf("%s period ending %0.3f ns, ratio %0d: period %0.3f ns, high %0.3f ns; expected %0.3f and %0.3f",
                   nm, m.t_end, m.ratio, m.period, m.high, exp_p, exp_h))
      end
    end else if (m.is_pll) begin
      if (tav_abs(m.period - exp_p) > worst_pll_err) worst_pll_err = tav_abs(m.period - exp_p);
    end
  endfunction

  function void tav_print_results(tav_clk_monitor mon, int f0, f1);
    int nd = 0, np = 0;
    foreach (div_seen[i]) nd += div_seen[i];
    foreach (pll_seen[i]) np += pll_seen[i];
    `uvm_info("RESULT", $sformatf("DIV periods checked=%0d mismatches=%0d (phase 1: %0d, phase 2: %0d) -> %s",
              checks[0], mism[0], mism_ph[0][1], mism_ph[0][2], (mism[0] == 0) ? "PASS" : "FAIL"), UVM_NONE)
    `uvm_info("RESULT", $sformatf("PLL periods checked=%0d mismatches=%0d (phase 1: %0d, phase 2: %0d) -> %s",
              checks[1], mism[1], mism_ph[1][1], mism_ph[1][2], (mism[1] == 0) ? "PASS" : "FAIL"), UVM_NONE)
    `uvm_info("RESULT", $sformatf("Lock assertions: A_LOCK_DROP fails=%0d, A_LOCK_TIME fails=%0d -> %s",
              f0, f1, (f0 + f1 == 0) ? "PASS" : "FAIL"), UVM_NONE)
    `uvm_info("REACH", $sformatf("DIV ratios seen=%0d of 14, periods with a ratio change inside=%0d",
              nd, mon.div_ratio_changed_inside), UVM_NONE)
    `uvm_info("REACH", $sformatf("PLL ratios seen=%0d of 8, lock events=%0d, periods skipped while unlocked=%0d, worst passing error=%0.1f ps",
              np, mon.lock_events, mon.pll_skipped_unlocked, worst_pll_err * 1000.0), UVM_NONE)
  endfunction
endclass

// ---------------------------------------------------------------------------
// Agent, environment, test
// ---------------------------------------------------------------------------
class tav_clk_agent extends uvm_agent;
  `uvm_component_utils(tav_clk_agent)
  uvm_sequencer #(tav_clk_item) sqr;
  tav_clk_driver  drv;
  tav_clk_monitor mon;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    sqr = uvm_sequencer#(tav_clk_item)::type_id::create("sqr", this);
    drv = tav_clk_driver::type_id::create("drv", this);
    mon = tav_clk_monitor::type_id::create("mon", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    drv.seq_item_port.connect(sqr.seq_item_export);
  endfunction
endclass

class tav_clk_env extends uvm_env;
  `uvm_component_utils(tav_clk_env)
  tav_clk_agent      agt;
  tav_clk_scoreboard scb;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    agt = tav_clk_agent::type_id::create("agt", this);
    scb = tav_clk_scoreboard::type_id::create("scb", this);
  endfunction
  function void connect_phase(uvm_phase phase);
    agt.mon.ap.connect(scb.imp);
  endfunction
endclass

class tav_clock_test extends uvm_test;
  `uvm_component_utils(tav_clock_test)
  tav_clk_env env;
  virtual tav_clk_if vif;
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  function void build_phase(uvm_phase phase);
    env = tav_clk_env::type_id::create("env", this);
    if (!uvm_config_db#(virtual tav_clk_if)::get(this, "", "vif", vif))
      `uvm_fatal("NOVIF", "no virtual interface")
  endfunction
  task run_phase(uvm_phase phase);
    tav_hold_seq   h = tav_hold_seq::type_id::create("h");
    tav_random_seq s = tav_random_seq::type_id::create("s");
    phase.raise_objection(this);
    tav_phase = 1;
    h.start(env.agt.sqr);
    tav_phase = 2;
    s.start(env.agt.sqr);
    repeat (40) @(negedge vif.ref_clk);   // let the last periods finish
    env.scb.tav_print_results(env.agt.mon, vif.tav_fails[0], vif.tav_fails[1]);
    phase.drop_objection(this);
  endtask
endclass

// ---------------------------------------------------------------------------
// Top
// ---------------------------------------------------------------------------
module tav_top;
  logic ref_clk = 0;
  int   BUG = 0;
  always #5 ref_clk = ~ref_clk;   // 100 MHz reference

  tav_clk_if vif (ref_clk);
  tav_clk_div   u_div (.ref_clk(ref_clk), .rst_n(vif.rst_n), .div_n(vif.div_n),
                       .div_out(vif.div_out), .bug(BUG));
  tav_pll_model u_pll (.ref_clk(ref_clk), .rst_n(vif.rst_n), .pll_m(vif.pll_m),
                       .pll_out(vif.pll_out), .pll_locked(vif.pll_locked), .bug(BUG));

  initial begin
    void'($value$plusargs("BUG=%d", BUG));
    `uvm_info("TOP", $sformatf("Running with BUG=%0d", BUG), UVM_NONE)
    uvm_config_db#(virtual tav_clk_if)::set(null, "uvm_test_top", "vif", vif);
    uvm_config_db#(virtual tav_clk_if)::set(null, "uvm_test_top.env.agt.*", "vif", vif);
    run_test("tav_clock_test");
  end
endmodule

What does the output show?

I ran the bench four times on Synopsys VCS X-2025.06-SP1 with UVM 1.2 on EDA Playground. The clean run:

UVM_INFO testbench.sv(328) @ 21050000: uvm_test_top.env.scb [RESULT] DIV periods checked=392 mismatches=0 (phase 1: 0, phase 2: 0) -> PASS
UVM_INFO testbench.sv(330) @ 21050000: uvm_test_top.env.scb [RESULT] PLL periods checked=8429 mismatches=0 (phase 1: 0, phase 2: 0) -> PASS
UVM_INFO testbench.sv(332) @ 21050000: uvm_test_top.env.scb [RESULT] Lock assertions: A_LOCK_DROP fails=0, A_LOCK_TIME fails=0 -> PASS
UVM_INFO testbench.sv(334) @ 21050000: uvm_test_top.env.scb [REACH] DIV ratios seen=13 of 14, periods with a ratio change inside=71
UVM_INFO testbench.sv(336) @ 21050000: uvm_test_top.env.scb [REACH] PLL ratios seen=7 of 8, lock events=18, periods skipped while unlocked=557, worst passing error=0.7 ps

Everything passed. Two numbers are worth a second look. The worst passing PLL error was 0.7 ps: that is the 1 ps rounding at work, and it is why an exact compare would have failed a correct PLL. And 557 PLL periods were skipped because the PLL was not locked. Those are periods a careless checker would have flagged.

The REACH lines also show what the random test did not reach: one divider ratio of 14 and one PLL ratio of 8 never came up. With 80 changes that is expected, and it is the first thing I would fix with a longer run or a coverage goal.

Results grid of four VCS runs: clean design passes all checks; BUG=1 fails all 311 divider periods; BUG=2 fails 73 divider periods, all in phase 2; BUG=3 fails 184 of 8,631 PLL periods while both lock assertions pass
Four runs, three bugs, and which check caught each one.

+BUG=1, divide by N + 1. All 311 divider periods were wrong, 59 of them in the held phase. The first message says it plainly:

UVM_ERROR testbench.sv(316) @ 105000: uvm_test_top.env.scb [MISMATCH] DIV period ending 105.000 ns, ratio 4: period 50.000 ns, high 20.000 ns; expected 40.000 and 20.000

One reference period too long, every time. Any test catches this one.

+BUG=2, ratio taken mid-period. 73 of 404 divider periods were wrong, and every one was in phase 2. The 300 held cycles of phase 1 passed with no error at all. A directed test that sets a ratio and holds it would have signed this divider off. The first mismatch shows the cost:

UVM_ERROR testbench.sv(316) @ 3145000: uvm_test_top.env.scb [MISMATCH] DIV period ending 3145.000 ns, ratio 4: period 90.000 ns, high 70.000 ns; expected 40.000 and 20.000

The period started with ratio 4, the ratio changed partway through, and the divider stretched that period to 90 ns.

+BUG=3, PLL locks early. 184 of 8,631 PLL periods were wrong, 11 of them right after the first lock in phase 1. Both lock assertions passed: lock dropped on every change and came back within 16 cycles. It just came back 3 cycles too soon:

UVM_ERROR testbench.sv(316) @ 111000: uvm_test_top.env.scb [MISMATCH] PLL period ending 111.203 ns, ratio 4: period 2.734 ns, high 1.367 ns; expected 2.500 and 1.250

2.734 ns against 2.5 ns is the model still running about 9% slow while claiming lock. Only the period check behind the lock gate caught it.

Each bug stayed in its own lane: the divider bugs never touched the PLL counts, and the PLL bug never touched the divider counts. That is what you want from a bench with two clocks: a failure tells you which clock to open first.

What are the common mistakes when verifying clocks?

Checking the PLL before it locks. Periods during settling are wrong by design. A checker that looks at them fails a good PLL. Gate every PLL check on lock, and drop the period that straddles the lock edge.

Trusting the lock signal to pick the ratio. The ratio pin can change while lock is still high. Save the ratio when lock rises and check against that one. The AI checker later in this post makes exactly this mistake.

Using only the lock signal to judge the PLL. In this bench both lock assertions pass with +BUG=3. Lock dropped on time and came back in time; it just came back too early. Only measuring the period after lock catches it.

Exact compares on a multiplied clock. With a 1ns/1ps timescale, 10 ns / 3 cannot be represented, so each half period rounds to 1.667 ns and the period comes out 3.334 ns. An exact compare against 3.3333 ns fails every period. Size the tolerance to your time precision. With a 1ns/1ns timescale it is worse: the delay rounds to a whole nanosecond and the model cannot make most ratios at all.

Holding the ratio still. Phase 1 of this bench holds N = 4 for 300 cycles. A divider that changes ratio mid-period passes that test perfectly. Change ratios at random points, including in the middle of a period, and count how often that happened.

Measuring from the wrong edge. A period runs from one rising edge to the next, and the ratio that applies is the one read at the first of the two. Reading div_n at the end of the period gives false alarms on every change.

Driving settings on the active edge. If the test changes div_n on the same rising edge the divider samples, which value wins depends on scheduling. Drive on the falling edge.

How do you debug a clock mismatch?

  1. Read the first MISMATCH line: which clock, the time the period ended, the ratio, and the measured versus expected period and high time.
  2. Compare the measured period with the expected one. Off by exactly one reference period means a counter bound (BUG=1). A short or long period near a ratio change means the ratio is not latched (BUG=2). Off by a few percent right after lock means the PLL locked early (BUG=3).
  3. Open the waveform at that time with the reference clock, the output, the ratio pin and, for the PLL, the lock signal.
  4. Find where the ratio changed relative to the period’s start, or where lock rose relative to the last period that settled.
  5. Check the phase split in the RESULT line: a bug that appears only in phase 2 depends on ratio changes.

Can AI write the PLL checker for you?

Task: write the PLL output checker from the spec alone.

Tool: Claude, in a fresh session with no files and no tools, so it saw only the prompt.

Prompt:

Write a SystemVerilog checker module for a PLL output clock. Ports: input ref_clk (100 MHz, 10 ns period), input rst_n, input [3:0] pll_m (multiply ratio, 1 to 8), input pll_out, input pll_locked. Spec: pll_out frequency = ref_clk frequency x pll_m. After reset or after pll_m changes, pll_locked goes low and comes back high within 16 ref_clk cycles. While pll_locked is high, every period of pll_out must be 10 ns / M with 50% duty cycle. The PLL is a behavioural model in simulation (timescale 1ns/1ps). The checker should print an error for every bad period, keep a count of checked periods and errors, and print a summary from a final block. Plain SystemVerilog module, no UVM, so I can instantiate it next to the PLL model.

What came back: a 224-line module, pll_out_checker, that looked careful. It measured periods and high time with $realtime, used a 5 ps tolerance and explained why (the 1 ps rounding of 10 / M), restarted its measurement when reset, lock or pll_m changed, added a stall check for a clock that stops while locked, checked lock time, and printed a PASS or FAIL summary. Here is the part that decides when it checks:

  // Period/duty checks are only active in this state
  function automatic bit checking();
    return (rst_n === 1'b1) && (pll_locked === 1'b1) && m_ok(pll_m);
  endfunction

  // ... (lines left out)

  always @(negedge rst_n or negedge pll_locked or pll_m) begin
    rise_valid    = 0;
    stall_flagged = 0;
  end

  // ... (lines left out)

  always @(posedge pll_out) begin
    realtime now, period, expd;
    now           = $realtime;
    last_edge     = now;
    stall_flagged = 0;

    if (pll_out !== 1'b1 || !checking()) begin
      rise_valid = 0;
    end
    else begin
      if (rise_valid) begin
        period = now - last_rise;
        expd   = exp_period(pll_m);
        n_periods++;
        if (absr(period - expd) > TOL_NS) begin
          n_period_err++;
          $error("[PLL_CHK] %t: bad pll_out period %0.4f ns, expected %0.4f ns (M=%0d)",
                 now, period, expd, pll_m);
        end
      end
      last_rise  = now;
      rise_valid = 1;
    end
  end

How I validated it: I did not change a line. tav_clock_ai_check.sv puts the AI module next to the same divider and PLL model, drives the same plan (300 cycles held, then 80 random changes), and runs on VCS twice: once on the clean model, where every error is a false alarm, and once with +BUG=3, where errors are the real bug.

What happened: on the clean PLL model, the AI checker printed its summary and said FAIL:

==================== PLL_CHK SUMMARY ====================
  Periods checked        : 8371
  Period errors          : 33
  Duty-cycle errors      : 43
  Stall errors           : 6
  Lock events seen       : 28
  Lock errors            : 0
  Illegal pll_m errors   : 0
  TOTAL ERRORS           : 82
  RESULT                 : FAIL
=========================================================

All 82 are false alarms. The design is the one my bench passed with 8,429 periods checked and zero wrong. pll_m changed 27 times in this run, and every error sits in the half cycle right after one of those changes. A typical one:

[PLL_CHK]  3474.990 ns: bad pll_out period 2.5000 ns, expected 10.0000 ns (M=1)

2.5 ns is exactly the old ratio, M = 4. The checker already wanted M = 1, and the PLL had not seen the change yet.

With +BUG=3 it did catch the real bug: 808 errors, 395 on the period and 407 on duty cycle, starting at 107 ns right after the first early lock. But those 808 include the same false alarms, and nothing in the log tells you which is which. Its own lock check also passed the bug, 0 lock errors, just like my two assertions. I also ran it with +BUG=1 and +BUG=2. Those are divider bugs, so the PLL checker printed the same 82 errors as the clean run, which is a useful sanity check that the false alarms come from the checker, not the design.

Where it failed: look at checking(). It trusts pll_locked and reads pll_m live. The bench changes pll_m on a falling reference edge, and the PLL only reacts on the next rising edge. For those 5 ns pll_locked is still high and the output still runs at the old ratio, but the checker already expects the new one. Every period in that window is flagged. The restart on a pll_m change does not help, because it clears the old start point and then measures the very next period, which is still at the old ratio.

The fix is small once you see it: save the ratio when pll_locked rises, and check against that saved value until lock drops. That is what lock_m does in my monitor. The AI wrote clean, readable code and even reasoned about picosecond rounding. It got the one thing wrong that needs a timing diagram of this particular design, and only running it against the design showed that.

Lesson: use AI for the first draft of a checker, then run it on a design you know is clean before you trust a single error it prints. A checker that cries wolf on good hardware is worse than none, because people learn to ignore it.

Interview questions on clock division and multiplication

  1. Why can’t a simple divider make a 1.5x slower clock? It can only change its output on reference edges, so every period is a whole number of reference periods. A fractional divider gets 1.5 on average by mixing ratios of 1 and 2, so single periods are not equal.
  2. How would you get a 50% duty cycle from a divide-by-5? Use both edges of the reference: make the counter output high for 2 of the 5 cycles, then OR it with a copy delayed by half a reference cycle on the falling edge, so the high time becomes 2.5 cycles.
  3. What is a glitch-free clock divider? One that takes a new ratio only at the start of a period, so no period is ever shorter than either the old or the new ratio allows.
  4. How do you verify a PLL in RTL simulation when the PLL is analog? You verify the behavioural model’s contract: output period and duty after lock, lock time, lock dropping on a change, and what the logic does while unlocked. The analog design is verified separately.
  5. Why would an exact period compare fail on a correct PLL model? Simulator time has a precision. At 1ns/1ps, 10 ns / 3 rounds, so the measured period is off by a fraction of a picosecond. Use a tolerance sized to the precision.
  6. Your PLL checker reports errors right after every ratio change, on a design you trust. What do you look at first? Whether the checker reads the ratio live while lock is still high. Save the ratio at lock instead.
  7. Lock assertions pass but the design misbehaves just after lock. Why? The lock signal can rise on time and still be early. Only a frequency check after lock tells you the output had settled.

Practice challenge

Take tav_clock_mult_div_uvm.sv and try these, in order:

  1. Change the divider so odd ratios have a 50% duty cycle using the falling reference edge. Update the scoreboard’s expected high time from the spec, not from your RTL, and rerun all three bugs.
  2. Add +BUG=4: pll_locked drops for one reference cycle while pll_m is stable. Write an assertion that catches it, and check whether the period check alone would have.
  3. Change the timescale to 1ns/1ns and run the clean design. Explain every mismatch you see.
  4. Delete phase 2 and run +BUG=2. Then write down, in one sentence, why a test that holds settings still can pass a broken divider.

Frequently asked questions

What is the difference between clock multiplication and clock division?

Division makes a slower clock by counting reference edges, so it only gives whole-number ratios below the reference. Multiplication uses a PLL to make a faster clock, or a non-integer ratio, by locking an oscillator to the reference. Division is simple and instant; multiplication needs lock time.

Why does a PLL need lock time?

Its oscillator is steered by a feedback loop. After power-up or a ratio change, the loop takes time to pull the output onto the new frequency and phase. Until the lock detector says it has settled, the output is not to be trusted.

How do you check a clock’s frequency in a SystemVerilog testbench?

Record $realtime on each rising edge, subtract the previous one to get the period, and compare it with the expected period. For a divided clock you can compare exactly. For a multiplied clock, allow a tolerance about the size of your time precision, and check only while the PLL reports lock.

Why should a PLL checker ignore periods before lock?

Because they are allowed to be wrong. A PLL that is settling is behaving correctly. Checking those periods only produces false alarms, and engineers then start ignoring the checker.

What is a glitch on a clock divider?

A period that is shorter than it should be, usually caused by changing the divide ratio in the middle of a period. Logic clocked by it can miss timing. A glitch-free divider applies the new ratio at the start of the next period.

Do I need UVM to check clocks?

No. A plain module with $realtime measurements works, as the AI checker in this post shows. UVM helps when the clock settings come from sequences and you want results per phase in one scoreboard.

References

Related lessons