-
Notifications
You must be signed in to change notification settings - Fork 8
/
backup.go
213 lines (182 loc) · 4.39 KB
/
backup.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
package main
import (
log "ndsemu/emu/logger"
"ndsemu/emu/spi"
"os"
"github.com/edsrzf/mmap-go"
)
var modBackup = log.NewModule("backup")
// HwBackupRam implements the save ram presents in most cartridge.
// It implements the spi.Device interface
type HwBackupRam struct {
sram mmap.MMap
addrSize int
addr int
wbuf []byte
writeEnabled bool
autodetect bool
auxCntrWritten bool
fn string
f *os.File
}
func NewHwBackupRam() *HwBackupRam {
b := &HwBackupRam{
autodetect: true,
}
for idx := range b.sram {
b.sram[idx] = 0xFF
}
return b
}
func (b *HwBackupRam) MapSaveFile(fn string) error {
b.fn = fn
return nil
}
func (b *HwBackupRam) checkSize(addr int) {
size := len(b.sram)
if addr < size {
return
}
if size == 0 {
size = 512
}
for size <= addr {
size *= 2
}
var err error
if b.sram != nil {
b.sram.Unmap()
}
if b.f == nil {
b.f, err = os.OpenFile(b.fn, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
if fi, err := b.f.Stat(); err != nil {
panic(err)
} else if fi.Size() > int64(size) {
size = int(fi.Size())
}
}
b.f.Truncate(int64(size))
b.sram, err = mmap.MapRegion(b.f, -1, mmap.RDWR, 0, 0)
if err != nil {
panic(err)
}
}
func (b *HwBackupRam) tryAutoDetect(data []byte) bool {
if len(data) == 1 {
b.auxCntrWritten = false
return false
}
if b.auxCntrWritten {
b.addrSize = len(data) - 2
modBackup.WarnZ("autodetect addr size").Int("size", b.addrSize).End()
b.autodetect = false
return true
}
modBackup.InfoZ("autodetect failed, waiting").End()
return false
}
func (b *HwBackupRam) SpiTransfer(data []byte) ([]byte, spi.ReqStatus) {
switch data[0] {
case 0x0:
// Dummy command that is sometimes sent. Ignore it
return nil, spi.ReqFinish
case 0x1: // WRSR
if len(data) < 2 {
return nil, spi.ReqContinue
}
modBackup.InfoZ("cmd WRSR").Hex8("val", data[1]).End()
return nil, spi.ReqFinish
case 0x5: // RDSR
modBackup.InfoZ("cmd RDSR").End()
var sr uint8
if b.writeEnabled {
sr |= 2
}
return []byte{sr}, spi.ReqFinish
case 0x4: // WRDI
modBackup.InfoZ("cmd WRDI").End()
b.writeEnabled = false
return nil, spi.ReqFinish
case 0x6: // WREN
modBackup.InfoZ("cmd WREN").End()
b.writeEnabled = true
return nil, spi.ReqFinish
case 0x3, 0xB: // RD
if b.autodetect && !b.tryAutoDetect(data) {
return nil, spi.ReqContinue
}
if len(data) < 1+b.addrSize {
return nil, spi.ReqContinue
}
if len(data) == 1+b.addrSize {
b.addr = 0
for _, v := range data[1:] {
b.addr <<= 8
b.addr |= int(v)
}
if b.addrSize == 1 && data[0] == 0xB {
// For 0.5k EEPROMS, cmd 0xB means "read high"
b.addr += 0x100
}
}
b.checkSize(b.addr)
modBackup.InfoZ("cmd RD").Int("addr", b.addr).End()
buf := make([]byte, 256)
sz := len(b.sram) - b.addr
if sz > 256 {
sz = 256
}
copy(buf[:sz], b.sram[b.addr:b.addr+sz])
return buf, spi.ReqContinue
case 0x2, 0xA: // WR
if !b.writeEnabled {
modBackup.FatalZ("writing with write disabled").End()
}
if b.autodetect {
modBackup.FatalZ("writing while autodetecting size").End()
}
if len(data) < 1+b.addrSize {
return nil, spi.ReqContinue
}
if len(data) == 1+b.addrSize {
b.addr = 0
for _, v := range data[1:] {
b.addr <<= 8
b.addr |= int(v)
}
if b.addrSize == 1 && data[0] == 0xA {
// For 0.5k EEPROMS, cmd 0xB means "read high"
b.addr += 0x100
}
modBackup.InfoZ("cmd WR").Int("addr", b.addr).End()
}
// Copy the whole buffer every time; I know it's inefficient,
// but never mind...
b.checkSize(b.addr)
copy(b.sram[b.addr:], data[1+b.addrSize:])
return nil, spi.ReqContinue
default:
modBackup.ErrorZ("unimplemented command").Blob("data", data).Int("len", len(data)).End()
if len(data) == 16 {
modBackup.FatalZ("data too long").End()
}
return nil, spi.ReqContinue
}
}
// This is a callback to inform that the AUXSPI control register has been written.
// It is normally not required on normal execution because it is already processed
// by gamecard to control the spi bus (that eventually calls HwBackupRam as spi.Device);
// but it's very useful when we are doing auto-detection of backup RAM size.
func (b *HwBackupRam) AuxSpiCntWritten(value uint16) {
b.auxCntrWritten = true
}
func (b *HwBackupRam) SpiBegin() {
modBackup.InfoZ("begin transfer").End()
}
func (b *HwBackupRam) SpiEnd() {
modBackup.InfoZ("end transfer").End()
b.sram.Flush()
}