TDEB Top Bar

Trending
Semiconductor Industry News — The Digital Electronics Blog
Follow Us

TDEB Masthead

The Semiconductor Hub

Semiconductors.  Chips and Systems.  Innovation.

Indexed by leading Search Engines, AI and referenced by Engineers worldwide 500+ Articles 10M+ Pageviews 30+ Reports 50+ Citations

Interview Questions: Clock Frequency Checks and Functional Coverage

mg
by
0
Share Facebook X LinkedIn WhatsApp Email

Two interview questions from a verification round: write Verilog that checks a clock never runs faster than its specified maximum, and comment on what 100% functional coverage and 100% code coverage really prove. Both look simple. Both are used to see whether a candidate understands what simulation can and cannot tell you.

Question 1: check that a clock meets its frequency limit

A communication device receives a clock up to X MHz. Write Verilog to verify that the clock meets this timing requirement.

"Up to X MHz" means the frequency must never exceed X MHz. In time terms, every clock period must be at least 1/X microseconds, which is 1000/X nanoseconds. A 10 MHz limit means no period may be shorter than 100 ns.

The checker therefore has one job: watch the clock coming into the device, measure the time between consecutive rising edges, and report any period that is too short. It must not generate the clock itself; it only observes it. The working code is in the deep dive.

What a good answer mentions

  • Measure edge to edge with the simulator's time, using $realtime, and convert the limit to the same time unit with a `timescale.
  • Skip the first edge, since there is no previous edge to measure from.
  • Report the time and the measured period, so the failure can be found in the waveform.
  • Mention the built-in alternative: the $period timing check, which belongs inside a specify block.
  • Optionally check the high and low pulse widths too, since a clock can meet its period and still violate a minimum pulse width.

Question 2: what coverage does and does not prove

a) "100% functional coverage ensures that the design can have no bugs."

This statement is not accurate. Reaching 100% functional coverage is a strong sign that the planned set of test scenarios has been exercised, but it does not prove the absence of bugs. Functional coverage measures only the items someone wrote into the coverage model. It says nothing about corner cases, boundary conditions or interactions that nobody thought to cover, and a scenario can be hit while the checker that should catch a wrong result is missing or weak. Bugs can still sit in untested or overlooked areas, and additional strategies, such as stress testing, negative testing and formal checks, may be needed to find them.

b) "100% code coverage guarantees that the test bench is very well written."

Also not accurate. 100% statement coverage means every line of the design was executed at least once. That is a useful minimum, but it does not show that the test bench checked the results. A test that executes every line and compares nothing reaches 100% code coverage and catches no bugs. Code coverage also says nothing about functionality that is missing from the RTL, because missing code cannot be left uncovered. Combine code coverage with functional coverage, self-checking tests and review of the verification plan.

Deep dive: the numbers and details

This part holds the corrected code, the timing check alternative and coverage details. Skip it if the overview is all you need.

A working period checker

`timescale 1ns/1ps
// Passive monitor: flags any clock period shorter than the limit.
module clk_freq_checker #(parameter real MAX_FREQ_MHZ = 10.0)
                         (input wire clk);
  localparam real MIN_PERIOD_NS = 1000.0 / MAX_FREQ_MHZ;  // 100 ns at 10 MHz

  realtime last_edge;
  reg      seen_edge;
  integer  violations;

  initial begin
    seen_edge  = 1'b0;
    violations = 0;
  end

  always @(posedge clk) begin
    if (seen_edge && ($realtime - last_edge) < MIN_PERIOD_NS) begin
      violations = violations + 1;
      $display("ERROR %0t: clock period %0.3f ns is below %0.3f ns (over %0.3f MHz)",
               $realtime, $realtime - last_edge, MIN_PERIOD_NS, MAX_FREQ_MHZ);
    end
    last_edge = $realtime;
    seen_edge = 1'b1;
  end
endmodule

Bind it to the device's clock input in the testbench, for example clk_freq_checker #(.MAX_FREQ_MHZ(10.0)) u_chk (.clk(dut_clk));. With a 9 MHz clock (period about 111.1 ns) it stays silent; switch the source to 12 MHz (period about 83.3 ns) and it reports each short period with its time stamp.

Why the earlier version of this answer did not work

The code first published with this question had four problems, so it has been replaced:

  • It generated the clock itself with forever #0.5 clk = ~clk;, so it could only ever check its own clock, never the device's input.
  • It called $period(clk) as if it were a function returning the period. $period is a timing check, not a function, and it is only legal inside a specify block.
  • It computed the target period as 1.0 / desired_frequency, which is in seconds (1e-7 for 10 MHz), and compared it against simulation time in different units.
  • It kept its own simulation_time counter, which drifts from real simulation time; $realtime gives the correct value directly.

The built-in timing check alternative

Verilog's timing checks do this job inside a component model. Stuart Sutherland's Verilog HDL Quick Reference Guide (Verilog-2001) lists the syntax as $period(reference_event, limit, notifier); and $width(reference_event, limit, width_threshold, notifier);, notes that the events must be module input ports, and that the optional notifier register toggles on each violation.

`timescale 1ns/1ps
module dev_model (input wire clk /* ... */);
  reg notifier;
  specify
    specparam tPERIOD_MIN = 100.0;   // 10 MHz maximum
    specparam tHIGH_MIN   = 40.0;    // illustrative pulse width limits
    specparam tLOW_MIN    = 40.0;
    $period(posedge clk, tPERIOD_MIN, notifier);
    $width(posedge clk, tHIGH_MIN, 0, notifier);
    $width(negedge clk, tLOW_MIN, 0, notifier);
  endspecify
endmodule

The behavioral monitor suits a testbench; the specify-block checks suit a device model that may later be back-annotated with SDF timing.

Worked numbers

Maximum frequencyMinimum periodA 5% fast clockResult
10 MHz100 ns10.5 MHz, 95.24 nsFlagged
25 MHz40 ns26.25 MHz, 38.10 nsFlagged
100 MHz10 ns105 MHz, 9.52 nsFlagged

Pick a `timescale precision well below the margin you care about: at 100 MHz a 1 ns precision cannot tell 9.52 ns from 10 ns, while 1 ps can.

Kinds of coverage and what each misses

MetricMeasuresBlind spot
Statement (line)Each line executedResults never checked; missing logic
BranchEach if/case outcome takenCombinations of conditions
Condition/expressionEach term of a condition toggling the outcomeSequences over time
ToggleEach bit going 0 to 1 and 1 to 0Whether the values were meaningful
FSMStates and transitions visitedData carried through those states
FunctionalScenarios in the coverage modelScenarios nobody wrote down

An interviewer may follow up with: "You have 100% code coverage and 85% functional coverage. What do you do?" A good answer: review the uncovered functional bins first, because they point at untested features; check that every test is self-checking; and review the coverage model against the specification to find scenarios that are missing from it entirely.

Share Facebook X LinkedIn WhatsApp Email

Post a Comment

0 Comments

Your comments will be moderated before it appears here.

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!