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: 8 bit , signed or unsigned
|
45 |
|
|
-- output data bit width: 10 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 DCT8AAN1 is
|
57 |
|
|
generic( d_signed:integer:=1;--1 input data signed 0 - unsigned, and for compression 1/2 is subtracted
|
58 |
|
|
scale_out:integer:=1); -- 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 (7 downto 0);
|
65 |
|
|
RDY: out STD_LOGIC; -- delayed START impulse, after it the 0-th result is outputted
|
66 |
|
|
DATA_OUT: out STD_LOGIC_VECTOR (9 downto 0) -- output data
|
67 |
|
|
);
|
68 |
|
|
end DCT8AAN1;
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
architecture STAGE of DCT8AAN1 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 (7 downto 0);
|
75 |
|
|
signal sr16:TARR16:=(others=>(others=>'0')); -- SRL16 array
|
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 (5 downto 0) := (others => '0');
|
94 |
|
|
constant a1_2 : STD_LOGIC_VECTOR (7 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 31;
|
100 |
|
|
signal di,a1,a2,a3,a4: STD_LOGIC_VECTOR (7 downto 0);
|
101 |
|
|
signal bp,bm,b1,b2,b3,b4,b6:STD_LOGIC_VECTOR (8 downto 0);
|
102 |
|
|
signal cp,cm,c1,c2,c3,c4:STD_LOGIC_VECTOR (10 downto 0);
|
103 |
|
|
signal dp,dm:STD_LOGIC_VECTOR (11 downto 0);
|
104 |
|
|
signal rd:STD_LOGIC_VECTOR (11 downto 0);
|
105 |
|
|
|
106 |
|
|
signal ep:STD_LOGIC_VECTOR (22 downto 0);
|
107 |
|
|
signal e27:STD_LOGIC_VECTOR (10 downto 0);
|
108 |
|
|
signal m1_4:STD_LOGIC_VECTOR (10 downto 0);
|
109 |
|
|
signal fp,fm,f45,s7,s07,spt:STD_LOGIC_VECTOR (11 downto 0);
|
110 |
|
|
signal SP : STD_LOGIC_VECTOR (22 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/=17 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,9) + a1;
|
169 |
|
|
bm<=a1 - SXT(di,9);
|
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,11)+b6;
|
199 |
|
|
when 2|3 =>cp<= SXT(b2,11)+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,11) - 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 |
|
|
--s6 <= (others => '0');
|
222 |
|
|
dp <= (others => '0');
|
223 |
|
|
dm <= (others => '0');
|
224 |
|
|
elsif CLK = '1' and CLK'event then
|
225 |
|
|
if en = '1' then
|
226 |
|
|
if cycle=3 or cycle=4 or cycle=5 then
|
227 |
|
|
c2<=cm;
|
228 |
|
|
end if;
|
229 |
|
|
if cycle = 1 then
|
230 |
|
|
c3<=c1;
|
231 |
|
|
end if;
|
232 |
|
|
if cycle = 2 then
|
233 |
|
|
c4<=SXT(b6,11);
|
234 |
|
|
elsif cycle=5 then
|
235 |
|
|
c4<=c2;
|
236 |
|
|
end if;
|
237 |
|
|
if cycle = 4 then
|
238 |
|
|
dp<= SXT(cm, 12)+c2(10 downto 0);
|
239 |
|
|
else
|
240 |
|
|
dp<= ep(20 downto 9)+c4(10 downto 0);
|
241 |
|
|
end if;
|
242 |
|
|
|
243 |
|
|
if cycle = 2 then
|
244 |
|
|
dm<= c3(10 downto 0) - SXT(cp, 12);
|
245 |
|
|
elsif cycle=3 or cycle =7 then
|
246 |
|
|
dm<= c4(10 downto 0) - ep(20 downto 9);
|
247 |
|
|
end if;
|
248 |
|
|
|
249 |
|
|
end if;
|
250 |
|
|
end if;
|
251 |
|
|
end process;
|
252 |
|
|
|
253 |
|
|
MPU1:process(clk,rst)
|
254 |
|
|
begin
|
255 |
|
|
if CLK = '1' and CLK'event then
|
256 |
|
|
if en = '1' then
|
257 |
|
|
case cycle is
|
258 |
|
|
when 1|5 => m1_4<=m1;
|
259 |
|
|
when 2 => m1_4<=m4;
|
260 |
|
|
when 3 => m1_4<=m2;
|
261 |
|
|
when others => m1_4<=m3;
|
262 |
|
|
end case ;
|
263 |
|
|
case cycle is
|
264 |
|
|
when 1|2 => rd<= SXT(cp,12);
|
265 |
|
|
when 3 => rd<=dm;
|
266 |
|
|
when 4 => rd<= SXT(c3,12);
|
267 |
|
|
when others => rd<=dp;
|
268 |
|
|
end case ;
|
269 |
|
|
ep<=rd*m1_4;
|
270 |
|
|
e27<= ep(19 downto 9);
|
271 |
|
|
end if;
|
272 |
|
|
end if;
|
273 |
|
|
end process;
|
274 |
|
|
|
275 |
|
|
SM_F:process(clk,rst)
|
276 |
|
|
begin
|
277 |
|
|
if RST = '1' then
|
278 |
|
|
fp <= (others => '0');
|
279 |
|
|
fm <= (others => '0');
|
280 |
|
|
f45 <= (others => '0');
|
281 |
|
|
s7 <= (others => '0');
|
282 |
|
|
elsif CLK = '1' and CLK'event then
|
283 |
|
|
if en = '1' then
|
284 |
|
|
case cycle is
|
285 |
|
|
when 3 => fp<=ep(19 downto 9) + SXT(c4,12);
|
286 |
|
|
when 5 => fp<=ep(19 downto 9) + SXT(e27,12);
|
287 |
|
|
when 6|0 => fp<=fp + f45;
|
288 |
|
|
when 7 => fp<=e27 +f45;
|
289 |
|
|
when others=> null;
|
290 |
|
|
end case;
|
291 |
|
|
|
292 |
|
|
if cycle=4 then
|
293 |
|
|
f45<=fp;
|
294 |
|
|
elsif cycle=6 then
|
295 |
|
|
f45<=SXT(e27,12);
|
296 |
|
|
elsif cycle=7 or cycle=0 then
|
297 |
|
|
f45<=SXT(dm,12);
|
298 |
|
|
end if;
|
299 |
|
|
fm<=f45 - fp;
|
300 |
|
|
if cycle=7 then
|
301 |
|
|
s7<=fm;
|
302 |
|
|
end if;
|
303 |
|
|
end if;
|
304 |
|
|
end if;
|
305 |
|
|
end process;
|
306 |
|
|
|
307 |
|
|
MPU2:process(clk,rst)
|
308 |
|
|
begin
|
309 |
|
|
if CLK = '1' and CLK'event then
|
310 |
|
|
if en = '1' then
|
311 |
|
|
sr<=s(cycle6);
|
312 |
|
|
case cycle is
|
313 |
|
|
when 6 => s07<= SXT(c1,12);
|
314 |
|
|
when 7|3 => s07<=fp;
|
315 |
|
|
when 0 => s07<= SXT(dp,12);
|
316 |
|
|
when 1 => s07<= SXT(fm,12);
|
317 |
|
|
when 2 => s07<= SXT(c2,12);
|
318 |
|
|
when 4 => s07<= SXT(f45,12);
|
319 |
|
|
when others => s07<=s7;
|
320 |
|
|
end case ;
|
321 |
|
|
if scale_out =0 then
|
322 |
|
|
sp<=s07*sr;
|
323 |
|
|
DATA_OUT <=sp(18 downto 9);
|
324 |
|
|
else
|
325 |
|
|
spt<=s07;
|
326 |
|
|
DATA_OUT <= spt(10 downto 1);
|
327 |
|
|
end if;
|
328 |
|
|
end if;
|
329 |
|
|
end if;
|
330 |
|
|
end process;
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
end STAGE;
|