OSDN Git Service

change jpeg IPCores. startgen signal distribute into all module
[fpga-leon-mjpeg/leon-mjpeg.git] / grlib-gpl-1.0.22-b4095 / lib / kuri / mjpeg / yccmemcont.vhd
1 ------------------------------------------------------------------------------
2 --  Copyright (C) 2011, Kenichi Kurimoto
3 --
4 --  This program is free software; you can redistribute it and/or modify
5 --  it under the terms of the GNU General Public License as published by
6 --  the Free Software Foundation; either version 2 of the License, or
7 --  (at your option) any later version.
8 --
9 --  This program is distributed in the hope that it will be useful,
10 --  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 --  GNU General Public License for more details.
13 --
14 --  You should have received a copy of the GNU General Public License
15 --  along with this program; if not, write to the Free Software
16 --  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
17 -----------------------------------------------------------------------------
18 -- Entity:      yccmemcont
19 -- File:        yccmemcont.vhd
20 -- Author:      Kenichi Kurimoto 
21 -- Description: YCbCr memory + controller
22 ------------------------------------------------------------------------------
23
24 library ieee;
25 use ieee.std_logic_1164.all;
26 use ieee.numeric_std.all;
27
28 library grlib;
29 --use grlib.amba.all;
30 use grlib.stdlib.all;
31 use grlib.devices.all;
32
33 library techmap;
34 use techmap.gencomp.all;
35
36 entity yccmemcont is
37    generic (
38       memtech : integer := DEFMEMTECH);
39    port (
40       rst   : in std_ulogic;
41       clk   : in std_ulogic;
42       kready1  : out std_logic;
43       kstrobe1 : in std_logic;
44       kdata1   : in std_logic_vector(7 downto 0);
45       kready2  : in std_logic;
46       kstrobe2 : out std_logic;
47       kdata2   : out std_logic_vector(23 downto 0);
48       samp_fact : in std_logic;
49       error     : out std_logic;
50       startgen  : in std_logic
51    );
52 end;
53 -- samp_fact = 0 -> 4:1:1
54 -- samp_fact = 1 -> 4:2:2
55
56 architecture rtl of yccmemcont is
57           
58 type sstate_type is (mem0, mem1);
59 type mstate_type is (empty, writing, full, reading, standby);
60
61 type control_reg is record
62     swf : sstate_type;
63     swb : sstate_type;
64     mem0state : mstate_type;
65     mem1state : mstate_type;
66     countf   : std_logic_vector(8 downto 0);
67 --    selectf  : std_logic_vector(1 downto 0);
68     countb   : std_logic_vector(7 downto 0);
69     stb2keep : std_logic;
70 end record;
71
72 signal r, rin : control_reg;
73 signal y0address, y1address : std_logic_vector(7 downto 0);
74 signal cb0address, cb1address, cr0address, cr1address : std_logic_vector(6 downto 0);
75 signal y0datain, y1datain, cb0datain, cb1datain, cr0datain, cr1datain : std_logic_vector(7 downto 0);
76 signal y0dataout, y1dataout, cb0dataout, cb1dataout, cr0dataout, cr1dataout : std_logic_vector(7 downto 0);
77 signal y0enable, y1enable, cb0enable, cb1enable, cr0enable, cr1enable : std_logic;
78 signal y0write, y1write, cb0write, cb1write, cr0write, cr1write : std_logic;
79
80 begin
81 yram0 : syncram generic map(tech => memtech, abits => 8, dbits => 8)
82             port map( clk, y0address, y0datain, y0dataout, y0enable, y0write);
83 yram1 : syncram generic map(tech => memtech, abits => 8, dbits => 8)
84             port map( clk, y1address, y1datain, y1dataout, y1enable, y1write);
85 cbram0 : syncram generic map(tech => memtech, abits => 7, dbits => 8)
86              port map( clk, cb0address, cb0datain, cb0dataout, cb0enable, cb0write);
87 cbram1 : syncram generic map(tech => memtech, abits => 7, dbits => 8)
88              port map( clk, cb1address, cb1datain, cb1dataout, cb1enable, cb1write);
89 crram0 : syncram generic map(tech => memtech, abits => 7, dbits => 8)
90              port map( clk, cr0address, cr0datain, cr0dataout, cr0enable, cr0write);
91 crram1 : syncram generic map(tech => memtech, abits => 7, dbits => 8)
92              port map( clk, cr1address, cr1datain, cr1dataout, cr1enable, cr1write);                 
93                  
94                  
95 comb : process (r, rst, kstrobe1, kdata1, kready2, samp_fact, y0dataout, y1dataout, 
96                    cb0dataout, cb1dataout, cr0dataout, cr1dataout, startgen)
97       variable v : control_reg;     
98       variable vkready1 : std_logic;
99       variable verror : std_logic;
100       variable vy0address, vy1address : std_logic_vector(7 downto 0);
101       variable vcb0address, vcb1address, vcr0address, vcr1address : std_logic_vector(6 downto 0);
102       variable vy0enable, vy1enable, vcb0enable, vcb1enable, vcr0enable, vcr1enable : std_logic;
103       variable vy0write, vy1write, vcb0write, vcb1write, vcr0write, vcr1write : std_logic;
104       variable fcountup, bcountup : std_logic;
105       variable fcntint : integer;
106       variable vstrobe : std_logic;
107       variable outdata : std_logic_vector(23 downto 0);
108    begin
109
110    v := r;
111    verror := '0';
112    vy0enable := '0'; vy1enable := '0'; vcb0enable := '0'; vcb1enable := '0'; vcr0enable := '0'; vcr1enable := '0';
113    vy0write := '0'; vy1write := '0'; vcb0write := '0'; vcb1write := '0'; vcr0write := '0'; vcr1write := '0';
114    fcountup := '0'; bcountup := '0';
115    vy0address := (others => '0'); vy1address := (others => '0'); 
116    vcb0address := (others => '0'); vcb1address := (others => '0');
117    vcr0address := (others => '0'); vcr1address := (others => '0');
118    
119    -- forward part
120    fcntint := to_integer(unsigned(r.countf));
121    if (kstrobe1 = '1') then
122        if ((r.swf = mem0 and (r.mem0state = full or r.mem0state = reading))or
123            (r.swf = mem1 and (r.mem1state = full or r.mem1state = reading)))then
124            verror := '1'; 
125        end if;
126        fcountup := '1';
127        if(r.swf = mem0) then
128            if(samp_fact = '0') then
129                if(fcntint < 256) then
130                    vy0enable := '1';
131                    vy0write := '1';
132                    vy0address := r.countf(7 downto 0);
133                elsif(fcntint < 320) then
134                    vcb0enable := '1';
135                    vcb0write := '1';
136                    vcb0address := r.countf(6 downto 0);
137                elsif(fcntint < 384) then
138                    vcr0enable := '1';
139                    vcr0write := '1';
140                    vcr0address := '0' & r.countf(5 downto 0);
141                else
142                    verror := '1';
143                end if;
144            else
145                if(fcntint < 256) then
146                    vy0enable := '1';
147                    vy0write := '1';
148                    vy0address := r.countf(7 downto 0);
149                elsif(fcntint < 384) then
150                    vcb0enable := '1';
151                    vcb0write := '1';
152                    vcb0address := r.countf(6 downto 0);
153                elsif(fcntint < 512) then
154                    vcr0enable := '1';
155                    vcr0write := '1';
156                    vcr0address := r.countf(6 downto 0);
157                else
158                    verror := '1';
159                end if;           
160            end if;
161        else
162            if(samp_fact = '0') then
163                if(fcntint < 256) then
164                    vy1enable := '1';
165                    vy1write := '1';
166                    vy1address := r.countf(7 downto 0);
167                elsif(fcntint < 320) then
168                    vcb1enable := '1';
169                    vcb1write := '1';
170                    vcb1address := r.countf(6 downto 0);
171                elsif(fcntint < 384) then
172                    vcr1enable := '1';
173                    vcr1write := '1';
174                    vcr1address := '0' & r.countf(5 downto 0);
175                else
176                    verror := '1';
177                end if;
178            else
179                if(fcntint < 256) then
180                    vy1enable := '1';
181                    vy1write := '1';
182                    vy1address := r.countf(7 downto 0);
183                elsif(fcntint < 384) then
184                    vcb1enable := '1';
185                    vcb1write := '1';
186                    vcb1address := r.countf(6 downto 0);
187                elsif(fcntint < 512) then
188                    vcr1enable := '1';
189                    vcr1write := '1';
190                    vcr1address := r.countf(6 downto 0);
191                else
192                    verror := '1';
193                end if;           
194            end if;
195        end if;              
196    end if;
197    
198    vkready1 := '0';
199    if (r.swf = mem0 and (r.mem0state = empty or r.mem0state = writing)) or (r.swf = mem1 and (r.mem1state = empty or r.mem1state = writing)) then
200        vkready1 := '1';
201    end if;
202    
203    --backward part
204    v.stb2keep := '0';   
205    if (kready2 = '1') then
206        if(r.swb = mem0 and (r.mem0state = full or r.mem0state = reading)) then
207            bcountup := '1';
208            v.stb2keep := '1';
209            vy0enable := '1';         
210            vcb0enable := '1';
211            vcr0enable := '1';
212            vy0address := r.countb(7) & r.countb(3) & r.countb(6 downto 4) & r.countb(2 downto 0);
213            if(samp_fact = '0') then   
214                vcb0address := '0' & r.countb(7 downto 5) & r.countb(3 downto 1);
215                vcr0address := '0' & r.countb(7 downto 5) & r.countb(3 downto 1);
216            else
217                vcb0address := r.countb(7 downto 1);  
218                vcr0address := r.countb(7 downto 1);
219            end if;
220        elsif(r.swb = mem1 and (r.mem1state = full or r.mem1state = reading))then
221            bcountup := '1';
222            v.stb2keep := '1';
223            vy1enable := '1';
224            vcb1enable := '1';
225            vcr1enable := '1';
226            vy1address := r.countb(7) & r.countb(3) & r.countb(6 downto 4) & r.countb(2 downto 0);
227            if(samp_fact = '0') then   
228                vcb1address := '0' & r.countb(7 downto 5) & r.countb(3 downto 1);
229                vcr1address := '0' & r.countb(7 downto 5) & r.countb(3 downto 1);
230            else
231                vcb1address := r.countb(7 downto 1);  
232                vcr1address := r.countb(7 downto 1);
233            end if;
234        end if;
235    end if;
236    
237    if(r.swb = mem0)then
238        outdata := y0dataout & cb0dataout & cr0dataout;
239    else
240        outdata := y1dataout & cb1dataout & cr1dataout;
241    end if;
242    
243    
244    --state-machine
245    --check empty case batting memory read write access
246    
247
248    case r.mem0state is
249    when empty =>
250        if (r.swf = mem0 and fcountup = '1') then
251            v.mem0state := writing;
252        end if;
253    when writing =>
254        if ((samp_fact = '0' and fcntint = 383 and fcountup = '1')or(samp_fact = '1' and fcntint = 511 and fcountup = '1')) then
255           v.mem0state := full; 
256           v.swf := mem1;
257        end if;
258    when full => 
259        if (r.swb = mem0 and kready2 = '1') then
260            v.mem0state := reading;
261        end if;
262    when reading =>
263        if (r.countb = "11111111") then
264            v.mem0state := standby;
265        end if;
266    when standby => 
267        v.swb := mem1;
268        v.mem0state := empty;
269    when others =>
270    end case;
271    
272    case r.mem1state is
273    when empty =>
274        if (r.swf = mem1 and fcountup = '1') then
275            v.mem1state := writing;
276        end if;
277    when writing =>
278        if ((samp_fact = '0' and fcntint = 383 and fcountup = '1')or(samp_fact = '1' and fcntint = 511 and fcountup = '1')) then
279           v.mem1state := full; 
280           v.swf := mem0;
281        end if;
282    when full => 
283        if (r.swb = mem1 and kready2 = '1') then
284            v.mem1state := reading;
285        end if;
286    when reading =>
287        if (r.countb = "11111111") then
288            v.mem1state := standby;
289        end if;
290    when standby =>
291        v.swb := mem0;
292        v.mem1state := empty;
293    when others =>
294    end case;   
295
296 -- counter
297    if(fcountup = '1') then
298        v.countf := r.countf + '1';
299        if (samp_fact = '0') and (fcntint = 383) then
300            v.countf := "000000000";
301        elsif (samp_fact = '1') and (fcntint = 511) then
302            v.countf := "000000000";
303        end if; 
304    end if;
305    if (bcountup = '1') then
306        v.countb := r.countb + '1';
307    end if;
308   
309 -- reset part
310    if rst = '0' or startgen = '1' then
311        v.swf := mem0;
312        v.swb := mem0;
313        v.mem0state := empty;
314        v.mem1state := empty;
315        v.countf := (others => '0');
316        v.countb := (others => '0');
317        v.stb2keep := '0';
318    end if;
319   
320 -- signal
321
322    rin <= v;   
323    kready1 <= vkready1;
324    kstrobe2 <= r.stb2keep;
325    kdata2 <= outdata;
326    error <= verror;
327    y0address <= vy0address;
328    y1address <= vy1address;
329    cb0address <= vcb0address;
330    cb1address <= vcb1address;
331    cr0address <= vcr0address;
332    cr1address <= vcr1address;
333    y0enable <= vy0enable;
334    y1enable <= vy1enable;
335    cb0enable <= vcb0enable;
336    cb1enable <= vcb1enable;
337    cr0enable <= vcr0enable;
338    cr1enable <= vcr1enable;
339    y0write <= vy0write;
340    y1write <= vy1write;
341    cb0write <= vcb0write;
342    cb1write <= vcb1write;
343    cr0write <= vcr0write;
344    cr1write <= vcr1write;
345    
346 end process;
347
348    y0datain <= kdata1;
349    y1datain <= kdata1;
350    cb0datain <= kdata1;
351    cb1datain <= kdata1;
352    cr0datain <= kdata1;
353    cr1datain <= kdata1;
354    
355 -- registers 
356 reg : process(clk)
357 begin
358    if rising_edge(clk) then
359         r <= rin;
360    end if;
361 end process;
362
363
364 end;
365