Part of Curriculum: CDC, RDC and Low-Power RTL (Module 5: Glitch-Free Clock Multiplexing)
View Full Roadmap →
Level 4 Hard ★ Flagship #38 50 pts ✓ Solved

Glitch-Free Clock Mux — Clock Switching

Clocking & Timing · cdc, industry-rtl, arbiters, clock-domain-crossing, hard, clock-switching

Design a 2-to-1 glitch-free clock multiplexer that switches cleanly between asynchronous clocks clk0 and clk1 using dual feedback handshake registers.

Write a synthesizable Verilog module named top_module matching the ports below exactly.
📐 Circuit Schematic Diagram
selectselclk_outMULTIPLEXERCONDITIONAL SELECT
⚡ Timing & Synthesizability Constraints

Timing Contract: When `select` changes, current clock is gated LOW at its falling edge before the new clock is enabled at its falling edge.

RTL Constraints: Never switch clocks with standard combinational MUXes! Output clock must never produce runt pulses or spikes.

Objective: Understand clock gating hazards, runt pulse prevention, cross-coupled feedback disabling, and safe dynamic clock switching.

Prerequisites: Clock gating, negative-edge flip-flops, glitch analysis.
NameDirWidthDescription
clk0input1First input clock
clk1input1Second input clock
areset_ninput1Asynchronous reset
selectinput1Clock select (0 = clk0, 1 = clk1)
clk_outoutput1Glitch-free output clock
Expected Waveform (Golden Timing Contract)
💡 Need Guidance? Progressive Hints (3 Levels)
Hint 1: Conceptual Direction & Hardware Primitives
A standard multiplexer `select ? clk1 : clk0` produces severe runt clock pulses and glitches whenever `select` toggles while one clock is high.
Hint 2: Implementation Architecture & Approach
Solution: Dual negative-edge synchronizers with mutual cross-feedback: `q0_en` cannot assert until `q1_en` has dropped to 0, and vice versa.
Hint 3: Edge Cases, Timing & Verilog Syntax
Sampling on `negedge` ensures that enable transitions occur only when the respective clock is LOW.
🔒 Reveal Verified Reference RTL Solution (Click to expand)
⚠️ Self-Discovery Warning: Try solving and simulating first! Viewing the verified reference implementation will bypass the real debugging experience.
Verified Synthesizable RTL
module top_module(
  input   clk0,
  input   clk1,
  input   areset_n,
  input   select,
  output  clk_out
);
  reg q0_meta, q0_en;
  reg q1_meta, q1_en;

  // Clock 0 domain gating path (sampled on negedge clk0)
  always @(negedge clk0 or negedge areset_n) begin
    if (!areset_n) begin
      q0_meta <= 1'b0;
      q0_en   <= 1'b0;
    end else begin
      q0_meta <= (!select) && (!q1_en);
      q0_en   <= q0_meta;
    end
  end

  // Clock 1 domain gating path (sampled on negedge clk1)
  always @(negedge clk1 or negedge areset_n) begin
    if (!areset_n) begin
      q1_meta <= 1'b0;
      q1_en   <= 1'b0;
    end else begin
      q1_meta <= select && (!q0_en);
      q1_en   <= q1_meta;
    end
  end

  // Gated clock outputs OR'ed together
  assign clk_out = (clk0 & q0_en) | (clk1 & q1_en);
endmodule
Verilog-2001 / SV Saved
Font: |
// Click Run Tests (or press Ctrl+Enter) to compile your RTL and simulate against hidden testbenches.
Simulation Timing Diagram (VCD Waveform)
100%
No simulation waveform yet. Run tests to simulate and generate VCD traces.
No compilation run yet. Syntax warnings and elaboration messages will appear here.
No simulation output yet. Raw stdout from vvp will appear here.

RTL Problems / Level 4 · Clocking & Timing

What You Will Learn

By solving Glitch-Free Clock Mux — Clock Switching, you will acquire core industry skills:

  • Synthesizable hardware modeling in standard Verilog-2001 / SystemVerilog.
  • Correct handling of interface port contracts, edge-triggered clocks, and active-low/high resets.
  • Simulation waveform interpretation and debugging using timing diagrams (VCD traces).
  • Architectural avoidance of timing violations, glitching hazards, and synthesis warnings.

Problem Statement & Requirements

Design a 2-to-1 glitch-free clock multiplexer that switches cleanly between asynchronous clocks clk0 and clk1 using dual feedback handshake registers.

Hardware design teams at leading semiconductor firms evaluate these exact port contracts, race-condition assertions, and corner cases during technical design reviews and ASIC engineer interviews.

Objective: Understand clock gating hazards, runt pulse prevention, cross-coupled feedback disabling, and safe dynamic clock switching.

Prerequisites: Clock gating, negative-edge flip-flops, glitch analysis.

Port List & Interface Timing

The hidden testbench instantiates top_module. Module port names and bit-widths must match the interface contract exactly:

NameDirWidthDescription
clk0input1First input clock
clk1input1Second input clock
areset_ninput1Asynchronous reset
selectinput1Clock select (0 = clk0, 1 = clk1)
clk_outoutput1Glitch-free output clock

Timing Constraints & Synthesizable RTL Rules

⚡ Synthesizable Implementation Guidelines

Timing Contract: When `select` changes, current clock is gated LOW at its falling edge before the new clock is enabled at its falling edge.

Synthesis Constraints: Never switch clocks with standard combinational MUXes! Output clock must never produce runt pulses or spikes.

Architectural Deep Dive & Implementation Walkthrough

Switching clocks is one of the most hazardous operations in digital hardware. A runt pulse (a clock pulse narrower than the minimum flip-flop pulse width $t_w$) can violate setup/hold times and trigger chip-wide functional collapse. The standard industry solution uses cross-coupled negative-edge flip-flops: the active clock is completely disabled while it is low, and only after confirmation of disablement is the second clock enabled while it is low.

Common RTL Designer Pitfalls

⚠️ Common Mistakes to Avoid

['Sampling on `posedge` instead of `negedge`: enabling a clock on its rising edge cuts into the clock phase, generating runt pulses.', 'Omitting the cross-coupling term `!q1_en` / `!q0_en`, allowing both clocks to be briefly enabled simultaneously.']

Silicon & Hardware Engineering Notes

🛠️ ASIC vs FPGA Hardware Considerations

This circuit is the exact standard cell architecture instantiated inside vendor clock multiplexer primitives (such as BUFGMUX on FPGAs or standard-cell CKLNQM cells in ASIC libraries).

Starter RTL Code

module top_module(
  input   clk0,
  input   clk1,
  input   areset_n,
  input   select,
  output  clk_out
);

  // 2-to-1 Glitch-Free Clock Multiplexer

endmodule

Verified Reference Solution

🔒 Reveal Verified Reference RTL Solution
module top_module(
  input   clk0,
  input   clk1,
  input   areset_n,
  input   select,
  output  clk_out
);
  reg q0_meta, q0_en;
  reg q1_meta, q1_en;

  // Clock 0 domain gating path (sampled on negedge clk0)
  always @(negedge clk0 or negedge areset_n) begin
    if (!areset_n) begin
      q0_meta <= 1'b0;
      q0_en   <= 1'b0;
    end else begin
      q0_meta <= (!select) && (!q1_en);
      q0_en   <= q0_meta;
    end
  end

  // Clock 1 domain gating path (sampled on negedge clk1)
  always @(negedge clk1 or negedge areset_n) begin
    if (!areset_n) begin
      q1_meta <= 1'b0;
      q1_en   <= 1'b0;
    end else begin
      q1_meta <= select && (!q0_en);
      q1_en   <= q1_meta;
    end
  end

  // Gated clock outputs OR'ed together
  assign clk_out = (clk0 & q0_en) | (clk1 & q1_en);
endmodule

Related RTL Problems in Level 4

Frequently Asked Questions

What does Glitch-Free Clock Mux — Clock Switching require?

Design a 2-to-1 glitch-free clock multiplexer that switches cleanly between asynchronous clocks clk0 and clk1 using dual feedback handshake registers. Implement it as Verilog module top_module matching the listed ports.

Is this design combinational or sequential?

Tags: cdc, industry-rtl, arbiters, clock-domain-crossing, hard, clock-switching. If a clock signal is present in the port table, implement it as sequential logic; otherwise, use continuous assignments or combinational blocks.

How is my solution graded?

A hidden SystemVerilog testbench simulates your module in the cloud judge. You pass when all directed corner cases match the golden model and the VCD timing waveform matches the contract.

How does this challenge help with semiconductor interviews?

Technical interviewers at NVIDIA, Qualcomm, Apple, and Intel routinely ask candidates to code, debug, and explain the timing waveforms of Glitch-Free Clock Mux — Clock Switching to assess real-world synthesizability and race-condition awareness.