#include <PSX/System/IOPorts/timer_io.hpp>
#include <PSX/System/syscalls.hpp>
#include <PSX/Timer/high_res_timer.hpp>

namespace JabyEngine {
    volatile uint16_t HighResTime :: global_counter_10ms = 0;

    namespace Timer {
        static SysCall::InterruptVerifierResult interrupt_verifier() {
            if(Interrupt::is_irq(Interrupt::Timer2)) {
                Interrupt::ack_irq(Interrupt::Timer2);
                return SysCall::InterruptVerifierResult::ExecuteHandler;
            }
            
            else {
                return SysCall::InterruptVerifierResult::SkipHandler;
            }
        }

        static void interrupt_handler(uint32_t) {
            HighResTime::global_counter_10ms = HighResTime::global_counter_10ms + 1;
            SysCall::ReturnFromException();
        }

        auto irq_callback = SysCall::InterruptCallback::from(interrupt_verifier, interrupt_handler);
    }
}