use super::{unblock_on, BlockingOn};
use crate::serial::SIOCNT_;
use crate::task;

/* IRQ unblocking routines. */

pub fn timer_irq(n: u16) {
    match n {
        0 => unblock_on(BlockingOn::Timer0),
        1 => unblock_on(BlockingOn::Timer1),
        2 => unblock_on(BlockingOn::Timer2),
        3 => unblock_on(BlockingOn::Timer3),
        _ => unreachable!(),
    };
}

pub fn vblank_irq() {
    unblock_on(BlockingOn::VBlank);
}

pub fn vcount_irq() {
    unblock_on(BlockingOn::VCount);
}

pub fn serial_irq(_: u16) {
    let state = SIOCNT_.read();

    if !state.rx_empty() {
        task::unblock_on(BlockingOn::SerialReadable);
    }

    if !state.tx_full() {
        task::unblock_on(BlockingOn::SerialWritable);
    }
}
