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

Subversion Repositories t48

[/] [t48/] [tags/] [rel_0_3_beta/] [rtl/] [vhdl/] [timer.vhd] - Blame information for rev 292

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 arniml
-------------------------------------------------------------------------------
2
--
3
-- The Timer/Counter unit.
4
--
5 129 arniml
-- $Id: timer.vhd,v 1.5 2004-07-11 16:51:33 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
entity timer is
53
 
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
end timer;
79
 
80
 
81
library ieee;
82
use ieee.numeric_std.all;
83
 
84
use work.t48_pack.all;
85
 
86
architecture rtl of timer is
87
 
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
 
192
    elsif clk_i'event and clk_i = clk_active_c then
193
      if en_clk_i then
194
 
195
        -- Counter Core and overflow ------------------------------------------
196
        overflow_q     <= false;
197
 
198
        if write_timer_i then
199
          counter_q    <= unsigned(data_i);
200
 
201
        elsif increment_s then
202
          counter_q    <= counter_q + 1;
203
 
204
          if counter_q = 255 then
205
            overflow_q <= true;
206
          end if;
207
 
208
        end if;
209
 
210
        -- T1 edge detector ---------------------------------------------------
211
        if (sample_t1_state_g = 3 and clk_mstate_i = MSTATE3) or
212 59 arniml
           (sample_t1_state_g = 4 and clk_mstate_i = MSTATE4) then
213 4 arniml
          t1_q <= t1_i;
214
        end if;
215
 
216
        -- Prescaler ----------------------------------------------------------
217
        if start_t_i then
218
          prescaler_q  <= (others => '0');
219
 
220
        elsif clk_mstate_i = MSTATE3 then
221
          prescaler_q  <= prescaler_q + 1;
222
 
223
        end if;
224
 
225
        -- Increment Selector -------------------------------------------------
226
        if start_t_i then
227
          inc_sel_q <= TIMER;
228
        elsif start_cnt_i then
229
          inc_sel_q <= COUNTER;
230
        elsif stop_tcnt_i then
231
          inc_sel_q <= NONE;
232
        end if;
233
 
234
      end if;
235
 
236
    end if;
237
 
238
  end process regs;
239
  --
240
  -----------------------------------------------------------------------------
241
 
242
 
243
  -----------------------------------------------------------------------------
244
  -- Output Mapping.
245
  -----------------------------------------------------------------------------
246
  data_o     <=   std_logic_vector(counter_q)
247
                when read_timer_i else
248
                  (others => bus_idle_level_c);
249
  overflow_o <= to_stdLogic(overflow_q);
250
 
251
end rtl;
252
 
253
 
254
-------------------------------------------------------------------------------
255
-- File History:
256
--
257
-- $Log: not supported by cvs2svn $
258 129 arniml
-- Revision 1.4  2004/07/04 13:06:45  arniml
259
-- counter_q is not cleared during reset
260
-- this would match all different descriptions of the Counter as
261
-- a) if the software assumes that the Counter is modified during reset, it
262
--    will initialize the Counter anyhow
263
-- b) the special case 'Counter not modified during reset' is covered
264
--
265 128 arniml
-- Revision 1.3  2004/05/16 15:32:57  arniml
266
-- fix edge detector bug for counter
267
--
268 91 arniml
-- Revision 1.2  2004/04/15 22:05:13  arniml
269
-- increment prescaler with MSTATE4
270
--
271 59 arniml
-- Revision 1.1  2004/03/23 21:31:53  arniml
272
-- initial check-in
273 4 arniml
--
274 59 arniml
--
275 4 arniml
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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