R11.2.2 The Loop That Never Ended

EasyDebugIcarus Verilog + Yosys · serverReview the topic

A remainder block hung a simulation and was refused by synthesis. A quick patch made it stop and broke its result. Find the first bad cycle, name the cause, and bound the loop the right way.

Hardware context: A leading-zero counter with a WIDTH parameter, built at widths 1, 5, 8 and 16; and a 4-bit remainder block that subtracts in a loop. Find the first bad cycle of the patched remainder loop, name the cause, and bound the loop for the worst case.

A small block gives the remainder of a divided by m, by subtracting m until what is left is below m. Its first version used a while loop. With m at 0 the simulation never ended, and Synthesize stopped with: unroll limit of 4000 exhausted.

A colleague patched the loop so that it always stops, added an err output for m = 0, and shipped it. Simulation ends now and synthesis passes. The remainders are sometimes wrong. The bench is correct. The bug is in the block.

Step 1: run the failing test. Find the first cycle where rem or err shows a value the brief does not give, and name the signal. Step 2: choose the cause from four real bugs. Each one first goes wrong in a different cycle.

Step 3 is optional: fix the block and run the tests. They apply eight pairs of a and m each and read rem and err after every pair.

Signals in the Timeline

SignalWidthRole
script64input of each check
Full specification
The block's ports (keep the name and the ports)
module mod_small (
  input  logic [3:0] a,
  input  logic [3:0] m,
  output logic [3:0] rem,        // a mod m; 0 when m is 0
  output logic       err         // 1 when m is 0
);
  ...
endmodule
The first version: the loop that never ended
always_comb begin
  logic [3:0] r;
  r = a;
  while (r >= m)                 // stops when the data says so
    r = r - m;
  rem = r;
end
Recorded for the first versionResult
SynthesizeERROR: unroll limit of 4000 exhausted [--unroll-limit=], on the while line
Icarus 12 with a = 9 and m = 0no line printed; the run was stopped at the time limit
Icarus 12 with a = 15 and m = 2rem=1
RuleCorrect block
m is not 0rem is a mod m, and err is 0.
m is 0rem is 0, and err is 1.
memoryNone: rem and err depend only on this cycle's a and m.

Cycle N is the stretch where the cycle counter shows N. The operands change at its start, and the bench checks the outputs before it ends. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.

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

Small Quotients
Input
7 mod 3, 13 mod 5, 9 mod 4, 15 mod 7, 11 mod 6, 14 mod 9, 5 mod 5, 8 mod 3
Output
rem_pair0 = 1, err_pair0 = no, rem_pair1 = 3, err_pair1 = no, rem_pair2 = 1, err_pair2 = no, rem_pair3 = 1, err_pair3 = no, rem_pair4 = 5, err_pair4 = no, rem_pair5 = 5, err_pair5 = no, rem_pair6 = 0, err_pair6 = no, rem_pair7 = 2, err_pair7 = no

Each pair needs one or two subtractions: 7 mod 3 is 1, 13 mod 5 is 3. A short loop still gets these right.

Example 2

Dividing by 1 and by 2
Input
15 mod 1, 9 mod 1, 1 mod 1, 15 mod 2, 14 mod 2, 13 mod 2, 0 mod 1, 10 mod 1
Output
rem_pair0 = 0, err_pair0 = no, rem_pair1 = 0, err_pair1 = no, rem_pair2 = 0, err_pair2 = no, rem_pair3 = 1, err_pair3 = no, rem_pair4 = 0, err_pair4 = no, rem_pair5 = 1, err_pair5 = no, rem_pair6 = 0, err_pair6 = no, rem_pair7 = 0, err_pair7 = no

15 mod 1 needs 15 subtractions, the most a 4-bit value can need. 15 mod 2 needs 7.

Example 3

m Is 0
Input
9 mod 0, 0 mod 0, 15 mod 0, 7 mod 3, 1 mod 0, 12 mod 4, 3 mod 0, 15 mod 15
Output
rem_pair0 = 0, err_pair0 = yes, rem_pair1 = 0, err_pair1 = yes, rem_pair2 = 0, err_pair2 = yes, rem_pair3 = 1, err_pair3 = no, rem_pair4 = 0, err_pair4 = yes, rem_pair5 = 0, err_pair5 = no, rem_pair6 = 0, err_pair6 = yes, rem_pair7 = 0, err_pair7 = no

With m at 0 the brief gives rem = 0 and err = 1, whatever a is.

Constraints and Contract

  • a and m are known 0/1 values.
  • Keep the module name and the ports.

Hints

Hint 1

For each cycle, work out how many times m must be subtracted from a. Compare that number with what the loop allows.

Hint 2

The first wrong cycle is not the first one with a large quotient. Check the cycle before it: how many subtractions did that one need?

Hint 3

A loop needs a bound that covers the worst case of the data. Which a and m need the most subtractions, and how many is that?

What the grader checks: The failing run is simulated live with the shipped block. The bench simulates the correct block and each buggy one, and the first bad cycle of every option comes from those runs. The optional fix is compared with an independent model on six directed and ten seeded tests of eight pairs each. The two outputs quoted for the first version were recorded on this site's tools.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

16 checks on Icarus Verilog + Yosys · server. 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

Small Quotients

Input
7 mod 3, 13 mod 5, 9 mod 4, 15 mod 7, 11 mod 6, 14 mod 9, 5 mod 5, 8 mod 3
Expected output
rem_pair0 = 1, err_pair0 = no, rem_pair1 = 3, err_pair1 = no, rem_pair2 = 1, err_pair2 = no, rem_pair3 = 1, err_pair3 = no, rem_pair4 = 5, err_pair4 = no, rem_pair5 = 5, err_pair5 = no, rem_pair6 = 0, err_pair6 = no, rem_pair7 = 2, err_pair7 = no

Each pair needs one or two subtractions: 7 mod 3 is 1, 13 mod 5 is 3. A short loop still gets these right.

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

Icarus Verilog + Yosys · server