ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

VHDL Clock Domain Crossing (CDC) Synchronizer

VHDL

Goal -- WPM

Ready
Exercise Algorithm Area
1library ieee;
2use ieee.std_logic_1164.all;
3
4entity cdc_synchronizer is
5generic (
6ASYNC_RESET : boolean := true -- Set to false for synchronous reset
7);
8port (
9clk_fast : in std_logic; -- Clock of the destination domain
10reset_fast : in std_logic; -- Reset for the destination domain
11data_slow : in std_logic; -- Input signal from the slower domain
12data_fast : out std_logic -- Output signal in the faster domain
13);
14end entity cdc_synchronizer;
15
16architecture rtl of cdc_synchronizer is
17
18signal sync_reg1 : std_logic := '0';
19signal sync_reg2 : std_logic := '0';
20
21-- Helper function to perform asynchronous reset if enabled
22procedure apply_async_reset (signal reg : out std_logic; reset_val : std_logic) is
23begin
24if reset_val = '1' then
25reg <= '0';
26end if;
27end procedure apply_async_reset;
28
29begin
30
31-- First flip-flop: Captures the input signal from the slow domain
32-- This flip-flop is sensitive to the fast clock.
33process (clk_fast, reset_fast)
34begin
35if ASYNC_RESET then
36apply_async_reset(sync_reg1, reset_fast);
37end if;
38
39if rising_edge(clk_fast) then
40if not ASYNC_RESET then
41if reset_fast = '1' then
42sync_reg1 <= '0';
43else
44sync_reg1 <= data_slow;
45end if;
46else
47sync_reg1 <= data_slow;
48end if;
49end if;
50end process;
51
52-- Second flip-flop: Synchronizes the output of the first flip-flop
53-- This stage helps to resolve metastability.
54process (clk_fast, reset_fast)
55begin
56if ASYNC_RESET then
57apply_async_reset(sync_reg2, reset_fast);
58end if;
59
60if rising_edge(clk_fast) then
61if not ASYNC_RESET then
62if reset_fast = '1' then
63sync_reg2 <= '0';
64else
65sync_reg2 <= sync_reg1;
66end if;
67else
68sync_reg2 <= sync_reg1;
69end if;
70end if;
71end process;
72
73-- Assign the synchronized output
74data_fast <= sync_reg2;
75
76end architecture rtl;
Algorithm description viewbox

VHDL Clock Domain Crossing (CDC) Synchronizer

Algorithm description:

This VHDL code implements a two-stage synchronizer, a common technique for Clock Domain Crossing (CDC). It takes a signal from a slower clock domain and reliably transfers it to a faster clock domain. This is essential in multi-clock systems to prevent data corruption due to timing violations and metastability.

Algorithm explanation:

The synchronizer uses two flip-flops clocked by the destination (fast) clock. The first flip-flop samples the input signal from the source (slow) domain. If the input signal changes near the clock edge of the fast clock, the first flip-flop might enter a metastable state. The second flip-flop, clocked by the same fast clock, samples the output of the first flip-flop. This second stage has a high probability of resolving the metastable state to a stable '0' or '1' by the time it is sampled. The probability of metastability propagating is significantly reduced. Reset logic is included for initialization. The use of a generic allows for either asynchronous or synchronous reset.

Pseudocode:

Define two registers, sync_reg1 and sync_reg2, clocked by clk_fast.
Apply reset logic to both registers.
In the first process (sensitive to clk_fast and reset_fast):
  If reset is active, sync_reg1 <= '0'.
  Else, sync_reg1 <= data_slow.
In the second process (sensitive to clk_fast and reset_fast):
  If reset is active, sync_reg2 <= '0'.
  Else, sync_reg2 <= sync_reg1.
Output data_fast <= sync_reg2.