1//
2// Copyright (c) 2010-2024 Antmicro
3//
4// This file is licensed under the MIT License.
5// Full license text is available in 'licenses/MIT.txt'.
6//
7
8`timescale 1ns / 1ps
9
10import renode_pkg::renode_runtime, renode_pkg::no_peripheral_index, renode_pkg::log_level_e;
11
12module renode_inputs #(
13    int unsigned InputsCount = 1
14) (
15    ref renode_runtime runtime,
16    input logic clk,
17    logic [InputsCount-1:0] inputs
18);
19  logic [InputsCount-1:0] inputs_prev;
20
21  initial inputs_prev = 0;
22  bit in_reset = 0;
23
24  task static reset_assert;
25    in_reset = 1;
26  endtask
27
28  task static reset_deassert;
29    in_reset = 0;
30  endtask
31
32  always @(posedge clk) begin
33    if (!in_reset) begin
34      for (int unsigned addr = 0; addr < InputsCount; addr++) begin
35        if (inputs[addr] != inputs_prev[addr]) begin
36`ifdef RENODE_DEBUG
37          runtime.connection.log(renode_pkg::LogDebug, $sformatf("GPIO %d changed: %d -> %d", addr, inputs_prev[addr], inputs[addr]));
38`endif
39          runtime.connection.send_to_async_receiver(renode_pkg::message_t'{
40              renode_pkg::interrupt,
41              renode_pkg::address_t'(addr),
42              renode_pkg::data_t'(inputs[addr]),
43              renode_pkg::no_peripheral_index
44            });
45        end
46      end
47      inputs_prev <= inputs;
48    end
49  end
50endmodule
51