1 |
25 |
mikel262 |
-------------------------------------------------------------------------------
|
2 |
|
|
-- File Name : ByteStuffer.vhd
|
3 |
|
|
--
|
4 |
|
|
-- Project : JPEG_ENC
|
5 |
|
|
--
|
6 |
|
|
-- Module : ByteStuffer
|
7 |
|
|
--
|
8 |
|
|
-- Content : ByteStuffer
|
9 |
|
|
--
|
10 |
|
|
-- Description : ByteStuffer core
|
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 ByteStuffer 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 |
|
|
|
53 |
|
|
-- HOST IF
|
54 |
|
|
sof : in std_logic;
|
55 |
|
|
num_enc_bytes : out std_logic_vector(23 downto 0);
|
56 |
|
|
outram_base_addr : in std_logic_vector(9 downto 0);
|
57 |
|
|
|
58 |
|
|
-- Huffman
|
59 |
|
|
huf_buf_sel : out std_logic;
|
60 |
|
|
huf_fifo_empty : in std_logic;
|
61 |
|
|
huf_rd_req : out std_logic;
|
62 |
|
|
huf_packed_byte : in std_logic_vector(7 downto 0);
|
63 |
|
|
|
64 |
|
|
-- OUT RAM
|
65 |
|
|
ram_byte : out std_logic_vector(7 downto 0);
|
66 |
|
|
ram_wren : out std_logic;
|
67 |
|
|
ram_wraddr : out std_logic_vector(23 downto 0)
|
68 |
|
|
);
|
69 |
|
|
end entity ByteStuffer;
|
70 |
|
|
|
71 |
|
|
-------------------------------------------------------------------------------
|
72 |
|
|
-------------------------------------------------------------------------------
|
73 |
|
|
----------------------------------- ARCHITECTURE ------------------------------
|
74 |
|
|
-------------------------------------------------------------------------------
|
75 |
|
|
-------------------------------------------------------------------------------
|
76 |
|
|
architecture RTL of ByteStuffer is
|
77 |
|
|
|
78 |
|
|
signal huf_data_val : std_logic_vector(3 downto 0);
|
79 |
|
|
signal wdata_reg : std_logic_vector(15 downto 0);
|
80 |
|
|
signal wraddr : unsigned(23 downto 0);
|
81 |
|
|
signal wr_n_cnt : unsigned(1 downto 0);
|
82 |
|
|
signal huf_buf_sel_s : std_logic;
|
83 |
|
|
signal rd_en : std_logic;
|
84 |
|
|
signal rd_en_d1 : std_logic;
|
85 |
|
|
signal huf_rd_req_s : std_logic;
|
86 |
|
|
signal latch_byte : std_logic_vector(7 downto 0);
|
87 |
|
|
signal data_valid : std_logic;
|
88 |
|
|
signal wait_for_ndata : std_logic;
|
89 |
|
|
|
90 |
|
|
-------------------------------------------------------------------------------
|
91 |
|
|
-- Architecture: begin
|
92 |
|
|
-------------------------------------------------------------------------------
|
93 |
|
|
begin
|
94 |
|
|
|
95 |
|
|
huf_buf_sel <= huf_buf_sel_s;
|
96 |
|
|
huf_rd_req <= huf_rd_req_s;
|
97 |
36 |
mikel262 |
|
98 |
25 |
mikel262 |
-------------------------------------------------------------------
|
99 |
|
|
-- CTRL_SM
|
100 |
|
|
-------------------------------------------------------------------
|
101 |
|
|
p_ctrl_sm : process(CLK, RST)
|
102 |
|
|
begin
|
103 |
|
|
if RST = '1' then
|
104 |
|
|
wr_n_cnt <= (others => '0');
|
105 |
|
|
ready_pb <= '0';
|
106 |
|
|
huf_rd_req_s <= '0';
|
107 |
|
|
huf_data_val <= (others => '0');
|
108 |
|
|
rd_en <= '0';
|
109 |
|
|
rd_en_d1 <= '0';
|
110 |
|
|
wdata_reg <= (others => '0');
|
111 |
|
|
ram_wren <= '0';
|
112 |
|
|
wraddr <= (others => '0');
|
113 |
|
|
ram_wraddr <= (others => '0');
|
114 |
|
|
ram_byte <= (others => '0');
|
115 |
|
|
latch_byte <= (others => '0');
|
116 |
|
|
wait_for_ndata <= '0';
|
117 |
|
|
data_valid <= '0';
|
118 |
|
|
elsif CLK'event and CLK = '1' then
|
119 |
|
|
huf_rd_req_s <= '0';
|
120 |
|
|
ready_pb <= '0';
|
121 |
|
|
huf_data_val <= huf_data_val(huf_data_val'length-2 downto 0) & huf_rd_req_s;
|
122 |
|
|
rd_en_d1 <= rd_en;
|
123 |
|
|
ram_wren <= '0';
|
124 |
|
|
data_valid <= '0';
|
125 |
|
|
|
126 |
|
|
if start_pb = '1' then
|
127 |
|
|
rd_en <= '1';
|
128 |
|
|
end if;
|
129 |
|
|
|
130 |
|
|
-- read FIFO until it becomes empty. wait until last byte read is
|
131 |
|
|
-- serviced
|
132 |
|
|
if rd_en_d1 = '1' and wait_for_ndata = '0' then
|
133 |
|
|
-- FIFO empty
|
134 |
|
|
if huf_fifo_empty = '1' then
|
135 |
|
|
rd_en <= '0';
|
136 |
|
|
rd_en_d1 <= '0';
|
137 |
|
|
ready_pb <= '1';
|
138 |
|
|
else
|
139 |
|
|
huf_rd_req_s <= '1';
|
140 |
|
|
wait_for_ndata <= '1';
|
141 |
|
|
end if;
|
142 |
|
|
end if;
|
143 |
|
|
|
144 |
|
|
-- show ahead FIFO, capture data early
|
145 |
|
|
if huf_rd_req_s = '1' then
|
146 |
|
|
latch_byte <= huf_packed_byte;
|
147 |
|
|
data_valid <= '1';
|
148 |
|
|
end if;
|
149 |
|
|
|
150 |
|
|
if huf_data_val(1) = '1' then
|
151 |
|
|
wait_for_ndata <= '0';
|
152 |
|
|
end if;
|
153 |
|
|
|
154 |
|
|
-- data from FIFO is valid
|
155 |
|
|
if data_valid = '1' then
|
156 |
|
|
-- stuffing necessary
|
157 |
|
|
if latch_byte = X"FF" then
|
158 |
|
|
-- two writes are necessary for byte stuffing
|
159 |
|
|
wr_n_cnt <= "10";
|
160 |
|
|
wdata_reg <= X"FF00";
|
161 |
|
|
-- no stuffing
|
162 |
|
|
else
|
163 |
|
|
wr_n_cnt <= "01";
|
164 |
|
|
wdata_reg <= X"00" & latch_byte;
|
165 |
|
|
end if;
|
166 |
|
|
end if;
|
167 |
|
|
|
168 |
|
|
if wr_n_cnt > 0 then
|
169 |
|
|
wr_n_cnt <= wr_n_cnt - 1;
|
170 |
|
|
ram_wren <= '1';
|
171 |
|
|
wraddr <= wraddr + 1;
|
172 |
|
|
end if;
|
173 |
|
|
-- delayed to make address post-increment
|
174 |
|
|
ram_wraddr <= std_logic_vector(wraddr);
|
175 |
|
|
|
176 |
|
|
-- stuffing
|
177 |
|
|
if wr_n_cnt = 2 then
|
178 |
|
|
ram_byte <= wdata_reg(15 downto 8);
|
179 |
|
|
elsif wr_n_cnt = 1 then
|
180 |
|
|
ram_byte <= wdata_reg(7 downto 0);
|
181 |
|
|
end if;
|
182 |
|
|
|
183 |
|
|
if sof = '1' then
|
184 |
|
|
wraddr <= to_unsigned(C_HDR_SIZE,wraddr'length);
|
185 |
|
|
end if;
|
186 |
|
|
end if;
|
187 |
|
|
end process;
|
188 |
|
|
|
189 |
|
|
-------------------------------------------------------------------
|
190 |
|
|
-- HUFFMAN buf_sel
|
191 |
|
|
-------------------------------------------------------------------
|
192 |
|
|
p_huf_buf_sel : process(CLK, RST)
|
193 |
|
|
begin
|
194 |
|
|
if RST = '1' then
|
195 |
|
|
huf_buf_sel_s <= '0';
|
196 |
|
|
elsif CLK'event and CLK = '1' then
|
197 |
|
|
if start_pb = '1' then
|
198 |
|
|
huf_buf_sel_s <= not huf_buf_sel_s;
|
199 |
|
|
end if;
|
200 |
|
|
end if;
|
201 |
|
|
end process;
|
202 |
36 |
mikel262 |
|
203 |
|
|
-------------------------------------------------------------------
|
204 |
|
|
-- num_enc_bytes
|
205 |
|
|
-------------------------------------------------------------------
|
206 |
|
|
p_num_enc_bytes : process(CLK, RST)
|
207 |
|
|
begin
|
208 |
|
|
if RST = '1' then
|
209 |
|
|
num_enc_bytes <= (others => '0');
|
210 |
|
|
elsif CLK'event and CLK = '1' then
|
211 |
|
|
-- plus 2 for EOI marker last bytes
|
212 |
|
|
num_enc_bytes <= std_logic_vector(wraddr + 2);
|
213 |
|
|
end if;
|
214 |
|
|
end process;
|
215 |
25 |
mikel262 |
|
216 |
|
|
|
217 |
|
|
end architecture RTL;
|
218 |
|
|
-------------------------------------------------------------------------------
|
219 |
|
|
-- Architecture: end
|
220 |
|
|
-------------------------------------------------------------------------------
|