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 277

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 273 arniml
-- $Id: timer.vhd,v 1.7 2006-11-30 14:31:59 arniml Exp $
6 4 arniml
--
7 129 arniml
-- Copyright (c) 2004, Arnim Laeuger (arniml@opencores.org)
8
--
9 4 arniml
-- 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/t48/
43
--
44
-------------------------------------------------------------------------------
45
 
46
library ieee;
47
use ieee.std_logic_1164.all;
48
 
49
use work.t48_pack.word_t;
50
use work.t48_pack.mstate_t;
51
 
52 179 arniml
entity t48_timer is
53 4 arniml
 
54
  generic (
55
    -- state in which T1 is sampled (3 or 4)
56
    sample_t1_state_g : integer := 4
57
  );
58
 
59
  port (
60
    -- Global Interface -------------------------------------------------------
61
    clk_i         : in  std_logic;
62
    res_i         : in  std_logic;
63
    en_clk_i      : in  boolean;
64
    t1_i          : in  std_logic;
65
    clk_mstate_i  : in  mstate_t;
66
    -- T48 Bus Interface ------------------------------------------------------
67
    data_i        : in  word_t;
68
    data_o        : out word_t;
69
    read_timer_i  : in  boolean;
70
    write_timer_i : in  boolean;
71
    -- Decoder Interface ------------------------------------------------------
72
    start_t_i     : in  boolean;
73
    start_cnt_i   : in  boolean;
74
    stop_tcnt_i   : in  boolean;
75
    overflow_o    : out std_logic
76
  );
77
 
78 179 arniml
end t48_timer;
79 4 arniml
 
80
 
81
library ieee;
82
use ieee.numeric_std.all;
83
 
84
use work.t48_pack.all;
85
 
86 179 arniml
architecture rtl of t48_timer is
87 4 arniml
 
88
  -- the 8 bit counter core
89
  signal counter_q   : unsigned(word_t'range);
90
  signal overflow_q  : boolean;
91
 
92
  -- increment signal for the counter core
93
  type   inc_type_t is (NONE, TIMER, COUNTER);
94
  signal increment_s : boolean;
95
  signal inc_sel_q   : inc_type_t;
96
 
97
  -- T1 edge detector
98
  signal t1_q        : std_logic;
99
  signal t1_inc_s    : boolean;
100
 
101
  -- timer prescaler
102
  signal prescaler_q : unsigned(4 downto 0);
103
  signal pre_inc_s   : boolean;
104
 
105
begin
106
 
107
  -----------------------------------------------------------------------------
108
  -- Verify the generics
109
  -----------------------------------------------------------------------------
110
 
111
  -- pragma translate_off
112
  assert (sample_t1_state_g = 3) or (sample_t1_state_g = 4)
113
    report "sample_t1_state_g must be either 3 or 4!"
114
    severity failure;
115
  -- pragma translate_on
116
 
117
 
118
  -----------------------------------------------------------------------------
119
  -- Process t1_edge
120
  --
121
  -- Purpose:
122
  --   Implements the edge detector for T1.
123
  --
124
  t1_edge: process (t1_i,
125
                    t1_q,
126
                    clk_mstate_i)
127
  begin
128
    t1_inc_s     <= false;
129
 
130
    -- sample in state according to generic
131
    -- Old devices: sample at the beginning of state 3
132
    -- New devices: sample in state 4
133
    if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
134 91 arniml
       (sample_t1_state_g = 4 and clk_mstate_i = MSTATE4) then
135 4 arniml
      -- detect falling edge
136
      if t1_q = '1' and t1_i = '0' then
137
        t1_inc_s <= true;
138
      end if;
139
    end if;
140
 
141
  end process t1_edge;
142
  --
143
  -----------------------------------------------------------------------------
144
 
145
 
146 59 arniml
  pre_inc_s <= clk_mstate_i = MSTATE4 and prescaler_q = 31;
147 4 arniml
 
148
 
149
  -----------------------------------------------------------------------------
150
  -- Process inc_sel
151
  --
152
  -- Purpose:
153
  --   Select increment source (timer, counter or none).
154
  --
155
  inc_sel: process (inc_sel_q,
156
                    pre_inc_s,
157
                    t1_inc_s)
158
  begin
159
    -- default assignment
160
    increment_s     <= false;
161
 
162
    case inc_sel_q is
163
      when NONE =>
164
        increment_s <= false;
165
      when TIMER =>
166
        increment_s <= pre_inc_s;
167
      when COUNTER =>
168
        increment_s <= t1_inc_s;
169
      when others =>
170
        null;
171
    end case;
172
 
173
  end process inc_sel;
174
  --
175
  -----------------------------------------------------------------------------
176
 
177
 
178
  -----------------------------------------------------------------------------
179
  -- Process regs
180
  --
181
  -- Purpose:
182
  --   Implements the counter, the prescaler and other registers.
183
  --
184
  regs: process (res_i, clk_i)
185
  begin
186
    if res_i = res_active_c then
187
      overflow_q     <= false;
188
      t1_q           <= '0';
189
      prescaler_q    <= (others => '0');
190
      inc_sel_q      <= NONE;
191 273 arniml
      counter_q      <= (others => '0');
192 4 arniml
 
193
    elsif clk_i'event and clk_i = clk_active_c then
194
      if en_clk_i then
195
 
196
        -- Counter Core and overflow ------------------------------------------
197
        overflow_q     <= false;
198
 
199
        if write_timer_i then
200
          counter_q    <= unsigned(data_i);
201
 
202
        elsif increment_s then
203
          counter_q    <= counter_q + 1;
204
 
205
          if counter_q = 255 then
206
            overflow_q <= true;
207
          end if;
208
 
209
        end if;
210
 
211
        -- T1 edge detector ---------------------------------------------------
212
        if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
213 59 arniml
           (sample_t1_state_g = 4 and clk_mstate_i = MSTATE4) then
214 4 arniml
          t1_q <= t1_i;
215
        end if;
216
 
217
        -- Prescaler ----------------------------------------------------------
218
        if start_t_i then
219
          prescaler_q  <= (others => '0');
220
 
221
        elsif clk_mstate_i = MSTATE3 then
222
          prescaler_q  <= prescaler_q + 1;
223
 
224
        end if;
225
 
226
        -- Increment Selector -------------------------------------------------
227
        if start_t_i then
228
          inc_sel_q <= TIMER;
229
        elsif start_cnt_i then
230
          inc_sel_q <= COUNTER;
231
        elsif stop_tcnt_i then
232
          inc_sel_q <= NONE;
233
        end if;
234
 
235
      end if;
236
 
237
    end if;
238
 
239
  end process regs;
240
  --
241
  -----------------------------------------------------------------------------
242
 
243
 
244
  -----------------------------------------------------------------------------
245
  -- Output Mapping.
246
  -----------------------------------------------------------------------------
247
  data_o     <=   std_logic_vector(counter_q)
248
                when read_timer_i else
249
                  (others => bus_idle_level_c);
250
  overflow_o <= to_stdLogic(overflow_q);
251
 
252
end rtl;
253
 
254
 
255
-------------------------------------------------------------------------------
256
-- File History:
257
--
258
-- $Log: not supported by cvs2svn $
259 273 arniml
-- Revision 1.6  2005/06/11 10:08:43  arniml
260
-- introduce prefix 't48_' for all packages, entities and configurations
261
--
262 179 arniml
-- Revision 1.5  2004/07/11 16:51:33  arniml
263
-- cleanup copyright notice
264
--
265 129 arniml
-- Revision 1.4  2004/07/04 13:06:45  arniml
266
-- counter_q is not cleared during reset
267
-- this would match all different descriptions of the Counter as
268
-- a) if the software assumes that the Counter is modified during reset, it
269
--    will initialize the Counter anyhow
270
-- b) the special case 'Counter not modified during reset' is covered
271
--
272 128 arniml
-- Revision 1.3  2004/05/16 15:32:57  arniml
273
-- fix edge detector bug for counter
274
--
275 91 arniml
-- Revision 1.2  2004/04/15 22:05:13  arniml
276
-- increment prescaler with MSTATE4
277
--
278 59 arniml
-- Revision 1.1  2004/03/23 21:31:53  arniml
279
-- initial check-in
280 4 arniml
--
281
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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