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

Subversion Repositories neorv32

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

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
  --
78
  signal ctrl : std_ulogic_vector(4 downto 0);
79
 
80
  -- access control --
81
  signal acc_en : std_ulogic; -- module access enable
82
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
83
  signal wren   : std_ulogic; -- word write enable
84
  signal rden   : std_ulogic; -- read enable
85
 
86
  -- clock generator --
87
  signal gptmr_clk_en : std_ulogic;
88
 
89
  -- timer core --
90
  type timer_t is record
91
    count  : std_ulogic_vector(31 downto 0); -- counter register
92
    thres  : std_ulogic_vector(31 downto 0); -- threshold value
93
    match  : std_ulogic; -- count == thres
94
    cnt_we : std_ulogic; -- write access to count
95
  end record;
96
  signal timer : timer_t;
97
 
98 69 zero_gravi
  -- interrupt detector --
99
  signal irq_detect : std_ulogic_vector(1 downto 0);
100 67 zero_gravi
 
101
begin
102
 
103
  -- Access Control -------------------------------------------------------------------------
104
  -- -------------------------------------------------------------------------------------------
105
  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';
106
  addr   <= gptmr_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
107
  wren   <= acc_en and wren_i;
108
  rden   <= acc_en and rden_i;
109
 
110
 
111
  -- Read/Write Access ----------------------------------------------------------------------
112
  -- -------------------------------------------------------------------------------------------
113
  rw_access: process(clk_i)
114
  begin
115
    if rising_edge(clk_i) then
116
      -- bus access acknowledge --
117
      ack_o <= rden or wren;
118
 
119
      -- write access --
120
      timer.cnt_we <= '0';
121
      if (wren = '1') then
122
        if (addr = gptmr_ctrl_addr_c) then -- control register
123
          ctrl(ctrl_en_c)    <= data_i(ctrl_en_c);
124
          ctrl(ctrl_prsc0_c) <= data_i(ctrl_prsc0_c);
125
          ctrl(ctrl_prsc1_c) <= data_i(ctrl_prsc1_c);
126
          ctrl(ctrl_prsc2_c) <= data_i(ctrl_prsc2_c);
127
          ctrl(ctrl_mode_c)  <= data_i(ctrl_mode_c);
128
        end if;
129
        if (addr = gptmr_thres_addr_c) then -- threshold register
130
          timer.thres <= data_i;
131
        end if;
132
        if (addr = gptmr_count_addr_c) then -- counter register
133
          timer.cnt_we <= '1';
134
        end if;
135
      end if;
136
 
137
      -- read access --
138
      data_o <= (others => '0');
139
      if (rden = '1') then
140
        case addr(3 downto 2) is
141
          when "00" => -- control register
142
            data_o(ctrl_en_c)    <= ctrl(ctrl_en_c);
143
            data_o(ctrl_prsc0_c) <= ctrl(ctrl_prsc0_c);
144
            data_o(ctrl_prsc1_c) <= ctrl(ctrl_prsc1_c);
145
            data_o(ctrl_prsc2_c) <= ctrl(ctrl_prsc2_c);
146
            data_o(ctrl_mode_c)  <= ctrl(ctrl_mode_c);
147
          when "01" => -- threshold register
148
            data_o <= timer.thres;
149
          when others => -- counter register
150
            data_o <= timer.count;
151
        end case;
152
      end if;
153
    end if;
154
  end process rw_access;
155
 
156
  -- clock generator enable --
157
  clkgen_en_o <= ctrl(ctrl_en_c);
158
 
159
  -- clock select --
160
  gptmr_clk_en <= clkgen_i(to_integer(unsigned(ctrl(ctrl_prsc2_c downto ctrl_prsc0_c))));
161
 
162
 
163
  -- Timer Core -----------------------------------------------------------------------------
164
  -- -------------------------------------------------------------------------------------------
165
  timer_core: process(clk_i)
166
  begin
167
    if rising_edge(clk_i) then
168
      if (timer.cnt_we = '1') then -- write access
169
        timer.count <= data_i;
170 68 zero_gravi
      elsif (ctrl(ctrl_en_c) = '1') and (gptmr_clk_en = '1') then -- enabled and clock tick
171 67 zero_gravi
        if (timer.match = '1') then
172
          if (ctrl(ctrl_mode_c) = '1') then -- reset counter if continuous mode
173
            timer.count <= (others => '0');
174
          end if;
175
        else
176
          timer.count <= std_ulogic_vector(unsigned(timer.count) + 1);
177
        end if;
178
      end if;
179
    end if;
180
  end process timer_core;
181
 
182
  -- counter = threshold? --
183
  timer.match <= '1' when (timer.count = timer.thres) else '0';
184
 
185
 
186
  -- Interrupt Generator --------------------------------------------------------------------
187
  -- -------------------------------------------------------------------------------------------
188
  irq_generator: process(clk_i)
189
  begin
190
    if rising_edge(clk_i) then
191
      if (ctrl(ctrl_en_c) = '0') then
192 69 zero_gravi
        irq_detect <= "00";
193 67 zero_gravi
      else
194 69 zero_gravi
        irq_detect <= irq_detect(0) & timer.match;
195 67 zero_gravi
      end if;
196
    end if;
197
  end process irq_generator;
198
 
199
  -- IRQ request to CPU --
200 69 zero_gravi
  irq_o <= '1' when (irq_detect = "01") else '0';
201 67 zero_gravi
 
202
 
203
end neorv32_gptmr_rtl;

powered by: WebSVN 2.1.0

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