|
| 1 | +// *************************************************************************** |
| 2 | +// *************************************************************************** |
| 3 | +// Copyright (C) 2025 Analog Devices, Inc. All rights reserved. |
| 4 | +// |
| 5 | +// In this HDL repository, there are many different and unique modules, consisting |
| 6 | +// of various HDL (Verilog or VHDL) components. The individual modules are |
| 7 | +// developed independently, and may be accompanied by separate and unique license |
| 8 | +// terms. |
| 9 | +// |
| 10 | +// The user should read each of these license terms, and understand the |
| 11 | +// freedoms and responsabilities that he or she has by using this source/core. |
| 12 | +// |
| 13 | +// This core is distributed in the hope that it will be useful, but WITHOUT ANY |
| 14 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 15 | +// A PARTICULAR PURPOSE. |
| 16 | +// |
| 17 | +// Redistribution and use of source or resulting binaries, with or without modification |
| 18 | +// of this file, are permitted under one of the following two license terms: |
| 19 | +// |
| 20 | +// 1. The GNU General Public License version 2 as published by the |
| 21 | +// Free Software Foundation, which can be found in the top level directory |
| 22 | +// of this repository (LICENSE_GPL2), and also online at: |
| 23 | +// <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html> |
| 24 | +// |
| 25 | +// OR |
| 26 | +// |
| 27 | +// 2. An ADI specific BSD license, which can be found in the top level directory |
| 28 | +// of this repository (LICENSE_ADIBSD), and also on-line at: |
| 29 | +// https://github.com/analogdevicesinc/hdl/blob/main/LICENSE_ADIBSD |
| 30 | +// This will allow to generate bit files and not release the source code, |
| 31 | +// as long as it attaches to an ADI device. |
| 32 | +// |
| 33 | +// *************************************************************************** |
| 34 | +// *************************************************************************** |
| 35 | + |
| 36 | +`include "utils.svh" |
| 37 | + |
| 38 | +package irq_handler_pkg; |
| 39 | + |
| 40 | + import logger_pkg::*; |
| 41 | + import adi_common_pkg::*; |
| 42 | + import adi_api_pkg::*; |
| 43 | + import m_axi_sequencer_pkg::*; |
| 44 | + import io_vip_if_base_pkg::*; |
| 45 | + |
| 46 | + class irq_handler_class extends adi_api; |
| 47 | + |
| 48 | + protected io_vip_if_base irq_vip_if; |
| 49 | + |
| 50 | + protected event irq_event_list [31:0]; |
| 51 | + protected bit [31:0] irq_valid_list; |
| 52 | + |
| 53 | + protected bit software_testing; |
| 54 | + |
| 55 | + // constructor |
| 56 | + function new( |
| 57 | + input string name, |
| 58 | + input m_axi_sequencer_base bus, |
| 59 | + input bit [31:0] base_address, |
| 60 | + input io_vip_if_base irq_vip_if, |
| 61 | + input adi_component parent = null); |
| 62 | + |
| 63 | + super.new(name, bus, base_address, parent); |
| 64 | + |
| 65 | + this.irq_vip_if = irq_vip_if; |
| 66 | + this.irq_valid_list = 'h0; |
| 67 | + this.software_testing = 1'b0; |
| 68 | + endfunction: new |
| 69 | + |
| 70 | + // enable software testing at startup |
| 71 | + function void enable_software_testing(); |
| 72 | + this.software_testing = 1'b1; |
| 73 | + endfunction: enable_software_testing |
| 74 | + |
| 75 | + // software IRQ test-phase |
| 76 | + task software_irq_testing(); |
| 77 | + logic [31:0] enable_irq = 'h0; |
| 78 | + |
| 79 | + for (int i=0; i<32; ++i) begin |
| 80 | + enable_irq = enable_irq | (this.irq_valid_list[i] << i); |
| 81 | + end |
| 82 | + this.info($sformatf("Enables: %d", enable_irq), ADI_VERBOSITY_HIGH); |
| 83 | + this.axi_write('h00, enable_irq, 1); // software trigger IRQ |
| 84 | + |
| 85 | + // negative edge detection |
| 86 | + if (this.irq_vip_if.get_io() == 1'b0) begin |
| 87 | + this.irq_vip_if.wait_io_change(); |
| 88 | + end |
| 89 | + this.irq_vip_if.wait_io_change(); |
| 90 | + endtask: software_irq_testing |
| 91 | + |
| 92 | + // IRQ event handler |
| 93 | + task start(); |
| 94 | + logic [31:0] irq_triggered; |
| 95 | + logic [31:0] enable_irq = 'h0; |
| 96 | + |
| 97 | + for (int i=0; i<32; ++i) begin |
| 98 | + enable_irq = enable_irq | (this.irq_valid_list[i] << i); |
| 99 | + end |
| 100 | + |
| 101 | + this.axi_write('h10, enable_irq, 1); // enable all IRQ handles |
| 102 | + this.axi_write('h1C, 'b01, 1); // enable IRQ controller / software interrupt disabled |
| 103 | + |
| 104 | + fork forever begin |
| 105 | + if (this.irq_vip_if.get_io() == 1'b0) begin |
| 106 | + this.irq_vip_if.wait_io_change(); |
| 107 | + end |
| 108 | + |
| 109 | + this.info($sformatf("IRQ controller triggered"), ADI_VERBOSITY_HIGH); |
| 110 | + |
| 111 | + this.axi_read('h04, irq_triggered, 1); // check pending IRQ (status + enable) |
| 112 | + |
| 113 | + for (int i=0; i<32; ++i) begin |
| 114 | + if (irq_triggered[i]) begin |
| 115 | + if (this.irq_valid_list[i] == 1'b0) begin |
| 116 | + this.fatal($sformatf("IRQ triggered on a non-registered device!")); |
| 117 | + end |
| 118 | + ->this.irq_event_list[i]; |
| 119 | + @(this.irq_event_list[i]); |
| 120 | + this.axi_write('h0C, 'h1 << i, 1); // acknowledge IRQ |
| 121 | + end |
| 122 | + end |
| 123 | + end join_none |
| 124 | + |
| 125 | + if (this.software_testing) begin |
| 126 | + this.software_irq_testing(); |
| 127 | + end |
| 128 | + |
| 129 | + this.axi_write('h1C, 'b11, 1); // enable IRQ controller + software interrupt enabled |
| 130 | + endtask: start |
| 131 | + |
| 132 | + // register IRQ device |
| 133 | + function event register_device(input int position); |
| 134 | + if (this.irq_valid_list[position]) begin |
| 135 | + this.fatal($sformatf("An interrupt handler is already assigned to position %0d!", position)); |
| 136 | + end |
| 137 | + this.irq_valid_list[position] = 1'b1; |
| 138 | + return this.irq_event_list[position]; |
| 139 | + endfunction: register_device |
| 140 | + |
| 141 | + endclass: irq_handler_class |
| 142 | + |
| 143 | +endpackage: irq_handler_pkg |
0 commit comments