1 |
25 |
mikel262 |
-------------------------------------------------------------------------------
|
2 |
|
|
-- File Name : RLE_TOP.vhd
|
3 |
|
|
--
|
4 |
|
|
-- Project : JPEG_ENC
|
5 |
|
|
--
|
6 |
|
|
-- Module : RLE_TOP
|
7 |
|
|
--
|
8 |
|
|
-- Content : Run Length Encoder top level
|
9 |
|
|
--
|
10 |
|
|
-- Description :
|
11 |
|
|
--
|
12 |
|
|
-- Spec. :
|
13 |
|
|
--
|
14 |
|
|
-- Author : Michal Krepa
|
15 |
|
|
--
|
16 |
|
|
-------------------------------------------------------------------------------
|
17 |
|
|
-- History :
|
18 |
|
|
-- 20090301: (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 RLE_TOP is
|
45 |
|
|
port
|
46 |
|
|
(
|
47 |
|
|
CLK : in std_logic;
|
48 |
|
|
RST : in std_logic;
|
49 |
|
|
-- CTRL
|
50 |
|
|
start_pb : in std_logic;
|
51 |
|
|
ready_pb : out std_logic;
|
52 |
|
|
rle_sm_settings : in T_SM_SETTINGS;
|
53 |
|
|
|
54 |
|
|
-- HUFFMAN
|
55 |
|
|
huf_buf_sel : in std_logic;
|
56 |
|
|
huf_rden : in std_logic;
|
57 |
|
|
huf_runlength : out std_logic_vector(3 downto 0);
|
58 |
|
|
huf_size : out std_logic_vector(3 downto 0);
|
59 |
|
|
huf_amplitude : out std_logic_vector(11 downto 0);
|
60 |
|
|
huf_dval : out std_logic;
|
61 |
|
|
huf_fifo_empty : out std_logic;
|
62 |
|
|
|
63 |
|
|
-- ZIGZAG
|
64 |
|
|
zig_buf_sel : out std_logic;
|
65 |
|
|
zig_rd_addr : out std_logic_vector(5 downto 0);
|
66 |
|
|
zig_data : in std_logic_vector(11 downto 0);
|
67 |
|
|
|
68 |
|
|
-- HostIF
|
69 |
|
|
sof : in std_logic
|
70 |
|
|
);
|
71 |
|
|
end entity RLE_TOP;
|
72 |
|
|
|
73 |
|
|
-------------------------------------------------------------------------------
|
74 |
|
|
-------------------------------------------------------------------------------
|
75 |
|
|
----------------------------------- ARCHITECTURE ------------------------------
|
76 |
|
|
-------------------------------------------------------------------------------
|
77 |
|
|
-------------------------------------------------------------------------------
|
78 |
|
|
architecture RTL of RLE_TOP is
|
79 |
|
|
|
80 |
|
|
signal dbuf_data : std_logic_vector(19 downto 0);
|
81 |
|
|
signal dbuf_q : std_logic_vector(19 downto 0);
|
82 |
|
|
signal dbuf_we : std_logic;
|
83 |
|
|
|
84 |
|
|
signal rd_cnt : unsigned(5 downto 0);
|
85 |
|
|
signal rd_en_d : std_logic_vector(5 downto 0);
|
86 |
|
|
signal rd_en : std_logic;
|
87 |
|
|
|
88 |
|
|
signal rle_runlength : std_logic_vector(3 downto 0);
|
89 |
|
|
signal rle_size : std_logic_vector(3 downto 0);
|
90 |
|
|
signal rle_amplitude : std_logic_vector(11 downto 0);
|
91 |
|
|
signal rle_dovalid : std_logic;
|
92 |
|
|
signal rle_di : std_logic_vector(11 downto 0);
|
93 |
|
|
signal rle_divalid : std_logic;
|
94 |
|
|
|
95 |
|
|
signal zig_buf_sel_s : std_logic;
|
96 |
|
|
signal huf_dval_p0 : std_logic;
|
97 |
|
|
|
98 |
|
|
signal wr_cnt : unsigned(5 downto 0);
|
99 |
|
|
|
100 |
|
|
-------------------------------------------------------------------------------
|
101 |
|
|
-- Architecture: begin
|
102 |
|
|
-------------------------------------------------------------------------------
|
103 |
|
|
begin
|
104 |
|
|
|
105 |
|
|
zig_rd_addr <= std_logic_vector(rd_cnt);
|
106 |
|
|
huf_runlength <= dbuf_q(19 downto 16);
|
107 |
|
|
huf_size <= dbuf_q(15 downto 12);
|
108 |
|
|
huf_amplitude <= dbuf_q(11 downto 0);
|
109 |
|
|
zig_buf_sel <= zig_buf_sel_s;
|
110 |
|
|
|
111 |
|
|
-------------------------------------------------------------------
|
112 |
|
|
-- RLE Core
|
113 |
|
|
-------------------------------------------------------------------
|
114 |
|
|
U_rle : entity work.rle
|
115 |
|
|
generic map
|
116 |
|
|
(
|
117 |
|
|
RAMADDR_W => 6,
|
118 |
|
|
RAMDATA_W => 12
|
119 |
|
|
)
|
120 |
|
|
port map
|
121 |
|
|
(
|
122 |
|
|
rst => RST,
|
123 |
|
|
clk => CLK,
|
124 |
|
|
di => rle_di,
|
125 |
|
|
divalid => rle_divalid,
|
126 |
|
|
start_pb => start_pb,
|
127 |
|
|
sof => sof,
|
128 |
|
|
rle_sm_settings => rle_sm_settings,
|
129 |
|
|
|
130 |
|
|
runlength => rle_runlength,
|
131 |
|
|
size => rle_size,
|
132 |
|
|
amplitude => rle_amplitude,
|
133 |
|
|
dovalid => rle_dovalid
|
134 |
|
|
);
|
135 |
|
|
|
136 |
|
|
rle_di <= zig_data;
|
137 |
|
|
rle_divalid <= rd_en_d(0);
|
138 |
|
|
|
139 |
|
|
-------------------------------------------------------------------
|
140 |
|
|
-- Double Fifo
|
141 |
|
|
-------------------------------------------------------------------
|
142 |
|
|
U_RleDoubleFifo : entity work.RleDoubleFifo
|
143 |
|
|
port map
|
144 |
|
|
(
|
145 |
|
|
CLK => CLK,
|
146 |
|
|
RST => RST,
|
147 |
|
|
-- RLE
|
148 |
|
|
data_in => dbuf_data,
|
149 |
|
|
wren => dbuf_we,
|
150 |
|
|
-- HUFFMAN
|
151 |
|
|
buf_sel => huf_buf_sel,
|
152 |
|
|
rd_req => huf_rden,
|
153 |
|
|
fifo_empty => huf_fifo_empty,
|
154 |
|
|
data_out => dbuf_q
|
155 |
|
|
);
|
156 |
|
|
dbuf_data <= rle_runlength & rle_size & rle_amplitude;
|
157 |
|
|
dbuf_we <= rle_dovalid;
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
-------------------------------------------------------------------
|
161 |
|
|
-- Counter1
|
162 |
|
|
-------------------------------------------------------------------
|
163 |
|
|
p_counter1 : process(CLK, RST)
|
164 |
|
|
begin
|
165 |
|
|
if RST = '1' then
|
166 |
|
|
rd_en <= '0';
|
167 |
|
|
rd_en_d <= (others => '0');
|
168 |
|
|
rd_cnt <= (others => '0');
|
169 |
|
|
elsif CLK'event and CLK = '1' then
|
170 |
|
|
rd_en_d <= rd_en_d(rd_en_d'length-2 downto 0) & rd_en;
|
171 |
|
|
|
172 |
|
|
if start_pb = '1' then
|
173 |
|
|
rd_cnt <= (others => '0');
|
174 |
|
|
rd_en <= '1';
|
175 |
|
|
end if;
|
176 |
|
|
|
177 |
|
|
if rd_en = '1' then
|
178 |
|
|
if rd_cnt = 64-1 then
|
179 |
|
|
rd_cnt <= (others => '0');
|
180 |
|
|
rd_en <= '0';
|
181 |
|
|
else
|
182 |
|
|
rd_cnt <= rd_cnt + 1;
|
183 |
|
|
end if;
|
184 |
|
|
end if;
|
185 |
|
|
|
186 |
|
|
end if;
|
187 |
|
|
end process;
|
188 |
|
|
|
189 |
|
|
-------------------------------------------------------------------
|
190 |
|
|
-- ready_pb
|
191 |
|
|
-------------------------------------------------------------------
|
192 |
|
|
p_ready_pb : process(CLK, RST)
|
193 |
|
|
begin
|
194 |
|
|
if RST = '1' then
|
195 |
|
|
ready_pb <= '0';
|
196 |
|
|
wr_cnt <= (others => '0');
|
197 |
|
|
elsif CLK'event and CLK = '1' then
|
198 |
|
|
ready_pb <= '0';
|
199 |
|
|
|
200 |
|
|
if start_pb = '1' then
|
201 |
|
|
wr_cnt <= (others => '0');
|
202 |
|
|
end if;
|
203 |
|
|
|
204 |
|
|
-- detect EOB (0,0) - end of RLE block
|
205 |
|
|
if rle_dovalid = '1' then
|
206 |
|
|
|
207 |
|
|
-- ZERO EXTENSION
|
208 |
|
|
if unsigned(rle_runlength) = 15 and unsigned(rle_size) = 0 then
|
209 |
|
|
wr_cnt <= wr_cnt + 16;
|
210 |
|
|
else
|
211 |
|
|
wr_cnt <= wr_cnt + 1 + resize(unsigned(rle_runlength), wr_cnt'length);
|
212 |
|
|
end if;
|
213 |
|
|
|
214 |
|
|
-- EOB can only be on AC!
|
215 |
|
|
if dbuf_data = (dbuf_data'range => '0') and wr_cnt /= 0 then
|
216 |
|
|
ready_pb <= '1';
|
217 |
|
|
else
|
218 |
|
|
if wr_cnt + resize(unsigned(rle_runlength), wr_cnt'length) = 63 then
|
219 |
|
|
ready_pb <= '1';
|
220 |
|
|
end if;
|
221 |
|
|
end if;
|
222 |
|
|
end if;
|
223 |
|
|
|
224 |
|
|
end if;
|
225 |
|
|
end process;
|
226 |
|
|
|
227 |
|
|
-------------------------------------------------------------------
|
228 |
|
|
-- fdct_buf_sel
|
229 |
|
|
-------------------------------------------------------------------
|
230 |
|
|
p_buf_sel : process(CLK, RST)
|
231 |
|
|
begin
|
232 |
|
|
if RST = '1' then
|
233 |
|
|
zig_buf_sel_s <= '0';
|
234 |
|
|
elsif CLK'event and CLK = '1' then
|
235 |
|
|
if start_pb = '1' then
|
236 |
|
|
zig_buf_sel_s <= not zig_buf_sel_s;
|
237 |
|
|
end if;
|
238 |
|
|
end if;
|
239 |
|
|
end process;
|
240 |
|
|
|
241 |
|
|
-------------------------------------------------------------------
|
242 |
|
|
-- output data valid
|
243 |
|
|
-------------------------------------------------------------------
|
244 |
|
|
p_dval : process(CLK, RST)
|
245 |
|
|
begin
|
246 |
|
|
if RST = '1' then
|
247 |
|
|
huf_dval_p0 <= '0';
|
248 |
|
|
huf_dval <= '0';
|
249 |
|
|
elsif CLK'event and CLK = '1' then
|
250 |
|
|
huf_dval_p0 <= huf_rden;
|
251 |
|
|
huf_dval <= huf_rden;
|
252 |
|
|
end if;
|
253 |
|
|
end process;
|
254 |
|
|
|
255 |
|
|
end architecture RTL;
|
256 |
|
|
-------------------------------------------------------------------------------
|
257 |
|
|
-- Architecture: end
|
258 |
|
|
-------------------------------------------------------------------------------
|