Skip to content

Commit

Permalink
EI takes one instruction to run
Browse files Browse the repository at this point in the history
  • Loading branch information
pineman committed Apr 27, 2024
1 parent 686cfe4 commit 8227d9e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions fpt/src/lr35902.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct LR35902 {
sp: u16,
pc: u16,
ime: bool,
imenc: bool,
mem: Bus,
next_cb: bool,
clock_cycles: u64,
Expand Down Expand Up @@ -45,6 +46,7 @@ impl LR35902 {
sp: 0,
pc: 0,
ime: false,
imenc: false,
mem: memory,
next_cb: false,
clock_cycles: 0,
Expand Down Expand Up @@ -213,6 +215,10 @@ impl LR35902 {
self.ime = ime;
}

pub fn set_interrupt_master_enable_next_instruction(&mut self) {
self.imenc = true;
}

pub fn set_next_cb(&mut self, value: bool) {
self.next_cb = value;
}
Expand Down Expand Up @@ -537,7 +543,7 @@ impl LR35902 {
fn reti(&mut self) {
// TODO: The interrupt master enable flag is returned to its pre-interrupt status.
// BUT: https://gbdev.io/pandocs/Interrupts.htm claims that RETI is EI followed by RET
self.set_interrupt_master_enable(true);
self.set_interrupt_master_enable_next_instruction();

// RET
let address = self.pop();
Expand Down Expand Up @@ -576,6 +582,10 @@ impl LR35902 {
if self.inst_cycle_count() < instruction.cycles {
return;
}
if self.imenc {
self.set_interrupt_master_enable(true);
self.imenc = false;
}
if self.next_cb() {
self.set_next_cb(false);
}
Expand Down Expand Up @@ -1758,7 +1768,7 @@ impl LR35902 {
}
0xFB => {
// EI
self.set_interrupt_master_enable(true);
self.set_interrupt_master_enable_next_instruction();
}
0xFC => {
// Not implemented
Expand Down

0 comments on commit 8227d9e

Please sign in to comment.