G6.5.2 The Counter That Skipped a Count

MediumDebugIcarus · browser samples / server checksReview the topic

Find the first cycle where a modulo-20 counter with clear, load and enable went wrong, and name its bug.

Hardware context: A kitchen timer's BCD seconds counter that counts up or down with a carry, and a display line counter with clear, load and enable that loses a line every frame. Find the first wrong cycle of a modulo-20 counter and name its bug.

A display controller counts lines 0 to 19 with a loadable counter. Its terminal count starts the next frame. After a change, one line per frame goes missing, 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 q or tc 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
script72input of each check
Full specification
The counter's ports
module mod20 (
  input  logic       clk,
  input  logic       clr,     // synchronous clear
  input  logic       load,    // load din
  input  logic       en,      // count up
  input  logic [4:0] din,
  output logic [4:0] q,       // 0 to 19
  output logic       tc       // 1 while en = 1 and q = 19
);
  ...
endmodule
At a rising edge of clkq becomes
clr = 10
otherwise, load = 1din
otherwise, en = 1q + 1, and 19 wraps to 0
otherwiseq

tc is combinational: 1 while en = 1 and q = 19. Cycle N is the stretch where the cycle counter shows N, from one rising edge to the next. The fix tests load a start value, then run eight edges.

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

Count Through the Wrap
Input
start 16; count, count, count, count, count, count, hold, count
Output
q_after_edge1 = 17, q_after_edge2 = 18, q_after_edge3 = 19, q_after_edge4 = 0, q_after_edge5 = 1, q_after_edge6 = 2, q_after_edge7 = 2, q_after_edge8 = 3, tc_before_each_edge = 'b00001000

From 16 the counter goes 17, 18, 19 and wraps to 0, then 1, 2, holds, and 3. tc is 1 only before the edge that leaves 19.

Example 2

Clear and Load Together
Input
start 5; count, clr+load, count, load 19, hold, count, count, count
Output
q_after_edge1 = 6, q_after_edge2 = 0, q_after_edge3 = 1, q_after_edge4 = 19, q_after_edge5 = 19, q_after_edge6 = 0, q_after_edge7 = 1, q_after_edge8 = 2, tc_before_each_edge = 'b00100000

When clr and load are both 1, clear wins and q is 0. A load with en = 0 still loads.

Example 3

Stalled at 19
Input
start 18; count, hold, hold, count, count, load 3, count, count
Output
q_after_edge1 = 19, q_after_edge2 = 19, q_after_edge3 = 19, q_after_edge4 = 0, q_after_edge5 = 1, q_after_edge6 = 3, q_after_edge7 = 4, q_after_edge8 = 5, tc_before_each_edge = 'b00001000

While en is 0 at 19, tc is 0 and q holds. tc turns 1 on the cycle where en returns.

Constraints and Contract

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

Hints

Hint 1

Work out the correct q for each cycle from the controls before each edge. Then compare with the waveform.

Hint 2

Look at the cycles around 19. What should come after 19, and what does tc do there?

Hint 3

Each cause fails in its own cycle. Look for an early wrap, a late tc, a wrong value after the wrap, or a lost clear.

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 fourteen-edge bench. The fix is compared with a JavaScript model on thirteen scripts of eight edges.

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 3 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

Count Through the Wrap

Input
start 16; count, count, count, count, count, count, hold, count
Expected output
q_after_edge1 = 17, q_after_edge2 = 18, q_after_edge3 = 19, q_after_edge4 = 0, q_after_edge5 = 1, q_after_edge6 = 2, q_after_edge7 = 2, q_after_edge8 = 3, tc_before_each_edge = 'b00001000

From 16 the counter goes 17, 18, 19 and wraps to 0, then 1, 2, holds, and 3. tc is 1 only before the edge that leaves 19.

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

Icarus · browser samples / server checks