mirror of
https://github.com/FranLMSP/rmg-001.git
synced 2024-11-23 10:12:11 +00:00
Compare commits
3 Commits
569df9edd3
...
22c09895d7
Author | SHA1 | Date | |
---|---|---|---|
22c09895d7 | |||
2e3122f1e0 | |||
70ac674fe9 |
@ -16,11 +16,12 @@ Any help or suggestion is welcome!
|
|||||||
- [x] NoMBC
|
- [x] NoMBC
|
||||||
- [x] MBC1
|
- [x] MBC1
|
||||||
- [x] MBC2
|
- [x] MBC2
|
||||||
- [ ] MBC3
|
- [ ] MBC3 (partially implemented)
|
||||||
- [ ] MBC5
|
- [X] MBC5
|
||||||
- [ ] MBC6
|
- [ ] MBC6
|
||||||
- [ ] MBC7
|
- [ ] MBC7
|
||||||
- [ ] HuC1
|
- [ ] HuC1
|
||||||
|
- [ ] Save files
|
||||||
- [ ] Web Assembly support (because this is a Rust project and it has to support Web Assembly)
|
- [ ] Web Assembly support (because this is a Rust project and it has to support Web Assembly)
|
||||||
- [ ] Gameboy boot ROM (Not important for now)
|
- [ ] Gameboy boot ROM (Not important for now)
|
||||||
- [ ] Gameboy Color compatibility
|
- [ ] Gameboy Color compatibility
|
||||||
|
@ -306,9 +306,7 @@ impl PPU {
|
|||||||
self.lcd_control = data;
|
self.lcd_control = data;
|
||||||
// Check if LCD is being turned on or off
|
// Check if LCD is being turned on or off
|
||||||
self.lcd_enable = get_bit(data, BitIndex::I7);
|
self.lcd_enable = get_bit(data, BitIndex::I7);
|
||||||
if (get_bit(data, BitIndex::I7) && !get_bit(self.lcd_control, BitIndex::I7)) ||
|
if !get_bit(data, BitIndex::I7) || (get_bit(data, BitIndex::I7) && !get_bit(self.lcd_control, BitIndex::I7)) {
|
||||||
!get_bit(data, BitIndex::I7)
|
|
||||||
{
|
|
||||||
self.io_registers[LCD_Y_ADDRESS as usize - 0xFF40] = 0x00;
|
self.io_registers[LCD_Y_ADDRESS as usize - 0xFF40] = 0x00;
|
||||||
// Set Hblank
|
// Set Hblank
|
||||||
let byte = self.io_registers[LCD_STATUS_ADDRESS as usize - 0xFF40];
|
let byte = self.io_registers[LCD_STATUS_ADDRESS as usize - 0xFF40];
|
||||||
@ -366,6 +364,7 @@ impl PPU {
|
|||||||
}
|
}
|
||||||
} else if self.lcd_y >= 144 && !self.get_lcd_status(LCDStatus::ModeFlag(LCDStatusModeFlag::VBlank)) {
|
} else if self.lcd_y >= 144 && !self.get_lcd_status(LCDStatus::ModeFlag(LCDStatusModeFlag::VBlank)) {
|
||||||
// Mode 1 Vertical blank
|
// Mode 1 Vertical blank
|
||||||
|
self.window_y_counter = 0;
|
||||||
self.set_lcd_status(LCDStatus::ModeFlag(LCDStatusModeFlag::VBlank), true);
|
self.set_lcd_status(LCDStatus::ModeFlag(LCDStatusModeFlag::VBlank), true);
|
||||||
self.set_interrupt(Interrupt::VBlank, true);
|
self.set_interrupt(Interrupt::VBlank, true);
|
||||||
self.stat_interrupt();
|
self.stat_interrupt();
|
||||||
@ -382,7 +381,6 @@ impl PPU {
|
|||||||
// Frame completed
|
// Frame completed
|
||||||
if self.lcd_y > 153 {
|
if self.lcd_y > 153 {
|
||||||
self.lcd_y = 0;
|
self.lcd_y = 0;
|
||||||
self.window_y_counter = 0;
|
|
||||||
}
|
}
|
||||||
self.force_set_register(LCD_Y_ADDRESS, self.lcd_y);
|
self.force_set_register(LCD_Y_ADDRESS, self.lcd_y);
|
||||||
self.stat_interrupt();
|
self.stat_interrupt();
|
||||||
|
188
src/rom.rs
188
src/rom.rs
@ -65,6 +65,7 @@ pub fn load_rom(filename: &str) -> std::io::Result<Box<dyn ROM>> {
|
|||||||
MBC::MBC1 => Box::new(MBC1::new(data, info)),
|
MBC::MBC1 => Box::new(MBC1::new(data, info)),
|
||||||
MBC::MBC2 => Box::new(MBC2::new(data, info)),
|
MBC::MBC2 => Box::new(MBC2::new(data, info)),
|
||||||
MBC::MBC3 => Box::new(MBC3::new(data, info)),
|
MBC::MBC3 => Box::new(MBC3::new(data, info)),
|
||||||
|
MBC::MBC5 => Box::new(MBC5::new(data, info)),
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -91,7 +92,7 @@ enum Region {
|
|||||||
NonJapanese,
|
NonJapanese,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, PartialEq)]
|
||||||
enum BankingMode {
|
enum BankingMode {
|
||||||
Simple,
|
Simple,
|
||||||
Advanced,
|
Advanced,
|
||||||
@ -194,6 +195,10 @@ impl ROMInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn rom_size(&self) -> usize {
|
||||||
|
0x4000 * self.rom_banks as usize
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ram_size(&self) -> usize {
|
pub fn ram_size(&self) -> usize {
|
||||||
0x2000 * self.ram_banks as usize
|
0x2000 * self.ram_banks as usize
|
||||||
}
|
}
|
||||||
@ -240,6 +245,7 @@ pub struct MBC1 {
|
|||||||
rom_bank: u16,
|
rom_bank: u16,
|
||||||
ram_bank: u8,
|
ram_bank: u8,
|
||||||
ram_enable: bool,
|
ram_enable: bool,
|
||||||
|
is_large_rom: bool,
|
||||||
banking_mode: BankingMode,
|
banking_mode: BankingMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,7 +256,8 @@ impl MBC1 {
|
|||||||
println!("Has RAM {}", info.has_ram);
|
println!("Has RAM {}", info.has_ram);
|
||||||
println!("ROM banks {}", info.rom_banks);
|
println!("ROM banks {}", info.rom_banks);
|
||||||
println!("RAM banks {}", info.ram_banks);
|
println!("RAM banks {}", info.ram_banks);
|
||||||
let ram = Vec::with_capacity(info.ram_size() as usize);
|
let ram = vec![0; info.ram_size() as usize];
|
||||||
|
let is_large_rom = info.rom_size() >= 1048576;
|
||||||
Self {
|
Self {
|
||||||
data,
|
data,
|
||||||
info,
|
info,
|
||||||
@ -258,31 +265,73 @@ impl MBC1 {
|
|||||||
rom_bank: 1,
|
rom_bank: 1,
|
||||||
ram_bank: 0,
|
ram_bank: 0,
|
||||||
ram_enable: false,
|
ram_enable: false,
|
||||||
|
is_large_rom,
|
||||||
banking_mode: BankingMode::Simple,
|
banking_mode: BankingMode::Simple,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn switch_rom_bank(&mut self, bank: u16) {
|
fn switch_rom_bank(&mut self, bank: u8) {
|
||||||
self.rom_bank = bank;
|
self.rom_bank = bank as u16 & 0b11111;
|
||||||
if self.rom_bank > self.info.rom_banks.saturating_sub(1) {
|
if self.rom_bank == 0 {
|
||||||
self.rom_bank = self.info.rom_banks.saturating_sub(1);
|
|
||||||
} else if self.rom_bank == 0 {
|
|
||||||
self.rom_bank = 1;
|
self.rom_bank = 1;
|
||||||
}
|
}
|
||||||
|
if self.rom_bank > self.info.rom_banks.saturating_sub(1) {
|
||||||
|
self.rom_bank = self.info.rom_banks.saturating_sub(1);
|
||||||
|
}
|
||||||
|
//println!("switched to ROM bank {}", self.rom_bank);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn switch_ram_bank(&mut self, bank: u8) {
|
||||||
|
self.ram_bank = bank & 0b11;
|
||||||
|
// println!("switched to RAM bank {}", self.ram_bank);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_bank_zero_address(&self, address: u16) -> usize {
|
||||||
|
if !self.is_large_rom {
|
||||||
|
return address as usize;
|
||||||
|
}
|
||||||
|
match self.banking_mode {
|
||||||
|
BankingMode::Simple => address as usize,
|
||||||
|
BankingMode::Advanced => {
|
||||||
|
((self.ram_bank as usize) << 5) * ((address & 0x3FFF) as usize)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_bank_switchable_address(&self, address: u16) -> usize {
|
||||||
|
if self.is_large_rom {
|
||||||
|
let rom_bank = ((self.ram_bank as u16) << 5) | (self.rom_bank & 0b11111);
|
||||||
|
return ((rom_bank as usize) << 14) | (address & 0x3FFF) as usize;
|
||||||
|
}
|
||||||
|
return ((self.rom_bank as usize) << 14) | (address & 0x3FFF) as usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_ram_address(&self, address: u16) -> usize {
|
||||||
|
let bank = match self.banking_mode {
|
||||||
|
BankingMode::Simple => 0,
|
||||||
|
BankingMode::Advanced => self.ram_bank,
|
||||||
|
};
|
||||||
|
((bank as usize) << 11) + ((address as usize) & 0x1FFF)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ROM for MBC1 {
|
impl ROM for MBC1 {
|
||||||
fn read(&self, address: u16) -> u8 {
|
fn read(&self, address: u16) -> u8 {
|
||||||
if BANK_ZERO.contains(&address) {
|
if BANK_ZERO.contains(&address) {
|
||||||
return self.data[address as usize];
|
return match self.data.get(self.get_bank_zero_address(address)) {
|
||||||
|
Some(byte) => *byte,
|
||||||
|
None => 0xFF,
|
||||||
|
};
|
||||||
} else if BANK_SWITCHABLE.contains(&address) {
|
} else if BANK_SWITCHABLE.contains(&address) {
|
||||||
return self.data[((self.rom_bank as usize * 0x4000) + (address as usize & 0x3FFF)) as usize];
|
return match self.data.get(self.get_bank_switchable_address(address)) {
|
||||||
|
Some(byte) => *byte,
|
||||||
|
None => 0xFF,
|
||||||
|
};
|
||||||
} else if EXTERNAL_RAM.contains(&address) {
|
} else if EXTERNAL_RAM.contains(&address) {
|
||||||
if !self.info.has_ram {
|
if !self.info.has_ram || !self.ram_enable {
|
||||||
return 0xFF;
|
return 0xFF;
|
||||||
}
|
}
|
||||||
return match self.ram.get((address - EXTERNAL_RAM.min().unwrap() + (0x2000 * self.ram_bank as u16)) as usize) {
|
return match self.ram.get(self.get_ram_address(address)) {
|
||||||
Some(data) => *data,
|
Some(data) => *data,
|
||||||
None => 0xFF,
|
None => 0xFF,
|
||||||
};
|
};
|
||||||
@ -301,26 +350,22 @@ impl ROM for MBC1 {
|
|||||||
};
|
};
|
||||||
return;
|
return;
|
||||||
} else if address >= 0x2000 && address <= 0x3FFF { // ROM bank number register
|
} else if address >= 0x2000 && address <= 0x3FFF { // ROM bank number register
|
||||||
// println!("Switch bank to {:02X}", data);
|
self.switch_rom_bank(data);
|
||||||
self.switch_rom_bank(data as u16 & 0b00011111);
|
|
||||||
} else if address >= 0x4000 && address <= 0x5FFF { // ROM and RAM bank number register
|
} else if address >= 0x4000 && address <= 0x5FFF { // ROM and RAM bank number register
|
||||||
// println!("RAM bank {:02X}", data);
|
self.switch_ram_bank(data);
|
||||||
self.ram_bank = data & 0b11;
|
|
||||||
} else if address >= 0x6000 && address <= 0x7FFF { // Banking mode select
|
} else if address >= 0x6000 && address <= 0x7FFF { // Banking mode select
|
||||||
self.banking_mode = match data & 1 {
|
self.banking_mode = match (data & 1) == 0 {
|
||||||
0 => BankingMode::Simple,
|
true => BankingMode::Simple,
|
||||||
1 => BankingMode::Advanced,
|
false => BankingMode::Advanced,
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
}
|
||||||
} else if EXTERNAL_RAM.contains(&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.min().unwrap() as usize + (EXTERNAL_RAM.min().unwrap() as usize * self.ram_bank as usize);
|
let address = self.get_ram_address(address);
|
||||||
if let Some(elem) = self.ram.get_mut(address) {
|
if let Some(elem) = self.ram.get_mut(address) {
|
||||||
*elem = data;
|
*elem = data;
|
||||||
}
|
}
|
||||||
self.switch_rom_bank(self.rom_bank + (data as u16 >> 5));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -330,7 +375,6 @@ pub struct MBC2 {
|
|||||||
info: ROMInfo,
|
info: ROMInfo,
|
||||||
ram: Vec<u8>,
|
ram: Vec<u8>,
|
||||||
rom_bank: u16,
|
rom_bank: u16,
|
||||||
ram_bank: u8,
|
|
||||||
ram_enable: bool,
|
ram_enable: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,19 +385,18 @@ impl MBC2 {
|
|||||||
println!("Has RAM {}", info.has_ram);
|
println!("Has RAM {}", info.has_ram);
|
||||||
println!("ROM banks {}", info.rom_banks);
|
println!("ROM banks {}", info.rom_banks);
|
||||||
println!("RAM banks {}", info.ram_banks);
|
println!("RAM banks {}", info.ram_banks);
|
||||||
let ram = Vec::with_capacity(info.ram_size() as usize);
|
let ram = vec![0; 0x200];
|
||||||
Self {
|
Self {
|
||||||
data,
|
data,
|
||||||
info,
|
info,
|
||||||
ram,
|
ram,
|
||||||
rom_bank: 1,
|
rom_bank: 1,
|
||||||
ram_bank: 0,
|
|
||||||
ram_enable: false,
|
ram_enable: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn switch_rom_bank(&mut self, bank: u16) {
|
fn switch_rom_bank(&mut self, bank: u16) {
|
||||||
self.rom_bank = bank;
|
self.rom_bank = bank & 0b1111;
|
||||||
if self.rom_bank > self.info.rom_banks.saturating_sub(1) {
|
if self.rom_bank > self.info.rom_banks.saturating_sub(1) {
|
||||||
self.rom_bank = self.info.rom_banks.saturating_sub(1);
|
self.rom_bank = self.info.rom_banks.saturating_sub(1);
|
||||||
} else if self.rom_bank == 0 {
|
} else if self.rom_bank == 0 {
|
||||||
@ -367,23 +410,22 @@ impl ROM for MBC2 {
|
|||||||
if BANK_ZERO.contains(&address) {
|
if BANK_ZERO.contains(&address) {
|
||||||
return self.data[address as usize];
|
return self.data[address as usize];
|
||||||
} else if BANK_SWITCHABLE.contains(&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 % 0x4000)) as usize];
|
||||||
} else if address >= 0xA000 && address <= 0xA1FF {
|
} else if address >= 0xA000 {
|
||||||
if !self.info.has_ram || !self.ram_enable {
|
let address = (address as usize) & 0x1FF;
|
||||||
|
if !self.ram_enable {
|
||||||
return 0xFF;
|
return 0xFF;
|
||||||
}
|
}
|
||||||
return match self.ram.get((address - EXTERNAL_RAM.min().unwrap() + (0x2000 * self.ram_bank as u16)) as usize) {
|
return match self.ram.get(address as usize) {
|
||||||
Some(data) => *data,
|
Some(data) => *data,
|
||||||
None => 0xFF,
|
None => 0xFF,
|
||||||
};
|
};
|
||||||
} else if address >= 0xA200 && address <= 0xBFFF {
|
|
||||||
return self.read(0xA000 + (address % 0x0200));
|
|
||||||
}
|
}
|
||||||
return 0xFF;
|
return 0xFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self, address: u16, data: u8) {
|
fn write(&mut self, address: u16, data: u8) {
|
||||||
if BANK_SWITCHABLE.contains(&address) {
|
if BANK_ZERO.contains(&address) {
|
||||||
if address.to_be_bytes()[0] & 1 == 0 {
|
if address.to_be_bytes()[0] & 1 == 0 {
|
||||||
match data {
|
match data {
|
||||||
0x0A => self.ram_enable = true,
|
0x0A => self.ram_enable = true,
|
||||||
@ -392,6 +434,14 @@ impl ROM for MBC2 {
|
|||||||
} else {
|
} else {
|
||||||
self.switch_rom_bank(data as u16);
|
self.switch_rom_bank(data as u16);
|
||||||
}
|
}
|
||||||
|
} else if address >= 0xA000 {
|
||||||
|
if !self.ram_enable {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let address = address & 0x1FF;
|
||||||
|
if let Some(elem) = self.ram.get_mut(address as usize) {
|
||||||
|
*elem = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -412,7 +462,7 @@ impl MBC3 {
|
|||||||
println!("Has RAM {}", info.has_ram);
|
println!("Has RAM {}", info.has_ram);
|
||||||
println!("ROM banks {}", info.rom_banks);
|
println!("ROM banks {}", info.rom_banks);
|
||||||
println!("RAM banks {}", info.ram_banks);
|
println!("RAM banks {}", info.ram_banks);
|
||||||
let ram = Vec::with_capacity(info.ram_size() as usize);
|
let ram = vec![0; info.ram_size() as usize];
|
||||||
Self {
|
Self {
|
||||||
data,
|
data,
|
||||||
info,
|
info,
|
||||||
@ -471,3 +521,75 @@ impl ROM for MBC3 {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct MBC5 {
|
||||||
|
data: Vec<u8>,
|
||||||
|
info: ROMInfo,
|
||||||
|
ram: Vec<u8>,
|
||||||
|
rom_bank: u16,
|
||||||
|
ram_bank: u8,
|
||||||
|
ram_enable: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MBC5 {
|
||||||
|
fn new(data: Vec<u8>, info: ROMInfo) -> Self {
|
||||||
|
println!("MBC {:?}", info.mbc);
|
||||||
|
println!("Region {:?}", info.region);
|
||||||
|
println!("Has RAM {}", info.has_ram);
|
||||||
|
println!("ROM banks {}", info.rom_banks);
|
||||||
|
println!("RAM banks {}", info.ram_banks);
|
||||||
|
let ram = vec![0; info.ram_size() as usize];
|
||||||
|
Self {
|
||||||
|
data,
|
||||||
|
info,
|
||||||
|
ram,
|
||||||
|
rom_bank: 1,
|
||||||
|
ram_bank: 0,
|
||||||
|
ram_enable: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ROM for MBC5 {
|
||||||
|
fn read(&self, address: u16) -> u8 {
|
||||||
|
if BANK_ZERO.contains(&address) {
|
||||||
|
return self.data[address as usize];
|
||||||
|
} else if BANK_SWITCHABLE.contains(&address) {
|
||||||
|
return match self.data.get(((self.rom_bank as usize * 0x4000) + (address as usize % 0x4000)) as usize) {
|
||||||
|
Some(byte) => *byte,
|
||||||
|
None => 0xFF,
|
||||||
|
};
|
||||||
|
} else if EXTERNAL_RAM.contains(&address) {
|
||||||
|
if !self.info.has_ram || !self.ram_enable {
|
||||||
|
return 0xFF;
|
||||||
|
}
|
||||||
|
return match self.ram.get(((self.ram_bank as usize * 0x2000) + (address as usize % 0x2000)) as usize) {
|
||||||
|
Some(data) => *data,
|
||||||
|
None => 0xFF,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write(&mut self, address: u16, data: u8) {
|
||||||
|
if address <= 0x1FFF {
|
||||||
|
match data & 0b1111 {
|
||||||
|
0x0A => self.ram_enable = true,
|
||||||
|
_ => self.ram_enable = false,
|
||||||
|
};
|
||||||
|
} else if address >= 0x2000 && address <= 0x2FFF {
|
||||||
|
self.rom_bank = data as u16;
|
||||||
|
} else if address >= 0x3000 && address <= 0x3FFF {
|
||||||
|
self.rom_bank = ((data & 1) as u16) | (self.rom_bank & 0xF);
|
||||||
|
} else if address >= 0x4000 && address <= 0x5FFF {
|
||||||
|
self.ram_bank = data & 0b1111;
|
||||||
|
} else if EXTERNAL_RAM.contains(&address) {
|
||||||
|
if !self.ram_enable || !self.info.has_ram {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if let Some(elem) = self.ram.get_mut(((self.ram_bank as usize * 0x2000) + (address as usize % 0x2000)) as usize) {
|
||||||
|
*elem = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user