Draft preview. Lessons are being written and reviewed.

Digital Design / Module 5: Counters and frequency dividers

29. Divide by 2, 4 and any even number

Create slower clocks correctly.

Lecture coming soon

Lab: Frequency divider lab

The idea

Dividing a clock means producing a slower clock from a faster one. The simplest divider is a flop that feeds back its inverted output: it toggles at every rising edge, so it runs at half the frequency with a 50% duty cycle.

always_ff @(posedge clk)
  if (rst) q <= 1'b0;
  else     q <= ~q;

Any even number

To divide by N, toggle every N/2 input cycles. Divide-by-6 toggles every 3 cycles.

module div_even #(parameter N = 6) (
  input clk, rst, output reg out
);
  reg [$clog2(N)-1:0] cnt;
  always_ff @(posedge clk)
    if (rst) begin
      cnt <= '0;
      out <= 1'b0;
    end else if (cnt == N/2 - 1) begin
      cnt <= '0;
      out <= ~out;
    end else
      cnt <= cnt + 1'b1;
endmodule

The output is high for three input cycles and low for three, so the duty cycle is exactly 50%.

Clock or enable?

In most cases, you don't need a second clock at all. Generate a one-cycle enable pulse every N cycles, and keep the slower logic on the original clock. That leaves one , with no extra clock tree and no crossing problems. Make a true divided clock only when something requires one, such as an output pin or a block running at low power.

In real chips

A divided clock is a new clock for timing. It requires a create_generated_clock constraint, so that the timing tool understands its relationship to the source. between the two also needs care. Real chips commonly use dedicated clock-divider cells, not plain RTL flops.

Common mistake

Driving flops from a divider's output without telling the timing tools. The tool will then either skip those paths or time them wrongly, and the bug can surface in silicon. Try the lab to see how the output tracks the count.

Key ideas

  • Toggle a flop to halve the frequency.
  • Even N: toggle every N/2 input cycles.
  • In real chips, generated clocks need constraints and clock-tree care.

Your exercise

Design a divide-by-6 with 50% duty cycle.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: This should divide by 6, but the output toggles every 4 clocks, which divides by 8.

Interview questions

coreDivide-by-2 circuit?

basicWith a 100 MHz input clock, what does a flop with its inverted output fed back to D produce? What do you get if a counter toggles its output every two input cycles instead?

basicFor a divide-by-10 with 50% duty cycle using a counter that starts at 0, how many input cycles between output toggles, and what count value do you compare against?

coreThis divider is instantiated with N = 6 but produces an output with a period of 8 input cycles. Why?

always @(posedge clk)
  if (rst) begin cnt <= 0; out <= 1'b0; end
  else if (cnt == N/2) begin
    cnt <= 0;
    out <= ~out;
  end else
    cnt <= cnt + 1'b1;

coreA 120 MHz clock is divided by 6 with 50% duty cycle. What are the output frequency, period and high time?

coreSlower logic needs to update at one sixth of the system clock. Why is an enable pulse usually better than a divided clock?

coreWrite logic that produces a one-cycle enable pulse every 6 clock cycles.

always @(posedge clk)
  if (rst) begin
    cnt  <= 3'd0;
    tick <= 1'b0;
  end else begin
    tick <= (cnt == 3'd5);
    cnt  <= (cnt == 3'd5) ? 3'd0 : cnt + 3'd1;
  end

advancedYou do use a divided clock from a flop to drive other flops. What must you tell the timing tools, and what goes wrong if you don't?

advancedSomeone makes a divided clock by decoding a binary counter combinationally, for example clkout = (cnt == 2'd0) | (cnt == 2'd1). Why is this risky as a clock?

Practise these 9 questions with animations