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.

  • q is a register, cleared to 0000 by the synchronous active-low reset rst_n.
  • On a rising edge with rst_n high and en high, q moves one step. With rev low it counts forward: 0000, 0001, 0011, 0111, 1111, 1110, 1100, 1000, then back to 0000. With rev high it walks the same eight states the other way. With en low, q holds.
  • pos is combinational: the position of q in the forward sequence above, 0 for 0000 up to 7 for 1000.
  • wrap is combinational: 1 when en is high and the coming edge (in the direction rev selects) takes q to 0000. It is not checked while rst_n is 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.