Sub topic 14
Registers, Shift Registers, Ring and Johnson Counters
This chapter assumes you know how D flip-flops work, the clock-edge behavior from the flip-flop and timing chapters, and basic binary counting. It covers storage registers, shift registers, and the two counters built from shift registers: the ring counter and the Johnson counter.
Registers
A register is a group of flip-flops that share one clock and store a multi-bit word. An n-bit register holds n bits and uses n D flip-flops.
Plain D flip-flops load new data on every clock edge. To keep a value, you add an enable. Each flip-flop's input then becomes D = EN·Din + EN'·Q, which is a 2-to-1 multiplexer that either takes the new bit or recirculates the old one. Gating the clock instead is poor practice because it creates skew and glitches.
module reg4(input clk, input rst, input en, input [3:0] d, output reg [3:0] q);
always @(posedge clk) begin
if (rst) q <= 4'b0000;
else if (en) q <= d;
end
endmoduleAll four bits update at the same edge, so a register is a parallel-in, parallel-out (PIPO) device.
Shift registers
A shift register is a chain of flip-flops in which the output of each stage feeds the input of the next. On each clock edge every bit moves one position. Data enters at one end and leaves at the other.
| Type | Input | Output | Typical use |
|---|---|---|---|
| SISO | Serial | Serial | Delay line of n clock cycles |
| SIPO | Serial | Parallel | Serial-to-parallel conversion |
| PISO | Parallel | Serial | Parallel-to-serial conversion |
| PIPO | Parallel | Parallel | Storage |
A 4-bit SISO register delays its input by 4 clock cycles. A SIPO register needs n clocks to collect n bits, and its parallel outputs are valid only after the nth edge. A PISO register needs a load/shift control signal. In load mode each flip-flop takes its parallel bit, and in shift mode it takes its neighbor's output. Each stage therefore needs a 2-to-1 multiplexer.
Why shifting works
All flip-flops sample their inputs at the same edge, before any output changes. Each stage therefore captures the old value of its neighbor. This is the same condition the timing chapter describes: the clock-to-Q delay must exceed the hold time, taking clock skew into account. If skew is large, a bit can race through two stages in one cycle.
In Verilog, use nonblocking assignments (<=) in clocked blocks. Blocking assignments in a shift chain make the new value ripple through all stages in one step.
Universal shift register
A universal shift register has two mode-select lines. It can hold, shift right, shift left, or load in parallel.
module usr4(input clk, input rst, input [1:0] mode,
input sl_in, input sr_in, input [3:0] d,
output reg [3:0] q);
always @(posedge clk) begin
if (rst) q <= 4'b0000;
else case (mode)
2'b00: q <= q; // hold
2'b01: q <= {sr_in, q[3:1]}; // shift right
2'b10: q <= {q[2:0], sl_in}; // shift left
2'b11: q <= d; // parallel load
endcase
end
endmoduleEach flip-flop needs a 4-to-1 multiplexer in front of its D input. The select lines are the mode bits.
Shifting also does arithmetic. Shifting left by one position multiplies an unsigned number by 2. Shifting right divides by 2, discarding the remainder. For signed 2's complement numbers, a right shift must replicate the sign bit. This is an arithmetic shift.
Ring counter
A ring counter is a shift register whose last output feeds back to the first input. A single 1 circulates through the register. The state is one-hot, so exactly one flip-flop is 1 at a time.
module ring4(input clk, input rst, output reg [3:0] q);
always @(posedge clk) begin
if (rst) q <= 4'b1000; // must preset a single 1
else q <= {q[0], q[3:1]};
end
endmoduleThe sequence is 1000, 0100, 0010, 0001, then back to 1000.
- An n-bit ring counter has n states, so it is a modulo-n counter.
- It needs n flip-flops for modulus n. This is the least efficient counter in flip-flop count.
- No decoding logic is needed. Each flip-flop output is already a clean, glitch-free timing signal, one per state.
- It must be initialized. Starting from 0000, the counter stays at 0000 forever.
Self-starting fix
A ring counter has 2^n − n unused states. For n = 4 that is 12 states. If noise corrupts the state, the counter can stay in an invalid cycle, such as 1100 → 0110 → 0011 → 1001. These states never reach the valid cycle.
To make it self-correcting, change the feedback. The first input becomes the NOR of all outputs except the last: D3 = ~(Q3 | Q2 | Q1). The 1 is injected only when no 1 exists in the top three bits, so stray bits shift out and the counter ends in the valid cycle.
Johnson counter
A Johnson counter, also called a twisted-ring or switch-tail counter, feeds the inverted last output back to the first input.
module johnson4(input clk, input rst, output reg [3:0] q);
always @(posedge clk) begin
if (rst) q <= 4'b0000;
else q <= {~q[0], q[3:1]};
end
endmoduleStarting from 0000, the sequence is:
| Clock | Q3 Q2 Q1 Q0 |
|---|---|
| 0 | 0000 |
| 1 | 1000 |
| 2 | 1100 |
| 3 | 1110 |
| 4 | 1111 |
| 5 | 0111 |
| 6 | 0011 |
| 7 | 0001 |
| 8 | 0000 |
- An n-bit Johnson counter has 2n states, so it is a modulo-2n counter.
- A modulo-N counter (N even) needs only N/2 flip-flops. That is half the number a ring counter needs.
- Only one bit changes between consecutive states. The sequence is therefore glitch-free, like Gray code, even though it is not the standard reflected Gray code.
- Each state can be decoded with a 2-input AND gate that looks at two adjacent bits. This is the same for any n. The decoding cost is small.
For the 4-bit counter, the decoding is:
| State | Decoder |
|---|---|
| 0000 | Q3'·Q0' |
| 1000 | Q3·Q2' |
| 1100 | Q2·Q1' |
| 1110 | Q1·Q0' |
| 1111 | Q3·Q0 |
| 0111 | Q3'·Q2 |
| 0011 | Q2'·Q1 |
| 0001 | Q1'·Q0 |
Lock-out
A 4-bit Johnson counter uses 8 of 16 states. The other 8 form a separate cycle: 0101 → 0010 → 1001 → 0100 → 1010 → 1101 → 0110 → 1011 → 0101. A counter that enters this cycle never returns to the valid one. Fixing it needs extra feedback logic or a reliable reset at power-up.
Comparing the counters
| Property | Ring | Johnson |
|---|---|---|
| Feedback | Q0 | Q0' |
| Flip-flops for n bits | n | n |
| Modulus | n | 2n |
| Flip-flops for modulus N | N | N/2 |
| Valid / unused states (n = 4) | 4 / 12 | 8 / 8 |
| Decoding | None | 2-input gate per state |
| Must be preset | Yes, to one-hot | No, 0000 is valid |
A binary counter with n flip-flops reaches modulus 2^n. It is the most economical in flip-flops but needs more decoding logic, and its outputs can glitch when several bits change at once.
Typical GATE reasoning
- Number of cycles to return to the start. A ring counter returns after n clocks and a Johnson counter after 2n clocks. To find the state after k clocks, compute k mod n or k mod 2n, then count the steps.
- Serial transfer time. Moving an n-bit word serially takes n clock periods. With a 100 MHz clock (10 ns period), 8 bits take 80 ns.
- Frequency division. A ring counter output has frequency f/n and a Johnson counter output has f/2n. Each output has a duty cycle of 1/n for the ring and 50% for the Johnson counter.
- Fixed-input shift register. If a shift register has a constant input, such as a hard-wired 0, it fills with that value after n clocks. With inverted feedback the pattern depends on the starting state, so trace it step by step.
Exercise
A 4-bit Johnson counter shifts right with feedback D3 = Q0', and starts in 0000. What is its state after 11 clock pulses, and what is its modulus?
Show answer
The modulus is 2n = 8. After 11 clocks the state equals the state after 11 mod 8 = 3 clocks. The sequence is 0000, 1000, 1100, 1110, so the state is 1110 (Q3 Q2 Q1 Q0).
Exercise
A modulo-12 counter with one flip-flop per state would need how many flip-flops as a ring counter, and how many as a Johnson counter? How many 2-input AND gates does the Johnson design need to decode all states?
Show answer
The ring counter needs 12 flip-flops. The Johnson counter needs 12/2 = 6 flip-flops. It has 12 states and each is decoded by one 2-input AND gate, so 12 gates are needed.
Exercise
An 8-bit SIPO register is clocked at 50 MHz. How long must the receiver wait after the first serial bit arrives before it can read all 8 bits in parallel?
Show answer
The clock period is 20 ns. The first bit is captured at edge 1, and the 8th bit at edge 8. The receiver waits 7 more periods after the first bit's edge, so 7 × 20 ns = 140 ns. Measured from the start of the transfer, 8 clocks take 160 ns.