-
Notifications
You must be signed in to change notification settings - Fork 19
/
fingerprint.go
397 lines (338 loc) · 10.7 KB
/
fingerprint.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package cartridge
import (
"fmt"
"github.com/jetsetilly/gopher2600/cartridgeloader"
"github.com/jetsetilly/gopher2600/curated"
"github.com/jetsetilly/gopher2600/hardware/instance"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/cdf"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/dpcplus"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/mapper"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/supercharger"
)
func fingerprint3e(b []byte) bool {
// 3E cart bankswitching is triggered by storing the bank number in address
// 3E using 'STA $3E', commonly followed by an immediate mode LDA
//
// fingerprint method taken from:
//
// https://gitlab.com/firmaplus/atari-2600-pluscart/-/blob/master/source/STM32firmware/PlusCart/Src/cartridge_detection.c#L140
for i := 0; i < len(b)-3; i++ {
if b[i] == 0x85 && b[i+1] == 0x3e && b[i+2] == 0xa9 && b[i+3] == 0x00 {
return true
}
}
return false
}
func fingerprint3ePlus(b []byte) bool {
// previous versions of this function worked similarly to the tigervision
// method but this is more accurate
//
// fingerprint method taken from:
//
// https://gitlab.com/firmaplus/atari-2600-pluscart/-/blob/master/source/STM32firmware/PlusCart/Src/cartridge_detection.c#L148
for i := 0; i < len(b)-3; i++ {
if b[i] == 'T' && b[i+1] == 'J' && b[i+2] == '3' && b[i+3] == 'E' {
return true
}
}
return false
}
func fingerprintMnetwork(b []byte) bool {
// Bump 'n' Jump is the fussiest mnetwork cartridge I've found. Matching
// hotspots:
//
// $fdd5 LDA BANK5
// $fde3 LDA BANK6
// $fe0e LDA BANK5
// $fe16 LDA BANK4
//
// This also catches modern games not created by mnetwork but which use the
// format, eg Pitkat
//
// (24/01/21)
//
// Splendidnut's Congobongo demo is even fussier than Bump 'n' Jump. While
// I expect the ROM to get more complex we should support the demo ROM if
// only because it exists.
//
// notably, it uses a non-primary-mirror cartridge address and there are
// only two bankswitch instructions in the data (although I didn't search
// it exhaustively - just LDA instructions).
//
// threshold has been reduced to two.
//
// (19/06/21)
//
// Thomas Jentzch's Elite demo requires a threshold of one
//
// https://atariage.com/forums/topic/155657-elite-3d-graphics/?do=findComment&comment=2444328
//
// with such a low threshold, mnetwork should probably be the very last
// type to check for
threshold := 1
for i := 0; i < len(b)-3; i++ {
if b[i] == 0xad && (b[i+1] >= 0xe0 && b[i+1] <= 0xe7) {
// bank switching can address any cartidge mirror so mask off
// insignificant bytes
//
// (09/03/21) mask wasn't correct (0x0f selects non-cartridge
// mirrors too) correct mask is 0x1f.
//
// the incorrect mask caused a false positive for Solaris when the
// threshold is 2.
//
// (20/06/21) this caused a falso positive for "Hack Em Hangly Pacman"
// when the threshold is 1
//
// change to only look for mirrors 0x1f and 0xff
if b[i+2] == 0x1f || b[i+2] == 0xff {
threshold--
if threshold == 0 {
return true
}
}
}
}
return false
}
func fingerprintParkerBros(b []byte) bool {
// fingerprint patterns taken from Stella CartDetector.cxx
for i := 0; i <= len(b)-3; i++ {
if (b[i] == 0x8d && b[i+1] == 0xe0 && b[i+2] == 0x1f) ||
(b[i] == 0x8d && b[i+1] == 0xe0 && b[i+2] == 0x5f) ||
(b[i] == 0x8d && b[i+1] == 0xe9 && b[i+2] == 0xff) ||
(b[i] == 0x0c && b[i+1] == 0xe0 && b[i+2] == 0x1f) ||
(b[i] == 0xad && b[i+1] == 0xe0 && b[i+2] == 0x1f) ||
(b[i] == 0xad && b[i+1] == 0xe9 && b[i+2] == 0xff) ||
(b[i] == 0xad && b[i+1] == 0xed && b[i+2] == 0xff) ||
(b[i] == 0xad && b[i+1] == 0xf3 && b[i+2] == 0xbf) {
return true
}
}
return false
}
func fingerprintDF(b []byte) bool {
if len(b) < 0xffb {
return false
}
return b[0xff8] == 'D' && b[0xff9] == 'F' && b[0xffa] == 'S' && b[0xffb] == 'C'
}
func fingerprintDPCplus(b []byte) bool {
if len(b) < 0x23 {
return false
}
return b[0x20] == 0x1e && b[0x21] == 0xab && b[0x22] == 0xad && b[0x23] == 0x10
}
func fingerprintCDF(b []byte) (bool, string) {
// CDFJ+ fingerprintin taken from Stella's CartridgeCDF:setupVersion()
//
// CDFJ+ ROMs are at least 64k bytes
if len(b) >= 65535 {
if b[0x174] == 0x50 && b[0x175] == 0x4c && b[0x176] == 0x55 && b[0x177] == 0x53 {
if b[0x178] == 0x43 && b[0x179] == 0x44 && b[0x17a] == 0x46 && b[0x17b] == 0x4a {
return true, "CDFJ+"
}
}
}
// all other CDF formats must be 32k bytes
if len(b) != 32768 {
return false, ""
}
count := 0
version := ""
for i := 0; i < len(b)-3; i++ {
if b[i] == 'C' && b[i+1] == 'D' && b[i+2] == 'F' {
var newVersion string
count++
// create version string. slightly different for CDFJ
if b[i+3] == 'J' {
newVersion = "CDFJ"
} else {
newVersion = fmt.Sprintf("CDF%1d", b[i+3])
}
// make sure the version number hasn't changed
if version != "" && version != newVersion {
return false, ""
}
version = newVersion
}
}
return count >= 3, version
}
func fingerprintSuperchargerFastLoad(cartload cartridgeloader.Loader) bool {
return len(*cartload.Data)%8448 == 0
}
func fingerprintTigervision(b []byte) bool {
// tigervision cartridges change banks by writing to memory address 0x3f. we
// can hypothesise that these types of cartridges will have that instruction
// sequence "85 3f" many times in a ROM whereas other cartridge types will not
threshold := 5
for i := 0; i < len(b)-1; i++ {
if b[i] == 0x85 && b[i+1] == 0x3f {
threshold--
}
if threshold == 0 {
return true
}
}
return false
}
func fingerprint8k(data []byte) func(*instance.Instance, []byte) (mapper.CartMapper, error) {
if fingerprintTigervision(data) {
return newTigervision
}
if fingerprintParkerBros(data) {
return newParkerBros
}
// mnetwork has the lowest threshold so place it at the end
if fingerprintMnetwork(data) {
return newMnetwork
}
return newAtari8k
}
func fingerprint16k(data []byte) func(*instance.Instance, []byte) (mapper.CartMapper, error) {
if fingerprintTigervision(data) {
return newTigervision
}
if fingerprintMnetwork(data) {
return newMnetwork
}
return newAtari16k
}
func fingerprint32k(data []byte) func(*instance.Instance, []byte) (mapper.CartMapper, error) {
if fingerprintTigervision(data) {
return newTigervision
}
return newAtari32k
}
func fingerprint64k(data []byte) func(*instance.Instance, []byte) (mapper.CartMapper, error) {
// some EF cartridges have EFEF embedded as a string but this isn't a
// requirement. for now, because only EF cartridges are 64k, we'll just
// assume the data should be treated as EF
return newEF
}
func fingerprint128k(data []byte) func(*instance.Instance, []byte) (mapper.CartMapper, error) {
if fingerprintDF(data) {
return newDF
}
return newSuperbank
}
func fingerprint256k(data []byte) func(*instance.Instance, []byte) (mapper.CartMapper, error) {
return newSuperbank
}
func (cart *Cartridge) fingerprint(cartload cartridgeloader.Loader) error {
var err error
if ok, version := fingerprintCDF(*cartload.Data); ok {
cart.mapper, err = cdf.NewCDF(cart.instance, cart.Filename, version, *cartload.Data)
return err
}
if fingerprintDPCplus(*cartload.Data) {
cart.mapper, err = dpcplus.NewDPCplus(cart.instance, cart.Filename, *cartload.Data)
return err
}
if fingerprintSuperchargerFastLoad(cartload) {
cart.mapper, err = supercharger.NewSupercharger(cart.instance, cartload)
return err
}
if fingerprint3ePlus(*cartload.Data) {
cart.mapper, err = new3ePlus(cart.instance, *cartload.Data)
return err
}
if fingerprint3e(*cartload.Data) {
cart.mapper, err = new3e(cart.instance, *cartload.Data)
return err
}
switch len(*cartload.Data) {
case 2048:
cart.mapper, err = newAtari2k(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 4096:
cart.mapper, err = newAtari4k(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 8192:
cart.mapper, err = fingerprint8k(*cartload.Data)(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 10240:
fallthrough
case 10495:
cart.mapper, err = newDPC(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 12288:
cart.mapper, err = newCBS(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 16384:
cart.mapper, err = fingerprint16k(*cartload.Data)(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 32768:
cart.mapper, err = fingerprint32k(*cartload.Data)(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 65536:
cart.mapper, err = fingerprint64k(*cartload.Data)(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 131072:
cart.mapper, err = fingerprint128k(*cartload.Data)(cart.instance, *cartload.Data)
if err != nil {
return err
}
case 262144:
cart.mapper, err = fingerprint256k(*cartload.Data)(cart.instance, *cartload.Data)
if err != nil {
return err
}
default:
return curated.Errorf("unrecognised size (%d bytes)", len(*cartload.Data))
}
// if cartridge mapper implements the optionalSuperChip interface then try
// to add the additional RAM
if superchip, ok := cart.mapper.(mapper.OptionalSuperchip); ok {
superchip.AddSuperchip()
}
return nil
}
// fingerprinting a PlusROM cartridge is slightly different to the main
// fingerprint() function above. the fingerprintPlusROM() function below is the
// first step. it checks for the byte sequence 8d f1 x1, which is the
// equivalent to STA $xff1, a necessary instruction in a PlusROM cartridge
//
// if this sequence is found then the function returns true, whereupon
// plusrom.NewPlusROM() can be called. the seoncd part of the fingerprinting
// process occurs in that function. if that fails then we can say that the true
// result from this function was a false positive.
func (cart *Cartridge) fingerprintPlusROM(cartload cartridgeloader.Loader) bool {
for i := 0; i < len(*cartload.Data)-2; i++ {
if (*cartload.Data)[i] == 0x8d && (*cartload.Data)[i+1] == 0xf1 && ((*cartload.Data)[i+2]&0x10) == 0x10 {
return true
}
}
return false
}