2021-06-07 01:47:11 +00:00
|
|
|
use crate::bus::BusIo;
|
|
|
|
|
2021-04-09 01:32:32 +00:00
|
|
|
const WORK_RAM_SIZE: usize = 0x1000;
|
2021-03-21 01:22:31 +00:00
|
|
|
const VARIABLE_WORK_RAM_SIZE: usize = WORK_RAM_SIZE;
|
|
|
|
const WORK_RAM_START_ADDRESS: usize = 0xC000;
|
|
|
|
const VARIABLE_WORK_RAM_START_ADDRESS: usize = 0xD000;
|
|
|
|
|
2021-01-03 06:28:07 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) struct WorkRam {
|
2021-03-21 01:22:31 +00:00
|
|
|
bank: Box<[u8; WORK_RAM_SIZE]>,
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 01:47:11 +00:00
|
|
|
impl BusIo for WorkRam {
|
|
|
|
fn write_byte(&mut self, addr: u16, byte: u8) {
|
2021-03-21 01:22:31 +00:00
|
|
|
self.bank[addr as usize - WORK_RAM_START_ADDRESS] = byte;
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 01:47:11 +00:00
|
|
|
fn read_byte(&self, addr: u16) -> u8 {
|
2021-03-21 01:22:31 +00:00
|
|
|
self.bank[addr as usize - WORK_RAM_START_ADDRESS]
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 06:05:13 +00:00
|
|
|
impl Default for WorkRam {
|
2021-01-03 06:28:07 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-03-21 01:22:31 +00:00
|
|
|
bank: Box::new([0u8; WORK_RAM_SIZE]),
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-06-07 01:47:11 +00:00
|
|
|
enum BankNumber {
|
2021-01-19 07:36:44 +00:00
|
|
|
One = 1,
|
|
|
|
Two = 2,
|
|
|
|
Three = 3,
|
|
|
|
Four = 4,
|
|
|
|
Five = 5,
|
|
|
|
Six = 6,
|
|
|
|
Seven = 7,
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2021-06-07 00:14:28 +00:00
|
|
|
pub(crate) struct VariableWorkRam {
|
2021-01-03 06:28:07 +00:00
|
|
|
current: BankNumber,
|
2021-03-21 01:22:31 +00:00
|
|
|
bank_n: Box<[[u8; VARIABLE_WORK_RAM_SIZE]; 7]>, // 4K for Variable amount of Banks (Banks 1 -> 7) in Game Boy Colour
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 06:05:13 +00:00
|
|
|
impl Default for VariableWorkRam {
|
2021-01-03 06:28:07 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
current: BankNumber::One,
|
2021-03-21 01:22:31 +00:00
|
|
|
bank_n: Box::new([[0u8; VARIABLE_WORK_RAM_SIZE]; 7]),
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 06:05:13 +00:00
|
|
|
impl VariableWorkRam {
|
2021-06-07 01:47:11 +00:00
|
|
|
fn set_current_bank(&mut self, bank: BankNumber) {
|
2021-01-03 06:28:07 +00:00
|
|
|
self.current = bank;
|
|
|
|
}
|
|
|
|
|
2021-06-07 01:47:11 +00:00
|
|
|
fn get_current_bank(&self) -> BankNumber {
|
2021-01-03 06:28:07 +00:00
|
|
|
self.current
|
|
|
|
}
|
2021-06-07 01:47:11 +00:00
|
|
|
}
|
2021-01-03 06:28:07 +00:00
|
|
|
|
2021-06-07 01:47:11 +00:00
|
|
|
impl BusIo for VariableWorkRam {
|
|
|
|
fn write_byte(&mut self, addr: u16, byte: u8) {
|
2021-03-21 01:22:31 +00:00
|
|
|
self.bank_n[self.current as usize][addr as usize - VARIABLE_WORK_RAM_START_ADDRESS] = byte;
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 01:47:11 +00:00
|
|
|
fn read_byte(&self, addr: u16) -> u8 {
|
2021-03-21 01:22:31 +00:00
|
|
|
self.bank_n[self.current as usize][addr as usize - VARIABLE_WORK_RAM_START_ADDRESS]
|
2021-01-03 06:28:07 +00:00
|
|
|
}
|
|
|
|
}
|