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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_xirq.vhd] - Blame information for rev 74

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 61 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - External Interrupt Controller (XIRQ) >>                                          #
3
-- # ********************************************************************************************* #
4
-- # Simple interrupt controller for platform (processor-external) interrupts. Up to 32 channels   #
5 62 zero_gravi
-- # are supported that get (optionally) prioritized into a single CPU interrupt.                  #
6 61 zero_gravi
-- #                                                                                               #
7 66 zero_gravi
-- # The actual trigger configuration has to be done BEFORE synthesis using the XIRQ_TRIGGER_TYPE  #
8 61 zero_gravi
-- # and XIRQ_TRIGGER_POLARITY generics. These allow to configure channel-independent low-level,   #
9
-- # high-level, falling-edge and rising-edge triggers.                                            #
10
-- # ********************************************************************************************* #
11
-- # BSD 3-Clause License                                                                          #
12
-- #                                                                                               #
13 74 zero_gravi
-- # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
14 61 zero_gravi
-- #                                                                                               #
15
-- # Redistribution and use in source and binary forms, with or without modification, are          #
16
-- # permitted provided that the following conditions are met:                                     #
17
-- #                                                                                               #
18
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
19
-- #    conditions and the following disclaimer.                                                   #
20
-- #                                                                                               #
21
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
22
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
23
-- #    provided with the distribution.                                                            #
24
-- #                                                                                               #
25
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
26
-- #    endorse or promote products derived from this software without specific prior written      #
27
-- #    permission.                                                                                #
28
-- #                                                                                               #
29
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
30
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
31
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
32
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
33
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
34
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
35
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
36
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
37
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
38
-- # ********************************************************************************************* #
39
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
40
-- #################################################################################################
41
 
42
library ieee;
43
use ieee.std_logic_1164.all;
44
use ieee.numeric_std.all;
45
 
46
library neorv32;
47
use neorv32.neorv32_package.all;
48
 
49
entity neorv32_xirq is
50
  generic (
51 62 zero_gravi
    XIRQ_NUM_CH           : natural; -- number of external IRQ channels (0..32)
52
    XIRQ_TRIGGER_TYPE     : std_ulogic_vector(31 downto 0); -- trigger type: 0=level, 1=edge
53
    XIRQ_TRIGGER_POLARITY : std_ulogic_vector(31 downto 0)  -- trigger polarity: 0=low-level/falling-edge, 1=high-level/rising-edge
54 61 zero_gravi
  );
55
  port (
56
    -- host access --
57
    clk_i     : in  std_ulogic; -- global clock line
58
    addr_i    : in  std_ulogic_vector(31 downto 0); -- address
59
    rden_i    : in  std_ulogic; -- read enable
60
    wren_i    : in  std_ulogic; -- write enable
61
    data_i    : in  std_ulogic_vector(31 downto 0); -- data in
62
    data_o    : out std_ulogic_vector(31 downto 0); -- data out
63
    ack_o     : out std_ulogic; -- transfer acknowledge
64
    -- external interrupt lines --
65 70 zero_gravi
    xirq_i    : in  std_ulogic_vector(31 downto 0);
66 61 zero_gravi
    -- CPU interrupt --
67
    cpu_irq_o : out std_ulogic
68
  );
69
end neorv32_xirq;
70
 
71
architecture neorv32_xirq_rtl of neorv32_xirq is
72
 
73
  -- IO space: module base address --
74
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
75
  constant lo_abb_c : natural := index_size_f(xirq_size_c); -- low address boundary bit
76
 
77
  -- access control --
78
  signal acc_en : std_ulogic; -- module access enable
79
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
80 66 zero_gravi
  signal wren   : std_ulogic; -- word write enable
81
  signal rden   : std_ulogic; -- read enable
82 61 zero_gravi
 
83
  -- control registers --
84
  signal irq_enable  : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0); -- r/w: interrupt enable
85 64 zero_gravi
  signal clr_pending : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0); -- r/w: clear pending IRQs
86
  signal irq_src     : std_ulogic_vector(4 downto 0); -- r/w: source IRQ, ACK on any write
87 61 zero_gravi
 
88
  -- interrupt trigger --
89
  signal irq_sync  : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
90
  signal irq_sync2 : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
91
  signal irq_trig  : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
92
 
93
  -- interrupt buffer --
94
  signal irq_buf  : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
95
  signal irq_fire : std_ulogic;
96
 
97
  -- interrupt source --
98 64 zero_gravi
  signal irq_src_nxt : std_ulogic_vector(4 downto 0);
99 61 zero_gravi
 
100
  -- arbiter --
101 66 zero_gravi
  signal irq_run : std_ulogic;
102 61 zero_gravi
 
103
begin
104
 
105
  -- Sanity Checks --------------------------------------------------------------------------
106
  -- -------------------------------------------------------------------------------------------
107
  assert not ((XIRQ_NUM_CH < 0) or (XIRQ_NUM_CH > 32)) report "NEORV32 PROCESSOR CONFIG ERROR: Number of XIRQ inputs <XIRQ_NUM_CH> has to be 0..32." severity error;
108
 
109
 
110
  -- Access Control -------------------------------------------------------------------------
111
  -- -------------------------------------------------------------------------------------------
112
  acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = xirq_base_c(hi_abb_c downto lo_abb_c)) else '0';
113
  addr   <= xirq_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
114 66 zero_gravi
  wren   <= acc_en and wren_i;
115
  rden   <= acc_en and rden_i;
116 61 zero_gravi
 
117
 
118
  -- Read/Write Access ----------------------------------------------------------------------
119
  -- -------------------------------------------------------------------------------------------
120
  rw_access: process(clk_i)
121
  begin
122
    if rising_edge(clk_i) then
123
      -- bus handshake --
124 66 zero_gravi
      ack_o <= rden or wren;
125 61 zero_gravi
 
126
      -- write access --
127 64 zero_gravi
      clr_pending <= (others => '1');
128 66 zero_gravi
      if (wren = '1') then
129 61 zero_gravi
        -- channel-enable --
130
        if (addr = xirq_enable_addr_c) then
131
          irq_enable <= data_i(XIRQ_NUM_CH-1 downto 0);
132
        end if;
133 64 zero_gravi
        -- clear pending IRQs --
134 61 zero_gravi
        if (addr = xirq_pending_addr_c) then
135 64 zero_gravi
          clr_pending <= data_i(XIRQ_NUM_CH-1 downto 0); -- set zero to clear pending IRQ
136 61 zero_gravi
        end if;
137
      end if;
138
 
139
      -- read access --
140
      data_o <= (others => '0');
141 66 zero_gravi
      if (rden = '1') then
142 61 zero_gravi
        case addr is
143
          when xirq_enable_addr_c  => data_o(XIRQ_NUM_CH-1 downto 0) <= irq_enable; -- channel-enable
144
          when xirq_pending_addr_c => data_o(XIRQ_NUM_CH-1 downto 0) <= irq_buf; -- pending IRQs
145
          when xirq_source_addr_c  => data_o(4 downto 0) <= irq_src; -- source IRQ
146
          when others => NULL;
147
        end case;
148
      end if;
149
    end if;
150
  end process rw_access;
151
 
152
 
153
  -- IRQ Trigger --------------------------------------------------------------
154
  -- -----------------------------------------------------------------------------
155
  irq_trigger: process(clk_i)
156
  begin
157
    if rising_edge(clk_i) then
158 70 zero_gravi
      irq_sync  <= xirq_i(XIRQ_NUM_CH-1 downto 0);
159 61 zero_gravi
      irq_sync2 <= irq_sync;
160
    end if;
161
  end process irq_trigger;
162
 
163
  irq_trigger_comb: process(irq_sync, irq_sync2)
164
    variable sel_v : std_ulogic_vector(1 downto 0);
165
  begin
166
    for i in 0 to XIRQ_NUM_CH-1 loop
167
      sel_v := XIRQ_TRIGGER_TYPE(i) & XIRQ_TRIGGER_POLARITY(i);
168
      case sel_v is
169
        when "00"   => irq_trig(i) <= not irq_sync(i); -- low-level
170
        when "01"   => irq_trig(i) <= irq_sync(i); -- high-level
171
        when "10"   => irq_trig(i) <= (not irq_sync(i)) and irq_sync2(i); -- falling-edge
172
        when "11"   => irq_trig(i) <= irq_sync(i) and (not irq_sync2(i)); -- rising-edge
173
        when others => irq_trig(i) <= '0';
174
      end case;
175
    end loop;
176
  end process irq_trigger_comb;
177
 
178
 
179
  -- IRQ Buffer ---------------------------------------------------------------
180
  -- -----------------------------------------------------------------------------
181
  irq_buffer: process(clk_i)
182
  begin
183
    if rising_edge(clk_i) then
184 64 zero_gravi
      irq_buf <= (irq_buf or (irq_trig and irq_enable)) and clr_pending;
185 61 zero_gravi
    end if;
186
  end process irq_buffer;
187
 
188
  -- anyone firing? --
189 74 zero_gravi
  irq_fire <= '1' when (or_reduce_f(irq_buf) = '1') else '0';
190 61 zero_gravi
 
191
 
192
  -- IRQ Priority Encoder -----------------------------------------------------
193
  -- -----------------------------------------------------------------------------
194
  irq_priority: process(irq_buf)
195
  begin
196
    irq_src_nxt <= (others => '0');
197 64 zero_gravi
    if (XIRQ_NUM_CH > 1) then
198
      for i in 0 to XIRQ_NUM_CH-1 loop
199
        if (irq_buf(i) = '1') then
200
          irq_src_nxt(index_size_f(XIRQ_NUM_CH)-1 downto 0) <= std_ulogic_vector(to_unsigned(i, index_size_f(XIRQ_NUM_CH)));
201
          exit;
202
        end if;
203
      end loop;
204
    end if;
205 61 zero_gravi
  end process irq_priority;
206
 
207
 
208
  -- IRQ Arbiter --------------------------------------------------------------
209
  -- -----------------------------------------------------------------------------
210
  irq_arbiter: process(clk_i)
211
  begin
212
    if rising_edge(clk_i) then
213 69 zero_gravi
      cpu_irq_o <= '0';
214 61 zero_gravi
      if (irq_run = '0') then -- no active IRQ
215
        if (irq_fire = '1') then
216 69 zero_gravi
          cpu_irq_o <= '1';
217
          irq_run   <= '1';
218
          irq_src   <= irq_src_nxt;
219 61 zero_gravi
        end if;
220 64 zero_gravi
      else -- active IRQ, wait for CPU to acknowledge
221 66 zero_gravi
        if (wren = '1') and (addr = xirq_source_addr_c) then -- write _any_ value to acknowledge
222 61 zero_gravi
          irq_run <= '0';
223
        end if;
224
      end if;
225
    end if;
226
  end process irq_arbiter;
227
 
228
 
229
end neorv32_xirq_rtl;

powered by: WebSVN 2.1.0

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