Codiode

Glitch Free Clock Multiplexer

HardVerilog / SystemVerilogBuild

Switching between two running clocks with a plain mux can produce a pulse shorter than either clock, which corrupts the flip-flops downstream. A glitch-free clock mux makes the switch only while both clocks are low.

The module takes two independent, asynchronous clocks (clk_a and clk_b) and routes one of them to clk_out based on the sel signal. When sel transitions, the currently active clock must be cleanly gated off. The system must wait until the active clock is confirmed disabled before enabling the new clock. This break-before-make behavior prevents overlapping clock pulses. To prevent cutting off a clock pulse mid-cycle, the gating signals must only transition when their respective clocks are low.

Timing and Reset Rules

  • Clock edges: All internal synchronization and gating flip-flops must be clocked on the negative edge (negedge) of their respective clocks (clk_a or clk_b).
  • Reset: rst_n is an asynchronous, active-low reset. When asserted, all internal flip-flops must be cleared to 0, driving clk_out to 0.
  • Priority and Feedback: The enable signal for domain A is driven by ~sel ANDed with the inverted final gating signal of domain B. The enable signal for domain B is driven by sel ANDed with the inverted final gating signal of domain A.
  • Synchronization: Each clock domain requires a 2-stage negative-edge synchronizer for its enable signal.
  • Output: clk_out is strictly combinational, combining the clocks and their synchronized enable signals.

Worked Trace

  • Cycle 1: rst_n=0. All synchronizers reset to 0. clk_out=0.
  • Cycle 2: rst_n=1, sel=0. Domain A enable becomes 1.
  • Cycle 3: clk_a negedge. Domain A stage 1 synchronizer becomes 1.
  • Cycle 4: clk_a negedge. Domain A stage 2 synchronizer becomes 1. clk_out now follows clk_a.
  • Cycle 5: sel=1. Domain A enable becomes 0.
  • Cycle 6: clk_a negedge. Domain A stage 1 synchronizer becomes 0.
  • Cycle 7: clk_a negedge. Domain A stage 2 synchronizer becomes 0. clk_a is gated off. Domain B enable becomes 1.
  • Cycle 8: clk_b negedge. Domain B stage 1 synchronizer becomes 1.
  • Cycle 9: clk_b negedge. Domain B stage 2 synchronizer becomes 1. clk_out follows clk_b.

Diagram

Port Table

Signal Direction Width Description
clk_a input 1 First clock source
clk_b input 1 Second clock source
rst_n input 1 Asynchronous active-low reset; clears all synchronizers
sel input 1 Clock select; 0 outputs clk_a, 1 outputs clk_b
clk_out output 1 Glitch-free multiplexed clock output

Constraints

  • All flip-flops must be triggered on the negative edge of their respective clocks.
  • The output clk_out must be strictly combinational logic.
  • A 2-stage synchronizer must be used in both clock domains.
  • Asynchronous active-low reset must clear all internal flip-flops to 0.

Topics

Clock Domain CrossingSynchronizationGlitch-Free

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Clock Domain Crossing and Synchronization

Problem 9 of 36 in the Clock Domain Crossing and Synchronization track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.