#include <cstdint>

#include "bios.h"
#include "gpio.h"
#include "intc.h"
#include "interrupts.h"
#include "pol0.h"
#include "timer.h"

namespace {

Gpio* leds;
Timer* timer;

void Timer0Isr() { 
    static int counter = 0;
    leds->data = counter++;

    timer->Pet();
    timer->ClearInterrupt();
}

void SetupTimer() {
    timer = Timer::Instance(TIMER0_BASE);
    timer->SetupAsWdt(100'000'000);
    timer->EnableT1();

    intc::SetIsr(TIMER0_IRQN, Timer0Isr);
    intc::SetIrqEnabled(TIMER0_IRQN, true);
}

}  // namespace

int main() {
    leds = Gpio::Instance(GPIO0_BASE);
    leds->data = 0xa0;

    SetupTimer();
    intc::EnableInterrupts();
    SetExternalInterruptHandler(intc::InterruptHandler);
    EnableExternalInterrupts();
    EnableInterrupts(true);

    leds->data = 0xa1;

    BiosWozmon();
}