-
Notifications
You must be signed in to change notification settings - Fork 1
/
interrupt.go
148 lines (124 loc) · 2.65 KB
/
interrupt.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
)
type Interrupt struct {
IME byte
IF byte
IE byte
IMEDelay bool
}
func (interrupt *Interrupt) requestVBlank() {
interrupt.IF |= 1
}
func (interrupt *Interrupt) resetVBlank() {
interrupt.IF &= 0xFE
// fmt.Println(interrupt.IF & 1)
}
func (interrupt *Interrupt) requestLCDCSTAT() {
interrupt.IF |= (1 << 1)
}
func (interrupt *Interrupt) resetLCDCSTAT() {
interrupt.IF &= 0xFD
}
func (interrupt *Interrupt) requestTimer() {
interrupt.IF |= (1 << 2)
}
func (interrupt *Interrupt) resetTimer() {
interrupt.IF &= 0xFB
}
func (interrupt *Interrupt) requestSerial() {
interrupt.IF |= (1 << 3)
}
func (interrupt *Interrupt) resetSerial() {
interrupt.IF &= 0xF7
}
func (interrupt *Interrupt) requestJoypad() {
interrupt.IF |= (1 << 4)
}
func (interrupt *Interrupt) resetJoypad() {
interrupt.IF &= 0xEF
}
func (interrupt *Interrupt) executeVBlank() {
hi := byte((cpu.PC) >> 8)
lo := byte(((cpu.PC) & 0xFF))
cpu.SP--
cpu.bus.write(cpu.SP, hi)
cpu.SP--
cpu.bus.write(cpu.SP, lo)
cpu.PC = 0x0040
// fmt.Println("good")
}
func (interrupt *Interrupt) executeLCDCSTAT() {
hi := byte((cpu.PC) >> 8)
lo := byte(((cpu.PC) & 0xFF))
cpu.SP--
cpu.bus.write(cpu.SP, hi)
cpu.SP--
cpu.bus.write(cpu.SP, lo)
cpu.PC = 0x0048
}
func (interrupt *Interrupt) executeTimer() {
hi := byte((cpu.PC) >> 8)
lo := byte(((cpu.PC) & 0xFF))
cpu.SP--
cpu.bus.write(cpu.SP, hi)
cpu.SP--
cpu.bus.write(cpu.SP, lo)
cpu.PC = 0x0050
}
func (interrupt *Interrupt) executeSerial() {
hi := byte((cpu.PC) >> 8)
lo := byte(((cpu.PC) & 0xFF))
cpu.SP--
cpu.bus.write(cpu.SP, hi)
cpu.SP--
cpu.bus.write(cpu.SP, lo)
cpu.PC = 0x0058
}
func (interrupt *Interrupt) executeJoypad() {
hi := byte((cpu.PC) >> 8)
lo := byte(((cpu.PC) & 0xFF))
cpu.SP--
cpu.bus.write(cpu.SP, hi)
cpu.SP--
cpu.bus.write(cpu.SP, lo)
cpu.PC = 0x0060
fmt.Println("GOOD!")
}
func (interrupt *Interrupt) handleInterrupts() {
// only handle interrupts if IME is set
if interrupt.IME == 1 {
for i := 0; i < 5; i++ {
// check if bit is a potential interrupt
if ((1 << i) & interrupt.IE) >> i == 1 && ((1 << i) & interrupt.IF) >> i == 1 {
// turn off corresponding bit in IF
switch i {
case 0:
interrupt.executeVBlank()
case 1:
interrupt.executeLCDCSTAT()
case 2:
interrupt.executeTimer()
case 3:
interrupt.executeSerial()
case 4:
interrupt.executeJoypad()
}
switch i {
case 0:
interrupt.resetVBlank()
case 1:
interrupt.resetLCDCSTAT()
case 2:
interrupt.resetTimer()
case 3:
interrupt.resetSerial()
case 4:
interrupt.resetJoypad()
}
interrupt.IME = 0
}
}
}
}