The Glitch-Free Clock Mux
The chip starts on one clock and moves to another while it runs. The switch between them is a mux, and an ordinary mux is the wrong part for the job. See why, and how the right part takes turns.
You Will Be Able To
- Explain why an ordinary mux makes a short pulse when it switches between two running clocks.
- Give the order of the steps in a glitch-free clock mux, and say on whose edge each one happens.
- Explain why the output pauses during a switch, and why a stopped source stops the switch.
- Lesson
- S5.10
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
The chip moves from its slow clock to its fast one a thousand times without trouble. Then one counter skips a step. The rate is right before the switch and right after it. So what went past in between?
Before You Start
You need Clock Sources and the Chip-Level Clock Tree: the RC oscillator, the PLL, lock and the source mux. You need Dividers and Clock Gates: a clock gate changes only while its clock is low.
One step of this page uses two flip-flops in a row to take in a signal from another clock. A page of the design track explains why. You can follow this page without it.
| Question | |
|---|---|
| 1 | Why does an ordinary mux make a short pulse when it switches between two clocks? |
| 2 | In a glitch-free clock mux, which happens first: the old source goes off, or the new one comes on? |
| 3 | On which edge of which clock does a grant change? |
| 4 | Why does the clock root pause during a switch? |
| 5 | What happens when the new source has stopped? |
The Idea: Switch off, Then Switch On
A mux for data is simple: the select changes and the output follows the other input at once. That is fine for data, which is looked at only on a clock edge. A clock is looked at all the time.
Put two running clocks on an ordinary mux. The request to switch comes when software sends it, at any point of either clock. The output jumps from one clock to the other in the middle of a pulse. What comes out can be a pulse far shorter than any real one. It is a runt pulse, with the same harm as on the last page.
The cure reuses the clock gate. Give each source a gate of its own, and let the gates take turns. First the gate of the old source closes, on a falling edge of the old clock. Only after that the gate of the new source opens, on a falling edge of the new clock.
Each step waits for a moment when its own clock is low, so no pulse is ever cut. Between the two steps both gates are closed and the output rests at 0. A switch therefore costs a short pause. It also needs edges from both sources, and that is a trap of its own.
New Words
| Word | Meaning |
|---|---|
| Clock mux | A circuit that passes one of two or more clocks to its output. |
| Glitch-free clock mux | A clock mux whose output carries whole pulses only, also while it switches. |
| Switch request | The bit from software that names the wanted source. This page says request for short. |
| Grant of a source | A stored bit, one for each source. While it is 1, the gate of that source is open. |
| Handover | The time from the request until the new source reaches the output. |
| Stopped source | A clock source that no longer toggles: an oscillator that is off, or a PLL without its reference. |
One Picture: Two Gates That Take Turns
- Clock / reset
- Control / FSM
- Memory
- Bus
- Two sources, one request
Two clocks run side by side. They share no edges. Software asks for one of them with a single bit, at a moment of its own choosing.
- A gate for each source
Each source gets a clock gate of its own. A stored bit opens it: call that bit the grant of the source. A grant changes only on a falling edge of its own clock.
- The outputs are joined
An OR gate joins the two gated clocks. While one grant is 1 and the other is 0, the root carries exactly one source.
- The old source goes off first
The request changes. The RC gate sees that it is no longer wanted. On a falling edge of the RC clock, its grant goes off. The last RC pulse was a whole one.
- Only then the new source comes on
The PLL gate is wanted, but it must also see that the RC grant is off. Then, on a falling edge of the PLL clock, its grant comes on. The first PLL pulse is a whole one.
- The pause in between
From the moment the old grant goes off until the new grant comes on, both gates are closed. The root is low. In the lab run it stays low for 25 ns, from 200 to 225 ns.
Read the diagram as text
- RC clock: the source in use
- PLL clock: the source that software wants
- Request: from software: 0 for RC, 1 for PLL
- RC grant and gate: changes on falling edges of the RC clock
- PLL grant and gate: changes on falling edges of the PLL clock
- OR gate: joins the two gated clocks
- Clock root: whole pulses only
- RC clock → RC grant and gate: clock
- PLL clock → PLL grant and gate: clock
- Request → RC grant and gate: wants RC
- Request → PLL grant and gate: wants PLL
- RC grant and gate → PLL grant and gate: RC grant is off
- PLL grant and gate → RC grant and gate: PLL grant is off
- RC grant and gate → OR gate: gated RC
- PLL grant and gate → OR gate: gated PLL
- OR gate → Clock root: one clock
The glitch-free clock mux as two clock gates. The lab models its behaviour; the first practice problem builds its gates.
The Smallest Example, Worked by Hand
Use the clocks of the lab chip. The RC clock has a period of 40 ns. It rises at 140 and 180 ns and falls at 160 and 200 ns. The PLL clock has a period of 10 ns. It rises at 145, 155 and so on, and falls at 150, 160 and so on.
Software sends the request at 141 ns. The RC clock rose 1 ns ago and is high. The PLL clock is low.
| Time | Ordinary mux | Glitch-free mux |
|---|---|---|
| 140 ns | Passes RC: the output rises. | Passes RC: the output rises. |
| 141 ns | Passes the PLL clock at once. It is low, so the output falls. A pulse of 1 ns. | Nothing yet. The RC grant waits for falling edges of RC. |
| 160 ns | Follows the PLL clock. | RC falls. The pulse that began at 140 was a whole 20 ns. |
| 200 ns | Follows the PLL clock. | RC falls again: the RC grant goes off. Both grants are 0. |
| 210, 220 ns | Follows the PLL clock. | The PLL clock falls twice. At 220 the PLL grant comes on. |
| 225 ns | Follows the PLL clock. | First PLL rise at the output. The output was low from 200 to 225: 25 ns. |
Why two falling edges at each step, and not one? Each gate takes the request in through two flip-flops in a row. The request comes from another clock, and two flip-flops give it time to settle. Our lab models that as a wait of two edges.
- Cycle 1
At 140 ns the RC clock rises. Both muxes pass it: both outputs go high.
- Cycle 2
At 141 ns the request changes. The ordinary mux now passes the PLL clock, which is low. Its pulse ends after 1 ns.
- Cycle 6
At 145 ns the PLL clock rises, and the ordinary mux follows it. The glitch-free mux is still passing the RC pulse.
- Cycle 11
The glitch-free mux has not changed its source yet. It lets the RC pulse run its full 20 ns.
Read phases and sample points (1 ns)
- Runt: columns 1 through 1.
Recorded samples from the lab on Icarus, one for each nanosecond from 139 ns to 150 ns.
- Cycle 0
The request has just changed. The RC grant is still 1, so the root still carries the RC clock.
- Cycle 8
The RC clock has fallen once since the request, at 160 ns. The mux waits for a second falling edge. One more whole RC pulse passes.
- Cycle 12
At 200 ns the RC clock falls again and the RC grant goes off. Both grants are 0 now. The root is low.
- Cycle 16
At 220 ns, on the second falling edge of the PLL clock, the PLL grant comes on. The PLL clock is low at that moment.
- Cycle 17
The first rising edge of the PLL clock reaches the root at 225 ns. Every pulse from here on is a whole PLL pulse.
Recorded samples from the lab on Icarus, one for each 5 ns from 143 ns to 238 ns.
The Rules
Rule 1: An ordinary mux must not switch between running clocks.
Its output changes source in the middle of a pulse.
Rule 2: Each source has a grant, and the output is the OR of the granted clocks.
A source with a grant of 0 gives a steady 0.
Rule 3: A grant changes only on a falling edge of its own clock.
So the pulse before the change, and the pulse after it, are whole.
Rule 4: The new grant comes on only after the old grant is off.
The two gates are never open together.
Rule 5: The output is low between the two grants.
The pause depends on both clocks. In the lab it is 25 ns: over a PLL period, under an RC period. Every block on that clock must bear it.
Rule 6: Each step needs an edge of its own source.
A stopped source can give no edge. The switch then never finishes.
Sources for These Rules
The glitch-free clock mux of this page is common design practice. No book or standard in our reference library describes it, so no rule here is quoted from one.
Rule 3 rests on the rule of the last page, that an enable must be stable while its clock is high: Weste and Harris, CMOS VLSI Design, fourth edition, section 10.3.5.
Two flip-flops in a row for a signal from another clock: Weste and Harris, CMOS VLSI Design, fourth edition, section 10.6.2.
The count of two falling edges at each step, and the times, belong to our lab model.
Now Run It
The lab at the end of this page gives one request to two muxes. The first is an ordinary mux. The second is a model of the glitch-free mux: it models the waiting, not the gates. A third mux gets a PLL input that never toggles.
request t=141 rc=1 pll=0
plain high=1 ns
root high=20 ns
rc grant off t=200
pll grant on t=220
root low for 25 ns
root high=5 ns source=PLL
dead source: rc_grant=0 pll_grant=0 root=0Rule 1 at work: the ordinary mux lets out a pulse of 1 ns. Rule 3 at work: the same pulse at the glitch-free mux is 20 ns, a whole RC pulse.
Rule 4 at work: the RC grant goes off at 200. The PLL grant comes on at 220, never the other way round. Rule 5 at work: the root is low for 25 ns. The first PLL pulse is high for 5 ns, half of its period.
Rule 6 at work, in the last row. The RC grant went off as it should. The PLL input gave no falling edge, so the PLL grant never came on. Both grants are 0 and the root is 0, for good.
This lab leaves things out. The model waits for edges with a few lines of test code. It has no flip-flops, so it cannot show the settling that the two flip-flops are for. Both sources are ideal.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Two gates that take turns | An ordinary mux, and a reset of the chip after each switch | The chip keeps running through a switch. The mux needs edges from both sources. |
| Off before on | Open the new gate first, for a seamless switch | No overlap, so no pulse made of two clocks. A pause on the output. |
| Grants change on falling edges | Grants change on rising edges | The gate closes while its clock is low, so nothing is cut. The same rule as the clock gate. |
| Two flip-flops at each gate | One flip-flop | Time for a request from another clock to settle. One more period of waiting at each step. |
The Trap: A Switch to a Stopped Clock
Back to the skipped count. That chip had an ordinary mux, and the request was not tied to either clock. Most requests fell where the two clocks agreed. One fell 1 ns after a rising edge, and a pulse of 1 ns went down the tree.
The glitch-free mux removes that fault and brings a new one. Suppose boot code asks for the PLL, and the crystal on this board is broken. The PLL has no reference clock, and its output never moves.
The mux does its duty. It closes the RC gate and waits for a falling edge of the PLL clock. None comes. Now the chip has no clock at all. The code that could ask for RC again needs a clock to run.
There are two cures, and a good chip has both. Software asks for the PLL only after lock. And a small supervisor on a clock of its own watches the switch. It forces a known state if the switch does not finish. The debug problem on this page is about that supervisor.
At Scale
This section looks ahead. One mux between two sources becomes a clock controller that software drives through registers.
A status bit for the switch
A clock controller reports which source is in use now. Software reads it after a request, and does not assume.
A clock monitor
A counter on an independent clock checks that each source still toggles. It can refuse a request for a stopped source.
The timing check must know
Two clocks reach every flip-flop below the mux, and never at once. The program that checks timing must be told so. The page on declaring clocks shows how.
The pause matters to devices
A UART in the middle of a byte sees its clock stop and change rate. Software switches clocks when its devices are idle.
Try It
Run the lab. An ordinary mux and the glitch-free mux get the same request at 141 ns. The last row comes from a second glitch-free mux whose PLL input is held at 0.
Move the request from 141 to 148. The PLL clock is high then. Predict the width of the cut pulse.
Move the request to 163, when both clocks are low. Does the ordinary mux still make a short pulse?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| request | The time at which software asks for the PLL, and the level of each clock at that moment. |
| plain high | The width of the pulse that the ordinary mux was passing when the request came. |
| root high | The width of the same pulse at the output of the glitch-free mux. |
| rc grant off | The time at which the gate of the RC clock closes. |
| pll grant on | The time at which the gate of the PLL clock opens. |
| root low for | How long the root stayed low between its last RC pulse and its first PLL pulse. |
| dead source | The grants and the output of the second mux, whose PLL input never toggles. |
request t=141 rc=1 pll=0 plain high=1 ns root high=20 ns rc grant off t=200 pll grant on t=220 root low for 25 ns root high=5 ns source=PLL dead source: rc_grant=0 pll_grant=0 root=0Read 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
Read included file: clock_path_model.sv
// Transaction-level handoff for the lab. Both sources must keep running.
// Start on RC. The requester holds its choice until the switch finishes.
module clock_path_model(input wire clk_rc, clk_pll, request_pll, gate_open,
output wire clk_root, clk_leaf, output bit clk_div);
bit pass_rc = 1, pass_pll = 0;
always begin
wait (request_pll);
repeat (2) @(negedge clk_rc);
pass_rc <= 0;
@(negedge pass_rc); // the new source counts only edges after the old grant is off
repeat (2) @(negedge clk_pll);
pass_pll <= 1;
wait (!request_pll);
repeat (2) @(negedge clk_pll);
pass_pll <= 0;
@(negedge pass_pll);
repeat (2) @(negedge clk_rc);
pass_rc <= 1;
end
assign clk_root = (pass_rc & clk_rc) | (pass_pll & clk_pll);
always @(posedge clk_root) clk_div <= ~clk_div;
bit held_open;
always_latch if (!clk_div) held_open <= gate_open;
assign clk_leaf = clk_div & held_open;
endmodule
Check Yourself
Common Mistakes
Wrong Switch between two running clocks with an ordinary mux.
The output changes source in the middle of a pulse.
Fix Close the old gate first, then open the new one, each on its own falling edge.
Wrong Expect the new clock on the very next edge.
A handover takes several edges of both clocks, with a pause between.
Fix Read the status of the switch, or wait long enough.
Wrong Ask for a source without checking that it runs.
The mux drops the old source and waits for an edge that never comes.
Fix Check lock first, and keep a supervisor on an independent clock.
Remember
- An ordinary mux cuts a pulse when it switches between two running clocks.
- A glitch-free clock mux is two clock gates: the old one closes first, then the new one opens.
- Each grant changes on a falling edge of its own clock. The root is low in between.
- A switch needs edges from both sources. A stopped source leaves the chip with no clock.