2020-12-24 06:27:06 +00:00
|
|
|
use super::cartridge::Cartridge;
|
2021-03-16 06:05:13 +00:00
|
|
|
use super::high_ram::HighRam;
|
2021-03-27 17:10:18 +00:00
|
|
|
use super::instruction::Cycle;
|
2021-03-21 00:53:56 +00:00
|
|
|
use super::interrupt::{Interrupt, InterruptFlag};
|
2021-03-21 02:11:45 +00:00
|
|
|
use super::joypad::Joypad;
|
2021-03-16 06:05:13 +00:00
|
|
|
use super::ppu::Ppu;
|
2021-01-19 07:36:44 +00:00
|
|
|
use super::serial::Serial;
|
2021-01-03 08:05:46 +00:00
|
|
|
use super::sound::Sound;
|
2021-01-03 07:21:19 +00:00
|
|
|
use super::timer::Timer;
|
2021-03-16 06:05:13 +00:00
|
|
|
use super::work_ram::{VariableWorkRam, WorkRam};
|
2021-03-27 16:56:47 +00:00
|
|
|
use std::{fs::File, io::Read};
|
2021-01-03 07:38:31 +00:00
|
|
|
|
2021-04-09 01:32:32 +00:00
|
|
|
const BOOT_ROM_SIZE: usize = 0x100;
|
2021-03-21 01:22:31 +00:00
|
|
|
|
2020-12-24 01:39:37 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-12-23 09:43:49 +00:00
|
|
|
pub struct Bus {
|
2021-03-21 01:22:31 +00:00
|
|
|
boot: Option<[u8; BOOT_ROM_SIZE]>, // Boot ROM is 256b long
|
2020-12-24 01:39:37 +00:00
|
|
|
cartridge: Option<Cartridge>,
|
2021-03-16 06:05:13 +00:00
|
|
|
pub ppu: Ppu,
|
2021-04-27 09:06:08 +00:00
|
|
|
work_ram: WorkRam,
|
|
|
|
var_ram: VariableWorkRam,
|
2021-01-03 07:21:19 +00:00
|
|
|
timer: Timer,
|
2021-04-04 06:52:53 +00:00
|
|
|
int: Interrupt,
|
2021-01-03 08:05:46 +00:00
|
|
|
sound: Sound,
|
2021-04-27 09:06:08 +00:00
|
|
|
high_ram: HighRam,
|
2021-01-19 07:36:44 +00:00
|
|
|
serial: Serial,
|
2021-03-21 02:11:45 +00:00
|
|
|
joypad: Joypad,
|
2020-12-23 09:43:49 +00:00
|
|
|
}
|
2020-12-23 09:25:16 +00:00
|
|
|
|
2020-12-23 09:43:49 +00:00
|
|
|
impl Default for Bus {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-01-19 07:36:44 +00:00
|
|
|
boot: None,
|
2020-12-24 01:39:37 +00:00
|
|
|
cartridge: None,
|
2020-12-24 06:27:06 +00:00
|
|
|
ppu: Default::default(),
|
2021-04-27 09:06:08 +00:00
|
|
|
work_ram: Default::default(),
|
|
|
|
var_ram: Default::default(),
|
2021-01-03 07:21:19 +00:00
|
|
|
timer: Default::default(),
|
2021-04-04 06:52:53 +00:00
|
|
|
int: Default::default(),
|
2021-01-03 08:05:46 +00:00
|
|
|
sound: Default::default(),
|
2021-04-27 09:06:08 +00:00
|
|
|
high_ram: Default::default(),
|
2021-01-19 07:36:44 +00:00
|
|
|
serial: Default::default(),
|
2021-03-21 02:11:45 +00:00
|
|
|
joypad: Default::default(),
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bus {
|
2021-03-23 02:41:22 +00:00
|
|
|
pub fn with_boot(path: &str) -> anyhow::Result<Self> {
|
|
|
|
let mut file = File::open(path)?;
|
2021-03-24 02:21:18 +00:00
|
|
|
let mut boot_rom = [0u8; 256];
|
2021-01-28 04:07:31 +00:00
|
|
|
|
2021-03-24 02:21:18 +00:00
|
|
|
file.read_exact(&mut boot_rom)?;
|
2021-01-28 04:07:31 +00:00
|
|
|
|
2021-03-23 02:41:22 +00:00
|
|
|
Ok(Self {
|
2021-01-28 04:07:31 +00:00
|
|
|
boot: Some(boot_rom),
|
2020-12-24 03:24:27 +00:00
|
|
|
..Default::default()
|
2021-03-23 02:41:22 +00:00
|
|
|
})
|
2020-12-23 09:43:49 +00:00
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
|
2021-03-23 02:41:22 +00:00
|
|
|
pub fn load_cartridge(&mut self, path: &str) -> std::io::Result<()> {
|
|
|
|
self.cartridge = Some(Cartridge::new(path)?);
|
|
|
|
Ok(())
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
2021-01-19 04:54:38 +00:00
|
|
|
|
2021-04-14 06:21:45 +00:00
|
|
|
pub fn rom_title(&self) -> Option<&str> {
|
|
|
|
self.cartridge.as_ref()?.title()
|
|
|
|
}
|
|
|
|
|
2021-03-27 17:10:18 +00:00
|
|
|
pub fn step(&mut self, cycles: Cycle) {
|
2021-04-04 06:52:53 +00:00
|
|
|
self.ppu.step(cycles);
|
2021-03-19 02:06:01 +00:00
|
|
|
self.timer.step(cycles);
|
|
|
|
self.sound.step(cycles);
|
2021-01-19 04:54:38 +00:00
|
|
|
}
|
2020-12-23 09:43:49 +00:00
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
|
|
|
|
impl Bus {
|
2020-09-04 05:41:19 +00:00
|
|
|
pub fn read_byte(&self, addr: u16) -> u8 {
|
2020-12-23 09:25:16 +00:00
|
|
|
match addr {
|
2020-12-24 01:39:37 +00:00
|
|
|
0x0000..=0x3FFF => {
|
|
|
|
// 16KB ROM bank 00
|
2021-03-24 02:01:33 +00:00
|
|
|
if addr < 0x100 {
|
2021-03-16 03:08:47 +00:00
|
|
|
if let Some(boot) = self.boot {
|
|
|
|
return boot[addr as usize];
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 03:08:47 +00:00
|
|
|
|
|
|
|
match self.cartridge.as_ref() {
|
|
|
|
Some(cart) => cart.read_byte(addr),
|
2021-03-21 08:03:03 +00:00
|
|
|
None => panic!("Tried to read from a non-existent cartridge"),
|
2021-03-16 03:08:47 +00:00
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
2021-03-16 03:08:47 +00:00
|
|
|
0x4000..=0x7FFF => match self.cartridge.as_ref() {
|
2020-12-24 01:39:37 +00:00
|
|
|
// 16KB ROM Bank 01 -> NN (switchable via MB)
|
|
|
|
Some(cart) => cart.read_byte(addr),
|
2021-03-21 08:03:03 +00:00
|
|
|
None => panic!("Tried to read from a non-existent cartridge"),
|
2020-12-24 01:39:37 +00:00
|
|
|
},
|
|
|
|
0x8000..=0x9FFF => {
|
|
|
|
// 8KB Video RAM
|
2021-03-17 05:29:36 +00:00
|
|
|
self.ppu.read_byte(addr)
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
2021-03-16 03:08:47 +00:00
|
|
|
0xA000..=0xBFFF => match self.cartridge.as_ref() {
|
2020-12-24 01:39:37 +00:00
|
|
|
// 8KB External RAM
|
2021-01-20 07:39:24 +00:00
|
|
|
Some(cart) => cart.read_byte(addr),
|
2021-03-23 03:33:56 +00:00
|
|
|
None => panic!("Tried to read from a non-existent cartridge"),
|
2021-01-20 07:39:24 +00:00
|
|
|
},
|
2020-12-24 01:39:37 +00:00
|
|
|
0xC000..=0xCFFF => {
|
|
|
|
// 4KB Work RAM Bank 0
|
2021-04-27 09:06:08 +00:00
|
|
|
self.work_ram.read_byte(addr)
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
0xD000..=0xDFFF => {
|
|
|
|
// 4KB Work RAM Bank 1 -> N
|
2021-04-27 09:06:08 +00:00
|
|
|
self.var_ram.read_byte(addr)
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
0xE000..=0xFDFF => {
|
|
|
|
// Mirror of 0xC000 to 0xDDFF
|
2021-04-08 22:58:20 +00:00
|
|
|
// ECHO RAM
|
|
|
|
|
|
|
|
match addr {
|
|
|
|
0xE000..=0xEFFF => {
|
|
|
|
// 4KB Work RAM Bank 0
|
2021-04-27 09:06:08 +00:00
|
|
|
self.work_ram.read_byte(addr)
|
2021-04-08 22:58:20 +00:00
|
|
|
}
|
|
|
|
0xF000..=0xFDFF => {
|
|
|
|
// 4KB Work RAM Bank 1 -> N
|
2021-04-27 09:06:08 +00:00
|
|
|
self.var_ram.read_byte(addr)
|
2021-04-08 22:58:20 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!("{:#06X} was incorrectly handled by ECHO RAM", addr),
|
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
0xFE00..=0xFE9F => {
|
2021-03-16 00:19:40 +00:00
|
|
|
// Sprite Attribute Table
|
2021-04-09 01:28:16 +00:00
|
|
|
self.ppu.oam.read_byte(addr)
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
2021-04-09 00:22:55 +00:00
|
|
|
0xFEA0..=0xFEFF => {
|
2021-05-03 08:27:23 +00:00
|
|
|
// eprintln!("Read from {:#06X}, which is prohibited", addr);
|
2021-04-09 00:22:55 +00:00
|
|
|
// TODO: Properly Emulate what can happen here
|
|
|
|
0x00
|
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
0xFF00..=0xFF7F => {
|
|
|
|
// IO Registers
|
2021-01-03 07:21:19 +00:00
|
|
|
match addr {
|
2021-03-21 02:11:45 +00:00
|
|
|
0xFF00 => self.joypad.status.into(),
|
2021-01-19 07:40:07 +00:00
|
|
|
0xFF01 => self.serial.next,
|
|
|
|
0xFF02 => self.serial.control.into(),
|
2021-03-27 16:56:47 +00:00
|
|
|
0xFF04 => (self.timer.divider >> 8) as u8,
|
2021-03-21 07:01:19 +00:00
|
|
|
0xFF05 => self.timer.counter,
|
2021-03-21 08:03:03 +00:00
|
|
|
0xFF06 => self.timer.modulo,
|
2021-01-03 07:21:19 +00:00
|
|
|
0xFF07 => self.timer.control.into(),
|
2021-03-21 00:53:56 +00:00
|
|
|
0xFF0F => self.interrupt_flag().into(),
|
2021-01-17 23:33:12 +00:00
|
|
|
0xFF11 => self.sound.ch1.sound_duty.into(),
|
|
|
|
0xFF12 => self.sound.ch1.vol_envelope.into(),
|
2021-03-16 02:16:11 +00:00
|
|
|
0xFF14 => self.sound.ch1.freq_hi.into(),
|
2021-01-18 00:58:57 +00:00
|
|
|
0xFF24 => self.sound.control.channel.into(),
|
2021-03-16 02:16:11 +00:00
|
|
|
0xFF25 => self.sound.control.output.into(),
|
2021-01-18 00:58:57 +00:00
|
|
|
0xFF26 => self.sound.control.status.into(),
|
2021-04-09 05:35:41 +00:00
|
|
|
0xFF40 => self.ppu.control.into(),
|
2021-01-18 08:22:45 +00:00
|
|
|
0xFF41 => self.ppu.stat.into(),
|
|
|
|
0xFF42 => self.ppu.pos.scroll_y,
|
|
|
|
0xFF43 => self.ppu.pos.scroll_x,
|
|
|
|
0xFF44 => self.ppu.pos.line_y,
|
2021-03-17 03:52:43 +00:00
|
|
|
0xFF45 => self.ppu.pos.ly_compare as u8,
|
2021-01-18 01:25:53 +00:00
|
|
|
0xFF47 => self.ppu.monochrome.bg_palette.into(),
|
2021-03-16 07:36:09 +00:00
|
|
|
0xFF48 => self.ppu.monochrome.obj_palette_0.into(),
|
|
|
|
0xFF49 => self.ppu.monochrome.obj_palette_1.into(),
|
2021-03-17 03:52:43 +00:00
|
|
|
0xFF4A => self.ppu.pos.window_y,
|
|
|
|
0xFF4B => self.ppu.pos.window_x,
|
2021-04-09 01:28:16 +00:00
|
|
|
0xFF4D => 0x00, // Reading from this address is useful on the CGB only
|
|
|
|
0xFF7F => 0x00, // Don't think this address is used for anything
|
2021-01-03 07:21:19 +00:00
|
|
|
_ => unimplemented!("Unable to read {:#06X} in I/O Registers", addr),
|
|
|
|
}
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
0xFF80..=0xFFFE => {
|
|
|
|
// High RAM
|
2021-04-27 09:06:08 +00:00
|
|
|
self.high_ram.read_byte(addr)
|
2020-12-24 01:39:37 +00:00
|
|
|
}
|
|
|
|
0xFFFF => {
|
2021-03-16 00:19:40 +00:00
|
|
|
// Interrupts Enable Register
|
2021-04-04 06:52:53 +00:00
|
|
|
self.int.enable.into()
|
2020-12-23 09:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:43:49 +00:00
|
|
|
pub fn write_byte(&mut self, addr: u16, byte: u8) {
|
2020-12-24 03:24:27 +00:00
|
|
|
match addr {
|
|
|
|
0x0000..=0x3FFF => {
|
|
|
|
// 16KB ROM bank 00
|
2021-01-20 07:39:24 +00:00
|
|
|
match self.cartridge.as_mut() {
|
|
|
|
Some(cart) => cart.write_byte(addr, byte),
|
2021-03-23 03:33:56 +00:00
|
|
|
None => panic!("Tried to write into non-existent cartridge"),
|
2021-01-20 07:39:24 +00:00
|
|
|
}
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0x4000..=0x7FFF => {
|
|
|
|
// 16KB ROM Bank 01 -> NN (switchable via MB)
|
2021-01-20 07:39:24 +00:00
|
|
|
match self.cartridge.as_mut() {
|
|
|
|
Some(cart) => cart.write_byte(addr, byte),
|
2021-03-23 03:33:56 +00:00
|
|
|
None => panic!("Tried to write into non-existent cartridge"),
|
2021-01-20 07:39:24 +00:00
|
|
|
}
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0x8000..=0x9FFF => {
|
|
|
|
// 8KB Video RAM
|
2021-03-17 05:29:36 +00:00
|
|
|
self.ppu.write_byte(addr, byte);
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0xA000..=0xBFFF => {
|
|
|
|
// 8KB External RAM
|
2021-01-20 07:39:24 +00:00
|
|
|
match self.cartridge.as_mut() {
|
|
|
|
Some(cart) => cart.write_byte(addr, byte),
|
2021-03-23 03:33:56 +00:00
|
|
|
None => panic!("Tried to write into non-existent cartridge"),
|
2021-01-20 07:39:24 +00:00
|
|
|
}
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0xC000..=0xCFFF => {
|
|
|
|
// 4KB Work RAM Bank 0
|
2021-04-27 09:06:08 +00:00
|
|
|
self.work_ram.write_byte(addr, byte);
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0xD000..=0xDFFF => {
|
|
|
|
// 4KB Work RAM Bank 1 -> N
|
2021-04-27 09:06:08 +00:00
|
|
|
self.var_ram.write_byte(addr, byte);
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0xE000..=0xFDFF => {
|
|
|
|
// Mirror of 0xC000 to 0xDDFF
|
2021-04-08 22:58:20 +00:00
|
|
|
// ECHO RAM
|
|
|
|
|
|
|
|
match addr {
|
|
|
|
0xE000..=0xEFFF => {
|
|
|
|
// 4KB Work RAM Bank 0
|
2021-04-27 09:06:08 +00:00
|
|
|
self.work_ram.write_byte(addr, byte);
|
2021-04-08 22:58:20 +00:00
|
|
|
}
|
|
|
|
0xF000..=0xFDFF => {
|
|
|
|
// 4KB Work RAM Bank 1 -> N
|
2021-04-27 09:06:08 +00:00
|
|
|
self.var_ram.write_byte(addr, byte);
|
2021-04-08 22:58:20 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!("{:#06X} was incorrectly handled by ECHO RAM", addr),
|
|
|
|
}
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0xFE00..=0xFE9F => {
|
2021-03-16 00:19:40 +00:00
|
|
|
// Sprite Attribute Table
|
2021-04-09 01:28:16 +00:00
|
|
|
self.ppu.oam.write_byte(addr, byte);
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
2021-04-09 00:22:55 +00:00
|
|
|
0xFEA0..=0xFEFF => {
|
2021-05-03 08:27:23 +00:00
|
|
|
// eprintln!("Wrote {:#04X} to {:#06X}, which is prohibited", byte, addr);
|
2021-04-09 00:22:55 +00:00
|
|
|
// TODO: Properly emulate what can happen here
|
|
|
|
}
|
2020-12-24 03:24:27 +00:00
|
|
|
0xFF00..=0xFF7F => {
|
|
|
|
// IO Registers
|
2021-01-03 07:21:19 +00:00
|
|
|
match addr {
|
2021-03-21 02:11:45 +00:00
|
|
|
0xFF00 => self.joypad.status = byte.into(),
|
2021-01-19 07:40:07 +00:00
|
|
|
0xFF01 => self.serial.next = byte,
|
|
|
|
0xFF02 => self.serial.control = byte.into(),
|
2021-03-21 08:03:03 +00:00
|
|
|
0xFF04 => self.timer.divider = 0x00,
|
2021-04-04 06:19:39 +00:00
|
|
|
0xFF05 => self.timer.counter = byte,
|
|
|
|
0xFF06 => self.timer.modulo = byte,
|
2021-01-03 07:21:19 +00:00
|
|
|
0xFF07 => self.timer.control = byte.into(),
|
2021-03-21 00:53:56 +00:00
|
|
|
0xFF0F => self.set_interrupt_flag(byte),
|
2021-01-17 23:33:12 +00:00
|
|
|
0xFF11 => self.sound.ch1.sound_duty = byte.into(),
|
|
|
|
0xFF12 => self.sound.ch1.vol_envelope = byte.into(),
|
2021-03-16 02:16:11 +00:00
|
|
|
0xFF13 => self.sound.ch1.freq_lo = byte.into(),
|
|
|
|
0xFF14 => self.sound.ch1.freq_hi = byte.into(),
|
2021-01-18 00:58:57 +00:00
|
|
|
0xFF24 => self.sound.control.channel = byte.into(),
|
2021-03-16 02:16:11 +00:00
|
|
|
0xFF25 => self.sound.control.output = byte.into(),
|
2021-01-18 00:58:57 +00:00
|
|
|
0xFF26 => self.sound.control.status = byte.into(), // FIXME: Should we control which bytes are written to here?
|
2021-04-09 05:35:41 +00:00
|
|
|
0xFF40 => self.ppu.control = byte.into(),
|
2021-01-18 08:22:45 +00:00
|
|
|
0xFF41 => self.ppu.stat = byte.into(),
|
|
|
|
0xFF42 => self.ppu.pos.scroll_y = byte,
|
|
|
|
0xFF43 => self.ppu.pos.scroll_x = byte,
|
|
|
|
0xFF44 => self.ppu.pos.line_y = byte,
|
2021-03-21 05:01:21 +00:00
|
|
|
0xFF45 => {
|
|
|
|
// Update LYC
|
|
|
|
self.ppu.pos.ly_compare = byte;
|
|
|
|
|
|
|
|
// Update Coincidence Flag
|
2021-04-04 06:31:31 +00:00
|
|
|
if self.ppu.stat.coincidence_int() {
|
2021-03-21 05:01:21 +00:00
|
|
|
let are_equal = self.ppu.pos.line_y == byte;
|
|
|
|
self.ppu.stat.set_coincidence(are_equal);
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 01:25:53 +00:00
|
|
|
0xFF47 => self.ppu.monochrome.bg_palette = byte.into(),
|
2021-03-16 07:36:09 +00:00
|
|
|
0xFF48 => self.ppu.monochrome.obj_palette_0 = byte.into(),
|
|
|
|
0xFF49 => self.ppu.monochrome.obj_palette_1 = byte.into(),
|
2021-03-17 03:52:43 +00:00
|
|
|
0xFF4A => self.ppu.pos.window_y = byte,
|
|
|
|
0xFF4B => self.ppu.pos.window_x = byte,
|
2021-04-09 01:28:16 +00:00
|
|
|
0xFF4D => {} // Writing to this address is useful on the CGB only
|
2021-01-19 06:30:10 +00:00
|
|
|
0xFF50 => {
|
|
|
|
// Disable Boot ROM
|
|
|
|
if byte != 0 {
|
|
|
|
self.boot = None;
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 01:28:16 +00:00
|
|
|
0xFF7F => {} // Don't think this address is used for anything
|
2021-01-03 07:21:19 +00:00
|
|
|
_ => unimplemented!("Unable to write to {:#06X} in I/O Registers", addr),
|
|
|
|
};
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0xFF80..=0xFFFE => {
|
|
|
|
// High RAM
|
2021-04-27 09:06:08 +00:00
|
|
|
self.high_ram.write_byte(addr, byte);
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
0xFFFF => {
|
2021-03-16 00:19:40 +00:00
|
|
|
// Interrupts Enable Register
|
2021-04-04 06:52:53 +00:00
|
|
|
self.int.enable = byte.into();
|
2020-12-24 03:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 05:41:19 +00:00
|
|
|
pub fn read_word(&self, addr: u16) -> u16 {
|
2021-01-03 04:49:25 +00:00
|
|
|
(self.read_byte(addr + 1) as u16) << 8 | self.read_byte(addr) as u16
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:43:49 +00:00
|
|
|
pub fn write_word(&mut self, addr: u16, word: u16) {
|
2021-01-03 04:49:25 +00:00
|
|
|
self.write_byte(addr + 1, (word >> 8) as u8);
|
|
|
|
self.write_byte(addr, (word & 0x00FF) as u8);
|
2020-08-29 23:38:27 +00:00
|
|
|
}
|
2020-09-04 05:41:19 +00:00
|
|
|
}
|
2021-03-21 00:53:56 +00:00
|
|
|
|
|
|
|
impl Bus {
|
|
|
|
fn interrupt_flag(&self) -> InterruptFlag {
|
2021-03-21 02:21:39 +00:00
|
|
|
// Read the current interrupt information from the PPU
|
2021-04-04 06:52:53 +00:00
|
|
|
let vblank = self.ppu.int.vblank();
|
|
|
|
let lcd_stat = self.ppu.int.lcd_stat();
|
2021-03-21 00:53:56 +00:00
|
|
|
|
2021-03-21 08:03:03 +00:00
|
|
|
// Read the current interrupt information from the Joypad
|
2021-03-21 02:21:39 +00:00
|
|
|
let joypad = self.joypad.interrupt();
|
2021-03-21 00:53:56 +00:00
|
|
|
|
2021-03-21 08:03:03 +00:00
|
|
|
// Read the current interrupt information from the Timer
|
|
|
|
let timer = self.timer.interrupt();
|
|
|
|
|
2021-03-21 00:53:56 +00:00
|
|
|
// Copy the Interrupt Flag register 0xFF0F
|
2021-04-04 06:52:53 +00:00
|
|
|
let mut flag = self.int.flag;
|
2021-03-21 00:53:56 +00:00
|
|
|
|
2021-03-21 02:21:39 +00:00
|
|
|
// Update the flag to have the most accurate information
|
|
|
|
flag.set_vblank(vblank);
|
|
|
|
flag.set_lcd_stat(lcd_stat);
|
|
|
|
flag.set_joypad(joypad);
|
2021-03-21 08:03:03 +00:00
|
|
|
flag.set_timer(timer);
|
2021-03-21 00:53:56 +00:00
|
|
|
flag
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_interrupt_flag(&mut self, byte: u8) {
|
|
|
|
// Update the Interrupt register 0xFF0F
|
2021-04-04 06:52:53 +00:00
|
|
|
self.int.flag = byte.into();
|
2021-03-21 00:53:56 +00:00
|
|
|
|
2021-04-04 06:52:53 +00:00
|
|
|
let vblank = self.int.flag.vblank();
|
|
|
|
let lcd_stat = self.int.flag.lcd_stat();
|
|
|
|
let joypad = self.int.flag.joypad();
|
|
|
|
let timer = self.int.flag.timer();
|
2021-03-21 00:53:56 +00:00
|
|
|
|
2021-03-21 02:21:39 +00:00
|
|
|
// Update the PPU's instance of the following interrupts
|
2021-04-04 06:52:53 +00:00
|
|
|
self.ppu.int.set_vblank(vblank);
|
|
|
|
self.ppu.int.set_lcd_stat(lcd_stat);
|
2021-03-21 02:21:39 +00:00
|
|
|
|
|
|
|
// Update the Joypad's instance of the following interrupts
|
|
|
|
self.joypad.set_interrupt(joypad);
|
2021-03-21 08:03:03 +00:00
|
|
|
|
|
|
|
// Update the Timer's instance of the following interrupts
|
|
|
|
self.timer.set_interrupt(timer);
|
2021-03-21 00:53:56 +00:00
|
|
|
}
|
2021-03-27 20:07:17 +00:00
|
|
|
|
|
|
|
pub fn boot_enabled(&self) -> bool {
|
|
|
|
self.boot.is_some()
|
|
|
|
}
|
2021-03-21 00:53:56 +00:00
|
|
|
}
|