OSDN Git Service

Merge branch 'hw-huffdctycc' into net-mjpeg
[fpga-leon-mjpeg/leon-mjpeg.git] / grlib-gpl-1.0.22-b4095 / lib / kuri / mjpeg / dctmem2cont.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:      dctmem2cont
19 -- File:        dctmem2cont.vhd
20 -- Author:      Kenichi Kurimoto 
21 -- Description: DCT memory and controller between idct1 and idct2 
22 ------------------------------------------------------------------------------
23
24
25 library ieee;
26 use ieee.std_logic_1164.all;
27 use ieee.numeric_std.all;
28
29 library grlib;
30 --use grlib.amba.all;
31 use grlib.stdlib.all;
32 use grlib.devices.all;
33
34 library techmap;
35 use techmap.gencomp.all;
36
37 entity dctmem2cont is
38    generic (
39       memtech : integer := DEFMEMTECH);
40    port (
41       rst   : in std_ulogic;
42       clk   : in std_ulogic;
43       kready1  : out std_logic;
44       kstrobe1 : in std_logic;
45       kdata1   : in std_logic_vector(15 downto 0);
46       kready2  : in std_logic;
47       kstrobe2 : out std_logic;
48       kdata2   : out std_logic_vector(15 downto 0);
49       error     : out std_logic
50    );
51 end;
52
53 architecture rtl of dctmem2cont is
54     
55 type sstate_type is (mem0, mem1);
56 type mstate_type is (empty, writing, full, reading, standby);
57
58 type control_reg is record
59    swf : sstate_type;
60    swb : sstate_type;
61    mem0state : mstate_type;
62    mem1state : mstate_type;
63    countf : std_logic_vector(5 downto 0);
64    countb : std_logic_vector(5 downto 0);
65    stb2keep : std_logic;
66 end record;
67
68 signal r, rin : control_reg;
69 signal m0address, m1address : std_logic_vector(5 downto 0);
70 signal m0datain, m1datain : std_logic_vector(15 downto 0);
71 signal m0dataout, m1dataout : std_logic_vector(15 downto 0);
72 signal m0enable, m1enable : std_logic;
73 signal m0write, m1write : std_logic;
74
75 begin
76     yram0 : syncram generic map(tech => memtech, abits => 6, dbits => 16)
77                 port map( clk, m0address, m0datain, m0dataout, m0enable, m0write);
78     yram1 : syncram generic map(tech => memtech, abits => 6, dbits => 16)
79                 port map( clk, m1address, m1datain, m1dataout, m1enable, m1write);
80
81 comb : process (r, rst, kstrobe1, kdata1, kready2, m0dataout, m1dataout)
82       variable v : control_reg;   
83       variable vkready1 : std_logic;
84       variable verror : std_logic;
85       variable vm0address, vm1address : std_logic_vector(5 downto 0);
86       variable vm0enable, vm1enable, vm0write, vm1write : std_logic;
87       variable fcountup, bcountup : std_logic;
88       variable fcntint : integer;
89       variable vstrobe : std_logic;
90       variable outdata : std_logic_vector(15 downto 0);
91       
92    begin
93    
94    v := r;
95    verror := '0';
96    vm0enable := '0'; vm1enable := '0'; vm0write := '0'; vm1write := '0';
97    fcountup := '0'; bcountup := '0';
98    vm0address := (others => '0'); vm1address := (others => '0');
99    
100    fcntint := to_integer(unsigned(r.countf));
101    if (kstrobe1 = '1') then
102        if ((r.swf = mem0 and (r.mem0state = full or r.mem0state = reading)) or
103            (r.swf = mem1 and (r.mem1state = full or r.mem1state = reading))) then
104            verror := '1';
105        end if;
106        fcountup := '1';
107        if(r.swf = mem0) then
108            vm0enable := '1';
109            vm0write := '1';
110            vm0address := r.countf(2 downto 0) & r.countf(5 downto 3);
111        else
112            vm1enable := '1';  
113            vm1write := '1';
114            vm1address := r.countf(2 downto 0) & r.countf(5 downto 3);
115        end if;
116    end if;
117    
118    vkready1 := '0';
119    if(r.swf = mem0 and (r.mem0state = empty or r.mem0state = writing)) or
120      (r.swf = mem1 and (r.mem1state = empty or r.mem1state = writing)) then
121        vkready1 := '1';
122    end if;
123    
124    -- backward part
125    v.stb2keep := '0';
126    if (kready2 = '1') then
127        if(r.swb = mem0 and (r.mem0state = full or r.mem0state = reading)) then
128            bcountup := '1';
129            v.stb2keep := '1';
130            vm0enable := '1';
131            vm0address := r.countb;
132        elsif(r.swb = mem1 and (r.mem1state = full or r.mem1state = reading)) then
133            bcountup := '1';
134            v.stb2keep := '1';
135            vm1enable := '1';
136            vm1address := r.countb;
137        end if;
138    end if;
139        
140    if(r.swb = mem0) then
141        outdata := m0dataout;
142    else
143        outdata := m1dataout;
144    end if;
145    
146    -- state-machine
147        
148    case r.mem0state is
149    when empty =>
150        if (r.swf = mem0 and fcountup = '1') then
151            v.mem0state := writing;
152        end if;
153    when writing =>
154        if ( fcntint = 63 and fcountup = '1') then
155           v.mem0state := full; 
156           v.swf := mem1;
157        end if;
158    when full => 
159        if (r.swb = mem0 and kready2 = '1') then
160            v.mem0state := reading;
161        end if;
162    when reading =>
163        if (r.countb = "111111") then
164            v.mem0state := standby;
165        end if;
166    when standby => 
167        v.swb := mem1;
168        v.mem0state := empty;
169    when others =>
170    end case;    
171
172    case r.mem1state is
173    when empty =>
174        if (r.swf = mem1 and fcountup = '1') then
175            v.mem1state := writing;
176        end if;
177    when writing =>
178        if ( fcntint = 63 and fcountup = '1') then
179           v.mem1state := full; 
180           v.swf := mem0;
181        end if;
182    when full => 
183        if (r.swb = mem1 and kready2 = '1') then
184            v.mem1state := reading;
185        end if;
186    when reading =>
187        if (r.countb = "111111") then
188            v.mem1state := standby;
189        end if;
190    when standby => 
191        v.swb := mem0;
192        v.mem1state := empty;
193    when others =>
194    end case;    
195        
196 -- counter
197    if(fcountup = '1') then
198        v.countf := r.countf + '1';
199    end if;
200    if(bcountup = '1') then
201        v.countb := r.countb + '1';
202    end if;
203    
204 -- reset part
205    if rst = '0' then
206        v.swf := mem0;
207        v.swb := mem0;
208        v.mem0state := empty;
209        v.mem1state := empty;
210        v.countf := (others => '0');
211        v.countb := (others => '0');
212        v.stb2keep := '0';
213    end if;
214    
215 -- signal
216
217    rin <= v;
218    kready1 <= vkready1;
219    kstrobe2 <= r.stb2keep;
220    kdata2 <= outdata;
221    error <= verror;
222    m0address <= vm0address;
223    m1address <= vm1address;
224    m0enable <= vm0enable;
225    m1enable <= vm1enable;
226    m0write <= vm0write;
227    m1write <= vm1write;
228    
229 end process;
230
231    m0datain <= kdata1;
232    m1datain <= kdata1;
233    
234    
235 --registers
236 reg : process(clk)
237 begin
238     if rising_edge(clk) then
239         r <= rin;
240     end if;
241 end process;
242
243 end;
244
245    
246    
247        
248        
249        
250        
251        
252        
253        
254        
255        
256        
257        
258        
259        
260        
261            
262       
263       
264       
265    
266       
267