forked from strayacode/RoseGB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.go
106 lines (92 loc) · 1.69 KB
/
cpu.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
package main
import (
// "fmt"
// "strconv"
)
type CPU struct {
A byte
B byte
C byte
D byte
E byte
F byte // used for storing z, n, h, c flags | ordered like znhc0000
H byte
L byte
PC uint16
SP uint16
Cycles int
Opcode byte
bus Bus
halt bool
}
func (cpu *CPU) tick() {
// run instructions when cpu is not halted
if !cpu.halt {
if cpu.Cycles == 0 {
if cpu.bus.interrupt.IMEDelay {
cpu.bus.interrupt.IMEDelay = false
cpu.bus.interrupt.IME = 1
}
cpu.Opcode = cpu.bus.read(cpu.PC)
cpu.PC++
cpu.Cycles = opcodes[cpu.Opcode].Cycles
opcodes[cpu.Opcode].Exec(cpu)
if cpu.bus.timer.readTimerEnable() {
cpu.bus.timer.elaspedCycles += opcodes[cpu.Opcode].Cycles
}
}
} else {
// once interrupt has been serviced
if cpu.bus.interrupt.IME == 0 {
pendingInterrupts := (cpu.bus.interrupt.IE & cpu.bus.interrupt.IF) != 0
if pendingInterrupts {
cpu.halt = false
}
}
if cpu.bus.timer.readTimerEnable() {
cpu.bus.timer.elaspedCycles += 4
}
cpu.Cycles += 4
}
cpu.Cycles--
}
func (cpu *CPU) ZFlag(value byte) {
if value == 1 {
cpu.F |= (1 << 7)
} else {
cpu.F &= 0x7F
}
}
func (cpu *CPU) NFlag(value byte) {
if value == 1 {
cpu.F |= (1 << 6)
} else {
cpu.F &= 0xBF
}
}
func (cpu *CPU) HFlag(value byte) {
if value == 1 {
cpu.F |= (1 << 5)
} else {
cpu.F &= 0xDF
}
}
func (cpu *CPU) CFlag(value byte) {
if value == 1 {
cpu.F |= (1 << 4)
} else {
cpu.F &= 0xEF
}
}
func (cpu *CPU) getZFlag() byte {
return (cpu.F & 0x80) >> 7
}
func (cpu *CPU) getNFlag() byte {
return (cpu.F & 0x40) >> 6
}
func (cpu *CPU) getHFlag() byte {
return (cpu.F & 0x20) >> 5
}
func (cpu *CPU) getCFlag() byte {
return (cpu.F & 0x10) >> 4
}