gb/src/work_ram.rs

67 lines
1.3 KiB
Rust
Raw Normal View History

2021-01-03 06:28:07 +00:00
#[derive(Debug, Clone)]
2021-03-16 06:05:13 +00:00
pub struct WorkRam {
2021-01-03 06:28:07 +00:00
bank: Box<[u8]>,
}
2021-03-16 06:05:13 +00:00
impl WorkRam {
2021-01-03 06:28:07 +00:00
pub fn write_byte(&mut self, index: usize, byte: u8) {
self.bank[index] = byte;
}
pub fn read_byte(&self, index: usize) -> u8 {
self.bank[index]
}
}
2021-03-16 06:05:13 +00:00
impl Default for WorkRam {
2021-01-03 06:28:07 +00:00
fn default() -> Self {
Self {
bank: vec![0u8; 4096].into_boxed_slice(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub 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-03-16 06:05:13 +00:00
pub struct VariableWorkRam {
2021-01-03 06:28:07 +00:00
current: BankNumber,
bank_n: Box<[[u8; 4096]]>, // 4K for Variable amount of Banks (Banks 1 -> 7) in Game Boy Colour
}
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,
bank_n: vec![[0u8; 4096]; 7].into_boxed_slice(),
}
}
}
2021-03-16 06:05:13 +00:00
impl VariableWorkRam {
2021-01-03 06:28:07 +00:00
pub fn set_current_bank(&mut self, bank: BankNumber) {
self.current = bank;
}
pub fn get_current_bank(&self) -> BankNumber {
self.current
}
pub fn write_byte(&mut self, index: usize, byte: u8) {
2021-01-19 07:36:44 +00:00
self.bank_n[self.current as usize][index] = byte;
2021-01-03 06:28:07 +00:00
}
pub fn read_byte(&self, index: usize) -> u8 {
2021-01-19 07:36:44 +00:00
self.bank_n[self.current as usize][index]
2021-01-03 06:28:07 +00:00
}
}