1 |
145 |
lanttu |
-------------------------------------------------------------------------------
|
2 |
|
|
-- Title : CDC (Clock Domain Crossing)
|
3 |
|
|
-- Project :
|
4 |
|
|
-------------------------------------------------------------------------------
|
5 |
|
|
-- File : cdc.vhd
|
6 |
|
|
-- Author : Lasse Lehtonen
|
7 |
|
|
-- Company :
|
8 |
|
|
-- Created : 2011-10-12
|
9 |
|
|
-- Last update: 2011-10-24
|
10 |
|
|
-- Platform :
|
11 |
|
|
-- Standard : VHDL'87
|
12 |
|
|
-------------------------------------------------------------------------------
|
13 |
|
|
-- Description:
|
14 |
|
|
--
|
15 |
|
|
-- Generics:
|
16 |
|
|
--
|
17 |
|
|
-- clock_mode_g 0 : single clock
|
18 |
|
|
-- clock_mode_g 1 : two asynchronous clocks
|
19 |
|
|
--
|
20 |
|
|
-------------------------------------------------------------------------------
|
21 |
|
|
-- Copyright (c) 2011
|
22 |
|
|
-------------------------------------------------------------------------------
|
23 |
|
|
-- Revisions :
|
24 |
|
|
-- Date Version Author Description
|
25 |
|
|
-- 2011-10-12 1.0 lehton87 Created
|
26 |
|
|
-------------------------------------------------------------------------------
|
27 |
|
|
|
28 |
|
|
library ieee;
|
29 |
|
|
use ieee.std_logic_1164.all;
|
30 |
|
|
|
31 |
|
|
entity cdc is
|
32 |
|
|
|
33 |
|
|
generic (
|
34 |
|
|
cmd_width_g : positive;
|
35 |
|
|
data_width_g : positive;
|
36 |
|
|
clock_mode_g : natural);
|
37 |
|
|
|
38 |
|
|
port (
|
39 |
|
|
clk_ip : in std_logic;
|
40 |
|
|
clk_net : in std_logic;
|
41 |
|
|
rst_n : in std_logic;
|
42 |
|
|
|
43 |
|
|
ip_cmd_out : out std_logic_vector(cmd_width_g-1 downto 0);
|
44 |
|
|
ip_data_out : out std_logic_vector(data_width_g-1 downto 0);
|
45 |
|
|
ip_stall_in : in std_logic;
|
46 |
|
|
|
47 |
|
|
ip_cmd_in : in std_logic_vector(cmd_width_g-1 downto 0);
|
48 |
|
|
ip_data_in : in std_logic_vector(data_width_g-1 downto 0);
|
49 |
|
|
ip_stall_out : out std_logic;
|
50 |
|
|
|
51 |
|
|
net_cmd_out : out std_logic_vector(cmd_width_g-1 downto 0);
|
52 |
|
|
net_data_out : out std_logic_vector(data_width_g-1 downto 0);
|
53 |
|
|
net_stall_in : in std_logic;
|
54 |
|
|
|
55 |
|
|
net_cmd_in : in std_logic_vector(cmd_width_g-1 downto 0);
|
56 |
|
|
net_data_in : in std_logic_vector(data_width_g-1 downto 0);
|
57 |
|
|
net_stall_out : out std_logic);
|
58 |
|
|
|
59 |
|
|
end cdc;
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
architecture rtl of cdc is
|
63 |
|
|
|
64 |
|
|
signal ip_in_cd : std_logic_vector(cmd_width_g+data_width_g-1 downto 0);
|
65 |
|
|
signal net_in_cd : std_logic_vector(cmd_width_g+data_width_g-1 downto 0);
|
66 |
|
|
signal ip_out_cd : std_logic_vector(cmd_width_g+data_width_g-1 downto 0);
|
67 |
|
|
signal net_out_cd : std_logic_vector(cmd_width_g+data_width_g-1 downto 0);
|
68 |
|
|
signal ip_out_cd_r : std_logic_vector(cmd_width_g+data_width_g-1 downto 0);
|
69 |
|
|
signal net_out_cd_r : std_logic_vector(cmd_width_g+data_width_g-1 downto 0);
|
70 |
|
|
signal ip_we : std_logic;
|
71 |
|
|
signal net_we : std_logic;
|
72 |
|
|
signal ip_re : std_logic;
|
73 |
|
|
signal net_re : std_logic;
|
74 |
|
|
signal ip_empty : std_logic;
|
75 |
|
|
signal net_empty : std_logic;
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
begin -- rtl
|
79 |
|
|
|
80 |
|
|
-----------------------------------------------------------------------------
|
81 |
|
|
-- ONE CLOCK
|
82 |
|
|
--
|
83 |
|
|
-- Just a direct combinatorial connection
|
84 |
|
|
-----------------------------------------------------------------------------
|
85 |
|
|
clock_mode_0 : if clock_mode_g = 0 generate
|
86 |
|
|
|
87 |
|
|
ip_cmd_out <= net_cmd_in;
|
88 |
|
|
ip_data_out <= net_data_in;
|
89 |
|
|
net_stall_out <= ip_stall_in;
|
90 |
|
|
|
91 |
|
|
net_cmd_out <= ip_cmd_in;
|
92 |
|
|
net_data_out <= ip_data_in;
|
93 |
|
|
ip_stall_out <= net_stall_in;
|
94 |
|
|
|
95 |
|
|
end generate clock_mode_0;
|
96 |
|
|
|
97 |
|
|
-----------------------------------------------------------------------------
|
98 |
|
|
-- TWO ASYNCHRONOUS CLOCKS
|
99 |
|
|
-----------------------------------------------------------------------------
|
100 |
|
|
clock_mode_1 : if clock_mode_g = 1 generate
|
101 |
|
|
|
102 |
|
|
---------------------------------------------------------------------------
|
103 |
|
|
-- FROM IP TO NET
|
104 |
|
|
---------------------------------------------------------------------------
|
105 |
|
|
ip_in_cd <= ip_cmd_in & ip_data_in;
|
106 |
|
|
net_re <= not net_stall_in;
|
107 |
|
|
|
108 |
|
|
ip_we_p : process (ip_cmd_in)
|
109 |
|
|
begin -- process ip_we_p
|
110 |
|
|
if ip_cmd_in /= "00" then
|
111 |
|
|
ip_we <= '1';
|
112 |
|
|
else
|
113 |
|
|
ip_we <= '0';
|
114 |
|
|
end if;
|
115 |
|
|
end process ip_we_p;
|
116 |
|
|
|
117 |
|
|
fifo_ip2net : entity work.fifo_2clk
|
118 |
|
|
generic map (
|
119 |
|
|
data_width_g => cmd_width_g+data_width_g,
|
120 |
|
|
depth_g => 4)
|
121 |
|
|
port map (
|
122 |
|
|
rst_n => rst_n,
|
123 |
|
|
|
124 |
|
|
clk_wr => clk_ip,
|
125 |
|
|
we_in => ip_we,
|
126 |
|
|
data_in => ip_in_cd,
|
127 |
|
|
full_out => ip_stall_out,
|
128 |
|
|
|
129 |
|
|
clk_rd => clk_net,
|
130 |
|
|
re_in => net_re,
|
131 |
|
|
data_out => net_out_cd,
|
132 |
|
|
empty_out => net_empty);
|
133 |
|
|
|
134 |
|
|
sto1_p: process (clk_net, rst_n)
|
135 |
|
|
begin -- process sto1_p
|
136 |
|
|
if rst_n = '0' then -- asynchronous reset (active low)
|
137 |
|
|
net_out_cd_r <= (others => '0');
|
138 |
|
|
elsif clk_net'event and clk_net = '1' then -- rising clock edge
|
139 |
|
|
if net_stall_in = '0' and net_empty = '0' then
|
140 |
|
|
net_out_cd_r <= net_out_cd;
|
141 |
|
|
end if;
|
142 |
|
|
end if;
|
143 |
|
|
end process sto1_p;
|
144 |
|
|
|
145 |
|
|
net_outs_p: process (net_stall_in, net_empty, net_out_cd, net_out_cd_r)
|
146 |
|
|
begin -- process net_outs_p
|
147 |
|
|
if net_stall_in = '1' then
|
148 |
|
|
net_cmd_out <= net_out_cd_r(cmd_width_g+data_width_g-1 downto
|
149 |
|
|
data_width_g);
|
150 |
|
|
net_data_out <= net_out_cd_r(data_width_g-1 downto 0);
|
151 |
|
|
elsif net_empty = '1' then
|
152 |
|
|
net_cmd_out <= (others => '0');
|
153 |
|
|
net_data_out <= (others => '0');
|
154 |
|
|
else
|
155 |
|
|
net_cmd_out <= net_out_cd(cmd_width_g+data_width_g-1 downto
|
156 |
|
|
data_width_g);
|
157 |
|
|
net_data_out <= net_out_cd(data_width_g-1 downto 0);
|
158 |
|
|
end if;
|
159 |
|
|
end process net_outs_p;
|
160 |
|
|
|
161 |
|
|
---------------------------------------------------------------------------
|
162 |
|
|
-- FROM NET TO IP
|
163 |
|
|
---------------------------------------------------------------------------
|
164 |
|
|
net_in_cd <= net_cmd_in & net_data_in;
|
165 |
|
|
ip_re <= not ip_stall_in;
|
166 |
|
|
|
167 |
|
|
net_we_p : process (net_cmd_in)
|
168 |
|
|
begin -- process ip_we_p
|
169 |
|
|
if net_cmd_in /= "00" then
|
170 |
|
|
net_we <= '1';
|
171 |
|
|
else
|
172 |
|
|
net_we <= '0';
|
173 |
|
|
end if;
|
174 |
|
|
end process net_we_p;
|
175 |
|
|
|
176 |
|
|
fifo_net2ip : entity work.fifo_2clk
|
177 |
|
|
generic map (
|
178 |
|
|
data_width_g => cmd_width_g+data_width_g,
|
179 |
|
|
depth_g => 4)
|
180 |
|
|
port map (
|
181 |
|
|
rst_n => rst_n,
|
182 |
|
|
|
183 |
|
|
clk_wr => clk_net,
|
184 |
|
|
we_in => net_we,
|
185 |
|
|
data_in => net_in_cd,
|
186 |
|
|
full_out => net_stall_out,
|
187 |
|
|
|
188 |
|
|
clk_rd => clk_ip,
|
189 |
|
|
re_in => ip_re,
|
190 |
|
|
data_out => ip_out_cd,
|
191 |
|
|
empty_out => ip_empty);
|
192 |
|
|
|
193 |
|
|
sto2_p: process (clk_ip, rst_n)
|
194 |
|
|
begin -- process sto1_p
|
195 |
|
|
if rst_n = '0' then -- asynchronous reset (active low)
|
196 |
|
|
ip_out_cd_r <= (others => '0');
|
197 |
|
|
elsif clk_ip'event and clk_ip = '1' then -- rising clock edge
|
198 |
|
|
if ip_stall_in = '0' and ip_empty = '0' then
|
199 |
|
|
ip_out_cd_r <= ip_out_cd;
|
200 |
|
|
end if;
|
201 |
|
|
end if;
|
202 |
|
|
end process sto2_p;
|
203 |
|
|
|
204 |
|
|
ip_outs_p: process (ip_stall_in, ip_empty, ip_out_cd, ip_out_cd_r)
|
205 |
|
|
begin -- process net_outs_p
|
206 |
|
|
if ip_stall_in = '1' then
|
207 |
|
|
ip_cmd_out <= ip_out_cd_r(cmd_width_g+data_width_g-1 downto
|
208 |
|
|
data_width_g);
|
209 |
|
|
ip_data_out <= ip_out_cd_r(data_width_g-1 downto 0);
|
210 |
|
|
elsif ip_empty = '1' then
|
211 |
|
|
ip_cmd_out <= (others => '0');
|
212 |
|
|
ip_data_out <= (others => '0');
|
213 |
|
|
else
|
214 |
|
|
ip_cmd_out <= ip_out_cd(cmd_width_g+data_width_g-1 downto
|
215 |
|
|
data_width_g);
|
216 |
|
|
ip_data_out <= ip_out_cd(data_width_g-1 downto 0);
|
217 |
|
|
end if;
|
218 |
|
|
end process ip_outs_p;
|
219 |
|
|
|
220 |
|
|
end generate clock_mode_1;
|
221 |
|
|
|
222 |
|
|
end rtl;
|