#include <io.h>
#include <timer.h>
#include <cpu.h>
#include <dma.h>
#include <lcd.h>
#include <gamepad.h>
#include <audio.h>
#include <serial.h>

u8 io_read(u16 address){
    if(address == 0xFF00) {
        return gamepad_get_output();
    }
    if(address == 0xFF01 || address == 0xFF02) {
        return serial_read(address);
    }

    if(BETWEEN(address, 0xFF04, 0xFF07)){
        return timer_read(address);
    }

    if(BETWEEN(address, 0xFF40, 0xFF4B)){
        return lcd_read(address);
    }

    if(address == 0xFF0F) {
        return cpu_get_int_flags();
    }

    if(BETWEEN(address, 0xFF10, 0xFF3F)) {
        return audio_read(address);
    }

    printf("UNSUPPORTED io_read(%04X)\n", address);
    return 0xFF;
}


void io_write(u16 address, u8 value){
    if(address == 0xFF00) {
        gamepad_set_sel(value);
        return;
    }
    if(address == 0xFF01 || address == 0xFF02) {
        serial_write(address, value);
        return;
    }

    if(BETWEEN(address, 0xFF04, 0xFF07)){
        timer_write(address, value);
        return;
    }

    if(address == 0xFF0F) {
        cpu_set_int_flags(value);
        return;
    }

    if(BETWEEN(address, 0xFF40, 0xFF4B)){
        lcd_write(address, value);
        return;
    }

    if(BETWEEN(address, 0xFF10, 0xFF3F)) {
        audio_write(address, value);
        return;
    }


    printf("UNSUPPORTED io_write(%04X)\n", address);
}

