-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrom.go
120 lines (93 loc) · 2.78 KB
/
rom.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
package goboy
import (
"fmt"
"time"
"github.com/pkg/errors"
)
func (gb *GameBoy) LoadROM(d []byte) {
gb.romData = d
gb.pc = 0
}
// ReadRom8 reads a byte from Rom at a given address, respecting Rom mapping
func (gb *GameBoy) ReadRom8(address uint16) (value byte) {
return gb.romData[address]
}
func (gb *GameBoy) ReadRom16(address uint16) (value uint16) {
lsb := gb.romData[address] // little endian
msb := gb.romData[address+1]
return mergeBytes(msb, lsb)
}
// WriteRom sets the value at a given address in Rom, respecting Rom mapping
func (gb *GameBoy) WriteRom(address uint16, value byte) {
gb.romData[address] = value
}
// inspired by https://docs.libretro.com/development/cores/developing-cores/#retro_run
func (gb *GameBoy) RunFrame() {
// this would probably continuously
// run instructions until a VBlank interrupt
for i := 0; i <= 24603; i++ {
gb.debugLnF("instruction #%d", i)
if gb.pc == 0x00A0 {
gb.debugLnF("breakpoint")
}
gb.RunInstruction()
}
}
func (gb *GameBoy) RunInstruction() {
start := time.Now()
defer func() {
gb.debugLnF("instruction ET: %s\n", time.Since(start))
}()
// first byte of instruction might be a prefix
gb.debugLnF("PC: %.4X", gb.pc)
prefix := gb.ReadRom8(gb.pc)
offset := uint16(1)
var opbytes OpBytes
var ok bool
var displacement, opcode byte
var immediate uint16
// check for known prefixes
if prefix == 0xCB {
opcode = gb.ReadRom8(gb.pc + offset)
offset++
opbytes, ok = cb[opcode]
} else {
// unprefixed, first byte must be opcode
opcode = prefix
prefix = 0
opbytes, ok = unprefixed[opcode]
}
// no matching instruction
if !ok {
// the actual gameboy does nothing, no crash, no change of flags, nothing
// TODO: remove this panic, eventually
panic(errors.New(fmt.Sprintf("unrecognized opcode %.2X at offset %.2X", opcode, gb.pc)))
}
// gb.debugPrintlnf("prefix: %.2X, opcode: %.2X, has displacement: %t, immediate size: %d", prefix, opcode, opbytes.HasDisplacement, opbytes.ImmediateSize)
if opbytes.HasDisplacement {
// byte after opcode
displacement = gb.ReadRom8(gb.pc + offset)
offset++
}
if opbytes.ImmediateSize == 1 {
immediate = uint16(gb.ReadRom8(gb.pc + offset))
} else if opbytes.ImmediateSize == 2 {
immediate = gb.ReadRom16(gb.pc + offset)
}
offset += uint16(opbytes.ImmediateSize)
// gb.debugPrintlnf("displacement: %.2X, immediate: %.4X", displacement, immediate)
if opbytes.Operation != nil {
opbytes.Operation(gb, prefix, OpCode(opcode), displacement, immediate)
} else if opcode != 0 {
gb.debugLnF("unknown opcode %.2X at PC %.4X", opcode, gb.pc)
}
gb.pc += offset
gb.debugLnF("HL: %.4X", gb.readHL())
gb.debugLnF("next PC: %.4X", gb.pc)
}
func (gb *GameBoy) debugLnF(format string, a ...interface{}) {
if gb.Debug {
fmt.Printf(format, a...)
fmt.Println()
}
}