S3 · Lesson 3 · Sequential logic in Verilog
Registers, resets, enables and counters
Everything that remembers in a synchronous design is a flip-flop, and nearly every flip-flop has the same three questions attached: what resets it, what enables it, and what clock drives it. Get them right and a design simulates and synthesizes the same way. Get them wrong (a gated clock, a reset released at the wrong moment, an enable written as a clock) and a design can pass simulation and still fail in silicon. This lesson writes registers, counters and dividers the way production RTL does, and counts what they cost.
- A 2-bit synchronous counter with an enable EN and a synchronous reset R, written as two flip-flops:
Q0 <= Q0 ^ ENandQ1 <= Q1 ^ (EN & Q0). - While EN is high it counts 0, 1, 2, 3, 0 on successive rising edges; drop EN and it holds. Every flip-flop sees the same clock; the enable is logic in front of the D input, never a gate on the clock.
- Raise R: the next edge clears the counter, since a synchronous reset acts only at clock edges.
Predict
A designer stops a register from loading by ANDing its clock with an enable signal. What's the risk?
Logic in the clock path is dangerous. If the enable changes while the clock is high, the AND gate's output can pulse, clocking the register when nobody meant to. The gate also delays the clock relative to other registers, breaking the timing assumptions between them. The safe form is a clock enable: a multiplexer in front of D that feeds back Q when the enable is off. (Real clock gating for power, lesson 20, uses special latch-based cells that can't glitch.)
Name it
Registers
always @(posedge clk)
if (rst) q <= 0;
else if (en) q <= d;
That's a register with a synchronous reset and an enable: the reset and enable become logic in front of D. Every bit of q is one flip-flop.
Resets
- Synchronous (
if (rst)inside aposedge clkblock): acts on clock edges, behaves like any other logic, and is easy to time. - Asynchronous (
always @(posedge clk or posedge rst)): acts immediately, even without a clock, which is useful at power-up. Its release must still be synchronized to the clock, or flip-flops may leave reset on different cycles; a reset synchronizer (two flip-flops) does that.
Enables, not gated clocks
A disabled register reloads its own output through a multiplexer; the clock still runs. One clock net, distributed with balanced delay, drives every flip-flop.
Counters and dividers
- A synchronous n-bit counter: each bit toggles when all lower bits are 1. Its carry chain sets its speed.
- A divide-by-N counter makes one output pulse every N clocks: .
- An LFSR (C7) cycles through states using only XOR feedback: a cheap pseudo-random sequence or a fast counter when the order doesn't matter.
Flip-flop timing
Each flip-flop needs its D input stable before the edge and after it, and its output changes after the edge. Violating setup or hold risks metastability (lesson 24).
Worked example
A 100 MHz system needs a 1 kHz tick and a 12-bit free-running counter. How many flip-flops do the divider and the counter need, and how often does the counter wrap?
Show the solution
- The divider. Dividing by needs a counter that reaches 99,999:
- The 12-bit counter's wrap. It wraps every clocks, which at 100 MHz is 40.96 µs.
- Total.
- Using the tick. The tick output is a one-clock enable pulse used by slower logic, never a clock in its own right.
Answer 17 flip-flops for the divider and 12 for the counter, 29 in all; the counter wraps every 40.96 µs.
Worked example
A 16-bit LFSR and a 16-bit binary counter both run from the same clock. How long are their sequences, and why might a designer pick the LFSR?
Show the solution
- The counter's sequence. It cycles through all states.
- The LFSR's sequence. It skips the all-zeros state:
- Why pick the LFSR. Its next state is a few XORs on a shift register, with no carry chain, so it runs at nearly the flip-flop's limit regardless of width. That suits fast timeouts and test-pattern generation, where the counting order doesn't matter.
Answer 65,536 states for the counter and 65,535 for the LFSR; the LFSR has no carry chain, so it stays fast at any width.
Worked example
A UART on a 50 MHz clock needs a tick at 115 200 baud. What division ratio does it use, how big is the frequency error, and how many flip-flops does the divider take?
Show the solution
- Division ratio. so divide by 434.
- Frequency error. The tick runs at 115 207 Hz, 0.0064% fast. Receivers tolerate a few percent, so the error is harmless.
- Divider size. Counting 0 to 433 takes
Answer divide by 434, which runs 0.0064% fast (115 207 Hz), with a 9-flip-flop divider.
Check
A divide-by-10 counter runs from a 100 MHz clock. What's its output frequency?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
How many bits does a counter need to divide a 50 MHz clock down to a 3 kHz tick?
Check
How many states does a maximal-length 12-bit LFSR cycle through?
Check
A free-running 6-bit counter runs at 50 MHz. How often does it wrap around?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A flip-flop has clock-to-Q 40 ps, setup 40 ps and hold 10 ps. How much of every clock period does it cost a path that starts and ends at such flip-flops?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
A register file of 16 registers, each with its own write enable, needs how many 2-to-1 multiplexers per bit to implement the enables?
Check
A 50 MHz design needs a 1 kHz tick, and has a 32-bit free-running counter.
(a)
What division ratio makes the tick?
(b)
How many flip-flops does the divider's counter need?
(c)
How often does the free-running counter wrap?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
An n-bit synchronous binary counter runs at clock frequency f.
Step 1
At what frequency does bit i toggle (bit 0 the least significant)?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How long is a full cycle of the counter?
Step 3
How long is the full sequence of an n-bit maximal-length LFSR on the same clock?
Explain why
Why should a register be disabled with a clock enable rather than by gating its clock?
Synchronous design assumes that every flip-flop sees the clock edge at very nearly the same instant and that nothing but the clock causes edges. Putting logic in the clock path breaks both. The gate delays the clock, so the gated register samples later than its neighbors, which can let data race through or arrive too late. And any glitch on the enable while the clock is high passes through the gate as a spurious clock edge. A clock enable keeps the clock clean and instead adds a multiplexer in the data path that reloads the register's own value when it shouldn't change. Timing tools analyze that like any other logic.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.