-
Notifications
You must be signed in to change notification settings - Fork 1
/
gbs.go
307 lines (254 loc) · 6.5 KB
/
gbs.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package dmgo
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
"time"
)
type gbsPlayer struct {
cpuState
Hdr gbsHeader
CurrentSong byte
CurrentSongStart time.Time
Paused bool
PauseStartTime time.Time
TextDisplay textDisplay
DbgScreen [160 * 144 * 4]byte
devMode bool
}
func (gp *gbsPlayer) SetDevMode(b bool) { gp.devMode = b }
func (gp *gbsPlayer) InDevMode() bool { return gp.devMode }
func (gp *gbsPlayer) GetCartRAM() []byte { return nil }
func (gp *gbsPlayer) SetCartRAM(ram []byte) error {
return fmt.Errorf("saves not implemented for GBSs")
}
func (gp *gbsPlayer) MakeSnapshot() []byte { return nil }
func (gp *gbsPlayer) LoadSnapshot(snapBytes []byte) (Emulator, error) {
return nil, fmt.Errorf("snapshots not implemented for GBSs")
}
type gbsHeader struct {
Magic [3]byte
Version byte
NumSongs byte
StartSong byte
LoadAddr uint16
InitAddr uint16
PlayAddr uint16
StackPtr uint16
TimerModulo byte
TimerControl byte
TitleString [32]byte
AuthorString [32]byte
CopyrightString [32]byte
}
func parseGbs(gbs []byte) (gbsHeader, []byte, error) {
hdr := gbsHeader{}
if err := readStructLE(gbs, &hdr); err != nil {
return gbsHeader{}, nil, fmt.Errorf("gbs player error\n%s", err.Error())
}
if hdr.Version != 1 {
return gbsHeader{}, nil, fmt.Errorf("gbs player error\nunsupported gbs version: %v", hdr.Version)
}
data := gbs[0x70:]
return hdr, data, nil
}
func readStructLE(structBytes []byte, iface interface{}) error {
return binary.Read(bytes.NewReader(structBytes), binary.LittleEndian, iface)
}
// NewGbsPlayer creates an gbsPlayer session
func NewGbsPlayer(gbs []byte, devMode bool) Emulator {
var hdr gbsHeader
var data []byte
var err error
hdr, data, err = parseGbs(gbs)
if err != nil {
return NewErrEmu(fmt.Sprintf("gbs player error\n%s", err.Error()))
}
cart := append(make([]byte, hdr.LoadAddr), data...)
paddingNeeded := len(cart) % 16 * 1024
if paddingNeeded != 0 {
cart = append(cart, make([]byte, paddingNeeded)...)
}
gp := gbsPlayer{
cpuState: cpuState{
Mem: mem{
mbc: &gbsMBC{},
cart: cart,
CartRAM: make([]byte, 8192),
InternalRAMBankNumber: 1,
},
},
devMode: devMode,
Hdr: hdr,
}
gp.TextDisplay = textDisplay{w: 160, h: 144, screen: gp.DbgScreen[:]}
gp.devPrintln("uses timer:", gp.usesTimer())
if gp.Hdr.TimerControl&0x80 > 0 {
gp.devPrintln("GBC Speed Requested")
gp.FastMode = true
}
if gp.usesTimer() {
gp.TimerFreqSelector = gp.Hdr.TimerControl & 0x03
gp.TimerModuloReg = gp.Hdr.TimerModulo
gp.TimerOn = true
}
gp.init()
gp.patchRsts()
gp.initTune(gp.Hdr.StartSong - 1)
gp.updateScreen()
return &gp
}
func (gp *gbsPlayer) devPrintln(s ...interface{}) {
if gp.InDevMode() {
fmt.Println(s...)
}
}
func (gp *gbsPlayer) usesTimer() bool {
return gp.Hdr.TimerControl&0x04 == 0x04
}
func (gp *gbsPlayer) patchRsts() {
addrs := []uint16{0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38}
for _, addr := range addrs {
newAddr := gp.Hdr.LoadAddr + addr
patch := []byte{0xcd, byte(newAddr), byte(newAddr >> 8)}
copy(gp.Mem.cart[addr:addr+3], patch)
}
}
func (gp *gbsPlayer) initTune(songNum byte) {
gp.F = 0
gp.B, gp.C = 0, 0
gp.D, gp.E = 0, 0
gp.H, gp.L = 0, 0
for i := uint16(0xa000); i < 0xfe00; i++ {
gp.write(i, 0)
}
for i := uint16(0xff80); i < 0xffff; i++ {
gp.write(i, 0)
}
gp.initIORegs()
gp.APU.Sounds[0].RestartRequested = false
gp.APU.Sounds[1].RestartRequested = false
gp.APU.Sounds[2].RestartRequested = false
gp.APU.Sounds[3].RestartRequested = false
gp.A = songNum
// force a call to INIT
gp.SP = gp.Hdr.StackPtr
gp.pushOp16(0x0130)
gp.PC = gp.Hdr.InitAddr
for gp.PC != 0x0130 {
gp.Step()
}
gp.CurrentSong = songNum
gp.CurrentSongStart = time.Now()
}
func (gp *gbsPlayer) updateScreen() {
gp.TextDisplay.clearScreen()
gp.TextDisplay.setPos(0, 1)
gp.TextDisplay.writeString("GBS Player\n\n")
gp.TextDisplay.writeString(string(gp.Hdr.TitleString[:]) + "\n")
gp.TextDisplay.writeString(string(gp.Hdr.AuthorString[:]) + "\n")
copyStr := string(gp.Hdr.CopyrightString[:])
copyParts := strings.SplitN(copyStr, " ", 2)
if len(copyParts) > 1 {
// almost always improves presentation
gp.TextDisplay.writeString(copyParts[0] + "\n")
gp.TextDisplay.writeString(copyParts[1] + "\n")
} else {
gp.TextDisplay.writeString(copyStr + "\n")
}
gp.TextDisplay.newline()
gp.TextDisplay.writeString(fmt.Sprintf("Track %02d/%02d\n", gp.CurrentSong+1, gp.Hdr.NumSongs))
nowTime := int(time.Now().Sub(gp.CurrentSongStart).Seconds())
nowTimeStr := fmt.Sprintf("%02d:%02d", nowTime/60, nowTime%60)
gp.TextDisplay.writeString(fmt.Sprintf("%s", nowTimeStr))
if gp.Paused {
gp.TextDisplay.writeString(" *PAUSED*\n")
} else {
gp.TextDisplay.newline()
}
}
func (gp *gbsPlayer) prevSong() {
if gp.CurrentSong > 0 {
gp.CurrentSong--
gp.initTune(gp.CurrentSong)
gp.updateScreen()
}
}
func (gp *gbsPlayer) nextSong() {
if gp.CurrentSong < gp.Hdr.NumSongs-1 {
gp.CurrentSong++
gp.initTune(gp.CurrentSong)
gp.updateScreen()
}
}
func (gp *gbsPlayer) togglePause() {
gp.Paused = !gp.Paused
if gp.Paused {
gp.PauseStartTime = time.Now()
} else {
gp.CurrentSongStart = gp.CurrentSongStart.Add(time.Now().Sub(gp.PauseStartTime))
}
gp.updateScreen()
}
var lastInput time.Time
func (gp *gbsPlayer) UpdateInput(input Input) {
now := time.Now()
if now.Sub(lastInput).Seconds() > 0.20 {
if input.Joypad.Left {
gp.prevSong()
lastInput = now
}
if input.Joypad.Right {
gp.nextSong()
lastInput = now
}
if input.Joypad.Start {
gp.togglePause()
lastInput = now
}
}
}
var lastScreenUpdate time.Time
func (gp *gbsPlayer) DbgStep() {
gp.cpuState.debugger.step(gp)
}
func (gp *gbsPlayer) Step() {
if !gp.Paused {
now := time.Now()
if now.Sub(lastScreenUpdate) >= 100*time.Millisecond {
lastScreenUpdate = now
gp.updateScreen()
}
doPlayCall := false
if gp.PC == 0x0130 {
if gp.usesTimer() {
if gp.TimerIRQ {
gp.TimerIRQ = false
doPlayCall = true
}
} else {
if gp.VBlankIRQ {
gp.VBlankIRQ = false
doPlayCall = true
}
}
}
if doPlayCall {
gp.SP = gp.Hdr.StackPtr
gp.pushOp16(0x0130)
gp.PC = gp.Hdr.PlayAddr
}
if gp.PC != 0x0130 {
gp.step()
} else {
gp.runCycles(4)
}
}
}
func (gp *gbsPlayer) ReadSoundBuffer(toFill []byte) []byte {
return gp.APU.readSoundBuffer(toFill)
}
func (gp *gbsPlayer) Framebuffer() []byte {
return gp.DbgScreen[:]
}