cpu - Fix timing of delay when enabling interrupts

This commit is contained in:
madmaurice 2023-08-29 12:10:10 +02:00
parent 1194340657
commit 002b745917
2 changed files with 12 additions and 3 deletions

View file

@ -191,8 +191,6 @@ void Cpu::handleInterrupts()
u8 si = state.IE & state.IF & INT_MASK; u8 si = state.IE & state.IF & INT_MASK;
if (state.IME == IME_SCHEDULED) if (state.IME == IME_SCHEDULED)
state.IME = IME_DELAYED;
else if (state.IME == IME_DELAYED)
state.IME = IME_ON; state.IME = IME_ON;
else if (state.IME == IME_ON && si != 0) else if (state.IME == IME_ON && si != 0)
{ {

View file

@ -43,11 +43,22 @@ enum InterruptType : u8
INT_MASK = 0x1F, INT_MASK = 0x1F,
}; };
/**
IME - Interrupt Master Enable
An EI instruction will enable the interrupts, but delayed. During the next instruction after EI,
interrupts are still disabled. For this to be emulated we use a small state machine. which works as follows
instruction EI - sets IME_SCHEDULED
handleInterrupts -> IME_SCHEDULED to IME_ON (but no call to isr yet)
instruction any - is IME_ON, but no chance for call to isr yet
handleInterrupts -> is IME_ON, do a call to isr if necessary
*/
enum IME_state enum IME_state
{ {
IME_OFF, IME_OFF,
IME_SCHEDULED, IME_SCHEDULED,
IME_DELAYED,
IME_ON, IME_ON,
}; };