1 |
2 |
unicore |
---------------------------------------------------------------------
|
2 |
|
|
---- ----
|
3 |
|
|
---- DCT IP core ----
|
4 |
|
|
---- ----
|
5 |
|
|
---- Authors: Anatoliy Sergienko, Volodya Lepeha ----
|
6 |
|
|
---- Company: Unicore Systems http://unicore.co.ua ----
|
7 |
|
|
---- ----
|
8 |
|
|
---- Downloaded from: http://www.opencores.org ----
|
9 |
|
|
---- ----
|
10 |
|
|
---------------------------------------------------------------------
|
11 |
|
|
---- ----
|
12 |
|
|
---- Copyright (C) 2006-2010 Unicore Systems LTD ----
|
13 |
|
|
---- www.unicore.co.ua ----
|
14 |
|
|
---- o.uzenkov@unicore.co.ua ----
|
15 |
|
|
---- ----
|
16 |
|
|
---- This source file may be used and distributed without ----
|
17 |
|
|
---- restriction provided that this copyright statement is not ----
|
18 |
|
|
---- removed from the file and that any derivative work contains ----
|
19 |
|
|
---- the original copyright notice and the associated disclaimer.----
|
20 |
|
|
---- ----
|
21 |
|
|
---- THIS SOFTWARE IS PROVIDED "AS IS" ----
|
22 |
|
|
---- AND ANY EXPRESSED OR IMPLIED WARRANTIES, ----
|
23 |
|
|
---- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ----
|
24 |
|
|
---- WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT ----
|
25 |
|
|
---- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ----
|
26 |
|
|
---- IN NO EVENT SHALL THE UNICORE SYSTEMS OR ITS ----
|
27 |
|
|
---- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ----
|
28 |
|
|
---- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ----
|
29 |
|
|
---- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT ----
|
30 |
|
|
---- OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ----
|
31 |
|
|
---- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ----
|
32 |
|
|
---- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ----
|
33 |
|
|
---- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ----
|
34 |
|
|
---- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING ----
|
35 |
|
|
---- IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, ----
|
36 |
|
|
---- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ----
|
37 |
|
|
---- ----
|
38 |
|
|
---------------------------------------------------------------------
|
39 |
|
|
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
40 |
|
|
-- DESCRIPTION:
|
41 |
|
|
--
|
42 |
|
|
-- FUNCTION Discrete Cosine Transform of 8 samples using algorithm by
|
43 |
|
|
-- Arai, Agui, and Nakajama
|
44 |
|
|
-- input data bit width: 10 bit , signed or unsigned
|
45 |
|
|
-- output data bit width: 12 bit
|
46 |
|
|
-- coefficient bit width: 11 bit
|
47 |
|
|
-- Synthesable for FPGAs of any vendor, preferably for Xilinx FPGA
|
48 |
|
|
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
library IEEE;
|
52 |
|
|
use IEEE.std_logic_1164.all;
|
53 |
|
|
use IEEE.std_logic_arith.all;
|
54 |
|
|
use IEEE.std_logic_signed.all;
|
55 |
|
|
|
56 |
|
|
entity DCT8AAN2 is
|
57 |
|
|
generic( d_signed:integer:=1; --1 input data signed; 0 - unsigned, and for compression 1/2 is subtracted
|
58 |
|
|
scale_out:integer:=0); -- 1 output data are scaled; 0 - genuine DCT
|
59 |
|
|
port (
|
60 |
|
|
CLK: in STD_LOGIC;
|
61 |
|
|
RST: in STD_LOGIC;
|
62 |
|
|
START: in STD_LOGIC; -- after this impulse the 0-th datum is sampled
|
63 |
|
|
EN: in STD_LOGIC; -- operation enable to slow-down the calculations
|
64 |
|
|
DATA_IN: in STD_LOGIC_VECTOR (9 downto 0);
|
65 |
|
|
RDY: out STD_LOGIC; -- delayed START impulse, after it the 0-th result is outpitted
|
66 |
|
|
DATA_OUT: out STD_LOGIC_VECTOR (11 downto 0) -- output data
|
67 |
|
|
);
|
68 |
|
|
end DCT8AAN2;
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
architecture STAGE of DCT8AAN2 is
|
72 |
|
|
|
73 |
|
|
type Tarr8 is array(0 to 7) of STD_LOGIC_VECTOR (10 downto 0);
|
74 |
|
|
type Tarr16 is array (0 to 15) of STD_LOGIC_VECTOR (9 downto 0);
|
75 |
|
|
signal sr16:TARR16:=(others=>(others=>'0')); -- SRL16
|
76 |
|
|
|
77 |
|
|
constant S:Tarr8:=--(0.5/sqrt(2.0), 0.25/cos(pii*1.0),0.25/cos(pii*2.0),0.25/cos(pii*3.0),
|
78 |
|
|
-- 0.25/cos(pii*4.0),0.25/cos(pii*5.0),0.25/cos(pii*6.0),0.25/cos(pii*7.0));
|
79 |
|
|
(conv_std_logic_vector(integer(0.35355*2.0**9),11),
|
80 |
|
|
conv_std_logic_vector(integer(0.2549*2.0**9),11),
|
81 |
|
|
conv_std_logic_vector(integer(0.2706*2.0**9),11),
|
82 |
|
|
conv_std_logic_vector(integer(0.30067*2.0**9),11),
|
83 |
|
|
conv_std_logic_vector(integer(0.35355*2.0**9),11),
|
84 |
|
|
conv_std_logic_vector(integer(0.44999*2.0**9),11),
|
85 |
|
|
conv_std_logic_vector(integer(0.65328*2.0**9),11),
|
86 |
|
|
conv_std_logic_vector(integer(1.2815*2.0**9),11) );
|
87 |
|
|
|
88 |
|
|
constant m1 : STD_LOGIC_VECTOR (10 downto 0) := conv_std_logic_vector(integer(0.70711*2.0**9),11); --cos(pii*4.0); --
|
89 |
|
|
constant m2 : STD_LOGIC_VECTOR (10 downto 0) := conv_std_logic_vector(integer(0.38268*2.0**9),11);--cos(pii*6.0); --
|
90 |
|
|
constant m3 : STD_LOGIC_VECTOR (10 downto 0) := conv_std_logic_vector(integer(0.5412 *2.0**9),11);--(cos(pii*2.0) - cos(pii*6.0)); --
|
91 |
|
|
constant m4 : STD_LOGIC_VECTOR (10 downto 0) := conv_std_logic_vector(integer(1.3066*2.0**9),11);--cos(pii*2.0) + cos(pii*6.0); --
|
92 |
|
|
|
93 |
|
|
constant zeros : STD_LOGIC_VECTOR (7 downto 0) := (others => '0');
|
94 |
|
|
constant a1_2 : STD_LOGIC_VECTOR (9 downto 0) := "10"&zeros;
|
95 |
|
|
|
96 |
|
|
signal sr: STD_LOGIC_VECTOR (10 downto 0) ;
|
97 |
|
|
|
98 |
|
|
signal cycle,ad1,cycle6: integer range 0 to 7;
|
99 |
|
|
signal cycles: integer range 0 to 16;
|
100 |
|
|
signal di,a1,a2,a3,a4: STD_LOGIC_VECTOR (9 downto 0);
|
101 |
|
|
signal bp,bm,b1,b2,b3,b4,b6:STD_LOGIC_VECTOR (10 downto 0);
|
102 |
|
|
signal cp,cm,c1,c2,c3,c4:STD_LOGIC_VECTOR (12 downto 0);
|
103 |
|
|
signal dp,dm:STD_LOGIC_VECTOR (12 downto 0);
|
104 |
|
|
signal rd:STD_LOGIC_VECTOR (12 downto 0);
|
105 |
|
|
|
106 |
|
|
signal ep:STD_LOGIC_VECTOR (23 downto 0);
|
107 |
|
|
signal e27:STD_LOGIC_VECTOR (12 downto 0);
|
108 |
|
|
signal m1_4:STD_LOGIC_VECTOR (10 downto 0);
|
109 |
|
|
signal fp,fm,f45,s7,s07,spt:STD_LOGIC_VECTOR (13 downto 0);
|
110 |
|
|
signal SP : STD_LOGIC_VECTOR (24 downto 0);
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
begin
|
114 |
|
|
UU_COUN0:process(CLK,RST)
|
115 |
|
|
begin
|
116 |
|
|
if RST = '1' then
|
117 |
|
|
cycle <=0;
|
118 |
|
|
cycle6 <=(-6) mod 8;
|
119 |
|
|
ad1<= ( - 5) mod 8;
|
120 |
|
|
cycles <=16;
|
121 |
|
|
RDY<='0';
|
122 |
|
|
elsif CLK = '1' and CLK'event then
|
123 |
|
|
if en = '1' then
|
124 |
|
|
RDY<='0';
|
125 |
|
|
if START = '1' then
|
126 |
|
|
cycle <=0;
|
127 |
|
|
cycle6 <=(-6) mod 8;
|
128 |
|
|
ad1<= ( - 5) mod 8;
|
129 |
|
|
cycles <=0;
|
130 |
|
|
elsif en = '1' then
|
131 |
|
|
cycle<=(cycle +1) mod 8 ;
|
132 |
|
|
cycle6<=(cycle6 +1) mod 8 ;
|
133 |
|
|
ad1<=(ad1 +1) mod 8;
|
134 |
|
|
if cycles=15 then
|
135 |
|
|
RDY<='1';
|
136 |
|
|
end if;
|
137 |
|
|
if cycles/=16 then
|
138 |
|
|
cycles<=(cycles +1) ;
|
139 |
|
|
end if;
|
140 |
|
|
end if;
|
141 |
|
|
end if;
|
142 |
|
|
end if;
|
143 |
|
|
end process;
|
144 |
|
|
|
145 |
|
|
SRL16_a:process(CLK) begin -- SRL16
|
146 |
|
|
if CLK'event and CLK='1' then
|
147 |
|
|
if en='1' and (cycle=1 or cycle=2 or cycle=3 or cycle=4) then
|
148 |
|
|
sr16<=di & sr16(0 to 14); -- shift SRL16
|
149 |
|
|
end if;
|
150 |
|
|
end if;
|
151 |
|
|
end process;
|
152 |
|
|
a1<= sr16(ad1); -- output from SRL16
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
SM_B:process(clk,rst)
|
156 |
|
|
begin
|
157 |
|
|
if RST = '1' then
|
158 |
|
|
di <= (others => '0');
|
159 |
|
|
bp <= (others => '0');
|
160 |
|
|
bm <= (others => '0');
|
161 |
|
|
elsif CLK = '1' and CLK'event then
|
162 |
|
|
if en = '1' then
|
163 |
|
|
if d_signed =0 then
|
164 |
|
|
di<=unsigned(DATA_IN) - unsigned( a1_2);
|
165 |
|
|
else
|
166 |
|
|
di<=DATA_IN;
|
167 |
|
|
end if;
|
168 |
|
|
bp<=SXT(di,11) + a1;
|
169 |
|
|
bm<=a1 - SXT(di,11);
|
170 |
|
|
end if;
|
171 |
|
|
end if;
|
172 |
|
|
end process;
|
173 |
|
|
|
174 |
|
|
SM_C:process(clk,rst)
|
175 |
|
|
begin
|
176 |
|
|
if RST = '1' then
|
177 |
|
|
b1 <= (others => '0');
|
178 |
|
|
b2 <= (others => '0');
|
179 |
|
|
b3 <= (others => '0');
|
180 |
|
|
b4 <= (others => '0');
|
181 |
|
|
b6 <= (others => '0');
|
182 |
|
|
cp <= (others => '0');
|
183 |
|
|
cm <= (others => '0');
|
184 |
|
|
c1 <= (others => '0');
|
185 |
|
|
|
186 |
|
|
elsif CLK = '1' and CLK'event then
|
187 |
|
|
if en = '1' then
|
188 |
|
|
b1<=bp;
|
189 |
|
|
b2<=b1;
|
190 |
|
|
if cycle = 2 then
|
191 |
|
|
b3<=b4;
|
192 |
|
|
else
|
193 |
|
|
b3<=b2;
|
194 |
|
|
end if;
|
195 |
|
|
b4<=b3;
|
196 |
|
|
b6<=bm;
|
197 |
|
|
case cycle is
|
198 |
|
|
when 0|1|7 =>cp<=SXT(bm,13)+b6;
|
199 |
|
|
when 2|3 =>cp<= SXT(b2,13)+b3;
|
200 |
|
|
when others=> cp<=cp+c1;
|
201 |
|
|
end case;
|
202 |
|
|
c1<=cp;
|
203 |
|
|
|
204 |
|
|
if cycle=2 or cycle=3 then
|
205 |
|
|
cm<=SXT(b2,13) - b3;
|
206 |
|
|
else
|
207 |
|
|
cm<=cp - c1;
|
208 |
|
|
end if;
|
209 |
|
|
|
210 |
|
|
end if;
|
211 |
|
|
end if;
|
212 |
|
|
end process;
|
213 |
|
|
|
214 |
|
|
|
215 |
|
|
SM_D:process(clk,rst)
|
216 |
|
|
begin
|
217 |
|
|
if RST = '1' then
|
218 |
|
|
c2 <= (others => '0');
|
219 |
|
|
c3 <= (others => '0');
|
220 |
|
|
c4 <= (others => '0');
|
221 |
|
|
dp <= (others => '0');
|
222 |
|
|
dm <= (others => '0');
|
223 |
|
|
elsif CLK = '1' and CLK'event then
|
224 |
|
|
if en = '1' then
|
225 |
|
|
if cycle=3 or cycle=4 or cycle=5 then
|
226 |
|
|
c2<=cm;
|
227 |
|
|
end if;
|
228 |
|
|
if cycle = 1 then
|
229 |
|
|
c3<=c1;
|
230 |
|
|
end if;
|
231 |
|
|
if cycle = 2 then
|
232 |
|
|
c4<=SXT(b6,13);
|
233 |
|
|
elsif cycle=5 then
|
234 |
|
|
c4<=c2;
|
235 |
|
|
end if;
|
236 |
|
|
if cycle = 4 then
|
237 |
|
|
dp<= SXT(cm, 13)+c2(12 downto 0);
|
238 |
|
|
else
|
239 |
|
|
dp<= ep(21 downto 9)+c4(12 downto 0);
|
240 |
|
|
end if;
|
241 |
|
|
|
242 |
|
|
if cycle = 2 then
|
243 |
|
|
dm<= c3(12 downto 0) - SXT(cp, 13);
|
244 |
|
|
elsif cycle=3 or cycle =7 then
|
245 |
|
|
dm<= c4(12 downto 0) - ep(21 downto 9);
|
246 |
|
|
end if;
|
247 |
|
|
|
248 |
|
|
end if;
|
249 |
|
|
end if;
|
250 |
|
|
end process;
|
251 |
|
|
|
252 |
|
|
MPU1:process(clk,rst)
|
253 |
|
|
begin
|
254 |
|
|
if CLK = '1' and CLK'event then
|
255 |
|
|
if en = '1' then
|
256 |
|
|
case cycle is
|
257 |
|
|
when 1|5 => m1_4<=m1;
|
258 |
|
|
when 2 => m1_4<=m4;
|
259 |
|
|
when 3 => m1_4<=m2;
|
260 |
|
|
when others => m1_4<=m3;
|
261 |
|
|
end case ;
|
262 |
|
|
case cycle is
|
263 |
|
|
when 1|2 => rd<= SXT(cp,13);
|
264 |
|
|
when 3 => rd<=dm;
|
265 |
|
|
when 4 => rd<= SXT(c3,13);
|
266 |
|
|
when others => rd<=dp;
|
267 |
|
|
end case ;
|
268 |
|
|
ep<=rd*m1_4;
|
269 |
|
|
e27<= ep(21 downto 9);
|
270 |
|
|
end if;
|
271 |
|
|
end if;
|
272 |
|
|
end process;
|
273 |
|
|
|
274 |
|
|
SM_F:process(clk,rst)
|
275 |
|
|
begin
|
276 |
|
|
if RST = '1' then
|
277 |
|
|
fp <= (others => '0');
|
278 |
|
|
fm <= (others => '0');
|
279 |
|
|
f45 <= (others => '0');
|
280 |
|
|
s7 <= (others => '0');
|
281 |
|
|
elsif CLK = '1' and CLK'event then
|
282 |
|
|
if en = '1' then
|
283 |
|
|
case cycle is
|
284 |
|
|
when 3 => fp<=ep(21 downto 9) + SXT(c4,14);
|
285 |
|
|
when 5 => fp<=ep(21 downto 9) + SXT(e27,14);
|
286 |
|
|
when 6|0 => fp<=fp + f45;
|
287 |
|
|
when 7 => fp<=e27 +f45;
|
288 |
|
|
when others=> null;
|
289 |
|
|
end case;
|
290 |
|
|
|
291 |
|
|
if cycle=4 then
|
292 |
|
|
f45<=fp;
|
293 |
|
|
elsif cycle=6 then
|
294 |
|
|
f45<=SXT(e27,14);
|
295 |
|
|
elsif cycle=7 or cycle=0 then
|
296 |
|
|
f45<=SXT(dm,14);
|
297 |
|
|
end if;
|
298 |
|
|
fm<=f45 - fp;
|
299 |
|
|
if cycle=7 then
|
300 |
|
|
s7<=fm;
|
301 |
|
|
end if;
|
302 |
|
|
end if;
|
303 |
|
|
end if;
|
304 |
|
|
end process;
|
305 |
|
|
|
306 |
|
|
MPU2:process(clk,rst)
|
307 |
|
|
begin
|
308 |
|
|
if CLK = '1' and CLK'event then
|
309 |
|
|
if en = '1' then
|
310 |
|
|
sr<=s(cycle6);
|
311 |
|
|
case cycle is
|
312 |
|
|
when 6 => s07<= SXT(c1,14);
|
313 |
|
|
when 7|3 => s07<=fp;
|
314 |
|
|
when 0 => s07<= SXT(dp,14);
|
315 |
|
|
when 1 => s07<= SXT(fm,14);
|
316 |
|
|
when 2 => s07<= SXT(c2,14);
|
317 |
|
|
when 4 => s07<= SXT(f45,14);
|
318 |
|
|
when others => s07<=s7;
|
319 |
|
|
end case ;
|
320 |
|
|
if scale_out =0 then
|
321 |
|
|
sp<=s07*sr;
|
322 |
|
|
DATA_OUT <=sp(20 downto 9);
|
323 |
|
|
else
|
324 |
|
|
spt<=s07;
|
325 |
|
|
DATA_OUT <= spt(12 downto 1);
|
326 |
|
|
end if;
|
327 |
|
|
end if;
|
328 |
|
|
end if;
|
329 |
|
|
end process;
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
end STAGE;
|