use crate::rom::{MBC, ROM};

#[derive(Debug)]
pub struct MMU {
    pub ram: [u8; 0x8000],
}

impl MMU {
    pub fn new(rom: &ROM) -> Self {
        let ram = match rom.get_mbc() {
            MBC::NoMBC => rom.rom_data.as_slice(),
            _ => &[u8::MAX; 0x8000],
        };

        MMU {
            ram: ram.try_into().expect("Issue converting rom data to ram"),
        }
    }

    pub fn read_byte(&self, addr: u16) -> u8 {
        self.ram[addr as usize]
    }

    pub fn read_word(&self, addr: u16) -> u16 {
        self.read_byte(addr) as u16 | (self.read_byte(addr + 1) as u16) << 8
    }

    // pub fn read_dword(&self, addr: u16) -> u16 {
    //     self.read_byte(addr) as u16 | (self.read_byte(addr + 1) as u16) << 8
    // }

    pub fn write_byte(&mut self, val: u8, addr: u16) {
        self.ram[addr as usize] = val;
    }

    pub fn write_word(&mut self, val: u16, addr: u16) {
        self.write_byte(val as u8, addr);
        self.write_byte((val >> 8) as u8, addr + 1); // i think this is correct. i didnt actually do the math
    }
}
