-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
interrupt.cpp
52 lines (43 loc) · 786 Bytes
/
interrupt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "interrupt.h"
#include "cpu.h"
uint8_t IME;
uint8_t IF;
uint8_t IE;
static uint8_t ime_delay;
bool interrupt_flush(void)
{
if(ime_delay >= 2)
{
ime_delay = 1;
return 0;
}
if (ime_delay == 1) {
IME = 1;
ime_delay = 0;
}
/* Returns true if the cpu should be unhalted */
if (!IME)
return !!(IF & IE);
/* Check interrupts from highest to lowest priority */
for (int i = 0; i < 5; ++i) {
if (IE & IF & (1 << i)) {
IME = 0;
IF &= ~(1 << i);
cpu_interrupt(0x40 + i*0x08);
return true;
}
}
return false;
}
void interrupt_enable(void)
{
ime_delay = 2;
}
void interrupt(uint8_t n)
{
/* Add this interrupt to pending queue */
IF |= n;
/* Interrupt requested, unhalt CPU if IF and IE have a match */
if(interrupt_flush())
halted = 0;
}