S33 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 ^ EN and Q1 <= 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?

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 a posedge clk block): 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: fout=fclk/N.
  • An LFSR (C7) cycles through 2n−1 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 tsetup before the edge and thold after it, and its output changes tclk-q 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
  1. The divider. Dividing by 105 needs a counter that reaches 99,999: ⌈log2⁡105⌉=17bits
  2. The 12-bit counter's wrap. It wraps every 212=4096 clocks, which at 100 MHz is 40.96 µs.
  3. Total. 17+12=29flip-flops
  4. 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
  1. The counter's sequence. It cycles through all 216=65,536 states.
  2. The LFSR's sequence. It skips the all-zeros state: 216−1=65,535states
  3. 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
  1. Division ratio. 50MHz115200=434.03 so divide by 434.
  2. Frequency error. The tick runs at 115 207 Hz, 0.0064% fast. Receivers tolerate a few percent, so the error is harmless.
  3. Divider size. Counting 0 to 433 takes ⌈log2⁡434⌉=9flip-flops

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°.

Explain why

Why should a register be disabled with a clock enable rather than by gating its clock?

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.