Dividers and Clock Gates

A timer wants a slower clock than the processor, and a block that sleeps wants no clock at all. Two small circuits do that. One of them can break a chip if it is built the obvious way.

You Will Be Able To

  • Work out the period and the duty cycle of a divided clock.
  • Explain why a plain AND gate can cut a clock pulse short.
  • Explain how a clock gate keeps every pulse whole, and what a closed gate leaves running.
Lesson
S5.2
Time
24 min
Runs on
Your browser
Check
6 questions

A block was switched off to save power. On the way down, one of its flip-flops changed and the others did not. Its clock had been stopped with a single logic gate. What reached that one flip-flop?

Before You Start

You need Clock Sources and the Chip-Level Clock Tree: the clock root, a clock leaf, and the period of a clock.

You need one fact about a latch, which this page repeats. It helps to have seen a counter. The D Latch and the page on dividing a clock teach both from the start.

The timer and UART0 appear here only as blocks that want a clock. Their insides are in The Anatomy of a Peripheral.

Five questions this page answers
Question
1What does a divider by N do to the period of a clock?
2What is the duty cycle of a clock?
3Why can a plain AND gate not stop a clock safely?
4How does a clock gate keep every pulse whole?
5A clock gate is closed. Which clocks still run?

The Idea: A Slower Clock, and No Clock

Not every block wants the rate of the clock root. A timer or a UART is content with a fraction of it, and a slower clock costs less power. A clock divider makes that slower clock. It counts the edges of the clock it is given and passes on one period for every N.

The period is half of the story. The other half is how long the divided clock stays high in each period. That share is the duty cycle. A block that also acts on falling edges cares about it.

Some blocks, some of the time, want no clock at all. A clock that toggles costs power even when no flip-flop changes. So a branch of the tree gets a switch that can stop its clock. That switch is a clock gate.

The obvious clock gate is a single gate, of the kind called AND. Its output is 1 only while both inputs are 1. The clock goes to one input and an enable to the other. It has a flaw. If the enable falls while the clock is high, the pulse ends at once, far too early. A pulse that short is taken by some flip-flops and missed by others.

The cure is a rule and a part. The rule: the gate may change only while its clock is low. The part is a latch in front of the AND gate, which holds the enable still through every high pulse.

New Words

New words on this page
WordMeaning
Clock dividerA small counter that makes a slower clock from a faster one.
Divide ratioThe number N of a divider: N periods go in for each period that comes out.
Parent clockThe clock at the input of a divider or of a clock gate.
Duty cycleThe high time of a clock divided by its period. Half means equal high and low times.
Clock gateA circuit that passes its parent clock or holds its output low. It changes only between pulses.
Gate enableThe input of a clock gate that says "pass the clock": 1 to pass, 0 to stop.
Gated clockThe output of a clock gate.
Runt pulseA pulse that is shorter than the circuit was designed for.
Transparent latchA storage cell with a control input. At one level of the control it copies its input. At the other level it holds. This page says latch for short.

One Picture: From the Parent Clock to the Block

A Divider and a Clock Gate on One BranchStep through the picture. It follows one branch of the clock tree, from the root to a block.
  • Clock / reset
  • Control / FSM
  • Memory
  • Bus
  • Peripheral
A Divider and a Clock Gate on One BranchStep through the picture. It follows one branch of the clock tree, from the root to a block. component Parent clock the clock thatenters; here 100MHz component Divider by two one period out fortwo periods in component Gate enable 1: this block wantsits clock component Latch copies the enable onlywhile the clock is low component AND gate passes the clock whileits other input is 1 component Block the flip-flops thatuse this clock 10 ns period20 ns periodgated clockdirectly: unsafeenableheld enableclock, to hold on
  1. component Parent clockthe clock that enters; here 100 MHz
    • 10 ns period → Divider by two
  2. component Blockthe flip-flops that use this clock
  3. component Divider by twoone period out for two periods in
    • 20 ns period → AND gate
    • clock, to hold on → Latch
  4. component Gate enable1: this block wants its clock
    • directly: unsafe → AND gate
    • enable → Latch
  5. component AND gatepasses the clock while its other input is 1
    • gated clock → Block
  6. component Latchcopies the enable only while the clock is low
    • held enable → AND gate
  1. A branch and its block

    A parent clock of 100 MHz enters this branch. The block at its end wants a slower clock, and at times it wants none.

  2. The divider

    The divider counts rising edges of its parent clock. This one toggles its output on every edge, so two periods in make one period out.

  3. A gate, the simple way

    An AND gate passes the clock while the gate enable is 1. This works, until the enable changes while the clock is high. Then the pulse is cut.

  4. A latch in front

    The latch copies the enable only while the clock is low. While the clock is high it holds what it had. So the AND gate sees a change of enable only between pulses.

  5. The clock gate

    The latch and the AND gate together are the clock gate. Every pulse that leaves it is a whole pulse.

  6. What a closed gate stops

    With the gate closed, the block gets no edges. The divider and the parent clock run on. A gate stops only what is below it.

Read the diagram as text
  • Parent clock: the clock that enters; here 100 MHz
  • Divider by two: one period out for two periods in
  • Gate enable: 1: this block wants its clock
  • Latch: copies the enable only while the clock is low
  • AND gate: passes the clock while its other input is 1
  • Block: the flip-flops that use this clock
  • Parent clock → Divider by two: 10 ns period
  • Divider by two → AND gate: 20 ns period
  • AND gate → Block: gated clock
  • Gate enable → AND gate: directly: unsafe
  • Gate enable → Latch: enable
  • Latch → AND gate: held enable
  • Divider by two → Latch: clock, to hold on

One branch of the clock tree of ap_soc, as the lab builds it. The enable has two paths in the picture: the lab runs both, side by side.

The Smallest Example, Worked by Hand

The parent clock has a period of 10 ns. First the divider by two. It toggles its output on every rising edge that comes in.

Divide by two, by hand
Rising edge of the parent1234
Time of that edge15 ns25 ns35 ns45 ns
Output after the edge1010

The output is high from 15 to 25 ns and low from 25 to 35 ns. High 10 ns, low 10 ns, period 20 ns: a duty cycle of one half.

Now a divider by three, built the simple way. A counter steps through 0, 1 and 2, and the output is high in state 1 only.

One state out of three, by hand
State of the counter012
Time spent in the state10 ns10 ns10 ns
Output010

High 10 ns, low 20 ns, period 30 ns. The period is right. The duty cycle is one third, not one half.

A divider by an odd number cannot reach one half by counting rising edges alone. The middle of its period falls on a falling edge of the parent clock.

Last, the gate. Take one high pulse of the divided clock: it is 10 ns wide. The enable falls 3 ns after the pulse began.

One pulse through two gates, by hand
GateWhat it does at 3 nsWidth of the pulse that leaves
Plain AND gateIts output follows the enable down at once.3 ns: a runt pulse
Clock gateThe clock is high, so the latch holds its 1. The output stays high until the clock falls.10 ns: the whole pulse

Opening has the same trap. If the enable rises 3 ns into a high pulse, the plain AND gate lets out the last 7 ns of it. The clock gate waits, and its first pulse is a whole one.

The Rules

  1. Rule 1: A divider by N passes one period for every N periods of its parent clock.

    The period grows N times. The rate falls to one Nth.

  2. Rule 2: Duty cycle is high time divided by period.

    A divider that toggles gives one half. A count of three with one high state gives one third.

  3. Rule 3: A clock gate changes only while its clock is low.

    Its latch copies the enable while the clock is low and holds it while the clock is high.

  4. Rule 4: So every pulse that leaves a clock gate is a whole pulse.

    A plain AND gate gives no such promise.

  5. Rule 5: A closed gate stops only what is below it.

    Its parent clock, and every other branch, run on.

  6. Rule 6: The gate enable must come from logic that still has a clock.

    A block cannot switch its own clock back on.

Sources for These Rules

Rules 3 and 4, an enable that must be stable while the clock is high: Weste and Harris, CMOS VLSI Design, fourth edition, section 10.3.5. The same warning for an enabled flip-flop: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 3.2.5.

Clock gates at the ends of the clock network, and their use to stop unused logic: Weste and Harris, CMOS VLSI Design, fourth edition, section 13.4.5. Clock gating as a way to save power: section 5.2.

Rules 1 and 2 are arithmetic on the counter. Rules 5 and 6 follow from the picture. Rule 6 is design practice, not a rule of any standard.

Now Run It

The lab at the end of this page has a parent clock of 10 ns and the two dividers. Both gates sit on the divided clock. One enable feeds the plain AND gate and the clock gate, so each row pair compares the two.

Printed by the lab on Icarus. All times are in nanoseconds.
half  high=10 low=10 period=20
third high=10 low=20 period=30
enable=0 while clk_half=1
plain AND high=3 ns
clock gate high=10 ns
closed: input edges=3 gated edges=0
enable=1 while clk_half=1
plain AND high=7 ns
plain AND high=10 ns
clock gate high=10 ns

Rules 1 and 2 at work: "half" is high 10 and low 10, with a period of 20. "third" is high 10 and low 20, with a period of 30.

Rules 3 and 4 at work: the enable falls while the clock is high. The plain AND gate lets out a pulse of 3 ns. The clock gate lets out all 10.

Rule 5 at work: in the row "closed", three edges went into the gate and none came out. The divider kept counting. After the enable rises again, the plain AND gate lets out 7 ns. The clock gate starts with a pulse of 10.

This lab leaves things out. Its gates have no delay, and its sources are ideal. On a real chip the latch and the AND gate are one cell from the library, placed by a tool. The lab also has no reset, so each divider starts at 0 by itself.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Divide the root for a slow blockA second oscillator for itThe slow clock keeps a fixed relation to the fast one. Only whole-number ratios are simple.
Stop the clock of an idle blockLeave the clock on, and hold the data stillThe clock wires of that block stop toggling, which saves power. The clock now has logic in its path.
A latch in front of the AND gateThe AND gate aloneEvery pulse is whole. One more cell, and the enable must arrive before the clock rises.
One gate high in the treeA gate at every flip-flopOne gate stops a whole branch, buffers included. It can only stop the branch as a whole.

Holding the data still with an enable on each flip-flop is the other way to make a block idle. Enables and Gated Clocks compares the two.

The Trap: A Gate That Passed Every Test

Back to the flip-flop that changed alone. The enable of that AND gate came from a flip-flop on the same clock. In simulation the gate looked clean, because both changed at the same instant.

On the chip the enable fell a little after the rising edge: the delay of one flip-flop. For that long the clock was high and the enable was still 1. Out came a pulse as wide as that delay. One flip-flop was fast enough to take it.

Nothing in the function was wrong, and no test of a thousand periods would show it. The latch removes the fault by construction: a change of enable waits for the low half.

The second trap is rule 6. A block is given the bit that enables its own clock. Software clears the bit to save power. The block is now deaf: the write that would set the bit again needs the clock that is off.

At Scale

This page2 dividers, 1 gate
A phone chipthousands of clock gates

This section looks ahead. One divider and one gate become a clock controller with a register for each.

  1. Gates are placed by tools

    A tool turns "this register changes only when enabled" into clock gates. Clock Gating That Pays shows when a gate saves more than it costs.

  2. Gates sit in the tree

    A clock gate is a cell in the clock tree, with its own delay to balance. Clock Gating Cells in the Tree covers it.

  3. Duty cycle is checked

    A path from a rising edge to a falling edge has only the high time. Half-Cycle Paths and Duty Cycle times one.

  4. The next device

    One more circuit sits in the tree: the switch between two sources. It has the same trap as the AND gate, twice over. That is the next page.

Try It

Run the lab. Two rows measure the two dividers. The rest compares a plain AND gate with the clock gate.

Change the delay before "enable = 0" from 3 to 8. Predict both widths before you run it.

Make clk_third high in two of its three states. Predict its high time and its low time.

Names Printed by the Lab
NameMeaning
halfThe output of the divider by two: its high time, low time and period, in nanoseconds.
thirdThe same three numbers for the divider that is high in one state out of three.
enable=0 while clk_half=1The enable fell in the middle of a high pulse of the divided clock.
plain AND highThe width of the pulse that left the plain AND gate.
clock gate highThe width of the pulse that left the clock gate.
closedThree rising edges went into the closed clock gate. The row counts the edges that came out.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: half high=10 low=10 period=20 third high=10 low=20 period=30 enable=0 while clk_half=1 plain AND high=3 ns clock gate high=10 ns closed: input edges=3 gated edges=0 enable=1 while clk_half=1 plain AND high=7 ns plain AND high=10 ns clock gate high=10 ns
Read included file: clock_source_model.sv
// Simulation-only nominal sources. REF_PERIOD is in ns; no jitter or analog loop.
// REF_START: how long the crystal oscillator takes to start, in ns (0: it runs from the first moment).
`timescale 1ns/1ns
module clock_source_model #(parameter REF_PERIOD = 20, LOCK_EDGES = 5, REF_START = 0)
  (output bit clk_ref, clk_rc, clk_pll, locked);
  bit ref_running;
  initial #(REF_START) ref_running <= 1;
  always #(REF_PERIOD/2) if (ref_running) clk_ref = ~clk_ref;
  always #20 clk_rc = ~clk_rc;
  initial begin
    @(posedge clk_ref);
    forever #(REF_PERIOD/4) clk_pll = ~clk_pll;
  end
  initial begin
    repeat (LOCK_EDGES) @(posedge clk_ref);
    locked <= 1;
  end
endmodule

Check Yourself

Predict the outputIn this run the enable falls 6 ns after the clock rose, and the run ends after that pulse. What is the last row the lab prints?
`timescale 1ns/1ns
module tb;
  wire clk_ref, clk_rc, clk_pll, locked;
  clock_source_model sources(.*);              // clk_pll, period 10 ns, is the input clock of this lab

  // Divide by two: the output toggles on every rising edge of the input.
  bit clk_half;
  always @(posedge clk_pll) clk_half <= ~clk_half;

  // One state out of three: a count of 0, 1, 2, and an output that is high in one state.
  bit [1:0] count3;
  always @(posedge clk_pll) count3 <= (count3 == 2) ? 2'd0 : count3 + 2'd1;
  wire clk_third = (count3 == 1);

  // Two ways to gate clk_half with the same enable.
  bit enable = 1;
  wire and_out = clk_half & enable;            // a plain AND gate
  bit held;                                    // a latch: it follows enable only while the clock is low
  always_latch if (!clk_half) held <= enable;
  wire clk_gated = clk_half & held;            // the clock gate

  // Print the width of every high pulse at the two outputs, once "watch" is set.
  bit watch;
  time and_rose, gated_rose;
  int gated_edges;
  always @(posedge and_out) and_rose = $time;
  always @(negedge and_out) if (watch) $display("plain AND high=%0d ns", $time - and_rose);
  always @(posedge clk_gated) begin gated_rose = $time; gated_edges++; end
  always @(negedge clk_gated) if (watch) $display("clock gate high=%0d ns", $time - gated_rose);

  time rose, fell;
  initial begin
    @(posedge clk_half); rose = $time;
    @(negedge clk_half); fell = $time;
    @(posedge clk_half);
    $display("half  high=%0d low=%0d period=%0d", fell - rose, $time - fell, $time - rose);
    @(posedge clk_third); rose = $time;
    @(negedge clk_third); fell = $time;
    @(posedge clk_third);
    $display("third high=%0d low=%0d period=%0d", fell - rose, $time - fell, $time - rose);
    @(posedge clk_half);
    watch = 1;
    #6 enable = 0;                               // close the gates in the middle of a high pulse
    $display("enable=0 while clk_half=%b", clk_half);
    @(negedge clk_half);
    #1;
    $finish;
  end
endmodule
Read included file: clock_source_model.sv
// Simulation-only nominal sources. REF_PERIOD is in ns; no jitter or analog loop.
// REF_START: how long the crystal oscillator takes to start, in ns (0: it runs from the first moment).
`timescale 1ns/1ns
module clock_source_model #(parameter REF_PERIOD = 20, LOCK_EDGES = 5, REF_START = 0)
  (output bit clk_ref, clk_rc, clk_pll, locked);
  bit ref_running;
  initial #(REF_START) ref_running <= 1;
  always #(REF_PERIOD/2) if (ref_running) clk_ref = ~clk_ref;
  always #20 clk_rc = ~clk_rc;
  initial begin
    @(posedge clk_ref);
    forever #(REF_PERIOD/4) clk_pll = ~clk_pll;
  end
  initial begin
    repeat (LOCK_EDGES) @(posedge clk_ref);
    locked <= 1;
  end
endmodule
ConceptWhat does a divider by N do to the period of a clock?
ConceptA clock is high for 10 ns and low for 20 ns. What is its duty cycle?
ConceptWhy can a plain AND gate not stop a clock safely?
ConceptHow does a clock gate keep every pulse whole?
ConceptThe clock gate of the timer is closed. Which clocks still run?

Common Mistakes

Wrong Stop a clock with a plain AND gate.

An enable that falls while the clock is high cuts the pulse.

Fix Use a clock gate: a latch holds the enable while the clock is high.

Wrong Check only the period of a divided clock.

A divider can have the right period and a duty cycle of one third.

Fix Measure the high time and the low time.

Wrong Let a block hold the enable of its own clock.

With the clock off, the block cannot take the write that turns it on.

Fix Keep the enable in logic whose clock still runs.

Remember

  • A divider by N makes the period N times longer. Its duty cycle depends on how it is built.
  • A plain AND gate on a clock cuts a pulse when its enable changes while the clock is high.
  • A clock gate holds its enable in a latch while the clock is high, so every pulse is whole.
  • A closed gate stops only its own branch, and its enable must live where a clock still runs.

Practice

Report a problem with this page