OSDN Git Service

479aeb58364f9295a03e43813f60c6ef2e44b0e0
[fpga-leon-mjpeg/leon-mjpeg.git] / grlib-gpl-1.0.22-b4095 / lib / kuri / mjpeg / idct1.vhd.bak
1
2 library ieee;
3 use ieee.std_logic_1164.all;
4 use ieee.numeric_std.all;
5
6 library grlib;
7 use grlib.stdlib.all;
8
9
10  
11
12 entity idct1 is
13     port ( rst : in  std_ulogic;
14            clk : 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
22            );
23 end idct1;
24
25 architecture rtl of idct1 is
26
27
28 function stdv2str(vec:std_logic_vector) return string is
29    variable str : string(vec'length downto 1);
30    begin
31    for i in vec'length-1 downto 0 loop
32       if(vec(i)='U') then
33           str(i+1) := 'U';
34       elsif(vec(i)='X') then
35           str(i+1) := 'X';
36       elsif(vec(i)='0') then
37           str(i+1) := '0';
38       elsif(vec(i)='1') then
39            str(i+1) := '1';
40       elsif(vec(i)='Z') then
41            str(i+1) := 'Z';
42       elsif(vec(i)='W') then
43            str(i+1) := 'W';
44       elsif(vec(i)='L') then
45            str(i+1) := 'L';
46       elsif(vec(i)='H') then
47            str(i+1) := 'H';
48       else
49            str(i+1) := '-';
50       end if;
51    end loop;
52    return str;
53 end;   
54
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);
57 begin
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;
65     return(z);
66 end;
67
68
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);
74 begin
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));
79        return(z1);
80     else
81        z2 := std_logic_vector(signed(ex_a) + signed(ex_b));
82        return(z2);
83     end if;
84 end;
85
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);
89 begin
90         judge := indata(11);
91         if (judge = '0') then
92                 z := indata(34 downto 12);
93         else 
94                 z := indata(34 downto 12) + 1;
95         end if;
96         return(z);
97 end;
98
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);
103 begin
104 --      judge := indata(11);
105         if (pol = '1') then
106                 tmpdata := (not indata) + 1 ;
107         else
108                 tmpdata := indata;
109         end if;
110         judge := tmpdata(11);
111         if (judge = '1') then
112                 z := tmpdata(34 downto 12) + 1;
113         else
114                 z := tmpdata(34 downto 12);
115         end if;
116         return(z);
117 end;
118
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);
122 begin
123         judge := indata(4);
124         if (judge = '0') then
125                 z := indata(20 downto 5);
126         else
127                 z := indata(20 downto 5) + 1;
128         end if;
129         return(z);
130 end;
131
132
133
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 :=
137
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");
146
147 --  d   d   d   d
148 --  0   c   6   c
149 --  8   3   e   9
150 --  c   4   1   a
151 --  d   4   4   d
152 --  6   1   c   c
153 --  3   9   8   e
154 --  c   a   c   1
155
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);
159
160
161 type d_reg is record
162         inreg : std_logic_vector(11 downto 0);
163 --      table_reg : tablereg_type;
164         accumulator : accumulator_type;
165         result_reg : resultreg_type;
166 end record;
167
168 type c_reg is record
169         counter : std_logic_vector(6 downto 0);
170 end record;
171
172 type all_reg is record
173         data_reg : d_reg;
174         control_reg : c_reg;
175 end record;
176         
177         
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);
184         
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);
198
199 begin
200
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;
211
212         variable count_num : integer;
213         variable vstrobe2 : std_logic;
214         variable vready1 : std_logic;
215 begin
216         
217         v := r;
218         vstrobe2 := '0';
219         count_num := to_integer(unsigned(r.control_reg.counter));
220         
221         node0 := mysigned_mul(coeffin, '0' & quantin);
222         v.data_reg.inreg := node0(11 downto 0);
223         
224         case count_num is
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);              
260         when others =>
261                 node1(0) := coeff_rom(0);
262                 node1(1) := coeff_rom(1);
263                 node1(2) := coeff_rom(2);
264                 node1(3) := coeff_rom(3);
265         end case;
266         
267         
268 --          report "node1_0=" & stdv2str(node1(0)) severity NOTE;
269 --       report "node1_0 integer =" & integer'image(TO_INTEGER(signed(node1(0)))) severity NOTE;
270
271
272         for i in 0 to 3 loop
273            node2(i) := mysigned_mul(node1(i), r.data_reg.inreg);
274            node3(i) := round1(node2(i));
275         end loop;
276         
277         
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
281                 pol := '1';
282         else
283                 pol := '0';
284         end if;
285                         
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);
290         
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
292                 for i in 0 to 7 loop
293                    node4(i) := (others => '0');
294                 end loop;
295         else
296            for i in 0 to 7 loop
297               node4(i) := r.data_reg.accumulator(i);
298            end loop;
299         end if;
300
301    for i in 0 to 7 loop
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));
305    end loop;
306         
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
308                 for i in 0 to 7 loop
309                    v.data_reg.result_reg(i) := node6(i);
310                 end loop;
311         else
312            for i in 0 to 6 loop
313               v.data_reg.result_reg(i) := r.data_reg.result_reg(i+1);
314            end loop;
315            v.data_reg.result_reg(7) := (others => '0');
316         end if;
317
318    if((count_num >= 10) and (count_num <= 73))then
319       vstrobe2 := '1';
320    end if; 
321
322 -- controller part
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');
327        end if;
328    end if;
329 --   vready1 := '0';
330 --   if (ready2 = '1' and v.control_reg.counter = "0000000") then
331 --       vready1 := '1';
332 --   end if;
333    vready1 := '0';
334    if(ready2 = '1' and count_num <= 63) then
335        vready1 := '1';
336    end if;
337
338 -- reset part
339    if rst = '0' then
340        v.data_reg.inreg := (others => '0');
341        for i in 0 to 7 loop
342           v.data_reg.accumulator(i) := (others => '0');
343           v.data_reg.result_reg(i) := (others => '0');
344        end loop;
345        v.control_reg.counter := (others => '0');
346    end if; 
347          
348 -- signal
349         outdata <= r.data_reg.result_reg(0);
350    strobe2 <= vstrobe2;
351    ready1 <= vready1;
352         rin <= v;
353
354 -- debug
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);
361
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);
368
369 end process;
370 -- registers
371 reg : process(clk)
372 begin
373         if rising_edge(clk) then
374                 r <= rin;
375         end if;
376 end process;
377
378
379
380 end rtl;
381