Refactor check_interrupts

This commit is contained in:
Franco Colmenarez 2021-11-11 09:24:09 -05:00
parent 66182da336
commit c68f24ea05
2 changed files with 7 additions and 16 deletions

View File

@ -184,18 +184,8 @@ impl Bus {
self.write(address.wrapping_add(1), bytes[1]);
}
pub fn set_interrupt_enable(&mut self, interrupt: Interrupt, val: bool) {
let byte = self.read(INTERRUPT_ENABLE_ADDRESS);
self.write(INTERRUPT_ENABLE_ADDRESS, interrupt.set(byte, val));
}
pub fn set_interrupt_flag(&mut self, interrupt: Interrupt, val: bool) {
let byte = self.read(INTERRUPT_FLAG_ADDRESS);
self.write(INTERRUPT_FLAG_ADDRESS, interrupt.set(byte, val));
}
pub fn get_interrupt(&mut self, interrupt: Interrupt) -> bool {
let byte = self.read(INTERRUPT_ENABLE_ADDRESS) & self.read(INTERRUPT_FLAG_ADDRESS);
interrupt.get(byte)
}
}

View File

@ -917,22 +917,23 @@ impl CPU {
}
pub fn check_interrupts(&mut self, bus: &mut Bus) -> Option<Interrupt> {
if (bus.read(INTERRUPT_ENABLE_ADDRESS) & 0b00011111) & (bus.read(INTERRUPT_FLAG_ADDRESS) & 0b00011111) != 0 {
let interrupts = (bus.read(INTERRUPT_ENABLE_ADDRESS) & 0b00011111) & (bus.read(INTERRUPT_FLAG_ADDRESS) & 0b00011111);
if interrupts != 0 {
self.is_halted = false;
}
if !self.ime {
return None;
}
if bus.get_interrupt(Interrupt::VBlank) {
if Interrupt::VBlank.get(interrupts) {
return Some(Interrupt::VBlank);
} else if bus.get_interrupt(Interrupt::LCDSTAT) {
} else if Interrupt::LCDSTAT.get(interrupts) {
return Some(Interrupt::LCDSTAT);
} else if bus.get_interrupt(Interrupt::Timer) {
} else if Interrupt::Timer.get(interrupts) {
return Some(Interrupt::Timer);
} else if bus.get_interrupt(Interrupt::Serial) {
} else if Interrupt::Serial.get(interrupts) {
return Some(Interrupt::Serial);
} else if bus.get_interrupt(Interrupt::Joypad) {
} else if Interrupt::Joypad.get(interrupts) {
return Some(Interrupt::Joypad);
}
None