Fix LCD status when turned on or off

This commit is contained in:
Franco Colmenarez 2021-11-12 19:52:46 -05:00
parent 7beeae01fa
commit d90a25e6f6
4 changed files with 38 additions and 13 deletions

View File

@ -138,9 +138,13 @@ impl Bus {
self.reset_timer = true; self.reset_timer = true;
} else if address == LCD_CONTROL_ADDRESS { } else if address == LCD_CONTROL_ADDRESS {
self.data[address as usize] = data; self.data[address as usize] = data;
// Check if LCD is being turned on // Check if LCD is being turned on or off
if get_bit(data, BitIndex::I7) && !get_bit(self.data[address as usize], BitIndex::I7) { if (get_bit(data, BitIndex::I7) && !get_bit(self.data[address as usize], BitIndex::I7)) ||
!get_bit(data, BitIndex::I7) {
self.data[LCD_Y_ADDRESS as usize] = 0x00; self.data[LCD_Y_ADDRESS as usize] = 0x00;
// Set Hblank
let byte = self.data[LCD_STATUS_ADDRESS as usize];
self.data[LCD_STATUS_ADDRESS as usize] = byte & 0b11111100;
} }
} else if address == LCD_Y_ADDRESS { } else if address == LCD_Y_ADDRESS {
// println!("Write to LCD_Y not allowed"); // println!("Write to LCD_Y not allowed");

View File

@ -857,7 +857,7 @@ impl CPU {
is_halted: false, is_halted: false,
ei_delay: false, ei_delay: false,
ime: true, ime: true,
enable_logs: !env::var("CPU_LOG").is_err(), enable_logs: !env::var("CPU_LOG").is_err() || !env::var("CPU_LOGS").is_err(),
} }
} }

View File

@ -23,8 +23,8 @@ pub const DMA_ADDRESS: u16 = 0xFF46;
pub const BACKGROUND_PALETTE_ADDRESS: u16 = 0xFF47; pub const BACKGROUND_PALETTE_ADDRESS: u16 = 0xFF47;
pub const OBJECT_PALETTE_0_ADDRESS: u16 = 0xFF48; pub const OBJECT_PALETTE_0_ADDRESS: u16 = 0xFF48;
pub const OBJECT_PALETTE_1_ADDRESS: u16 = 0xFF49; pub const OBJECT_PALETTE_1_ADDRESS: u16 = 0xFF49;
pub const WINDOW_X_ADDRESS: u16 = 0xFF4B;
pub const WINDOW_Y_ADDRESS: u16 = 0xFF4A; pub const WINDOW_Y_ADDRESS: u16 = 0xFF4A;
pub const WINDOW_X_ADDRESS: u16 = 0xFF4B;
pub const TILE_MAP_ADDRESS: u16 = 0x9800; pub const TILE_MAP_ADDRESS: u16 = 0x9800;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]

View File

@ -14,6 +14,7 @@ pub const RAM_SIZE_ADDRESS: u16 = 0x0149;
pub const ROM_SIZE_ADDRESS: u16 = 0x0148; pub const ROM_SIZE_ADDRESS: u16 = 0x0148;
pub const DESTINATION_CODE_ADDRESS: u16 = 0x014A; pub const DESTINATION_CODE_ADDRESS: u16 = 0x014A;
#[derive(Debug)]
enum Region { enum Region {
Japanese, Japanese,
NonJapanese, NonJapanese,
@ -37,11 +38,13 @@ enum MBC {
BandaiTIMA5, BandaiTIMA5,
} }
#[derive(Debug)]
enum BankingMode { enum BankingMode {
Simple, Simple,
Advanced, Advanced,
} }
#[derive(Debug)]
pub struct ROMInfo { pub struct ROMInfo {
mbc: MBC, mbc: MBC,
publisher: String, publisher: String,
@ -139,7 +142,7 @@ impl ROMInfo {
} }
pub fn ram_size(&self) -> usize { pub fn ram_size(&self) -> usize {
0x4000 * self.ram_banks as usize 0x2000 * self.ram_banks as usize
} }
} }
@ -160,8 +163,11 @@ impl ROM {
file.read_to_end(&mut data)?; file.read_to_end(&mut data)?;
let info = ROMInfo::from_bytes(&data); let info = ROMInfo::from_bytes(&data);
println!("has ram {}", info.has_ram); println!("MBC {:?}", info.mbc);
println!("mbc {:?}", info.mbc); println!("Has RAM {}", info.has_ram);
println!("ROM banks {}", info.rom_banks);
println!("RAM banks {}", info.ram_banks);
println!("Region {:?}", info.region);
let ram = Vec::with_capacity(info.ram_size() as usize); let ram = Vec::with_capacity(info.ram_size() as usize);
Ok(Self { Ok(Self {
@ -177,6 +183,12 @@ impl ROM {
pub fn read(&self, address: u16) -> u8 { pub fn read(&self, address: u16) -> u8 {
match self.info.mbc { match self.info.mbc {
MBC::NoMBC => {
return match self.data.get(address as usize) {
Some(data) => *data,
None => 0xFF,
};
},
MBC::MBC1 => { MBC::MBC1 => {
if BANK_ZERO.in_range(address) { if BANK_ZERO.in_range(address) {
return self.data[address as usize]; return self.data[address as usize];
@ -188,19 +200,26 @@ impl ROM {
if !self.info.has_ram { if !self.info.has_ram {
return 0xFF; return 0xFF;
} }
return self.ram[(address - EXTERNAL_RAM.begin() + (EXTERNAL_RAM.begin() * self.ram_bank as u16)) as usize]; return match self.ram.get((address - EXTERNAL_RAM.begin() + (0x2000 * self.ram_bank as u16)) as usize) {
Some(data) => *data,
None => 0xFF,
};
} }
unreachable!("ROM read: Address {} not valid", address);
}, },
_ => {}, _ => unimplemented!(),
} }
self.data[address as usize] self.data[address as usize]
} }
pub fn write(&mut self, address: u16, data: u8) { pub fn write(&mut self, address: u16, data: u8) {
match self.info.mbc { match self.info.mbc {
MBC::NoMBC => {},
MBC::MBC1 => { MBC::MBC1 => {
if address >= 0x0000 && address <= 0x1FFF { // RAM enable register if address >= 0x0000 && address <= 0x1FFF { // RAM enable register
if !self.info.has_ram {
return;
}
self.ram_enable = match data & 0x0F { self.ram_enable = match data & 0x0F {
0x0A => true, 0x0A => true,
_ => false, _ => false,
@ -218,12 +237,14 @@ impl ROM {
if !self.ram_enable || !self.info.has_ram { if !self.ram_enable || !self.info.has_ram {
return; return;
} }
let address = (address - EXTERNAL_RAM.begin() + (EXTERNAL_RAM.begin() * self.ram_bank as u16)) as usize; let address = address as usize - EXTERNAL_RAM.begin() as usize + (EXTERNAL_RAM.begin() as usize * self.ram_bank as usize);
self.ram[address] = data; if let Some(elem) = self.ram.get_mut(address) {
*elem = data;
}
self.switch_rom_bank(self.rom_bank + (data as u16 >> 5)); self.switch_rom_bank(self.rom_bank + (data as u16 >> 5));
} }
}, },
_ => {}, _ => unimplemented!(),
} }
} }