1 |
25 |
mikel262 |
-------------------------------------------------------------------------------
|
2 |
|
|
-- File Name : BUF_FIFO.vhd
|
3 |
|
|
--
|
4 |
|
|
-- Project : JPEG_ENC
|
5 |
|
|
--
|
6 |
|
|
-- Module : BUF_FIFO
|
7 |
|
|
--
|
8 |
|
|
-- Content : Input FIFO Buffer
|
9 |
|
|
--
|
10 |
|
|
-- Description :
|
11 |
|
|
--
|
12 |
|
|
-- Spec. :
|
13 |
|
|
--
|
14 |
|
|
-- Author : Michal Krepa
|
15 |
|
|
--
|
16 |
|
|
-------------------------------------------------------------------------------
|
17 |
|
|
-- History :
|
18 |
|
|
-- 20090311: (MK): Initial Creation.
|
19 |
|
|
-------------------------------------------------------------------------------
|
20 |
|
|
|
21 |
|
|
-------------------------------------------------------------------------------
|
22 |
|
|
-------------------------------------------------------------------------------
|
23 |
|
|
----------------------------------- LIBRARY/PACKAGE ---------------------------
|
24 |
|
|
-------------------------------------------------------------------------------
|
25 |
|
|
-------------------------------------------------------------------------------
|
26 |
|
|
|
27 |
|
|
-------------------------------------------------------------------------------
|
28 |
|
|
-- generic packages/libraries:
|
29 |
|
|
-------------------------------------------------------------------------------
|
30 |
|
|
library ieee;
|
31 |
|
|
use ieee.std_logic_1164.all;
|
32 |
|
|
use ieee.numeric_std.all;
|
33 |
|
|
|
34 |
|
|
-------------------------------------------------------------------------------
|
35 |
|
|
-- user packages/libraries:
|
36 |
|
|
-------------------------------------------------------------------------------
|
37 |
|
|
library work;
|
38 |
|
|
use work.JPEG_PKG.all;
|
39 |
|
|
-------------------------------------------------------------------------------
|
40 |
|
|
-------------------------------------------------------------------------------
|
41 |
|
|
----------------------------------- ENTITY ------------------------------------
|
42 |
|
|
-------------------------------------------------------------------------------
|
43 |
|
|
-------------------------------------------------------------------------------
|
44 |
|
|
entity BUF_FIFO is
|
45 |
|
|
port
|
46 |
|
|
(
|
47 |
|
|
CLK : in std_logic;
|
48 |
|
|
RST : in std_logic;
|
49 |
|
|
-- HOST PROG
|
50 |
|
|
img_size_x : in std_logic_vector(15 downto 0);
|
51 |
|
|
img_size_y : in std_logic_vector(15 downto 0);
|
52 |
|
|
sof : in std_logic;
|
53 |
|
|
|
54 |
|
|
-- HOST DATA
|
55 |
|
|
iram_wren : in std_logic;
|
56 |
|
|
iram_wdata : in std_logic_vector(23 downto 0);
|
57 |
|
|
fifo_almost_full : out std_logic;
|
58 |
|
|
|
59 |
|
|
-- FDCT
|
60 |
|
|
fdct_block_cnt : in std_logic_vector(12 downto 0);
|
61 |
|
|
fdct_fifo_rd : in std_logic;
|
62 |
|
|
fdct_fifo_empty : out std_logic;
|
63 |
|
|
fdct_fifo_q : out std_logic_vector(23 downto 0);
|
64 |
|
|
fdct_fifo_hf_full : out std_logic
|
65 |
|
|
);
|
66 |
|
|
end entity BUF_FIFO;
|
67 |
|
|
|
68 |
|
|
-------------------------------------------------------------------------------
|
69 |
|
|
-------------------------------------------------------------------------------
|
70 |
|
|
----------------------------------- ARCHITECTURE ------------------------------
|
71 |
|
|
-------------------------------------------------------------------------------
|
72 |
|
|
-------------------------------------------------------------------------------
|
73 |
|
|
architecture RTL of BUF_FIFO is
|
74 |
|
|
|
75 |
28 |
mikel262 |
constant C_NUM_SUBF : integer := C_MAX_LINE_WIDTH/8;
|
76 |
|
|
constant C_PIXEL_BITS : integer := 24;
|
77 |
|
|
constant C_SUBF_ADDRW : integer := 7-C_MEMORY_OPTIMIZED;
|
78 |
|
|
--constant C_LOG2_NUM_SUBF : integer := integer(log2(real(C_NUM_SUBF)));
|
79 |
25 |
mikel262 |
|
80 |
|
|
type T_DATA_ARR is array (0 to C_NUM_SUBF-1) of std_logic_vector(23 downto 0);
|
81 |
|
|
type T_CNT_ARR is array (0 to C_NUM_SUBF-1) of
|
82 |
28 |
mikel262 |
std_logic_vector(C_SUBF_ADDRW downto 0);
|
83 |
|
|
|
84 |
|
|
type T_FIFO_RAMADDR is array (0 to C_NUM_SUBF-1) of
|
85 |
|
|
STD_LOGIC_VECTOR(C_SUBF_ADDRW-1 downto 0);
|
86 |
25 |
mikel262 |
|
87 |
|
|
signal fifo_rd : std_logic_vector(C_NUM_SUBF-1 downto 0);
|
88 |
|
|
signal fifo_wr : std_logic_vector(C_NUM_SUBF-1 downto 0);
|
89 |
|
|
signal fifo_data : std_logic_vector(23 downto 0);
|
90 |
|
|
signal fifo_data_d1 : std_logic_vector(23 downto 0);
|
91 |
|
|
signal fifo_full : std_logic_vector(C_NUM_SUBF-1 downto 0);
|
92 |
|
|
signal fifo_empty : std_logic_vector(C_NUM_SUBF-1 downto 0);
|
93 |
|
|
signal fifo_half_full : std_logic_vector(C_NUM_SUBF-1 downto 0);
|
94 |
|
|
signal fifo_count : T_CNT_ARR;
|
95 |
|
|
|
96 |
|
|
signal pixel_cnt : unsigned(15 downto 0);
|
97 |
|
|
signal wblock_cnt : unsigned(12 downto 0);
|
98 |
|
|
signal last_idx : unsigned(12 downto 0);
|
99 |
|
|
signal idx_reg : unsigned(log2(C_NUM_SUBF)-1 downto 0);
|
100 |
28 |
mikel262 |
signal wr_idx_reg : unsigned(log2(C_NUM_SUBF)-1 downto 0);
|
101 |
25 |
mikel262 |
|
102 |
28 |
mikel262 |
signal ramq : STD_LOGIC_VECTOR(C_PIXEL_BITS-1 downto 0);
|
103 |
|
|
signal ramd : STD_LOGIC_VECTOR (C_PIXEL_BITS-1 downto 0);
|
104 |
|
|
signal ramwaddr : STD_LOGIC_VECTOR
|
105 |
|
|
(log2(C_NUM_SUBF)+C_SUBF_ADDRW-1 downto 0);
|
106 |
|
|
signal ramwaddr_offset : unsigned(C_SUBF_ADDRW-1 downto 0);
|
107 |
|
|
signal ramwaddr_base : unsigned(log2(C_NUM_SUBF)+C_SUBF_ADDRW downto 0);
|
108 |
|
|
signal ramenw : STD_LOGIC;
|
109 |
|
|
signal ramenw_m1 : STD_LOGIC;
|
110 |
|
|
signal ramenw_m2 : STD_LOGIC;
|
111 |
|
|
signal ramraddr : STD_LOGIC_VECTOR
|
112 |
|
|
(log2(C_NUM_SUBF)+C_SUBF_ADDRW-1 downto 0);
|
113 |
|
|
signal ramraddr_base : unsigned(log2(C_NUM_SUBF)+C_SUBF_ADDRW downto 0);
|
114 |
|
|
signal ramraddr_offset : unsigned(C_SUBF_ADDRW-1 downto 0);
|
115 |
|
|
signal ramenr : STD_LOGIC;
|
116 |
|
|
|
117 |
|
|
signal fifo_ramwaddr : T_FIFO_RAMADDR;
|
118 |
|
|
signal fifo_ramenw : STD_LOGIC_VECTOR(C_NUM_SUBF-1 downto 0);
|
119 |
|
|
signal fifo_ramraddr : T_FIFO_RAMADDR;
|
120 |
|
|
signal fifo_ramenr : STD_LOGIC_VECTOR(C_NUM_SUBF-1 downto 0);
|
121 |
|
|
|
122 |
|
|
signal offset_ramwaddr : STD_LOGIC_VECTOR(C_SUBF_ADDRW-1 downto 0);
|
123 |
25 |
mikel262 |
-------------------------------------------------------------------------------
|
124 |
|
|
-- Architecture: begin
|
125 |
|
|
-------------------------------------------------------------------------------
|
126 |
|
|
begin
|
127 |
|
|
|
128 |
|
|
-------------------------------------------------------------------
|
129 |
|
|
-- SUB_FIFOs
|
130 |
|
|
-------------------------------------------------------------------
|
131 |
|
|
G_SUB_FIFO : for i in 0 to C_NUM_SUBF-1 generate
|
132 |
|
|
|
133 |
28 |
mikel262 |
U_SUB_FIFO : entity work.SUB_FIFO
|
134 |
25 |
mikel262 |
generic map
|
135 |
|
|
(
|
136 |
28 |
mikel262 |
DATA_WIDTH => C_PIXEL_BITS,
|
137 |
|
|
ADDR_WIDTH => C_SUBF_ADDRW
|
138 |
25 |
mikel262 |
)
|
139 |
|
|
port map
|
140 |
|
|
(
|
141 |
|
|
rst => RST,
|
142 |
|
|
clk => CLK,
|
143 |
|
|
rinc => fifo_rd(i),
|
144 |
|
|
winc => fifo_wr(i),
|
145 |
|
|
|
146 |
|
|
fullo => fifo_full(i),
|
147 |
|
|
emptyo => fifo_empty(i),
|
148 |
28 |
mikel262 |
count => fifo_count(i),
|
149 |
|
|
|
150 |
|
|
ramwaddr => fifo_ramwaddr(i),
|
151 |
|
|
ramenw => fifo_ramenw(i),
|
152 |
|
|
ramraddr => fifo_ramraddr(i),
|
153 |
|
|
ramenr => fifo_ramenr(i)
|
154 |
25 |
mikel262 |
);
|
155 |
|
|
end generate G_SUB_FIFO;
|
156 |
|
|
|
157 |
|
|
-------------------------------------------------------------------
|
158 |
28 |
mikel262 |
-- RAM for SUB_FIFOs
|
159 |
|
|
-------------------------------------------------------------------
|
160 |
|
|
U_SUB_RAMZ : entity work.SUB_RAMZ
|
161 |
|
|
generic map
|
162 |
|
|
(
|
163 |
|
|
RAMADDR_W => log2(C_NUM_SUBF)+C_SUBF_ADDRW,
|
164 |
|
|
RAMDATA_W => C_PIXEL_BITS
|
165 |
|
|
)
|
166 |
|
|
port map
|
167 |
|
|
(
|
168 |
|
|
d => ramd,
|
169 |
|
|
waddr => ramwaddr,
|
170 |
|
|
raddr => ramraddr,
|
171 |
|
|
we => ramenw,
|
172 |
|
|
clk => clk,
|
173 |
|
|
|
174 |
|
|
q => ramq
|
175 |
|
|
);
|
176 |
|
|
|
177 |
|
|
-------------------------------------------------------------------
|
178 |
25 |
mikel262 |
-- FIFO almost full
|
179 |
|
|
-------------------------------------------------------------------
|
180 |
|
|
p_fifo_almost_full : process(CLK, RST)
|
181 |
|
|
begin
|
182 |
|
|
if RST = '1' then
|
183 |
|
|
fifo_almost_full <= '1';
|
184 |
|
|
last_idx <= (others => '0');
|
185 |
|
|
elsif CLK'event and CLK = '1' then
|
186 |
|
|
if img_size_x = (img_size_x'range => '0') then
|
187 |
|
|
last_idx <= (others => '0');
|
188 |
|
|
else
|
189 |
|
|
last_idx <= unsigned(img_size_x(15 downto 3))-1;
|
190 |
|
|
end if;
|
191 |
|
|
|
192 |
|
|
if last_idx > 0 then
|
193 |
|
|
if C_MEMORY_OPTIMIZED = 0 then
|
194 |
|
|
if unsigned(fifo_count(to_integer(last_idx)-2)) > to_unsigned(128-2*8,8) then
|
195 |
|
|
fifo_almost_full <= '1';
|
196 |
|
|
else
|
197 |
|
|
fifo_almost_full <= '0';
|
198 |
|
|
end if;
|
199 |
|
|
else
|
200 |
|
|
if unsigned(fifo_count(to_integer(last_idx))) = to_unsigned(64,8) then
|
201 |
|
|
fifo_almost_full <= '1';
|
202 |
|
|
else
|
203 |
|
|
fifo_almost_full <= '0';
|
204 |
|
|
end if;
|
205 |
|
|
end if;
|
206 |
|
|
end if;
|
207 |
|
|
end if;
|
208 |
|
|
end process;
|
209 |
|
|
|
210 |
|
|
-------------------------------------------------------------------
|
211 |
|
|
-- pixel_cnt
|
212 |
|
|
-------------------------------------------------------------------
|
213 |
|
|
p_pixel_cnt : process(CLK, RST)
|
214 |
|
|
begin
|
215 |
|
|
if RST = '1' then
|
216 |
|
|
pixel_cnt <= (others => '0');
|
217 |
|
|
elsif CLK'event and CLK = '1' then
|
218 |
|
|
if iram_wren = '1' then
|
219 |
|
|
if pixel_cnt = unsigned(img_size_x)-1 then
|
220 |
|
|
pixel_cnt <= (others => '0');
|
221 |
|
|
else
|
222 |
|
|
pixel_cnt <= pixel_cnt + 1;
|
223 |
|
|
end if;
|
224 |
|
|
end if;
|
225 |
|
|
|
226 |
|
|
if sof = '1' then
|
227 |
|
|
pixel_cnt <= (others => '0');
|
228 |
|
|
end if;
|
229 |
|
|
end if;
|
230 |
|
|
end process;
|
231 |
|
|
|
232 |
|
|
wblock_cnt <= pixel_cnt(pixel_cnt'high downto 3);
|
233 |
|
|
|
234 |
|
|
-------------------------------------------------------------------
|
235 |
|
|
-- FIFO half full
|
236 |
|
|
-------------------------------------------------------------------
|
237 |
|
|
p_half_full : process(CLK, RST)
|
238 |
|
|
begin
|
239 |
|
|
if RST = '1' then
|
240 |
|
|
for i in 0 to C_NUM_SUBF-1 loop
|
241 |
|
|
fifo_half_full(i) <= '0';
|
242 |
|
|
end loop;
|
243 |
|
|
elsif CLK'event and CLK = '1' then
|
244 |
|
|
for i in 0 to C_NUM_SUBF-1 loop
|
245 |
|
|
if unsigned(fifo_count(i)) >= 64 then
|
246 |
|
|
fifo_half_full(i) <= '1';
|
247 |
|
|
else
|
248 |
|
|
fifo_half_full(i) <= '0';
|
249 |
|
|
end if;
|
250 |
|
|
end loop;
|
251 |
|
|
end if;
|
252 |
|
|
end process;
|
253 |
|
|
|
254 |
|
|
-------------------------------------------------------------------
|
255 |
|
|
-- Mux1
|
256 |
|
|
-------------------------------------------------------------------
|
257 |
|
|
p_mux1 : process(CLK, RST)
|
258 |
|
|
begin
|
259 |
|
|
if RST = '1' then
|
260 |
|
|
for i in 0 to C_NUM_SUBF-1 loop
|
261 |
|
|
fifo_wr(i) <= '0';
|
262 |
|
|
end loop;
|
263 |
|
|
elsif CLK'event and CLK = '1' then
|
264 |
|
|
for i in 0 to C_NUM_SUBF-1 loop
|
265 |
|
|
if wblock_cnt(log2(C_NUM_SUBF)-1 downto 0) = i then
|
266 |
|
|
fifo_wr(i) <= iram_wren;
|
267 |
|
|
else
|
268 |
|
|
fifo_wr(i) <= '0';
|
269 |
|
|
end if;
|
270 |
|
|
end loop;
|
271 |
|
|
end if;
|
272 |
|
|
end process;
|
273 |
|
|
|
274 |
|
|
-------------------------------------------------------------------
|
275 |
|
|
-- Mux2
|
276 |
|
|
-------------------------------------------------------------------
|
277 |
|
|
p_mux2 : process(CLK, RST)
|
278 |
|
|
begin
|
279 |
|
|
if RST = '1' then
|
280 |
|
|
for i in 0 to C_NUM_SUBF-1 loop
|
281 |
28 |
mikel262 |
fifo_rd(i) <= '0';
|
282 |
25 |
mikel262 |
end loop;
|
283 |
28 |
mikel262 |
fdct_fifo_empty <= '0';
|
284 |
|
|
fdct_fifo_hf_full <= '0';
|
285 |
|
|
idx_reg <= (others => '0');
|
286 |
25 |
mikel262 |
elsif CLK'event and CLK = '1' then
|
287 |
|
|
idx_reg <= unsigned(fdct_block_cnt(log2(C_NUM_SUBF)-1 downto 0));
|
288 |
|
|
|
289 |
|
|
for i in 0 to C_NUM_SUBF-1 loop
|
290 |
|
|
if idx_reg = i then
|
291 |
28 |
mikel262 |
fifo_rd(i) <= fdct_fifo_rd;
|
292 |
25 |
mikel262 |
else
|
293 |
|
|
fifo_rd(i) <= '0';
|
294 |
|
|
end if;
|
295 |
|
|
end loop;
|
296 |
|
|
|
297 |
|
|
fdct_fifo_empty <= fifo_empty(to_integer(idx_reg));
|
298 |
|
|
fdct_fifo_hf_full <= fifo_half_full(to_integer(idx_reg));
|
299 |
|
|
end if;
|
300 |
|
|
end process;
|
301 |
|
|
|
302 |
28 |
mikel262 |
fdct_fifo_q <= ramq;
|
303 |
25 |
mikel262 |
|
304 |
28 |
mikel262 |
-------------------------------------------------------------------
|
305 |
|
|
-- Mux3
|
306 |
|
|
-------------------------------------------------------------------
|
307 |
|
|
p_mux3 : process(CLK, RST)
|
308 |
|
|
begin
|
309 |
|
|
if RST = '1' then
|
310 |
|
|
ramwaddr <= (others => '0');
|
311 |
|
|
ramwaddr_offset <= (others => '0');
|
312 |
|
|
ramwaddr_base <= (others => '0');
|
313 |
|
|
ramenw <= '0';
|
314 |
|
|
ramenw_m1 <= '0';
|
315 |
|
|
wr_idx_reg <= (others => '0');
|
316 |
|
|
ramd <= (others => '0');
|
317 |
|
|
fifo_data <= (others => '0');
|
318 |
|
|
fifo_data_d1 <= (others => '0');
|
319 |
|
|
elsif CLK'event and CLK = '1' then
|
320 |
|
|
wr_idx_reg <= unsigned(wblock_cnt(log2(C_NUM_SUBF)-1 downto 0));
|
321 |
|
|
|
322 |
|
|
fifo_data <= iram_wdata;
|
323 |
|
|
fifo_data_d1 <= fifo_data;
|
324 |
|
|
ramd <= fifo_data_d1;
|
325 |
|
|
|
326 |
|
|
ramenw_m1 <= fifo_ramenw(to_integer(wr_idx_reg));
|
327 |
|
|
ramenw <= ramenw_m1;
|
328 |
|
|
|
329 |
|
|
ramwaddr_offset <= unsigned(fifo_ramwaddr(to_integer(wr_idx_reg)));
|
330 |
|
|
ramwaddr_base <= to_unsigned(2**C_SUBF_ADDRW, C_SUBF_ADDRW+1) *
|
331 |
|
|
wr_idx_reg;
|
332 |
|
|
ramwaddr <= std_logic_vector(ramwaddr_base(ramwaddr'range) +
|
333 |
|
|
resize(ramwaddr_offset, ramwaddr'length));
|
334 |
|
|
end if;
|
335 |
|
|
end process;
|
336 |
|
|
|
337 |
|
|
-------------------------------------------------------------------
|
338 |
|
|
-- Mux4
|
339 |
|
|
-------------------------------------------------------------------
|
340 |
|
|
p_mux4 : process(CLK, RST)
|
341 |
|
|
begin
|
342 |
|
|
if RST = '1' then
|
343 |
|
|
ramraddr <= (others => '0');
|
344 |
|
|
ramraddr_base <= (others => '0');
|
345 |
|
|
ramraddr_offset <= (others => '0');
|
346 |
|
|
elsif CLK'event and CLK = '1' then
|
347 |
|
|
ramraddr_offset <= unsigned(fifo_ramraddr(to_integer(idx_reg)));
|
348 |
|
|
ramraddr_base <= to_unsigned(2**C_SUBF_ADDRW, C_SUBF_ADDRW+1) *
|
349 |
|
|
idx_reg;
|
350 |
|
|
ramraddr <= std_logic_vector(ramraddr_base(ramraddr'range) +
|
351 |
|
|
resize(unsigned(ramraddr_offset), ramraddr'length));
|
352 |
|
|
end if;
|
353 |
|
|
end process;
|
354 |
25 |
mikel262 |
|
355 |
|
|
end architecture RTL;
|
356 |
|
|
-------------------------------------------------------------------------------
|
357 |
|
|
-- Architecture: end
|
358 |
|
|
-------------------------------------------------------------------------------
|