]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-dad.git/blob - hw/lx_crosdom_ser_fifo.vhd
Implemented multiple samples per pixel and times tuning in the test software.
[fpga/lx-cpu1/lx-dad.git] / hw / lx_crosdom_ser_fifo.vhd
1 -- Clock Cross Domain Synchronization Elastic Buffer/FIFO
2 --
3 -- Copyright (c) 2014, Pavel Pisa <pisa@cmp.felk.cvut.cz>
4 -- Designed for PiKRON company robotic controller
5 -- All rights reserved.
6 --
7 -- Redistribution and use in source and binary forms, with or without
8 -- modification, are permitted provided that the following conditions are met:
9 --
10 -- 1. Redistributions of source code must retain the above copyright notice, this
11 --    list of conditions and the following disclaimer.
12 -- 2. Redistributions in binary form must reproduce the above copyright notice,
13 --    this list of conditions and the following disclaimer in the documentation
14 --    and/or other materials provided with the distribution.
15 --
16 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 -- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 -- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 -- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 -- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 -- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 -- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 --
27 -- Can be used and distributed under GPLv3 license as well
28
29 library ieee;
30 use ieee.std_logic_1164.all;
31 use ieee.numeric_std.all;
32 use work.util_pkg.all;
33 use work.lx_dad_pkg.all;
34
35 entity lx_crosdom_ser_fifo is
36 generic
37 (
38         fifo_len_g   : positive := 8;
39         sync_adj_g   : integer := 0
40 );
41 port
42 (
43         -- Asynchronous clock domain interface
44         acd_clock_i  : in std_logic;
45         acd_miso_i   : in std_logic;
46         acd_sync_i   : in std_logic;
47         -- Clock
48         clk_i        : in std_logic;
49         reset_i      : in std_logic;
50         -- Output synchronous with clk_i
51         miso_o       : out std_logic;
52         sync_o       : out std_logic;
53         data_ready_o : out std_logic
54 );
55 end lx_crosdom_ser_fifo;
56
57 architecture Behavioral of lx_crosdom_ser_fifo is
58         signal fifo_bits_s     : std_logic_vector(0 to fifo_len_g - 1);
59         signal fifo_bits_r     : std_logic_vector(0 to fifo_len_g - 1);
60
61         signal acd_miso_r      : std_logic;
62         signal acd_sync_r      : std_logic;
63         signal acd_sync_prev_s : std_logic;
64         signal acd_sync_prev_r : std_logic;
65
66         signal acd_in_loc_s    : natural range 0 to fifo_len_g - 1;
67         signal acd_in_loc_r    : natural range 0 to fifo_len_g - 1;
68
69         signal out_loc_s       : natural range 0 to fifo_len_g - 1;
70         signal out_loc_r       : natural range 0 to fifo_len_g - 1;
71
72         signal out_sync_s      : std_logic_vector(0 to fifo_len_g / 2 - 1);
73         signal out_sync_r      : std_logic_vector(0 to fifo_len_g / 2 - 1);
74
75         signal out_miso_s      : std_logic;
76
77   attribute REGISTER_DUPLICATION : string;
78         attribute REGISTER_DUPLICATION of fifo_bits_s : signal is "NO";
79         attribute REGISTER_DUPLICATION of fifo_bits_r : signal is "NO";
80         attribute REGISTER_DUPLICATION of acd_sync_r : signal is "NO";
81         attribute REGISTER_DUPLICATION of out_sync_s : signal is "NO";
82         attribute REGISTER_DUPLICATION of out_sync_r : signal is "NO";
83
84 begin
85         sync_o <= out_sync_r(max(-sync_adj_g, 0));
86
87         data_ready_o <= '0';
88
89 acd_logic:
90         process (acd_miso_r, acd_sync_r, acd_sync_prev_r, acd_in_loc_r, fifo_bits_r)
91         begin
92                 acd_sync_prev_s <= acd_sync_r;
93                 fifo_bits_s <= fifo_bits_r;
94                 if (acd_sync_r = '1') and (acd_sync_prev_r = '0') then
95                         acd_in_loc_s <= 0;
96                         fifo_bits_s(0) <= acd_miso_r;
97                 else
98                         fifo_bits_s(acd_in_loc_r) <= acd_miso_r;
99                         if acd_in_loc_r /= fifo_len_g - 1 then
100                                 acd_in_loc_s <= acd_in_loc_r + 1;
101                         else
102                                 acd_in_loc_s <= 0;
103                         end if;
104                 end if;
105         end process;
106
107 acd_update:
108         process
109         begin
110                 wait until acd_clock_i'event and acd_clock_i = '1';
111
112                 acd_miso_r <= acd_miso_i;
113                 acd_sync_r <= acd_sync_i;
114                 acd_sync_prev_r <= acd_sync_prev_s;
115                 acd_in_loc_r <= acd_in_loc_s;
116                 fifo_bits_r <= fifo_bits_s;
117         end process;
118
119 sync_logic:
120         process (fifo_bits_r, out_loc_r, out_sync_r, acd_sync_r)
121         begin
122                 out_sync_s <= out_sync_r(1 to out_sync_r'length - 1 ) & acd_sync_r;
123                 if out_sync_r(max(sync_adj_g, 0)) = '0' then
124                         out_loc_s <= 0;
125                         out_miso_s <= '0';
126                 else
127                         out_miso_s <= fifo_bits_r(out_loc_r);
128                         if out_loc_r /= fifo_len_g - 1 then
129                                 out_loc_s <= out_loc_r + 1;
130                         else
131                                 out_loc_s <= 0;
132                         end if;
133                 end if;
134         end process;
135
136 sync_update:
137         process
138         begin
139                 wait until clk_i'event and clk_i = '1';
140
141                 out_loc_r <= out_loc_s;
142                 miso_o <= out_miso_s;
143                 out_sync_r <= out_sync_s;
144         end process;
145
146 end Behavioral;