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

Subversion Repositories gamepads

[/] [gamepads/] [trunk/] [gcpad/] [rtl/] [vhdl/] [gcpad_tx.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_tx.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
 
52
entity gcpad_tx is
53
 
54
  generic (
55
    reset_level_g    :     natural := 0;
56
    clocks_per_1us_g :     natural := 2
57
  );
58
  port (
59
    -- System Interface -------------------------------------------------------
60
    clk_i            : in  std_logic;
61
    reset_i          : in  std_logic;
62
    -- Pad Interface ----------------------------------------------------------
63
    pad_data_o       : out std_logic;
64
    -- Control Interface ------------------------------------------------------
65
    tx_start_i       : in  boolean;
66
    tx_finished_o    : out boolean;
67 12 arniml
    tx_size_i        : in  std_logic_vector( 1 downto 0);
68 11 arniml
    tx_command_i     : in  std_logic_vector(23 downto 0)
69
  );
70
 
71
end gcpad_tx;
72
 
73
 
74
library ieee;
75
use ieee.numeric_std.all;
76
 
77
use work.gcpad_pack.all;
78
 
79
architecture rtl of gcpad_tx is
80
 
81
  subtype  command_t is std_logic_vector(24 downto 0);
82
 
83
  signal   command_q      : command_t;
84
  signal   load_command_s : boolean;
85
  signal   shift_bits_s   : boolean;
86
 
87
  constant cnt_long_c       : natural := clocks_per_1us_g * 4 - 1;
88
  constant cnt_short_c      : natural := clocks_per_1us_g * 1 - 1;
89
  subtype  cnt_t            is natural range 0 to cnt_long_c;
90
  signal   cnt_q            : cnt_t;
91
  signal   cnt_load_long_s  : boolean;
92
  signal   cnt_load_short_s : boolean;
93
  signal   cnt_finished_s   : boolean;
94
 
95
  subtype  num_bits_t      is unsigned(4 downto 0);
96
  signal   num_bits_q      : num_bits_t;
97
  signal   all_bits_sent_s : boolean;
98
  signal   cnt_bit_s       : boolean;
99
 
100
  type     state_t is (IDLE,
101
                       LOAD_COMMAND,
102
                       SEND_COMMAND_PHASE1,
103
                       SEND_COMMAND_PHASE2);
104
  signal   state_s,
105
           state_q  : state_t;
106
 
107
  signal   pad_data_s,
108
           pad_data_q  : std_logic;
109
 
110
  signal   tx_finished_s,
111
           tx_finished_q  : boolean;
112
 
113
begin
114
 
115
  -----------------------------------------------------------------------------
116
  -- Process seq
117
  --
118
  -- Purpose:
119
  --   Implements the sequential elements of this module.
120
  --
121
  seq: process (reset_i, clk_i)
122 12 arniml
    variable size_v : std_logic_vector(num_bits_t'range);
123 11 arniml
  begin
124
    if reset_i = reset_level_g then
125
      command_q     <= (others => '1');
126
      cnt_q         <= cnt_long_c;
127
      num_bits_q    <= (others => '0');
128
      pad_data_q    <= '1';
129
      state_q       <= IDLE;
130
      tx_finished_q <= false;
131
 
132
    elsif clk_i'event and clk_i = '1' then
133
      tx_finished_q <= tx_finished_s;
134
 
135
      -- fsm
136
      state_q    <= state_s;
137
 
138
      -- command register and bit counter
139
      if load_command_s then
140
        command_q(24 downto 1)  <= tx_command_i;
141
        command_q(0)            <= '1';
142
 
143 12 arniml
        -- workaround for GHDL concatenation
144
        size_v(num_bits_t'high downto 3) := tx_size_i;
145
        size_v(2 downto 0)               := (others => '0');
146
        num_bits_q              <= unsigned(size_v) + 1;
147
 
148 11 arniml
      else
149
        if shift_bits_s then
150
          command_q(command_t'high downto 1) <= command_q(command_t'high-1 downto 0);
151
        end if;
152
 
153
        if cnt_bit_s and not all_bits_sent_s then
154
          num_bits_q <= num_bits_q - 1;
155
        end if;
156
 
157
      end if;
158
 
159
 
160
      -- PWM counter
161
      if cnt_load_long_s then
162
        cnt_q   <= cnt_long_c;
163
      elsif cnt_load_short_s then
164
        cnt_q   <= cnt_short_c;
165
      else
166
        if not cnt_finished_s then
167
          cnt_q <= cnt_q - 1;
168
        end if;
169
      end if;
170
 
171
      -- PWM output = pad data
172
      pad_data_q <= pad_data_s;
173
 
174
    end if;
175
 
176
  end process seq;
177
  --
178
  -----------------------------------------------------------------------------
179
 
180
  -- indicates that PWM counter has finished
181
  cnt_finished_s  <= cnt_q = 0;
182
  -- indicates that all bits have been sent
183
  all_bits_sent_s <= num_bits_q = 0;
184
 
185
 
186
  -----------------------------------------------------------------------------
187
  -- Process fsm
188
  --
189
  -- Purpose:
190
  --   Models the controlling state machine.
191
  --
192
  fsm: process (state_q,
193
                cnt_finished_s,
194
                all_bits_sent_s,
195
                tx_start_i,
196
                command_q)
197
  begin
198
    -- defaul assignments
199
    state_s          <= IDLE;
200
    shift_bits_s     <= false;
201
    cnt_load_long_s  <= false;
202
    cnt_load_short_s <= false;
203
    pad_data_s       <= '1';
204
    tx_finished_s    <= false;
205
    load_command_s   <= false;
206
    cnt_bit_s        <= false;
207
 
208
    case state_q is
209
      -- IDLE -----------------------------------------------------------------
210
      -- The idle state.
211
      -- Advances when the transmitter is started
212
      when IDLE =>
213
        if tx_start_i then
214
          state_s <= LOAD_COMMAND;
215
        else
216
          state_s <= IDLE;
217
        end if;
218
 
219
 
220
      -- LOAD_COMMAND ---------------------------------------------------------
221
      -- Prepares the first and all subsequent low phases on pad_data_s.
222
      when LOAD_COMMAND =>
223
        state_s          <= SEND_COMMAND_PHASE2;
224
        load_command_s   <= true;
225
 
226
        -- start counter once to kick the loop
227
        cnt_load_short_s <= true;
228
 
229
 
230
      -- SEND_COMMAND_PHASE1 --------------------------------------------------
231
      -- Wait for completion of phase 1, the low phase of pad_data_s.
232
      -- The high phase is prepared when the PWM counter has expired.
233
      when SEND_COMMAND_PHASE1 =>
234
        state_s              <= SEND_COMMAND_PHASE1;
235
        pad_data_s           <= '0';
236
 
237
        if cnt_finished_s then
238
          -- initiate high phase
239
          pad_data_s         <= '1';
240
          if command_q(command_t'high) = '1' then
241
            cnt_load_long_s  <= true;
242
          else
243
            cnt_load_short_s <= true;
244
          end if;
245
 
246
          state_s            <= SEND_COMMAND_PHASE2;
247
          -- provide next bit
248
          shift_bits_s       <= true;
249
 
250
        end if;
251
 
252
      -- SEND_COMMAND_PHASE2 --------------------------------------------------
253
      -- Wait for completion of phase 2, the high phase of pad_data_s.
254
      -- The next low phase is prepared when the PWM counter has expired.
255
      -- In case all bits have been sent, the tx handshake is asserted and
256
      -- the FSM returns to IDLE state.
257
      when SEND_COMMAND_PHASE2 =>
258
        pad_data_s        <= '1';
259
        state_s           <= SEND_COMMAND_PHASE2;
260
 
261
        if cnt_finished_s then
262
          if not all_bits_sent_s then
263
            -- more bits to send so loop
264
 
265
            -- prepare low phase
266
            if command_q(command_t'high) = '1' then
267
              cnt_load_short_s <= true;
268
            else
269
              cnt_load_long_s  <= true;
270
            end if;
271
 
272
            -- decrement bit counter
273
            cnt_bit_s     <= true;
274
 
275
            state_s       <= SEND_COMMAND_PHASE1;
276
 
277
          else
278
            -- all bits sent, we're finished
279
            tx_finished_s <= true;
280
            state_s       <= IDLE;
281
 
282
          end if;
283
 
284
        end if;
285
 
286
      when others =>
287
        null;
288
 
289
    end case;
290
 
291
  end process fsm;
292
  --
293
  -----------------------------------------------------------------------------
294
 
295
 
296
  -----------------------------------------------------------------------------
297
  -- Output mapping
298
  -----------------------------------------------------------------------------
299
  tx_finished_o <= tx_finished_q;
300
  pad_data_o    <= pad_data_q;
301
 
302
end rtl;

powered by: WebSVN 2.1.0

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