G5.9.2 The Gated Clock That Doubled a Count

MediumDebugIcarus · browser samples / server checksReview the topic

Find the first cycle where an event counter counted too much, and name the synchronous-design rule its clock broke.

Hardware context: A low-power event counter whose enable was ANDed into its clock, and a clock tree with skew between two flops. Diagnose a counter with an AND-gated clock and rebuild it with an enable.

An event counter should add 1 at each rising clock edge where en is 1. It clears when both clear requests are 1 at an edge, and resets at once on rst. To save power, a designer changed how it is clocked. Now the counts drift upward in the field, and the test below ends with errors. The bench is correct.

Run the failing test to see its log and waveform.

Step 1: find the first cycle where count shows a value a correct counter would not, and name the signal. Step 2: choose the cause from four real bugs; each fails in a different cycle. Step 3 is optional: fix the counter and run the tests.

Signals in the Timeline

SignalWidthRole
script32input of each check
Full specification
The counter's ports
module evcount (
  input  logic       clk,
  input  logic       rst,            // asynchronous reset
  input  logic       en,             // count this cycle
  input  logic       clr_a, clr_b,   // clear when both are 1 at a clock edge
  output logic [3:0] count
);
  ...
endmodule
At a rising edge of clkcount becomes
clr_a = clr_b = 10
otherwise, en = 1count + 1
otherwisecount
rst = 1, at any time0 at once
SignalIn the failing run
enChanges in either half of the clock period, never at an edge.
clr_a, clr_bDriven by two other blocks; they may cross, 1 ns apart, while neither is meant to clear.

Cycle N is the stretch where the cycle counter shows N, from one rising edge to the next. The fix tests change en in both halves of each period and let the clear requests cross.

Diagnose the Failing Run

The test below runs the buggy design. Read its log, then find where it first goes wrong in the waveform.

Log

Waveform

Click a signal at a cycle to select it. Or focus the waveform and use the arrow keys: Left and Right move one cycle, Up and Down one signal.

Step 1 · The first bad cycle and signal
Step 2 · The cause

Step 3 is optional: fix the code in the editor, then run the tests.

Examples

Example 1

En Held High
Input
per period (en in the high then the low phase; clr_a clr_b): en 11 clr 00, en 11 clr 00, en 11 clr 00, en 00 clr 00, en 00 clr 00, en 00 clr 00
Output
count_period1 = 0, count_period2 = 1, count_period3 = 2, count_period4 = 3, count_period5 = 3, count_period6 = 3

en is 1 at three rising edges in a row, so count goes 1, 2, 3, and then holds.

Example 2

En Rises While the Clock Is High
Input
per period (en in the high then the low phase; clr_a clr_b): en 00 clr 00, en 11 clr 00, en 00 clr 00, en 11 clr 00, en 10 clr 00, en 00 clr 00
Output
count_period1 = 0, count_period2 = 0, count_period3 = 1, count_period4 = 1, count_period5 = 2, count_period6 = 2

Each rise of en happens in the high half of the clock. A correct counter waits for the next rising edge; it never counts at the moment en rises.

Example 3

Clear Requests That Cross Without Meeting
Input
per period (en in the high then the low phase; clr_a clr_b): en 11 clr 01, en 11 clr 10, en 11 clr 00, en 11 clr 11, en 11 clr 00, en 00 clr 00
Output
count_period1 = 0, count_period2 = 1, count_period3 = 2, count_period4 = 3, count_period5 = 0, count_period6 = 1

clr_a and clr_b pass each other 1 unit apart. They are both 1 at an edge only in the fourth period, and only that edge clears.

Constraints and Contract

  • All inputs are known 0/1 values.
  • Keep the module name and the ports.

Hints

Hint 1

Work out the correct count for each cycle from the en value just before each rising edge. Then compare with the waveform.

Hint 2

Look at the cycles where en rises while the clock is high. What does an AND of clk and en do at that moment?

Hint 3

A counter must change only at rising edges of clk. Any other edge, on a gated, divided or falling clock or on a reset, breaks the rule.

What the grader checks: The failing run is simulated live with the shipped counter. The first bad cycle and every option's first wrong value come from simulations on the same twelve-cycle bench. The fix is compared with a JavaScript model on thirteen scripts of six periods with en changing in both halves.

13 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

En Held High

Input
per period (en in the high then the low phase; clr_a clr_b): en 11 clr 00, en 11 clr 00, en 11 clr 00, en 00 clr 00, en 00 clr 00, en 00 clr 00
Expected output
count_period1 = 0, count_period2 = 1, count_period3 = 2, count_period4 = 3, count_period5 = 3, count_period6 = 3

en is 1 at three rising edges in a row, so count goes 1, 2, 3, and then holds.

Run samples executes all 3 public checks. Check all tests runs 13 checks on the server.

Icarus · browser samples / server checks