| 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 |
|
|
-- # are supported that get prioritized into a single CPU interrupt. #
|
| 6 |
|
|
-- # #
|
| 7 |
|
|
-- # The actual trigger configuration has to be done before synthesis using the XIRQ_TRIGGER_TYPE #
|
| 8 |
|
|
-- # 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 |
|
|
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
| 14 |
|
|
-- # #
|
| 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 |
|
|
XIRQ_NUM_CH : natural := 32; -- number of external IRQ channels (0..32)
|
| 52 |
|
|
XIRQ_TRIGGER_TYPE : std_ulogic_vector(31 downto 0) := (others => '1'); -- trigger type: 0=level, 1=edge
|
| 53 |
|
|
XIRQ_TRIGGER_POLARITY : std_ulogic_vector(31 downto 0) := (others => '1') -- trigger polarity: 0=low-level/falling-edge, 1=high-level/rising-edge
|
| 54 |
|
|
);
|
| 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 |
|
|
xirq_i : in std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
|
| 66 |
|
|
-- 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 |
|
|
|
| 81 |
|
|
-- control registers --
|
| 82 |
|
|
signal irq_enable : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0); -- r/w: interrupt enable
|
| 83 |
|
|
signal clr_pending : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0); -- (r)/w: clear/ack pending IRQs
|
| 84 |
|
|
|
| 85 |
|
|
-- interrupt trigger --
|
| 86 |
|
|
signal irq_sync : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
|
| 87 |
|
|
signal irq_sync2 : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
|
| 88 |
|
|
signal irq_trig : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
|
| 89 |
|
|
|
| 90 |
|
|
-- interrupt buffer --
|
| 91 |
|
|
signal irq_buf : std_ulogic_vector(XIRQ_NUM_CH-1 downto 0);
|
| 92 |
|
|
signal irq_fire : std_ulogic;
|
| 93 |
|
|
|
| 94 |
|
|
-- interrupt source --
|
| 95 |
|
|
signal irq_src, irq_src_nxt : std_ulogic_vector(04 downto 0);
|
| 96 |
|
|
|
| 97 |
|
|
-- arbiter --
|
| 98 |
|
|
signal irq_run : std_ulogic;
|
| 99 |
|
|
signal irq_run_ff : std_ulogic;
|
| 100 |
|
|
signal host_ack : std_ulogic;
|
| 101 |
|
|
|
| 102 |
|
|
begin
|
| 103 |
|
|
|
| 104 |
|
|
-- Sanity Checks --------------------------------------------------------------------------
|
| 105 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 106 |
|
|
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;
|
| 107 |
|
|
|
| 108 |
|
|
|
| 109 |
|
|
-- Access Control -------------------------------------------------------------------------
|
| 110 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 111 |
|
|
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';
|
| 112 |
|
|
addr <= xirq_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
| 113 |
|
|
|
| 114 |
|
|
|
| 115 |
|
|
-- Read/Write Access ----------------------------------------------------------------------
|
| 116 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 117 |
|
|
rw_access: process(clk_i)
|
| 118 |
|
|
begin
|
| 119 |
|
|
if rising_edge(clk_i) then
|
| 120 |
|
|
-- bus handshake --
|
| 121 |
|
|
ack_o <= acc_en and (rden_i or wren_i);
|
| 122 |
|
|
|
| 123 |
|
|
-- write access --
|
| 124 |
|
|
host_ack <= '0';
|
| 125 |
|
|
clr_pending <= (others => '0');
|
| 126 |
|
|
if ((acc_en and wren_i) = '1') then
|
| 127 |
|
|
-- channel-enable --
|
| 128 |
|
|
if (addr = xirq_enable_addr_c) then
|
| 129 |
|
|
irq_enable <= data_i(XIRQ_NUM_CH-1 downto 0);
|
| 130 |
|
|
end if;
|
| 131 |
|
|
-- clear/ACK pending IRQ --
|
| 132 |
|
|
if (addr = xirq_pending_addr_c) then
|
| 133 |
|
|
host_ack <= '1';
|
| 134 |
|
|
clr_pending <= data_i(XIRQ_NUM_CH-1 downto 0);
|
| 135 |
|
|
end if;
|
| 136 |
|
|
end if;
|
| 137 |
|
|
|
| 138 |
|
|
-- read access --
|
| 139 |
|
|
data_o <= (others => '0');
|
| 140 |
|
|
if ((acc_en and rden_i) = '1') then
|
| 141 |
|
|
case addr is
|
| 142 |
|
|
when xirq_enable_addr_c => data_o(XIRQ_NUM_CH-1 downto 0) <= irq_enable; -- channel-enable
|
| 143 |
|
|
when xirq_pending_addr_c => data_o(XIRQ_NUM_CH-1 downto 0) <= irq_buf; -- pending IRQs
|
| 144 |
|
|
when xirq_source_addr_c => data_o(4 downto 0) <= irq_src; -- source IRQ
|
| 145 |
|
|
when others => NULL;
|
| 146 |
|
|
end case;
|
| 147 |
|
|
end if;
|
| 148 |
|
|
end if;
|
| 149 |
|
|
end process rw_access;
|
| 150 |
|
|
|
| 151 |
|
|
|
| 152 |
|
|
-- IRQ Trigger --------------------------------------------------------------
|
| 153 |
|
|
-- -----------------------------------------------------------------------------
|
| 154 |
|
|
irq_trigger: process(clk_i)
|
| 155 |
|
|
begin
|
| 156 |
|
|
if rising_edge(clk_i) then
|
| 157 |
|
|
irq_sync <= xirq_i;
|
| 158 |
|
|
irq_sync2 <= irq_sync;
|
| 159 |
|
|
end if;
|
| 160 |
|
|
end process irq_trigger;
|
| 161 |
|
|
|
| 162 |
|
|
irq_trigger_comb: process(irq_sync, irq_sync2)
|
| 163 |
|
|
variable sel_v : std_ulogic_vector(1 downto 0);
|
| 164 |
|
|
begin
|
| 165 |
|
|
for i in 0 to XIRQ_NUM_CH-1 loop
|
| 166 |
|
|
sel_v := XIRQ_TRIGGER_TYPE(i) & XIRQ_TRIGGER_POLARITY(i);
|
| 167 |
|
|
case sel_v is
|
| 168 |
|
|
when "00" => irq_trig(i) <= not irq_sync(i); -- low-level
|
| 169 |
|
|
when "01" => irq_trig(i) <= irq_sync(i); -- high-level
|
| 170 |
|
|
when "10" => irq_trig(i) <= (not irq_sync(i)) and irq_sync2(i); -- falling-edge
|
| 171 |
|
|
when "11" => irq_trig(i) <= irq_sync(i) and (not irq_sync2(i)); -- rising-edge
|
| 172 |
|
|
when others => irq_trig(i) <= '0';
|
| 173 |
|
|
end case;
|
| 174 |
|
|
end loop;
|
| 175 |
|
|
end process irq_trigger_comb;
|
| 176 |
|
|
|
| 177 |
|
|
|
| 178 |
|
|
-- IRQ Buffer ---------------------------------------------------------------
|
| 179 |
|
|
-- -----------------------------------------------------------------------------
|
| 180 |
|
|
irq_buffer: process(clk_i)
|
| 181 |
|
|
begin
|
| 182 |
|
|
if rising_edge(clk_i) then
|
| 183 |
|
|
irq_buf <= (irq_buf or (irq_trig and irq_enable)) and (not clr_pending);
|
| 184 |
|
|
end if;
|
| 185 |
|
|
end process irq_buffer;
|
| 186 |
|
|
|
| 187 |
|
|
-- anyone firing? --
|
| 188 |
|
|
irq_fire <= or_reduce_f(irq_buf);
|
| 189 |
|
|
|
| 190 |
|
|
|
| 191 |
|
|
-- IRQ Priority Encoder -----------------------------------------------------
|
| 192 |
|
|
-- -----------------------------------------------------------------------------
|
| 193 |
|
|
irq_priority: process(irq_buf)
|
| 194 |
|
|
begin
|
| 195 |
|
|
irq_src_nxt <= (others => '0');
|
| 196 |
|
|
for i in 0 to XIRQ_NUM_CH-1 loop
|
| 197 |
|
|
if (irq_buf(i) = '1') then
|
| 198 |
|
|
irq_src_nxt <= std_ulogic_vector(to_unsigned(i, 5));
|
| 199 |
|
|
exit;
|
| 200 |
|
|
end if;
|
| 201 |
|
|
end loop;
|
| 202 |
|
|
end process irq_priority;
|
| 203 |
|
|
|
| 204 |
|
|
|
| 205 |
|
|
-- IRQ Arbiter --------------------------------------------------------------
|
| 206 |
|
|
-- -----------------------------------------------------------------------------
|
| 207 |
|
|
irq_arbiter: process(clk_i)
|
| 208 |
|
|
begin
|
| 209 |
|
|
if rising_edge(clk_i) then
|
| 210 |
|
|
irq_run_ff <= irq_run;
|
| 211 |
|
|
if (irq_run = '0') then -- no active IRQ
|
| 212 |
|
|
if (irq_fire = '1') then
|
| 213 |
|
|
irq_run <= '1';
|
| 214 |
|
|
irq_src <= irq_src_nxt;
|
| 215 |
|
|
end if;
|
| 216 |
|
|
else -- active IRQ, wait for CPU acknowledge
|
| 217 |
|
|
if (host_ack = '1') then
|
| 218 |
|
|
irq_run <= '0';
|
| 219 |
|
|
end if;
|
| 220 |
|
|
end if;
|
| 221 |
|
|
end if;
|
| 222 |
|
|
end process irq_arbiter;
|
| 223 |
|
|
|
| 224 |
|
|
-- rising-edge detector --
|
| 225 |
|
|
cpu_irq_o <= irq_run and (not irq_run_ff);
|
| 226 |
|
|
|
| 227 |
|
|
|
| 228 |
|
|
end neorv32_xirq_rtl;
|