OpenCores
URL https://opencores.org/ocsvn/gamepads/gamepads/trunk

Subversion Repositories gamepads

[/] [gamepads/] [trunk/] [gcpad/] [rtl/] [vhdl/] [gcpad_rx.vhd] - Blame information for rev 41

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 arniml
-------------------------------------------------------------------------------
2
--
3
-- GCpad controller core
4
--
5 41 arniml
-- $Id: gcpad_rx.vhd 41 2009-04-01 19:58:04Z arniml $
6 11 arniml
--
7
-- Copyright (c) 2004, Arnim Laeuger (arniml@opencores.org)
8
--
9
-- All rights reserved
10
--
11
-- Redistribution and use in source and synthezised forms, with or without
12
-- modification, are permitted provided that the following conditions are met:
13
--
14
-- Redistributions of source code must retain the above copyright notice,
15
-- this list of conditions and the following disclaimer.
16
--
17
-- Redistributions in synthesized form must reproduce the above copyright
18
-- notice, this list of conditions and the following disclaimer in the
19
-- documentation and/or other materials provided with the distribution.
20
--
21
-- Neither the name of the author nor the names of other contributors may
22
-- be used to endorse or promote products derived from this software without
23
-- specific prior written permission.
24
--
25
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
29
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
-- POSSIBILITY OF SUCH DAMAGE.
36
--
37
-- Please report bugs to the author, but before you do so, please
38
-- make sure that this is not a derivative work and that
39
-- you have the latest version of this file.
40
--
41
-- The latest version of this file can be found at:
42
--      http://www.opencores.org/cvsweb.shtml/gamepads/
43
--
44
-- The project homepage is located at:
45
--      http://www.opencores.org/projects.cgi/web/gamepads/overview
46
--
47
-------------------------------------------------------------------------------
48
 
49
library ieee;
50
use ieee.std_logic_1164.all;
51 17 arniml
use work.gcpad_pack.buttons_t;
52 11 arniml
 
53
entity gcpad_rx is
54
 
55
  generic (
56
    reset_level_g    :     integer := 0;
57
    clocks_per_1us_g :     integer := 2
58
  );
59
  port (
60
    -- System Interface -------------------------------------------------------
61
    clk_i            : in  std_logic;
62
    reset_i          : in  std_logic;
63
    -- Control Interface ------------------------------------------------------
64
    rx_en_i          : in  boolean;
65
    rx_done_o        : out boolean;
66 20 arniml
    rx_data_ok_o     : out boolean;
67 12 arniml
    rx_size_i        : in  std_logic_vector(3 downto 0);
68 11 arniml
    -- Gamepad Interface ------------------------------------------------------
69
    pad_data_i       : in  std_logic;
70 17 arniml
    -- Data Interface ---------------------------------------------------------
71
    rx_data_o        : out buttons_t
72 11 arniml
  );
73
 
74
end gcpad_rx;
75
 
76
 
77
library ieee;
78
use ieee.numeric_std.all;
79
use work.gcpad_pack.all;
80
 
81
architecture rtl of gcpad_rx is
82
 
83 13 arniml
  component gcpad_sampler
84
    generic (
85
      reset_level_g      :     integer := 0;
86
      clocks_per_1us_g   :     integer := 2
87
    );
88
    port (
89
      clk_i              : in  std_logic;
90
      reset_i            : in  std_logic;
91
      wrap_sample_i      : in  boolean;
92
      sync_sample_i      : in  boolean;
93
      sample_underflow_o : out boolean;
94
      pad_data_i         : in  std_logic;
95
      pad_data_o         : out std_logic;
96
      sample_o           : out std_logic
97
    );
98
  end component;
99
 
100 11 arniml
  type state_t is (IDLE,
101
                   DETECT_TIMEOUT,
102
                   WAIT_FOR_1,
103
                   WAIT_FOR_0,
104
                   FINISHED);
105
  signal state_s,
106
         state_q  : state_t;
107
 
108
  signal buttons_q,
109
         shift_buttons_q : buttons_t;
110
  signal save_buttons_s  : boolean;
111
  signal shift_buttons_s : boolean;
112
 
113 13 arniml
  signal sync_sample_s   : boolean;
114
  signal wrap_sample_s   : boolean;
115 11 arniml
 
116
  -- timeout counter counts three sample undeflows
117
  constant cnt_timeout_high_c : natural := 3;
118
  subtype  cnt_timeout_t      is natural range 0 to cnt_timeout_high_c;
119
  signal   cnt_timeout_q      : cnt_timeout_t;
120
  signal   timeout_q          : boolean;
121
  signal   sync_timeout_s     : boolean;
122
 
123
 
124
  subtype num_buttons_read_t  is unsigned(6 downto 0);
125
  signal  num_buttons_read_q  : num_buttons_read_t;
126
  signal  all_buttons_read_s  : boolean;
127
  signal  reset_num_buttons_s : boolean;
128
 
129 13 arniml
  signal pad_data_s         : std_logic;
130
  signal sample_s           : std_logic;
131
  signal sample_underflow_s : boolean;
132 11 arniml
 
133 20 arniml
  signal rx_done_s,
134
         rx_done_q  : boolean;
135 11 arniml
 
136
begin
137
 
138 13 arniml
  sampler_b : gcpad_sampler
139
    generic map (
140
      reset_level_g => reset_level_g,
141
      clocks_per_1us_g => clocks_per_1us_g
142
    )
143
    port map (
144
      clk_i              => clk_i,
145
      reset_i            => reset_i,
146
      wrap_sample_i      => wrap_sample_s,
147
      sync_sample_i      => sync_sample_s,
148
      sample_underflow_o => sample_underflow_s,
149
      pad_data_i         => pad_data_i,
150
      pad_data_o         => pad_data_s,
151
      sample_o           => sample_s
152
    );
153
 
154 11 arniml
  -----------------------------------------------------------------------------
155
  -- Process seq
156
  --
157
  -- Purpose:
158
  --   Implements the sequential elements of this module.
159
  --
160
  seq: process (reset_i, clk_i)
161 13 arniml
    variable size_v : std_logic_vector(num_buttons_read_t'range);
162 11 arniml
  begin
163
    if reset_i = reset_level_g then
164
      buttons_q       <= (others => '0');
165
      shift_buttons_q <= (others => '0');
166
 
167
      state_q              <= IDLE;
168
 
169
      cnt_timeout_q        <= cnt_timeout_high_c;
170
 
171
      timeout_q            <= false;
172
 
173
      num_buttons_read_q   <= (others => '0');
174
      rx_done_q            <= false;
175
 
176
    elsif clk_i'event and clk_i = '1' then
177 20 arniml
      state_q   <= state_s;
178 11 arniml
 
179 20 arniml
      rx_done_q <= rx_done_s;
180
 
181 11 arniml
      -- timeout counter
182
      if sync_timeout_s then
183
        -- explicit preload
184
        cnt_timeout_q <= cnt_timeout_high_c;
185
        timeout_q     <= false;
186
      elsif cnt_timeout_q = 0 then
187
        -- wrap-around
188
        cnt_timeout_q <= cnt_timeout_high_c;
189
        timeout_q     <= true;
190 13 arniml
      elsif sample_underflow_s then
191 11 arniml
        -- decrement counter when sampler wraps around
192
        cnt_timeout_q <= cnt_timeout_q - 1;
193
      end if;
194
 
195
 
196
      -- count remaining number of buttons to read
197
      if shift_buttons_s then
198
        shift_buttons_q(buttons_t'high downto 1) <= shift_buttons_q(buttons_t'high-1 downto 0);
199
 
200 13 arniml
        if sample_s = '1' then
201 11 arniml
          shift_buttons_q(0) <= '1';
202
        else
203
          shift_buttons_q(0) <= '0';
204
        end if;
205
 
206
      end if;
207
 
208
      if reset_num_buttons_s then
209
        -- explicit preload
210 12 arniml
        size_v(num_buttons_read_t'high downto 3) := rx_size_i;
211
        size_v(2 downto 0)                       := (others => '0');
212
        num_buttons_read_q   <= unsigned(size_v);
213 11 arniml
      elsif shift_buttons_s then
214
        -- decrement counter when a button bit has been read
215
        if not all_buttons_read_s then
216
          num_buttons_read_q <= num_buttons_read_q - 1;
217
        end if;
218
      end if;
219
 
220
 
221
      -- the buttons
222
      if save_buttons_s then
223
        buttons_q <= shift_buttons_q;
224
      end if;
225
 
226
    end if;
227
 
228
  end process seq;
229
  --
230
  -----------------------------------------------------------------------------
231
 
232
  -- indicates that all buttons have been read
233
  all_buttons_read_s <= num_buttons_read_q = 0;
234
 
235
 
236
  -----------------------------------------------------------------------------
237
  -- Process fsm
238
  --
239
  -- Purpose:
240
  --   Models the controlling state machine.
241
  --
242
  fsm: process (state_q,
243
                rx_en_i,
244
                pad_data_s,
245 13 arniml
                wrap_sample_s,
246 11 arniml
                all_buttons_read_s,
247 13 arniml
                sample_underflow_s,
248 11 arniml
                timeout_q)
249
  begin
250
    sync_sample_s       <= false;
251
    sync_timeout_s      <= false;
252
    state_s             <= IDLE;
253
    shift_buttons_s     <= false;
254
    save_buttons_s      <= false;
255 20 arniml
    rx_done_s           <= false;
256 11 arniml
    reset_num_buttons_s <= false;
257
    wrap_sample_s       <= false;
258
 
259
    case state_q is
260
      -- IDLE -----------------------------------------------------------------
261
      -- The idle state.
262
      when IDLE =>
263
        if rx_en_i then
264
          state_s             <= DETECT_TIMEOUT;
265
 
266
        else
267
          -- keep counters synchronized when no reception is running
268
          sync_sample_s       <= true;
269
          sync_timeout_s      <= true;
270
          reset_num_buttons_s <= true;
271
          state_s             <= IDLE;
272
 
273
        end if;
274
 
275
      when DETECT_TIMEOUT =>
276
        state_s             <= DETECT_TIMEOUT;
277
 
278
        if pad_data_s = '0' then
279
          sync_sample_s     <= true;
280
          state_s           <= WAIT_FOR_1;
281
 
282
        else
283
          -- wait for timeout
284
          wrap_sample_s     <= true;
285
          if timeout_q then
286 20 arniml
            rx_done_s       <= true;
287 11 arniml
            state_s         <= IDLE;
288
          end if;
289
 
290
        end if;
291
 
292
 
293
      -- WAIT_FOR_1 -----------------------------------------------------------
294
      -- Sample counter has expired and a 0 bit has been detected.
295
      -- We must now wait for pad_data_s to become 1.
296
      -- Or abort upon timeout.
297
      when WAIT_FOR_1 =>
298
        if pad_data_s = '0' then
299 13 arniml
          if not sample_underflow_s then
300 11 arniml
            state_s       <= WAIT_FOR_1;
301
          else
302
            -- timeout while reading buttons!
303 20 arniml
            rx_done_s     <= true;
304 11 arniml
            state_s       <= IDLE;
305
          end if;
306
 
307
        else
308
          state_s         <= WAIT_FOR_0;
309
        end if;
310
 
311
      -- WAIT_FOR_0 -----------------------------------------------------------
312
      -- pad_data_s is at 1 level now and no timeout occured so far.
313
      -- We wait for the next 0 level on pad_data_s or abort upon timeout.
314
      when WAIT_FOR_0 =>
315
        -- wait for falling edge of pad data
316
        if pad_data_s = '0' then
317
          sync_sample_s       <= true;
318
 
319 20 arniml
          -- loop again in any case
320
          state_s             <= WAIT_FOR_1;
321 11 arniml
          if not all_buttons_read_s then
322
            shift_buttons_s   <= true;
323
          end if;
324
 
325
        else
326 13 arniml
          if sample_underflow_s then
327 11 arniml
            if all_buttons_read_s then
328
              -- last button was read
329
              -- so it's ok to timeout
330
              state_s         <= FINISHED;
331
            else
332
              -- timeout while reading buttons!
333 20 arniml
              rx_done_s       <= true;
334 11 arniml
              state_s         <= IDLE;
335
            end if;
336
 
337
          else
338
            state_s           <= WAIT_FOR_0;
339
 
340
          end if;
341
 
342
        end if;
343
 
344
      when FINISHED =>
345
        -- finally save buttons
346
        save_buttons_s <= true;
347 20 arniml
        rx_done_s      <= true;
348 11 arniml
 
349
      when others =>
350
        null;
351
 
352
    end case;
353
 
354
  end process fsm;
355
  --
356
  -----------------------------------------------------------------------------
357
 
358
 
359
  -----------------------------------------------------------------------------
360
  -- Output Mapping
361
  -----------------------------------------------------------------------------
362 20 arniml
  rx_done_o    <= rx_done_q;
363
  rx_data_ok_o <= save_buttons_s;
364
  rx_data_o    <= buttons_q;
365 11 arniml
 
366
 
367
end rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.