Codiode

The Glitchy Clock Multiplexer

HardVerilog / SystemVerilogBuild

Switching between a fast PLL clock and a slow oscillator with a plain mux can produce a pulse shorter than either clock. That runt pulse can corrupt every flip-flop on the output clock.

The solution module must safely switch the output clock between two free-running asynchronous clocks based on a select signal. When the select signal changes, the module must ensure a clean, break-before-make handoff. The current clock's enable must be fully de-asserted and synchronized into the new clock's domain before the new clock's enable can be asserted. This prevents any overlapping high periods that would cause a glitch.

To achieve this, you must implement a classic two-stage synchronizer feedback loop. Each clock domain requires its own two-stage synchronizer to process the enable signals. To prevent the enable signal from changing while the clock is high, all synchronization flip-flops must be triggered on the negative edge of their respective clocks.

Port Table

Signal Direction Width Description
clk_a input 1 First asynchronous free-running clock
clk_b input 1 Second asynchronous free-running clock
rst_n input 1 Asynchronous active-low reset; drives output to 0 and clears all internal registers
sel input 1 Clock select signal; 0 selects clk_a, 1 selects clk_b
clk_out output 1 The multiplexed clock output

Worked Trace

  • Event 1: rst_n=0, sel=0, clk_a=0, clk_b=0 → clk_out=0
  • Event 2: rst_n=1, clk_a negedge 1 → A-domain sync stage 1 loaded
  • Event 3: clk_a negedge 2 → A-domain sync stage 2 loaded, domain A enabled
  • Event 4: clk_a goes high → clk_out=1 (following clk_a)
  • Event 5: sel=1 → select changes to B, but domain A remains enabled
  • Event 6: clk_a negedge 1 → A-domain sync stage 1 cleared
  • Event 7: clk_a negedge 2 → A-domain sync stage 2 cleared, domain A disabled
  • Event 8: clk_b negedge 1 → B-domain sync stage 1 loaded (since domain A is now disabled)
  • Event 9: clk_b negedge 2 → B-domain sync stage 2 loaded, domain B enabled
  • Event 10: clk_b goes high → clk_out=1 (following clk_b)

Constraints

  • All synchronization flip-flops must be negative-edge triggered.
  • You must use exactly two flip-flops in sequence for each clock domain's synchronizer.
  • The design must not produce any pulses shorter than the high-time of either clock.
  • rst_n must asynchronously clear all internal registers to 0.
  • The final output must be a combinational logic function of the clocks and the synchronized enable signals.

Topics

FSMSynchronizationClockingGlitch-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 10 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.