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.
nextis the slot you land on after movingstepslots fromidx. A positivestepmoves forward, a negativestepmoves backward, and the answer is always between0andN-1. The ring may be circled many times:stepcan be anything from -128 to 127.wrappedis 1 when the move went past either end of the ring, that is, whenidx + stepis below 0 or isNor more. It is 0 when the move stays inside the slots0toN-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.