//! Boot ROM.

use std::cell::RefCell;
use std::rc::Rc;

use remus::bus::adapt::Bank;
use remus::dev::Device;
use remus::reg::Register;
use remus::{mem, Block};

/// Boot ROM raw bytes.
const BOOTROM: [u8; 0x100] = [
    0x31, 0xfe, 0xff, 0xaf, 0x21, 0xff, 0x9f, 0x32, 0xcb, 0x7c, 0x20, 0xfb, 0x21, 0x26, 0xff, 0x0e,
    0x11, 0x3e, 0x80, 0x32, 0xe2, 0x0c, 0x3e, 0xf3, 0xe2, 0x32, 0x3e, 0x77, 0x77, 0x3e, 0xfc, 0xe0,
    0x47, 0x11, 0x04, 0x01, 0x21, 0x10, 0x80, 0x1a, 0xcd, 0x95, 0x00, 0xcd, 0x96, 0x00, 0x13, 0x7b,
    0xfe, 0x34, 0x20, 0xf3, 0x11, 0xd8, 0x00, 0x06, 0x08, 0x1a, 0x13, 0x22, 0x23, 0x05, 0x20, 0xf9,
    0x3e, 0x19, 0xea, 0x10, 0x99, 0x21, 0x2f, 0x99, 0x0e, 0x0c, 0x3d, 0x28, 0x08, 0x32, 0x0d, 0x20,
    0xf9, 0x2e, 0x0f, 0x18, 0xf3, 0x67, 0x3e, 0x64, 0x57, 0xe0, 0x42, 0x3e, 0x91, 0xe0, 0x40, 0x04,
    0x1e, 0x02, 0x0e, 0x0c, 0xf0, 0x44, 0xfe, 0x90, 0x20, 0xfa, 0x0d, 0x20, 0xf7, 0x1d, 0x20, 0xf2,
    0x0e, 0x13, 0x24, 0x7c, 0x1e, 0x83, 0xfe, 0x62, 0x28, 0x06, 0x1e, 0xc1, 0xfe, 0x64, 0x20, 0x06,
    0x7b, 0xe2, 0x0c, 0x3e, 0x87, 0xe2, 0xf0, 0x42, 0x90, 0xe0, 0x42, 0x15, 0x20, 0xd2, 0x05, 0x20,
    0x4f, 0x16, 0x20, 0x18, 0xcb, 0x4f, 0x06, 0x04, 0xc5, 0xcb, 0x11, 0x17, 0xc1, 0xcb, 0x11, 0x17,
    0x05, 0x20, 0xf5, 0x22, 0x23, 0x22, 0x23, 0xc9, 0xce, 0xed, 0x66, 0x66, 0xcc, 0x0d, 0x00, 0x0b,
    0x03, 0x73, 0x00, 0x83, 0x00, 0x0c, 0x00, 0x0d, 0x00, 0x08, 0x11, 0x1f, 0x88, 0x89, 0x00, 0x0e,
    0xdc, 0xcc, 0x6e, 0xe6, 0xdd, 0xdd, 0xd9, 0x99, 0xbb, 0xbb, 0x67, 0x63, 0x6e, 0x0e, 0xec, 0xcc,
    0xdd, 0xdc, 0x99, 0x9f, 0xbb, 0xb9, 0x33, 0x3e, 0x3c, 0x42, 0xb9, 0xa5, 0xb9, 0xa5, 0x42, 0x3c,
    0x21, 0x04, 0x01, 0x11, 0xa8, 0x00, 0x1a, 0x13, 0xbe, 0x20, 0xfe, 0x23, 0x7d, 0xfe, 0x34, 0x20,
    0xf5, 0x06, 0x19, 0x78, 0x86, 0x23, 0x05, 0x20, 0xfb, 0x86, 0x20, 0xfe, 0x3e, 0x01, 0xe0, 0x50,
];

/// Boot ROM management [`Device`](Device).
#[derive(Debug)]
pub struct Rom {
    pub ctl: Rc<RefCell<RomDisable>>,
    bank: Rc<RefCell<Bank>>,
    rom: Rc<RefCell<mem::Rom<0x100>>>,
}

impl Block for Rom {
    fn reset(&mut self) {
        // Reset controller
        self.ctl.borrow_mut().reset();
        self.ctl.borrow_mut().bank = self.bank.clone();
        // Reset bank
        self.bank.borrow_mut().reset();
        self.bank.borrow_mut().add(self.rom.clone());
        self.bank.borrow_mut().set(0);
    }
}

impl Default for Rom {
    fn default() -> Self {
        Self {
            ctl: Rc::new(RefCell::new(RomDisable::default())),
            bank: Default::default(),
            rom: Rc::new(RefCell::new(mem::Rom::from(&BOOTROM))),
        }
    }
}

impl Device for Rom {
    fn contains(&self, index: usize) -> bool {
        self.bank.borrow().contains(index)
    }

    fn len(&self) -> usize {
        self.bank.borrow().len()
    }

    fn read(&self, index: usize) -> u8 {
        self.bank.borrow().read(index)
    }

    fn write(&mut self, index: usize, value: u8) {
        self.bank.borrow_mut().write(index, value);
    }
}

/// Boot ROM disable [`Register`](Register).
#[derive(Debug, Default)]
pub struct RomDisable {
    reg: Register<u8>,
    bank: Rc<RefCell<Bank>>,
}

impl Block for RomDisable {
    fn reset(&mut self) {
        // Reset controller
        self.reg.reset();
        // Reset bank
        self.bank.borrow_mut().set(*self.reg as usize);
    }
}

impl Device for RomDisable {
    fn contains(&self, index: usize) -> bool {
        self.reg.contains(index)
    }

    fn len(&self) -> usize {
        self.reg.len()
    }

    fn read(&self, index: usize) -> u8 {
        self.reg.read(index)
    }

    fn write(&mut self, index: usize, value: u8) {
        self.reg.write(index, value);
        self.bank.borrow_mut().set(value as usize);
    }
}
