-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap.go
188 lines (160 loc) · 4.65 KB
/
snap.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
package famigo
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
)
const currentSnapshotVersion = 3
const infoString = "famigo snapshot"
type snapshot struct {
Version int
Info string
State json.RawMessage
MMC marshalledMMC
ChrRAM []byte
}
func (emu *emuState) loadSnapshot(snapBytes []byte) (*emuState, error) {
var err error
var reader io.Reader
var unpackedBytes []byte
var snap snapshot
if reader, err = gzip.NewReader(bytes.NewReader(snapBytes)); err != nil {
return nil, err
} else if unpackedBytes, err = ioutil.ReadAll(reader); err != nil {
return nil, err
} else if err = json.Unmarshal(unpackedBytes, &snap); err != nil {
return nil, err
} else if snap.Version < currentSnapshotVersion {
return emu.convertOldSnapshot(&snap)
} else if snap.Version > currentSnapshotVersion {
return nil, fmt.Errorf("this version of famigo is too old to open this snapshot")
}
// NOTE: what about external RAM? Doesn't this overwrite .sav files with whatever's in the snapshot?
return emu.convertLatestSnapshot(&snap)
}
func (emu *emuState) convertLatestSnapshot(snap *snapshot) (*emuState, error) {
var err error
var newState emuState
if err = json.Unmarshal(snap.State, &newState); err != nil {
return nil, err
}
if newState.Mem.mmc, err = unmarshalMMC(snap.MMC); err != nil {
return nil, err
}
newState.Mem.prgROM = emu.Mem.prgROM
if emu.CartInfo.IsChrRAM() {
newState.Mem.chrROM = snap.ChrRAM
} else {
newState.Mem.chrROM = emu.Mem.chrROM
}
newState.CPU.Write = newState.write
newState.CPU.Read = newState.read
newState.CPU.RunCycles = newState.runCycles
newState.CPU.Err = func(e error) { emuErr(e) }
newState.devMode = emu.devMode
return &newState, nil
}
var snapshotConverters = map[int]func(map[string]interface{}) error{
// If new field can be zero, no need for converter.
// added 2018-06-13
1: convertSnap1To2,
// added 2018-12-21
2: convertSnap2To3,
}
// added 2018-06-13
func convertSnap1To2(state map[string]interface{}) error {
newCPU := map[string]interface{}{
"IgnoreDecimalMode": true,
}
keys := []string{
"PC",
"P", "A", "X", "Y", "S",
"IRQ", "BRK", "NMI", "RESET",
"LastStepsP",
"Steps",
}
for _, key := range keys {
val, ok := state[key]
if !ok {
return fmt.Errorf("missing key %v", key)
}
newCPU[key] = val
delete(state, key)
}
state["CPU"] = newCPU
return nil
}
// added 2018-12-21
func convertSnap2To3(state map[string]interface{}) error {
convertSound := func(soundName string) error {
if sound, _, err := followJSON(state, "APU", soundName); err == nil {
if soundMap, ok := sound.(map[string]interface{}); ok {
soundMap["T"] = 0
soundMap["PolySample"] = 0
} else {
return fmt.Errorf("sound var is of unknown type")
}
} else {
return err
}
return nil
}
if err := convertSound("Pulse1"); err != nil {
return fmt.Errorf("could not convert old v2 snapshot: %v", err)
} else if err := convertSound("Pulse2"); err != nil {
return fmt.Errorf("could not convert old v2 snapshot: %v", err)
} else if err := convertSound("Triangle"); err != nil {
return fmt.Errorf("could not convert old v2 snapshot: %v", err)
} else if err := convertSound("DMC"); err != nil {
return fmt.Errorf("could not convert old v2 snapshot: %v", err)
} else if err := convertSound("Noise"); err != nil {
return fmt.Errorf("could not convert old v2 snapshot: %v", err)
}
return nil
}
func (emu *emuState) convertOldSnapshot(snap *snapshot) (*emuState, error) {
var state map[string]interface{}
if err := json.Unmarshal(snap.State, &state); err != nil {
return nil, fmt.Errorf("json unpack err: %v", err)
}
for i := snap.Version; i < currentSnapshotVersion; i++ {
if converterFn, ok := snapshotConverters[i]; !ok {
return nil, fmt.Errorf("could not find converter for snapshot version: %v", i)
} else if err := converterFn(state); err != nil {
return nil, fmt.Errorf("error converting snapshot version %v: %v", i, err)
}
}
var err error
if snap.State, err = json.Marshal(state); err != nil {
return nil, fmt.Errorf("json pack err: %v", err)
}
return emu.convertLatestSnapshot(snap)
}
func (emu *emuState) makeSnapshot() []byte {
var err error
var emuJSON []byte
var snapJSON []byte
if emuJSON, err = json.Marshal(emu); err != nil {
panic(err)
}
snap := snapshot{
Version: currentSnapshotVersion,
Info: infoString,
State: json.RawMessage(emuJSON),
MMC: emu.Mem.mmc.Marshal(),
}
if emu.CartInfo.IsChrRAM() {
snap.ChrRAM = emu.Mem.chrROM
}
if snapJSON, err = json.Marshal(&snap); err != nil {
panic(err)
}
buf := &bytes.Buffer{}
writer := gzip.NewWriter(buf)
writer.Write(snapJSON)
writer.Close()
return buf.Bytes()
}