use crate::constants::constants_system::SIZE_32KB;
use crate::periph::at28c256::At28C256;

const CHIP_SIZE: usize = SIZE_32KB ;

impl At28C256 {
    /// Tick the ROM
    /// address_bus
    /// data_bus
    /// CS -> Chip Select
    /// OE -> Output Enable
    /// WE -> Write Enable
    pub fn signal_tick(&mut self, address_bus: u16, data_bus: u8, cs: bool, oe: bool, we: bool) -> (u8) {

        // if we aren't selected and we aren't able to write to the bus...
        if !cs || !we || !oe { return data_bus };

        // if we aren't being addressed directly
        if !(address_bus <= self.max_offset && address_bus >= self.offset) {
            return data_bus
        };

        let internal_address = address_bus - self.offset;

        let result = if internal_address < CHIP_SIZE as u16 {
            self.data[internal_address as usize]
        } else {
            data_bus
        };
        println!("At28C256 EADDR: ${address_bus:04x} IADDR: ${internal_address:04x} = {result:02x}");
        result
    }
}
