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 |
|
|
|
98 |
|
|
num_enc_bytes <= std_logic_vector(wraddr);
|
99 |
|
|
|
100 |
|
|
-------------------------------------------------------------------
|
101 |
|
|
-- CTRL_SM
|
102 |
|
|
-------------------------------------------------------------------
|
103 |
|
|
p_ctrl_sm : process(CLK, RST)
|
104 |
|
|
begin
|
105 |
|
|
if RST = '1' then
|
106 |
|
|
wr_n_cnt <= (others => '0');
|
107 |
|
|
ready_pb <= '0';
|
108 |
|
|
huf_rd_req_s <= '0';
|
109 |
|
|
huf_data_val <= (others => '0');
|
110 |
|
|
rd_en <= '0';
|
111 |
|
|
rd_en_d1 <= '0';
|
112 |
|
|
wdata_reg <= (others => '0');
|
113 |
|
|
ram_wren <= '0';
|
114 |
|
|
wraddr <= (others => '0');
|
115 |
|
|
ram_wraddr <= (others => '0');
|
116 |
|
|
ram_byte <= (others => '0');
|
117 |
|
|
latch_byte <= (others => '0');
|
118 |
|
|
wait_for_ndata <= '0';
|
119 |
|
|
data_valid <= '0';
|
120 |
|
|
elsif CLK'event and CLK = '1' then
|
121 |
|
|
huf_rd_req_s <= '0';
|
122 |
|
|
ready_pb <= '0';
|
123 |
|
|
huf_data_val <= huf_data_val(huf_data_val'length-2 downto 0) & huf_rd_req_s;
|
124 |
|
|
rd_en_d1 <= rd_en;
|
125 |
|
|
ram_wren <= '0';
|
126 |
|
|
data_valid <= '0';
|
127 |
|
|
|
128 |
|
|
if start_pb = '1' then
|
129 |
|
|
rd_en <= '1';
|
130 |
|
|
end if;
|
131 |
|
|
|
132 |
|
|
-- read FIFO until it becomes empty. wait until last byte read is
|
133 |
|
|
-- serviced
|
134 |
|
|
if rd_en_d1 = '1' and wait_for_ndata = '0' then
|
135 |
|
|
-- FIFO empty
|
136 |
|
|
if huf_fifo_empty = '1' then
|
137 |
|
|
rd_en <= '0';
|
138 |
|
|
rd_en_d1 <= '0';
|
139 |
|
|
ready_pb <= '1';
|
140 |
|
|
else
|
141 |
|
|
huf_rd_req_s <= '1';
|
142 |
|
|
wait_for_ndata <= '1';
|
143 |
|
|
end if;
|
144 |
|
|
end if;
|
145 |
|
|
|
146 |
|
|
-- show ahead FIFO, capture data early
|
147 |
|
|
if huf_rd_req_s = '1' then
|
148 |
|
|
latch_byte <= huf_packed_byte;
|
149 |
|
|
data_valid <= '1';
|
150 |
|
|
end if;
|
151 |
|
|
|
152 |
|
|
if huf_data_val(1) = '1' then
|
153 |
|
|
wait_for_ndata <= '0';
|
154 |
|
|
end if;
|
155 |
|
|
|
156 |
|
|
-- data from FIFO is valid
|
157 |
|
|
if data_valid = '1' then
|
158 |
|
|
-- stuffing necessary
|
159 |
|
|
if latch_byte = X"FF" then
|
160 |
|
|
-- two writes are necessary for byte stuffing
|
161 |
|
|
wr_n_cnt <= "10";
|
162 |
|
|
wdata_reg <= X"FF00";
|
163 |
|
|
-- no stuffing
|
164 |
|
|
else
|
165 |
|
|
wr_n_cnt <= "01";
|
166 |
|
|
wdata_reg <= X"00" & latch_byte;
|
167 |
|
|
end if;
|
168 |
|
|
end if;
|
169 |
|
|
|
170 |
|
|
if wr_n_cnt > 0 then
|
171 |
|
|
wr_n_cnt <= wr_n_cnt - 1;
|
172 |
|
|
ram_wren <= '1';
|
173 |
|
|
wraddr <= wraddr + 1;
|
174 |
|
|
end if;
|
175 |
|
|
-- delayed to make address post-increment
|
176 |
|
|
ram_wraddr <= std_logic_vector(wraddr);
|
177 |
|
|
|
178 |
|
|
-- stuffing
|
179 |
|
|
if wr_n_cnt = 2 then
|
180 |
|
|
ram_byte <= wdata_reg(15 downto 8);
|
181 |
|
|
elsif wr_n_cnt = 1 then
|
182 |
|
|
ram_byte <= wdata_reg(7 downto 0);
|
183 |
|
|
end if;
|
184 |
|
|
|
185 |
|
|
if sof = '1' then
|
186 |
|
|
wraddr <= to_unsigned(C_HDR_SIZE,wraddr'length);
|
187 |
|
|
end if;
|
188 |
|
|
end if;
|
189 |
|
|
end process;
|
190 |
|
|
|
191 |
|
|
-------------------------------------------------------------------
|
192 |
|
|
-- HUFFMAN buf_sel
|
193 |
|
|
-------------------------------------------------------------------
|
194 |
|
|
p_huf_buf_sel : process(CLK, RST)
|
195 |
|
|
begin
|
196 |
|
|
if RST = '1' then
|
197 |
|
|
huf_buf_sel_s <= '0';
|
198 |
|
|
elsif CLK'event and CLK = '1' then
|
199 |
|
|
if start_pb = '1' then
|
200 |
|
|
huf_buf_sel_s <= not huf_buf_sel_s;
|
201 |
|
|
end if;
|
202 |
|
|
end if;
|
203 |
|
|
end process;
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
end architecture RTL;
|
207 |
|
|
-------------------------------------------------------------------------------
|
208 |
|
|
-- Architecture: end
|
209 |
|
|
-------------------------------------------------------------------------------
|