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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_gptmr.vhd] - Blame information for rev 68

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

Line No. Rev Author Line
1 67 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - General Purpose Timer (GPTMR) >>                                                 #
3
-- # ********************************************************************************************* #
4
-- # 32-bit timer with configurable clock prescaler. The timer fires an interrupt whenever the     #
5
-- # counter register value reaches the programmed threshold value. The timer can operate in       #
6
-- # single-shot mode (count until it reaches THRESHOLD and stop) or in continuous mode (count     #
7
-- # until it reaches THRESHOLD and auto-reset).                                                   #
8
-- # ********************************************************************************************* #
9
-- # BSD 3-Clause License                                                                          #
10
-- #                                                                                               #
11
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
12
-- #                                                                                               #
13
-- # Redistribution and use in source and binary forms, with or without modification, are          #
14
-- # permitted provided that the following conditions are met:                                     #
15
-- #                                                                                               #
16
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
17
-- #    conditions and the following disclaimer.                                                   #
18
-- #                                                                                               #
19
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
20
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
21
-- #    provided with the distribution.                                                            #
22
-- #                                                                                               #
23
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
24
-- #    endorse or promote products derived from this software without specific prior written      #
25
-- #    permission.                                                                                #
26
-- #                                                                                               #
27
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
28
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
29
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
30
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
31
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
32
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
33
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
34
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
35
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
36
-- # ********************************************************************************************* #
37
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
38
-- #################################################################################################
39
 
40
library ieee;
41
use ieee.std_logic_1164.all;
42
use ieee.numeric_std.all;
43
 
44
library neorv32;
45
use neorv32.neorv32_package.all;
46
 
47
entity neorv32_gptmr is
48
  port (
49
    -- host access --
50
    clk_i       : in  std_ulogic; -- global clock line
51
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
52
    rden_i      : in  std_ulogic; -- read enable
53
    wren_i      : in  std_ulogic; -- write enable
54
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
55
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
56
    ack_o       : out std_ulogic; -- transfer acknowledge
57
    -- clock generator --
58
    clkgen_en_o : out std_ulogic; -- enable clock generator
59
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
60
    -- interrupt --
61
    irq_o       : out std_ulogic -- transmission done interrupt
62
  );
63
end neorv32_gptmr;
64
 
65
architecture neorv32_gptmr_rtl of neorv32_gptmr is
66
 
67
  -- IO space: module base address --
68
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
69
  constant lo_abb_c : natural := index_size_f(gptmr_size_c); -- low address boundary bit
70
 
71
  -- control register --
72
  constant ctrl_en_c    : natural := 0; -- r/w: timer enable
73
  constant ctrl_prsc0_c : natural := 1; -- r/w: clock prescaler select bit 0
74
  constant ctrl_prsc1_c : natural := 2; -- r/w: clock prescaler select bit 1
75
  constant ctrl_prsc2_c : natural := 3; -- r/w: clock prescaler select bit 2
76
  constant ctrl_mode_c  : natural := 4; -- r/w: mode (0=single-shot, 1=continuous)
77
  constant ctrl_alarm_c : natural := 5; -- r/c: alarm flag (interrupt), cleared by writing zero
78
  --
79
  signal ctrl : std_ulogic_vector(4 downto 0);
80
 
81
  -- access control --
82
  signal acc_en : std_ulogic; -- module access enable
83
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
84
  signal wren   : std_ulogic; -- word write enable
85
  signal rden   : std_ulogic; -- read enable
86
 
87
  -- clock generator --
88
  signal gptmr_clk_en : std_ulogic;
89
 
90
  -- timer core --
91
  type timer_t is record
92
    count  : std_ulogic_vector(31 downto 0); -- counter register
93
    thres  : std_ulogic_vector(31 downto 0); -- threshold value
94
    match  : std_ulogic; -- count == thres
95
    cnt_we : std_ulogic; -- write access to count
96
  end record;
97
  signal timer : timer_t;
98
 
99
  -- interrupt generator --
100
  type irq_t is record
101
    pending : std_ulogic; -- pending interrupt request
102
    detect  : std_ulogic_vector(1 downto 0); -- rising-edge detector
103
    clearn  : std_ulogic; -- clear/ack IRQ request, active-low
104
  end record;
105
  signal irq : irq_t;
106
 
107
begin
108
 
109
  -- Access Control -------------------------------------------------------------------------
110
  -- -------------------------------------------------------------------------------------------
111
  acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = gptmr_base_c(hi_abb_c downto lo_abb_c)) else '0';
112
  addr   <= gptmr_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
113
  wren   <= acc_en and wren_i;
114
  rden   <= acc_en and rden_i;
115
 
116
 
117
  -- Read/Write Access ----------------------------------------------------------------------
118
  -- -------------------------------------------------------------------------------------------
119
  rw_access: process(clk_i)
120
  begin
121
    if rising_edge(clk_i) then
122
      -- bus access acknowledge --
123
      ack_o <= rden or wren;
124
 
125
      -- write access --
126
      irq.clearn   <= '1';
127
      timer.cnt_we <= '0';
128
      if (wren = '1') then
129
        if (addr = gptmr_ctrl_addr_c) then -- control register
130
          ctrl(ctrl_en_c)    <= data_i(ctrl_en_c);
131
          ctrl(ctrl_prsc0_c) <= data_i(ctrl_prsc0_c);
132
          ctrl(ctrl_prsc1_c) <= data_i(ctrl_prsc1_c);
133
          ctrl(ctrl_prsc2_c) <= data_i(ctrl_prsc2_c);
134
          ctrl(ctrl_mode_c)  <= data_i(ctrl_mode_c);
135
          irq.clearn         <= data_i(ctrl_alarm_c);
136
        end if;
137
        if (addr = gptmr_thres_addr_c) then -- threshold register
138
          timer.thres <= data_i;
139
        end if;
140
        if (addr = gptmr_count_addr_c) then -- counter register
141
          timer.cnt_we <= '1';
142
        end if;
143
      end if;
144
 
145
      -- read access --
146
      data_o <= (others => '0');
147
      if (rden = '1') then
148
        case addr(3 downto 2) is
149
          when "00" => -- control register
150
            data_o(ctrl_en_c)    <= ctrl(ctrl_en_c);
151
            data_o(ctrl_prsc0_c) <= ctrl(ctrl_prsc0_c);
152
            data_o(ctrl_prsc1_c) <= ctrl(ctrl_prsc1_c);
153
            data_o(ctrl_prsc2_c) <= ctrl(ctrl_prsc2_c);
154
            data_o(ctrl_mode_c)  <= ctrl(ctrl_mode_c);
155
            data_o(ctrl_alarm_c) <= irq.pending;
156
          when "01" => -- threshold register
157
            data_o <= timer.thres;
158
          when others => -- counter register
159
            data_o <= timer.count;
160
        end case;
161
      end if;
162
    end if;
163
  end process rw_access;
164
 
165
  -- clock generator enable --
166
  clkgen_en_o <= ctrl(ctrl_en_c);
167
 
168
  -- clock select --
169
  gptmr_clk_en <= clkgen_i(to_integer(unsigned(ctrl(ctrl_prsc2_c downto ctrl_prsc0_c))));
170
 
171
 
172
  -- Timer Core -----------------------------------------------------------------------------
173
  -- -------------------------------------------------------------------------------------------
174
  timer_core: process(clk_i)
175
  begin
176
    if rising_edge(clk_i) then
177
      if (timer.cnt_we = '1') then -- write access
178
        timer.count <= data_i;
179 68 zero_gravi
      elsif (ctrl(ctrl_en_c) = '1') and (gptmr_clk_en = '1') then -- enabled and clock tick
180 67 zero_gravi
        if (timer.match = '1') then
181
          if (ctrl(ctrl_mode_c) = '1') then -- reset counter if continuous mode
182
            timer.count <= (others => '0');
183
          end if;
184
        else
185
          timer.count <= std_ulogic_vector(unsigned(timer.count) + 1);
186
        end if;
187
      end if;
188
    end if;
189
  end process timer_core;
190
 
191
  -- counter = threshold? --
192
  timer.match <= '1' when (timer.count = timer.thres) else '0';
193
 
194
 
195
  -- Interrupt Generator --------------------------------------------------------------------
196
  -- -------------------------------------------------------------------------------------------
197
  irq_generator: process(clk_i)
198
  begin
199
    if rising_edge(clk_i) then
200
      if (ctrl(ctrl_en_c) = '0') then
201
        irq.detect  <= "00";
202
        irq.pending <= '0';
203
      else
204
        irq.detect <= irq.detect(0) & timer.match;
205
        if (irq.detect = "01") then -- rising edge
206
          irq.pending <= '1';
207
        elsif (irq.clearn = '0') then
208
          irq.pending <= '0';
209
        end if;
210
      end if;
211
    end if;
212
  end process irq_generator;
213
 
214
  -- IRQ request to CPU --
215
  irq_o <= irq.pending;
216
 
217
 
218
end neorv32_gptmr_rtl;

powered by: WebSVN 2.1.0

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