Replace AddressRange struct by ranges

This commit is contained in:
Franco Colmenarez 2021-11-15 09:46:59 -05:00
parent 5acb3ba6d5
commit de021c2eff
3 changed files with 29 additions and 47 deletions

View File

@ -1,5 +1,6 @@
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::ops::RangeInclusive;
use crate::utils::{ use crate::utils::{
get_bit, get_bit,
BitIndex, BitIndex,
@ -16,37 +17,18 @@ use crate::cpu::{Interrupt};
use crate::timer::{Timer, TIMER_DIVIDER_REGISTER_ADDRESS}; use crate::timer::{Timer, TIMER_DIVIDER_REGISTER_ADDRESS};
use crate::joypad::{Joypad, JOYPAD_ADDRESS}; use crate::joypad::{Joypad, JOYPAD_ADDRESS};
pub struct AddressRange { pub const BANK_ZERO: RangeInclusive<u16> = 0x0000..=0x3FFF;
begin: u16, pub const BANK_SWITCHABLE: RangeInclusive<u16> = 0x4000..=0x7FFF;
end: u16, pub const VIDEO_RAM: RangeInclusive<u16> = 0x8000..=0x9FFF;
} pub const EXTERNAL_RAM: RangeInclusive<u16> = 0xA000..=0xBFFF;
pub const WORK_RAM_1: RangeInclusive<u16> = 0xC000..=0xCFFF;
impl AddressRange { pub const WORK_RAM_2: RangeInclusive<u16> = 0xD000..=0xDFFF;
pub fn begin(&self) -> u16 { pub const ECHO_RAM: RangeInclusive<u16> = 0xE000..=0xFDFF;
self.begin pub const SPRITE_ATTRIBUTE_TABLE: RangeInclusive<u16> = 0xFE00..=0xFE9F;
} pub const NOT_USABLE: RangeInclusive<u16> = 0xFEA0..=0xFEFF;
pub const IO_REGISTERS: RangeInclusive<u16> = 0xFF00..=0xFF7F;
pub fn end(&self) -> u16 { pub const HIGH_RAM: RangeInclusive<u16> = 0xFF80..=0xFFFE;
self.end pub const INTERRUPT_ENABLE_REGISTER: RangeInclusive<u16> = 0xFFFF..=0xFFFF;
}
pub fn in_range(&self, address: u16) -> bool {
address >= self.begin && address <= self.end
}
}
pub const BANK_ZERO: AddressRange = AddressRange{begin: 0x0000, end: 0x3FFF};
pub const BANK_SWITCHABLE: AddressRange = AddressRange{begin: 0x4000, end: 0x7FFF};
pub const VIDEO_RAM: AddressRange = AddressRange{begin: 0x8000, end: 0x9FFF};
pub const EXTERNAL_RAM: AddressRange = AddressRange{begin: 0xA000, end: 0xBFFF};
pub const WORK_RAM_1: AddressRange = AddressRange{begin: 0xC000, end: 0xCFFF};
pub const WORK_RAM_2: AddressRange = AddressRange{begin: 0xD000, end: 0xDFFF};
pub const ECHO_RAM: AddressRange = AddressRange{begin: 0xE000, end: 0xFDFF};
pub const SPRITE_ATTRIBUTE_TABLE: AddressRange = AddressRange{begin: 0xFE00, end: 0xFE9F};
pub const NOT_USABLE: AddressRange = AddressRange{begin: 0xFEA0, end: 0xFEFF};
pub const IO_REGISTERS: AddressRange = AddressRange{begin: 0xFF00, end: 0xFF7F};
pub const HIGH_RAM: AddressRange = AddressRange{begin: 0xFF80, end: 0xFFFE};
pub const INTERRUPT_ENABLE_REGISTER: AddressRange = AddressRange{begin: 0xFFFF, end: 0xFFFF};
pub const INTERRUPT_ENABLE_ADDRESS: u16 = 0xFFFF; pub const INTERRUPT_ENABLE_ADDRESS: u16 = 0xFFFF;
pub const INTERRUPT_FLAG_ADDRESS: u16 = 0xFF0F; pub const INTERRUPT_FLAG_ADDRESS: u16 = 0xFF0F;
@ -104,7 +86,7 @@ impl Bus {
} }
pub fn read(&self, address: u16) -> u8 { pub fn read(&self, address: u16) -> u8 {
if BANK_ZERO.in_range(address) || BANK_SWITCHABLE.in_range(address) || EXTERNAL_RAM.in_range(address) { if BANK_ZERO.contains(&address) || BANK_SWITCHABLE.contains(&address) || EXTERNAL_RAM.contains(&address) {
return self.game_rom.read(address); return self.game_rom.read(address);
} else if address == INTERRUPT_ENABLE_ADDRESS || address == INTERRUPT_FLAG_ADDRESS { } else if address == INTERRUPT_ENABLE_ADDRESS || address == INTERRUPT_FLAG_ADDRESS {
return 0b11100000 | self.data[address as usize]; return 0b11100000 | self.data[address as usize];
@ -125,19 +107,19 @@ impl Bus {
// print!("{}", data as char); // print!("{}", data as char);
} }
if BANK_ZERO.in_range(address) || BANK_SWITCHABLE.in_range(address) || EXTERNAL_RAM.in_range(address) { if BANK_ZERO.contains(&address) || BANK_SWITCHABLE.contains(&address) || EXTERNAL_RAM.contains(&address) {
self.game_rom.write(address, data); self.game_rom.write(address, data);
} else if WORK_RAM_1.in_range(address) || WORK_RAM_2.in_range(address) { } else if WORK_RAM_1.contains(&address) || WORK_RAM_2.contains(&address) {
self.data[address as usize] = data; self.data[address as usize] = data;
// Copy to the ECHO RAM // Copy to the ECHO RAM
if address <= 0xDDFF { if address <= 0xDDFF {
self.data[(ECHO_RAM.begin() + (address - WORK_RAM_1.begin())) as usize] = data; self.data[(ECHO_RAM.min().unwrap() + (address - WORK_RAM_1.min().unwrap())) as usize] = data;
} }
} else if EXTERNAL_RAM.in_range(address) { } else if EXTERNAL_RAM.contains(&address) {
// self.game_rom.write(address, data); // self.game_rom.write(address, data);
} else if ECHO_RAM.in_range(address) { } else if ECHO_RAM.contains(&address) {
self.data[address as usize] = data; self.data[address as usize] = data;
self.data[(WORK_RAM_1.begin() + (address - ECHO_RAM.begin())) as usize] = data; // Copy to the working RAM self.data[(WORK_RAM_1.min().unwrap() + (address - ECHO_RAM.min().unwrap())) as usize] = data; // Copy to the working RAM
} else if address == TIMER_DIVIDER_REGISTER_ADDRESS { } else if address == TIMER_DIVIDER_REGISTER_ADDRESS {
self.timer.borrow_mut().reset(); self.timer.borrow_mut().reset();
} else if address == LCD_CONTROL_ADDRESS { } else if address == LCD_CONTROL_ADDRESS {
@ -163,7 +145,7 @@ impl Bus {
self.data[address as usize] = data; self.data[address as usize] = data;
let source = (data as usize) * 0x100; let source = (data as usize) * 0x100;
let mut count = 0; let mut count = 0;
let oam_addr = SPRITE_ATTRIBUTE_TABLE.begin() as usize; let oam_addr = SPRITE_ATTRIBUTE_TABLE.min().unwrap() as usize;
while count < 160 { while count < 160 {
self.data[oam_addr + count] = self.data[source + count]; self.data[oam_addr + count] = self.data[source + count];
count += 1; count += 1;

View File

@ -340,8 +340,8 @@ impl PPU {
let palette_0 = bus.read(OBJECT_PALETTE_0_ADDRESS); let palette_0 = bus.read(OBJECT_PALETTE_0_ADDRESS);
let palette_1 = bus.read(OBJECT_PALETTE_1_ADDRESS); let palette_1 = bus.read(OBJECT_PALETTE_1_ADDRESS);
let long_sprites = self.get_lcd_control(bus, LCDControl::ObjectSize); let long_sprites = self.get_lcd_control(bus, LCDControl::ObjectSize);
let mut addr = SPRITE_ATTRIBUTE_TABLE.begin(); let mut addr = SPRITE_ATTRIBUTE_TABLE.min().unwrap();
while addr <= SPRITE_ATTRIBUTE_TABLE.end() { while addr <= SPRITE_ATTRIBUTE_TABLE.max().unwrap() {
// The gameboy only supports 10 sprites per line, // The gameboy only supports 10 sprites per line,
// but since we are on an emulator we can avoud that limitation // but since we are on an emulator we can avoud that limitation
if self.sprite_buffer.len() >= 10 { if self.sprite_buffer.len() >= 10 {

View File

@ -190,15 +190,15 @@ impl ROM {
}; };
}, },
MBC::MBC1 => { MBC::MBC1 => {
if BANK_ZERO.in_range(address) { if BANK_ZERO.contains(&address) {
return self.data[address as usize]; return self.data[address as usize];
} else if BANK_SWITCHABLE.in_range(address) { } else if BANK_SWITCHABLE.contains(&address) {
return self.data[((self.rom_bank as usize * 0x4000) + (address as usize & 0x3FFF)) as usize]; return self.data[((self.rom_bank as usize * 0x4000) + (address as usize & 0x3FFF)) as usize];
} else if EXTERNAL_RAM.in_range(address) { } else if EXTERNAL_RAM.contains(&address) {
if !self.info.has_ram { if !self.info.has_ram {
return 0xFF; return 0xFF;
} }
return match self.ram.get((address - EXTERNAL_RAM.begin() + (0x2000 * self.ram_bank as u16)) as usize) { return match self.ram.get((address - EXTERNAL_RAM.min().unwrap() + (0x2000 * self.ram_bank as u16)) as usize) {
Some(data) => *data, Some(data) => *data,
None => 0xFF, None => 0xFF,
}; };
@ -235,11 +235,11 @@ impl ROM {
1 => BankingMode::Advanced, 1 => BankingMode::Advanced,
_ => unreachable!(), _ => unreachable!(),
} }
} else if EXTERNAL_RAM.in_range(address) { } else if EXTERNAL_RAM.contains(&address) {
if !self.ram_enable || !self.info.has_ram { if !self.ram_enable || !self.info.has_ram {
return; return;
} }
let address = address as usize - EXTERNAL_RAM.begin() as usize + (EXTERNAL_RAM.begin() as usize * self.ram_bank as usize); let address = address as usize - EXTERNAL_RAM.min().unwrap() as usize + (EXTERNAL_RAM.min().unwrap() as usize * self.ram_bank as usize);
if let Some(elem) = self.ram.get_mut(address) { if let Some(elem) = self.ram.get_mut(address) {
*elem = data; *elem = data;
} }