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.
| Question | |
|---|---|
| 1 | What does a divider by N do to the period of a clock? |
| 2 | What is the duty cycle of a clock? |
| 3 | Why can a plain AND gate not stop a clock safely? |
| 4 | How does a clock gate keep every pulse whole? |
| 5 | A 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
| Word | Meaning |
|---|---|
| Clock divider | A small counter that makes a slower clock from a faster one. |
| Divide ratio | The number N of a divider: N periods go in for each period that comes out. |
| Parent clock | The clock at the input of a divider or of a clock gate. |
| Duty cycle | The high time of a clock divided by its period. Half means equal high and low times. |
| Clock gate | A circuit that passes its parent clock or holds its output low. It changes only between pulses. |
| Gate enable | The input of a clock gate that says "pass the clock": 1 to pass, 0 to stop. |
| Gated clock | The output of a clock gate. |
| Runt pulse | A pulse that is shorter than the circuit was designed for. |
| Transparent latch | A 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
- Clock / reset
- Control / FSM
- Memory
- Bus
- Peripheral
- 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.
- 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.
- 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.
- 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.
- The clock gate
The latch and the AND gate together are the clock gate. Every pulse that leaves it is a whole pulse.
- 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.
| Rising edge of the parent | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Time of that edge | 15 ns | 25 ns | 35 ns | 45 ns |
| Output after the edge | 1 | 0 | 1 | 0 |
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.
| State of the counter | 0 | 1 | 2 |
|---|---|---|---|
| Time spent in the state | 10 ns | 10 ns | 10 ns |
| Output | 0 | 1 | 0 |
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.
| Gate | What it does at 3 ns | Width of the pulse that leaves |
|---|---|---|
| Plain AND gate | Its output follows the enable down at once. | 3 ns: a runt pulse |
| Clock gate | The 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
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.
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.
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.
Rule 4: So every pulse that leaves a clock gate is a whole pulse.
A plain AND gate gives no such promise.
Rule 5: A closed gate stops only what is below it.
Its parent clock, and every other branch, run on.
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.
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 nsRules 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Divide the root for a slow block | A second oscillator for it | The slow clock keeps a fixed relation to the fast one. Only whole-number ratios are simple. |
| Stop the clock of an idle block | Leave the clock on, and hold the data still | The 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 gate | The AND gate alone | Every pulse is whole. One more cell, and the enable must arrive before the clock rises. |
| One gate high in the tree | A gate at every flip-flop | One 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 section looks ahead. One divider and one gate become a clock controller with a register for each.
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.
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.
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.
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
| Name | Meaning |
|---|---|
| half | The output of the divider by two: its high time, low time and period, in nanoseconds. |
| third | The same three numbers for the divider that is high in one state out of three. |
| enable=0 while clk_half=1 | The enable fell in the middle of a high pulse of the divided clock. |
| plain AND high | The width of the pulse that left the plain AND gate. |
| clock gate high | The width of the pulse that left the clock gate. |
| closed | Three rising edges went into the closed clock gate. The row counts the edges that came out. |
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 nsRead 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
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.