RTL Design #69 · 2026-12-01 · Johnson counters · Easy · about 15 min
Bidirectional Johnson counter
Write jc_count: a 4-bit Johnson (twisted-ring) counter that can run in either direction, with a decoded position and a wrap flag.
qis a register, cleared to0000by the synchronous active-low resetrst_n.- On a rising edge with
rst_nhigh andenhigh,qmoves one step. Withrevlow it counts forward:0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000, then back to0000. Withrevhigh it walks the same eight states the other way. Withenlow,qholds. posis combinational: the position ofqin the forward sequence above,0for0000up to7for1000.wrapis combinational: 1 whenenis high and the coming edge (in the directionrevselects) takesqto0000. It is not checked whilerst_nis low.
Example, en = 1 and rev = 0 throughout, starting from reset:
| clock edge | q after | pos | wrap just before the edge |
|---|---|---|---|
| (reset) | 0000 |
0 | 0 |
| 1 | 0001 |
1 | 0 |
| 2 | 0011 |
2 | 0 |
| ... | |||
| 7 | 1000 |
7 | 0 |
| 8 | 0000 |
0 | 1 |
Fill in jc_count.sv. Run walks the counter around once and back a few steps.
Write and run your answer in the browser. The hidden tests check it, and it’s free.