/*----------------*\
|Nanite OS         |
|Copyright (C) 2026|
|Xircon            |
\*----------------*/
#include "irq.h"
#include "pic.h"
#include "io.h"
#include <stddef.h>
#include <stdio.h>
#include <dri/serial.h>

#define PIC_REMAP_OFFSET        0x20

extern uint16_t DEBUG_COM_PORT;

IRQHandler g_IRQHandlers[16];

void IRQ_Handler(Registers* regs)
{
    int irq = regs->interrupt - PIC_REMAP_OFFSET;

    if (g_IRQHandlers[irq] != NULL)
    {
        // handle IRQ
        g_IRQHandlers[irq](regs);
    }
    else
    {
        Serial_Printf(DEBUG_COM_PORT, "Unhandled IRQ %d...\n", irq);
    }

    PIC_SendEndOfInterrupt(irq);
}

void IRQ_Initialize()
{
    PIC_Configure(PIC_REMAP_OFFSET, PIC_REMAP_OFFSET + 8);

    // register ISR handlers for each of the 16 IRQ lines
    for (int i = 0; i < 16; i++)
        ISR_RegisterHandler(PIC_REMAP_OFFSET + i, IRQ_Handler);

    // enable interrupts 
    EnableInterrupts();
}

void IRQ_RegisterHandler(int irq, IRQHandler handler)
{
    g_IRQHandlers[irq] = handler;
}