Design a clock domain crossing synchronizer for a single-bit signal from fast to slow clock. Use a pulse stretcher with toggle flop followed by double flop synchronizer to prevent metastability and ensure the pulse is wide enough to sample.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is a great question about clock domain crossing (CDC) design. Let me create a comprehensive design for synchronizing a single-bit signal from a fast clock domain to a slow clock domain using:
A pulse stretcher to ensure the pulse is wide enough
A toggle flop to convert the pulse to a level
A double flop synchronizer (2-stage shift register) to prevent metastability
Let me design this step by step with both the SystemVerilog/Verilog code and a detailed explanation.
// Pulse Stretcher - Fast Clock Domain
// Ensures pulse is wide enough (N fast clock cycles)
module pulse_stretcher #(
parameter STRETCH_CYCLES = 4 // Minimum pulse width in fast clock cycles
) (
input logic clk_fast,
input logic rst_n,
input logic pulse_in,
output logic pulse_stretched
);
// 0: no pulse, 1-STRETCH_CYCLES: counting down
logic [$clog2(STRETCH_CYCLES+1)-1:0] counter;
// Counter logic
always_ff @(posedge clk_fast or negedge rst_n) begin
if (!rst_n) begin
counter <= '0;
end else if (pulse_in) begin
// New pulse detected, reload counter
counter <= STRETCH_CYCLES;
end else if (counter != '0) begin
// Counting down
counter <= counter - 1;
end
end
// Output is high while counter is non-zero
assign pulse_stretched = (counter != '0);
endmodule
2.2 Toggle Flop (Fast Clock Domain)
systemverilog
// Toggle Flop - Fast Clock Domain
// Toggles output on every rising edge of pulse_stretched
// Converts pulse train to level signal for synchronization
module toggle_flop (
input logic clk_fast,
input logic rst_n,
input logic pulse_in, // Stretched pulse
output logic toggle_out // Level signal (toggles per pulse)
);
logic pulse_in_r;
logic toggle_ff;
// Register stretched pulse for edge detection
always_ff @(posedge clk_fast or negedge rst_n) begin
if (!rst_n) begin
pulse_in_r <= 1'b0;
toggle_ff <= 1'b0;
end else begin
pulse_in_r <= pulse_in;
// Toggle on rising edge detection
if (pulse_in && !pulse_in_r) begin
toggle_ff <= ~toggle_ff;
end
end
end
assign toggle_out = toggle_ff;
endmodule
2.3 Double-Flop Synchronizer (Slow Clock Domain)
systemverilog
// Double Flop Synchronizer - Slow Clock Domain
// Metastability-hardened synchronization
module double_flop_sync (
input logic clk_slow,
input logic rst_n,
input logic toggle_in, // From fast domain toggle flop
output logic toggle_out_sync // Synchronized to slow clock
);
// Two-stage shift register for synchronization
logic sync_stage1, sync_stage2;
always_ff @(posedge clk_slow or negedge rst_n) begin
if (!rst_n) begin
sync_stage1 <= 1'b0;
sync_stage2 <= 1'b0;
end else begin
sync_stage1 <= toggle_in; // First stage synchronizer
sync_stage2 <= sync_stage1; // Second stage
end
end
assign toggle_out_sync = sync_stage2;
endmodule
2.4 Edge Detector (Slow Clock Domain)
systemverilog
// Edge Detector - Slow Clock Domain
// Detects toggle changes and generates output pulse
module edge_detector (
input logic clk_slow,
input logic rst_n,
input logic toggle_in, // Synchronized toggle signal
output logic pulse_out // Output pulse (1 slow clock cycle)
);
logic toggle_ff;
always_ff @(posedge clk_slow or negedge rst_n) begin
if (!rst_n) begin
toggle_ff <= 1'b0;
end else begin
toggle_ff <= toggle_in;
end
end
// Detect any change (rising or falling edge)
assign pulse_out = toggle_in ^ toggle_ff;
endmodule
/*
TIMING ANALYSIS:
================
Signal Path: pulse_in → pulse_stretcher → toggle_flop →
double_flop_sync → edge_detector → pulse_out
Latency Calculation:
- Fast Clock Domain Latency:
• Pulse Stretcher: 0-STRETCH_CYCLES fast clock cycles
• Toggle Detection: 1-2 fast clock cycles
Total in fast domain: ~STRETCH_CYCLES + 2 cycles
- CDC Crossing:
• Synchronization: 2 slow clock cycles
• Edge Detection: 1 slow clock cycle
Total crossing: ~3 slow clock cycles
- Total Latency:
= (STRETCH_CYCLES + 2) * Tfast + 3 * Tslow
= (4 + 2) * 10ns + 3 * 100ns
= 60ns + 300ns
= 360ns minimum
METASTABILITY ANALYSIS:
=======================
The double-flop synchronizer provides:
- First flip-flop: Catches metastable signal
- Second flip-flop: Ensures clean output
- MTBF = exp(Tsetup/t0) / (C * Tdata)
where t0 ≈ 6 tau for typical library
For 100MHz fast / 10MHz slow clock:
- MTBF >> product lifetime (excellent)
PULSE WIDTH REQUIREMENTS:
=========================
Minimum slow clock cycles needed: 3-4
- 1 cycle for synchronizer stage 1
- 1 cycle for synchronizer stage 2
- 1 cycle for edge detection
With STRETCH_CYCLES = 4 and 10:1 frequency ratio:
- Stretched pulse = 4 * 10ns = 40ns
- In slow clock: 40ns / 100ns ≈ 0.4 cycles (becomes ~1-2 cycles after sync)
- Adequate margin for reliable detection
*/
5. Configuration Recommendations
systemverilog
module cdc_config_examples();
/*
SCENARIO 1: 10x Frequency Ratio (100MHz → 10MHz)
================================================*/
// STRETCH_CYCLES = 4-6 recommended
// Ensures pulse visible in slow domain
/*
SCENARIO 2: 100x Frequency Ratio (1GHz → 10MHz)
================================================*/
// STRETCH_CYCLES = 10-20 recommended
// Higher clock ratio needs wider pulse margin
/*
SCENARIO 3: Asynchronous Reset Synchronizer
============================================*/
// Use separate double-flop for rst_n!
module reset_sync (
input logic clk_dst,
input logic rst_async,
output logic rst_sync_n
);
logic sync_ff1, sync_ff2;
always_ff @(posedge clk_dst or negedge rst_async) begin
if (!rst_async) begin
sync_ff1 <= 1'b0;
sync_ff2 <= 1'b0;
end else begin
sync_ff1 <= 1'b1;
sync_ff2 <= sync_ff1;
end
end
assign rst_sync_n = sync_ff2;
endmodule
endmodule
6. Key Design Features
Feature
Benefit
Pulse Stretcher
Ensures pulse visible in slow clock domain
Toggle Flop
Converts all pulses to level changes (reliable CDC)
Double-Flop Sync
Proven CDC structure, handles metastability
Edge Detector
Recovers pulse-like output in slow domain
Configurable STRETCH_CYCLES
Adapts to different clock ratios
This design ensures reliable pulse detection across clock domains with proven metastability protection. The pulse stretcher is critical for maintaining pulse visibility across large frequency ratios.
module cdc_sync_tb();
// Parameters
localparam STRETCH_CYCLES = 4;
localparam FAST_CLK_PERIOD = 10; // 10 ns = 100 MHz
localparam SLOW_CLK_PERIOD = 100; // 100 ns = 10 MHz
// Testbench signals
logic clk_fast, clk_slow;
logic rst_n;
logic pulse_in;
logic pulse_out;
integer pulse_count = 0;
integer output_pulse_count = 0;
// DUT instantiation
cdc_fast_to_slow_sync #(
.STRETCH_CYCLES(STRETCH_CYCLES)
) dut (
.clk_fast (clk_fast),
.clk_slow (clk_slow),
.rst_n (rst_n),
.pulse_in (pulse_in),
.pulse_out (pulse_out)
);
// ====== Clock Generation ======
initial begin
clk_fast = 1'b0;
forever #(FAST_CLK_PERIOD/2) clk_fast = ~clk_fast;
end
initial begin
clk_slow = 1'b0;
forever #(SLOW_CLK_PERIOD/2) clk_slow = ~clk_slow;
end
// ====== Reset Generation ======
initial begin
rst_n = 1'b0;
#100 rst_n = 1'b1;
end
// ====== Test Stimulus ======
initial begin
pulse_in = 1'b0;
@(posedge rst_n);
#200;
// Test 1: Single pulse at random time
repeat (5) @(posedge clk_fast);
test_single_pulse("Test 1: Single Pulse");
#500;
// Test 2: Pulse train
test_pulse_train("Test 2: Pulse Train", 3, 50);
#500;
// Test 3: Back-to-back pulses
test_back_to_back_pulses("Test 3: Back-to-Back Pulses", 5);
#500;
// Test 4: Very narrow pulse (shorter than STRETCH_CYCLES)
test_narrow_pulse("Test 4: Narrow Pulse Input");
#500;
$display("\n===== Test Summary =====");
$display("Total pulses injected: %0d", pulse_count);
$display("Total pulses received: %0d", output_pulse_count);
$finish;
end
// ====== Test Tasks ======
task test_single_pulse(string test_name);
$display("\n%s @ %0t ns", test_name, $time);
@(posedge clk_fast);
pulse_in = 1'b1;
pulse_count++;
@(posedge clk_fast);
pulse_in = 1'b0;
endtask
task test_pulse_train(string test_name, int num_pulses, int gap_cycles);
$display("\n%s @ %0t ns", test_name, test_name);
repeat (num_pulses) begin
@(posedge clk_fast);
pulse_in = 1'b1;
pulse_count++;
@(posedge clk_fast);
pulse_in = 1'b0;
repeat (gap_cycles) @(posedge clk_fast);
end
endtask
task test_back_to_back_pulses(string test_name, int num_pulses);
$display("\n%s @ %0t ns", test_name, $time);
repeat (num_pulses) begin
@(posedge clk_fast);
pulse_in = 1'b1;
pulse_count++;
@(posedge clk_fast);
pulse_in = 1'b0;
repeat (20) @(posedge clk_fast);
end
endtask
task test_narrow_pulse(string test_name);
$display("\n%s @ %0t ns", test_name, $time);
@(posedge clk_fast);
pulse_in = 1'b1;
pulse_count++;
#(FAST_CLK_PERIOD/3); // Pulse narrower than 1 clock cycle
pulse_in = 1'b0;
endtask
// ====== Monitor Output ======
initial begin
@(posedge rst_n);
forever begin
@(posedge clk_slow);
if (pulse_out) begin
output_pulse_count++;
$display("Output pulse detected @ %0t ns", $time);
end
end
end
// ====== Waveform Dumping ======
initial begin
$dumpfile("cdc_sync_tb.vcd");
$dumpvars(0, cdc_sync_tb);
end
endmodule