use log::debug;
use crate::constants::constants_system::{SIZE_1KB, SIZE_32KB};
use crate::periph::mos6520::Mos6520;
use crate::periph::mos6530::Mos6530;
use crate::traits::bus_device::BusDevice;
use crate::traits::gpio_chip::{GpioChip, GpioChipPorts};
use crate::traits::memory_chip::MemoryChip;
use crate::traits::pia_chip::PiaChip;
use crate::traits::ram_chip::RamChip;
use crate::traits::rom_chip::RomChip;

impl GpioChip for Mos6530 {
    fn set_port(&mut self, new_value: u8, port: GpioChipPorts) {
        match port {
            GpioChipPorts::PORT_A => {
                self.port_a = new_value
            }
            GpioChipPorts::PORT_B => {
                self.port_b = new_value
            }
        }
    }

    fn set_ddr(&mut self, new_ddr_value: u8, port: GpioChipPorts) {
        match port {
            GpioChipPorts::PORT_A => {
                self.ddra = new_ddr_value
            }
            GpioChipPorts::PORT_B => {
                self.ddrb = new_ddr_value
            }
        }

    }

    fn get_port(&self, port: GpioChipPorts) -> u8 {
        match port {
            GpioChipPorts::PORT_A => {
                self.port_a
            }
            GpioChipPorts::PORT_B => {
                self.port_b
            }
        }
    }

    fn get_ddr(&self, port: GpioChipPorts) -> u8 {
        match port {
            GpioChipPorts::PORT_A => {
                self.ddra
            }
            GpioChipPorts::PORT_B => {
                self.ddrb
            }
        }
    }

    fn get_in(&self, port: GpioChipPorts) -> u8 {
        match port {
            GpioChipPorts::PORT_A => {
                self.in_a
            }
            GpioChipPorts::PORT_B => {
                self.in_b
            }
        }
    }

    fn get_out(&self, port: GpioChipPorts) -> u8 {
        match port {
            GpioChipPorts::PORT_A => {
                self.port_a
            }
            GpioChipPorts::PORT_B => {
                self.port_b
            }
        }
    }

    fn update_ports(&mut self) {
        let (in_a, out_a) = Mos6520::split_port(self.port_a, self.ddra);
        self.in_a = in_a;
        self.out_a = out_a;

        // Port B
        let (in_b, out_b) = Mos6520::split_port(self.port_b, self.ddrb);
        self.in_b = in_b;
        self.out_b = out_b;
    }
}

impl PiaChip for Mos6530 {
}
