3 use ieee.std_logic_1164.all;
4 use ieee.numeric_std.all;
13 port ( rst : in std_ulogic;
15 ready1 : out std_logic;
16 strobe1 : in std_logic;
17 coeffin : in std_logic_vector (11 downto 0);
18 quantin : in std_logic_vector (7 downto 0);
19 outdata : out std_logic_vector (15 downto 0);
20 ready2 : in std_logic;
21 strobe2 : out std_logic
25 architecture rtl of idct1 is
28 function stdv2str(vec:std_logic_vector) return string is
29 variable str : string(vec'length downto 1);
31 for i in vec'length-1 downto 0 loop
34 elsif(vec(i)='X') then
36 elsif(vec(i)='0') then
38 elsif(vec(i)='1') then
40 elsif(vec(i)='Z') then
42 elsif(vec(i)='W') then
44 elsif(vec(i)='L') then
46 elsif(vec(i)='H') then
55 function mysigned_mul(a,b : std_logic_vector) return std_logic_vector is
56 variable z : std_logic_vector(a'length + b'length -1 downto 0);
58 z := std_logic_vector(signed(a) * signed(b));
59 -- report "a=" & stdv2str(a) severity NOTE;
60 -- report "a integer =" & integer'image(TO_INTEGER(signed(a))) severity NOTE;
61 -- report "b=" & stdv2str(b) severity NOTE;
62 -- report "b integer =" & integer'image(TO_INTEGER(signed(b))) severity NOTE;
63 -- report "a*b integer =" & integer'image(TO_INTEGER(signed(a))* TO_INTEGER(signed(b))) severity NOTE;
64 -- report "a*b=" & stdv2str(z) severity NOTE;
69 function mysigned_add(a,b : std_logic_vector) return std_logic_vector is
70 variable ex_a : std_logic_vector(a'length downto 0);
71 variable ex_b : std_logic_vector(b'length downto 0);
72 variable z1 : std_logic_vector(a'length downto 0);
73 variable z2 : std_logic_vector(b'length downto 0);
75 ex_a := a(a'left) & a;
76 ex_b := b(b'left) & b;
77 if( a'length > b'length)then
78 z1 := std_logic_vector(signed(ex_a) + signed(ex_b));
81 z2 := std_logic_vector(signed(ex_a) + signed(ex_b));
86 function round1(indata : std_logic_vector(34 downto 0)) return std_logic_vector is
87 variable judge :std_logic;
88 variable z : std_logic_vector(22 downto 0);
92 z := indata(34 downto 12);
94 z := indata(34 downto 12) + 1;
99 function round2(indata : std_logic_vector(34 downto 0); pol : std_logic) return std_logic_vector is
100 variable judge : std_logic;
101 variable tmpdata : std_logic_vector(34 downto 0);
102 variable z : std_logic_vector(22 downto 0);
104 -- judge := indata(11);
106 tmpdata := (not indata) + 1 ;
110 judge := tmpdata(11);
111 if (judge = '1') then
112 z := tmpdata(34 downto 12) + 1;
114 z := tmpdata(34 downto 12);
119 function round3(indata : std_logic_vector(25 downto 0)) return std_logic_vector is
120 variable judge : std_logic;
121 variable z : std_logic_vector(15 downto 0);
124 if (judge = '0') then
125 z := indata(20 downto 5);
127 z := indata(20 downto 5) + 1;
134 subtype coeff23 is std_logic_vector(22 downto 0);
135 type coeff_array1 is array(0 to 31) of coeff23;
136 constant coeff_rom : coeff_array1 :=
138 ("01011010100000100111101","01011010100000100111101","01011010100000100111101","01011010100000100111101",
139 "01111101100010100110000","01101010011011011001100","01000111000111001110110","00011000111110001011100",
140 "01110110010000011011000","00110000111110111100011","11001111000001000011110","10001001101111100101001",
141 "01101010011011011001100","11100111000001110100100","10000010011101011010001","10111000111000110001010",
142 "01011010100000100111101","10100101011111011000100","10100101011111011000100","01011010100000100111101",
143 "01000111000111001110110","10000010011101011010001","00011000111110001011100","01101010011011011001100",
144 "00110000111110111100011","10001001101111100101001","01110110010000011011000","11001111000001000011110",
145 "00011000111110001011100","10111000111000110001010","01101010011011011001100","10000010011101011010001");
156 type tablereg_type is array (0 to 3) of std_logic_vector(22 downto 0);
157 type accumulator_type is array (0 to 7) of std_logic_vector(25 downto 0);
158 type resultreg_type is array (0 to 7) of std_logic_vector(15 downto 0);
162 inreg : std_logic_vector(11 downto 0);
163 -- table_reg : tablereg_type;
164 accumulator : accumulator_type;
165 result_reg : resultreg_type;
169 counter : std_logic_vector(6 downto 0);
172 type all_reg is record
178 type node1_array is array (0 to 3) of std_logic_vector(22 downto 0);
179 type node2_array is array (0 to 3) of std_logic_vector(34 downto 0);
180 type node3_array is array (0 to 7) of std_logic_vector(22 downto 0);
181 type node4_array is array (0 to 7) of std_logic_vector(25 downto 0);
182 type node5_array is array (0 to 7) of std_logic_vector(26 downto 0);
183 type node6_array is array (0 to 7) of std_logic_vector(15 downto 0);
185 signal r, rin : all_reg;
186 signal sig_node1_0 : std_logic_vector(22 downto 0);
187 signal sig_node2_0 : std_logic_vector(34 downto 0);
188 signal sig_node3_0 : std_logic_vector(22 downto 0);
189 signal sig_node4_0 : std_logic_vector(25 downto 0);
190 signal sig_node5_0 : std_logic_vector(26 downto 0);
191 signal sig_node6_0 : std_logic_vector(15 downto 0);
192 signal sig_node1_1 : std_logic_vector(22 downto 0);
193 signal sig_node2_1 : std_logic_vector(34 downto 0);
194 signal sig_node3_6 : std_logic_vector(22 downto 0);
195 signal sig_node4_6 : std_logic_vector(25 downto 0);
196 signal sig_node5_6 : std_logic_vector(26 downto 0);
197 signal sig_node6_6 : std_logic_vector(15 downto 0);
201 comb : process(r, rst, strobe1, ready2, coeffin, quantin)
202 variable v : all_reg;
203 variable node0 : std_logic_vector(20 downto 0);
204 variable node1 : node1_array;
205 variable node2 : node2_array;
206 variable node3 : node3_array;
207 variable node4 : node4_array;
208 variable node5 : node5_array;
209 variable node6 : node6_array;
210 variable pol : std_logic;
212 variable count_num : integer;
213 variable vstrobe2 : std_logic;
214 variable vready1 : std_logic;
219 count_num := to_integer(unsigned(r.control_reg.counter));
221 node0 := mysigned_mul(coeffin, '0' & quantin);
222 v.data_reg.inreg := node0(11 downto 0);
225 when 2 | 10 | 18 | 26 | 34 | 42 | 50 | 58 =>
226 node1(0) := coeff_rom(4);
227 node1(1) := coeff_rom(5);
228 node1(2) := coeff_rom(6);
229 node1(3) := coeff_rom(7);
230 when 3 | 11 | 19 | 27 | 35 | 43 | 51 | 59 =>
231 node1(0) := coeff_rom(8);
232 node1(1) := coeff_rom(9);
233 node1(2) := coeff_rom(10);
234 node1(3) := coeff_rom(11);
235 when 4 | 12 | 20 | 28 | 36 | 44 | 52 | 60 =>
236 node1(0) := coeff_rom(12);
237 node1(1) := coeff_rom(13);
238 node1(2) := coeff_rom(14);
239 node1(3) := coeff_rom(15);
240 when 5 | 13 | 21 | 29 | 37 | 45 | 53 | 61 =>
241 node1(0) := coeff_rom(16);
242 node1(1) := coeff_rom(17);
243 node1(2) := coeff_rom(18);
244 node1(3) := coeff_rom(19);
245 when 6 | 14 | 22 | 30 | 38 | 46 | 54 | 62 =>
246 node1(0) := coeff_rom(20);
247 node1(1) := coeff_rom(21);
248 node1(2) := coeff_rom(22);
249 node1(3) := coeff_rom(23);
250 when 7 | 15 | 23 | 31 | 39 | 47 | 55 | 63 =>
251 node1(0) := coeff_rom(24);
252 node1(1) := coeff_rom(25);
253 node1(2) := coeff_rom(26);
254 node1(3) := coeff_rom(27);
255 when 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 =>
256 node1(0) := coeff_rom(28);
257 node1(1) := coeff_rom(29);
258 node1(2) := coeff_rom(30);
259 node1(3) := coeff_rom(31);
261 node1(0) := coeff_rom(0);
262 node1(1) := coeff_rom(1);
263 node1(2) := coeff_rom(2);
264 node1(3) := coeff_rom(3);
268 -- report "node1_0=" & stdv2str(node1(0)) severity NOTE;
269 -- report "node1_0 integer =" & integer'image(TO_INTEGER(signed(node1(0)))) severity NOTE;
273 node2(i) := mysigned_mul(node1(i), r.data_reg.inreg);
274 node3(i) := round1(node2(i));
278 -- when 3 | 5 | 7 | 9 | 11 | 13 | 15 | 17 | 19 | 21 | 23 | ..... | 65
279 -- when 2 4 6 8 10 12 14 16 18 20 22 64
280 if((count_num mod 2) = 0 and (count_num >= 2) and (count_num <= 64))then
286 node3(4) := round2(node2(3), pol);
287 node3(5) := round2(node2(2), pol);
288 node3(6) := round2(node2(1), pol);
289 node3(7) := round2(node2(0), pol);
291 if((count_num = 1) or (count_num = 9) or (count_num = 17) or (count_num = 25) or (count_num = 33) or (count_num = 41) or (count_num = 49) or (count_num = 57)) then
293 node4(i) := (others => '0');
297 node4(i) := r.data_reg.accumulator(i);
302 node5(i) := mysigned_add(node3(i), node4(i));
303 v.data_reg.accumulator(i) := node5(i)(25 downto 0);
304 node6(i) := round3(r.data_reg.accumulator(i));
307 if((count_num = 9) or (count_num = 17) or (count_num = 25) or (count_num = 33) or (count_num = 41) or (count_num = 49) or (count_num = 57) or (count_num = 65)) then
309 v.data_reg.result_reg(i) := node6(i);
313 v.data_reg.result_reg(i) := r.data_reg.result_reg(i+1);
315 v.data_reg.result_reg(7) := (others => '0');
318 if((count_num >= 10) and (count_num <= 73))then
323 if ((count_num = 0 and strobe1 = '1') or count_num /= 0) then
324 v.control_reg.counter := std_logic_vector(to_unsigned(count_num + 1,7));
325 if(count_num = 73)then
326 v.control_reg.counter := (others => '0');
330 -- if (ready2 = '1' and v.control_reg.counter = "0000000") then
334 if(ready2 = '1' and count_num <= 63) then
340 v.data_reg.inreg := (others => '0');
342 v.data_reg.accumulator(i) := (others => '0');
343 v.data_reg.result_reg(i) := (others => '0');
345 v.control_reg.counter := (others => '0');
349 outdata <= r.data_reg.result_reg(0);
355 sig_node1_0 <= node1(0);
356 sig_node2_0 <= node2(0);
357 sig_node3_0 <= node3(0);
358 sig_node4_0 <= node4(0);
359 sig_node5_0 <= node5(0);
360 sig_node6_0 <= node6(0);
362 sig_node1_1 <= node1(1);
363 sig_node2_1 <= node2(1);
364 sig_node3_6 <= node3(6);
365 sig_node4_6 <= node4(6);
366 sig_node5_6 <= node5(6);
367 sig_node6_6 <= node6(6);
373 if rising_edge(clk) then