-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemu.go
99 lines (78 loc) · 2.14 KB
/
emu.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
package famigo
import "fmt"
// Emulator exposes the public facing fns for an emulation session
type Emulator interface {
Step()
MakeSnapshot() []byte
LoadSnapshot([]byte) (Emulator, error)
SetPrgRAM([]byte) error
GetPrgRAM() []byte
Framebuffer() []byte
FlipRequested() bool
UpdateInput(input Input)
ReadSoundBuffer([]byte) []byte
GetSoundBufferUsed() int
InDevMode() bool
SetDevMode(b bool)
}
// Input covers all outside info sent to the Emulator
type Input struct {
Joypad Joypad
}
// NewEmulator creates an emulation session
func NewEmulator(cart []byte, devMode bool) Emulator {
return newState(cart, devMode)
}
func (emu *emuState) MakeSnapshot() []byte {
return emu.makeSnapshot()
}
func (emu *emuState) LoadSnapshot(snapBytes []byte) (Emulator, error) {
return emu.loadSnapshot(snapBytes)
}
// GetSoundBuffer returns a 44100hz * 16bit * 2ch sound buffer.
// A pre-sized buffer must be provided, which is returned resized
// if the buffer was less full than the length requested.
func (emu *emuState) ReadSoundBuffer(toFill []byte) []byte {
return emu.APU.readSoundBuffer(emu, toFill)
}
func (emu *emuState) GetSoundBufferUsed() int {
return int(emu.APU.buffer.size())
}
func (emu *emuState) UpdateInput(input Input) {
// prevent impossible inputs on original dpad
if input.Joypad.Up {
input.Joypad.Down = false
}
if input.Joypad.Left {
input.Joypad.Right = false
}
emu.CurrentJoypad1 = input.Joypad
}
// Framebuffer returns the current state of the screen
func (emu *emuState) Framebuffer() []byte {
return emu.PPU.FrameBuffer[:]
}
// FlipRequested indicates if a draw request is pending
// and clears it before returning
func (emu *emuState) FlipRequested() bool {
result := emu.flipRequested
emu.flipRequested = false
return result
}
func (emu *emuState) GetPrgRAM() []byte {
if emu.CartInfo.HasBatteryBackedRAM() {
return emu.Mem.PrgRAM
}
return nil
}
func (emu *emuState) SetPrgRAM(ram []byte) error {
if len(emu.Mem.PrgRAM) == len(ram) {
copy(emu.Mem.PrgRAM, ram)
return nil
}
// TODO: better checks if possible (e.g. checksums, etc)
return fmt.Errorf("ram size mismatch")
}
func (emu *emuState) Step() {
emu.step()
}