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

Subversion Repositories t48

[/] [t48/] [tags/] [rel_1_0/] [rtl/] [vhdl/] [timer.vhd] - Blame information for rev 59

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 arniml
-------------------------------------------------------------------------------
2
--
3
-- The Timer/Counter unit.
4
--
5 59 arniml
-- $Id: timer.vhd,v 1.2 2004-04-15 22:05:13 arniml Exp $
6 4 arniml
--
7
-- All rights reserved
8
--
9
-- Redistribution and use in source and synthezised forms, with or without
10
-- modification, are permitted provided that the following conditions are met:
11
--
12
-- Redistributions of source code must retain the above copyright notice,
13
-- this list of conditions and the following disclaimer.
14
--
15
-- Redistributions in synthesized form must reproduce the above copyright
16
-- notice, this list of conditions and the following disclaimer in the
17
-- documentation and/or other materials provided with the distribution.
18
--
19
-- Neither the name of the author nor the names of other contributors may
20
-- be used to endorse or promote products derived from this software without
21
-- specific prior written permission.
22
--
23
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
27
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
-- POSSIBILITY OF SUCH DAMAGE.
34
--
35
-- Please report bugs to the author, but before you do so, please
36
-- make sure that this is not a derivative work and that
37
-- you have the latest version of this file.
38
--
39
-- The latest version of this file can be found at:
40
--      http://www.opencores.org/cvsweb.shtml/t48/
41
--
42
-------------------------------------------------------------------------------
43
 
44
library ieee;
45
use ieee.std_logic_1164.all;
46
 
47
use work.t48_pack.word_t;
48
use work.t48_pack.mstate_t;
49
 
50
entity timer is
51
 
52
  generic (
53
    -- state in which T1 is sampled (3 or 4)
54
    sample_t1_state_g : integer := 4
55
  );
56
 
57
  port (
58
    -- Global Interface -------------------------------------------------------
59
    clk_i         : in  std_logic;
60
    res_i         : in  std_logic;
61
    en_clk_i      : in  boolean;
62
    t1_i          : in  std_logic;
63
    clk_mstate_i  : in  mstate_t;
64
    -- T48 Bus Interface ------------------------------------------------------
65
    data_i        : in  word_t;
66
    data_o        : out word_t;
67
    read_timer_i  : in  boolean;
68
    write_timer_i : in  boolean;
69
    -- Decoder Interface ------------------------------------------------------
70
    start_t_i     : in  boolean;
71
    start_cnt_i   : in  boolean;
72
    stop_tcnt_i   : in  boolean;
73
    overflow_o    : out std_logic
74
  );
75
 
76
end timer;
77
 
78
 
79
library ieee;
80
use ieee.numeric_std.all;
81
 
82
use work.t48_pack.all;
83
 
84
architecture rtl of timer is
85
 
86
  -- the 8 bit counter core
87
  signal counter_q   : unsigned(word_t'range);
88
  signal overflow_q  : boolean;
89
 
90
  -- increment signal for the counter core
91
  type   inc_type_t is (NONE, TIMER, COUNTER);
92
  signal increment_s : boolean;
93
  signal inc_sel_q   : inc_type_t;
94
 
95
  -- T1 edge detector
96
  signal t1_q        : std_logic;
97
  signal t1_inc_s    : boolean;
98
 
99
  -- timer prescaler
100
  signal prescaler_q : unsigned(4 downto 0);
101
  signal pre_inc_s   : boolean;
102
 
103
begin
104
 
105
  -----------------------------------------------------------------------------
106
  -- Verify the generics
107
  -----------------------------------------------------------------------------
108
 
109
  -- pragma translate_off
110
  assert (sample_t1_state_g = 3) or (sample_t1_state_g = 4)
111
    report "sample_t1_state_g must be either 3 or 4!"
112
    severity failure;
113
  -- pragma translate_on
114
 
115
 
116
  -----------------------------------------------------------------------------
117
  -- Process t1_edge
118
  --
119
  -- Purpose:
120
  --   Implements the edge detector for T1.
121
  --
122
  t1_edge: process (t1_i,
123
                    t1_q,
124
                    clk_mstate_i)
125
  begin
126
    t1_inc_s     <= false;
127
 
128
    -- sample in state according to generic
129
    -- Old devices: sample at the beginning of state 3
130
    -- New devices: sample in state 4
131
    if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
132
       (sample_t1_state_g = 4 and clk_mstate_i = MSTATE3) then
133
      -- detect falling edge
134
      if t1_q = '1' and t1_i = '0' then
135
        t1_inc_s <= true;
136
      end if;
137
    end if;
138
 
139
  end process t1_edge;
140
  --
141
  -----------------------------------------------------------------------------
142
 
143
 
144 59 arniml
  pre_inc_s <= clk_mstate_i = MSTATE4 and prescaler_q = 31;
145 4 arniml
 
146
 
147
  -----------------------------------------------------------------------------
148
  -- Process inc_sel
149
  --
150
  -- Purpose:
151
  --   Select increment source (timer, counter or none).
152
  --
153
  inc_sel: process (inc_sel_q,
154
                    pre_inc_s,
155
                    t1_inc_s)
156
  begin
157
    -- default assignment
158
    increment_s     <= false;
159
 
160
    case inc_sel_q is
161
      when NONE =>
162
        increment_s <= false;
163
      when TIMER =>
164
        increment_s <= pre_inc_s;
165
      when COUNTER =>
166
        increment_s <= t1_inc_s;
167
      when others =>
168
        null;
169
    end case;
170
 
171
  end process inc_sel;
172
  --
173
  -----------------------------------------------------------------------------
174
 
175
 
176
  -----------------------------------------------------------------------------
177
  -- Process regs
178
  --
179
  -- Purpose:
180
  --   Implements the counter, the prescaler and other registers.
181
  --
182
  regs: process (res_i, clk_i)
183
  begin
184
    if res_i = res_active_c then
185
      counter_q      <= (others => '0');
186
      overflow_q     <= false;
187
      t1_q           <= '0';
188
      prescaler_q    <= (others => '0');
189
      inc_sel_q      <= NONE;
190
 
191
    elsif clk_i'event and clk_i = clk_active_c then
192
      if en_clk_i then
193
 
194
        -- Counter Core and overflow ------------------------------------------
195
        overflow_q     <= false;
196
 
197
        if write_timer_i then
198
          counter_q    <= unsigned(data_i);
199
 
200
        elsif increment_s then
201
          counter_q    <= counter_q + 1;
202
 
203
          if counter_q = 255 then
204
            overflow_q <= true;
205
          end if;
206
 
207
        end if;
208
 
209
        -- T1 edge detector ---------------------------------------------------
210
        if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
211 59 arniml
           (sample_t1_state_g = 4 and clk_mstate_i = MSTATE4) then
212 4 arniml
          t1_q <= t1_i;
213
        end if;
214
 
215
        -- Prescaler ----------------------------------------------------------
216
        if start_t_i then
217
          prescaler_q  <= (others => '0');
218
 
219
        elsif clk_mstate_i = MSTATE3 then
220
          prescaler_q  <= prescaler_q + 1;
221
 
222
        end if;
223
 
224
        -- Increment Selector -------------------------------------------------
225
        if start_t_i then
226
          inc_sel_q <= TIMER;
227
        elsif start_cnt_i then
228
          inc_sel_q <= COUNTER;
229
        elsif stop_tcnt_i then
230
          inc_sel_q <= NONE;
231
        end if;
232
 
233
      end if;
234
 
235
    end if;
236
 
237
  end process regs;
238
  --
239
  -----------------------------------------------------------------------------
240
 
241
 
242
  -----------------------------------------------------------------------------
243
  -- Output Mapping.
244
  -----------------------------------------------------------------------------
245
  data_o     <=   std_logic_vector(counter_q)
246
                when read_timer_i else
247
                  (others => bus_idle_level_c);
248
  overflow_o <= to_stdLogic(overflow_q);
249
 
250
end rtl;
251
 
252
 
253
-------------------------------------------------------------------------------
254
-- File History:
255
--
256
-- $Log: not supported by cvs2svn $
257 59 arniml
-- Revision 1.1  2004/03/23 21:31:53  arniml
258
-- initial check-in
259 4 arniml
--
260 59 arniml
--
261 4 arniml
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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