SayoriOS  0.3.3
interrupt.c
1 #include "interrupt.h"
2 #include "cpu.h"
3 
4 static int enabled;
5 
6 /* Pending interrupt flags */
7 static int interrupt_IF = 0xE0;
8 
9 /* Interrupt masks */
10 static int interrupt_IE = 0;
11 
12 int interrupt_pending(void)
13 {
14  return interrupt_IF & interrupt_IE & 0x1F;
15 }
16 
17 void interrupt_flush(void)
18 {
19  unsigned int pending;
20 
21  pending = interrupt_pending();
22 
23  if(!pending)
24  return;
25 
26  if(!enabled)
27  {
28  if(cpu_halted())
29  cpu_unhalt();
30  return;
31  }
32 
33  cpu_interrupt_begin();
34 
35  /* Check again here incase the above changed IF through PUSH SP (push_ei.gb) */
36  pending = interrupt_pending();
37 
38  if(pending & INTR_VBLANK)
39  {
40  interrupt_IF ^= INTR_VBLANK;
41  cpu_interrupt(0x40);
42  }
43  else if(pending & INTR_LCDSTAT)
44  {
45  interrupt_IF ^= INTR_LCDSTAT;
46  cpu_interrupt(0x48);
47  }
48  else if(pending & INTR_TIMER)
49  {
50  interrupt_IF ^= INTR_TIMER;
51  cpu_interrupt(0x50);
52  }
53  else if(pending & INTR_SERIAL)
54  {
55  interrupt_IF ^= INTR_SERIAL;
56  cpu_interrupt(0x58);
57  }
58  else if(pending & INTR_JOYPAD)
59  {
60  interrupt_IF ^= INTR_JOYPAD;
61  cpu_interrupt(0x60);
62  }
63  else
64  {
65  cpu_interrupt(0x00);
66  }
67 }
68 
69 int interrupt_enabled(void)
70 {
71  return enabled;
72 }
73 
74 void interrupt_enable(void)
75 {
76  enabled = 1;
77 }
78 
79 void interrupt_disable(void)
80 {
81  enabled = 0;
82 }
83 
84 int interrupt_get_enabled(void)
85 {
86  return enabled;
87 }
88 
89 void interrupt(unsigned int n)
90 {
91  interrupt_IF |= n;
92 }
93 
94 unsigned char interrupt_get_IF(void)
95 {
96  return interrupt_IF;
97 }
98 
99 void interrupt_set_IF(unsigned char mask)
100 {
101  interrupt_IF = 0xE0 | mask;
102 }
103 
104 unsigned char interrupt_get_mask(void)
105 {
106  return interrupt_IE;
107 }
108 
109 void interrupt_set_mask(unsigned char mask)
110 {
111  interrupt_IE = mask;
112 }