SystemVerilog #71 · 2026-12-03 · Modulo of negative numbers · Easy · about 15 min

Walking backward around a ring

A ring buffer has N slots numbered 0 to N-1, and the slot after N-1 is slot 0 again. Write ring_step, a combinational module that moves a position around the ring by a signed number of slots.

  • next is the slot you land on after moving step slots from idx. A positive step moves forward, a negative step moves backward, and the answer is always between 0 and N-1. The ring may be circled many times: step can be anything from -128 to 127.
  • wrapped is 1 when the move went past either end of the ring, that is, when idx + step is below 0 or is N or more. It is 0 when the move stays inside the slots 0 to N-1.

idx is always between 0 and N-1. N is a parameter from 2 to 8.

Examples with N = 6:

idx step next wrapped
2 3 5 0
4 3 1 1
5 1 0 1
1 14 3 1

Fill in ring.sv. Run tries a few forward moves.

Write and run your answer in the browser. The hidden tests check it, and it’s free.