-
Notifications
You must be signed in to change notification settings - Fork 20
/
fingerprint.go
219 lines (181 loc) · 5 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
// 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/errors"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/harmony"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/supercharger"
)
func fingerprint3ePlus(b []byte) bool {
// 3e is similar to tigervision, a key difference being that it uses 0x3e
// to switch ram, in addition to 0x3f for switching banks.
//
// postulating that the fingerprint method can be the same except for the
// write address.
threshold3e := 5
threshold3f := 5
for i := range b {
if b[i] == 0x85 && b[i+1] == 0x3e {
threshold3e--
}
if b[i] == 0x85 && b[i+1] == 0x3f {
threshold3f--
}
if threshold3e <= 0 && threshold3f <= 0 {
return true
}
}
return false
}
func fingerprintMnetwork(b []byte) bool {
threshold := 2
for i := 0; i < len(b)-3; i++ {
if b[i] == 0x7e && b[i+1] == 0x66 && b[i+2] == 0x66 && b[i+3] == 0x66 {
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 fingerprintHarmony(b []byte) bool {
return b[0x20] == 0x1e && b[0x21] == 0xab && b[0x22] == 0xad && b[0x23] == 0x10
}
func fingerprintSuperchargerFastLoad(b []byte) bool {
l := len(b)
return l == 8448 || l == 25344 || l == 33792
}
func fingerprintTigervision(b []byte) bool {
// tigervision cartridges change banks by writing to memory address 0x3f. we
// can hypothesize 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([]byte) (cartMapper, error) {
if fingerprintTigervision(data) {
return newTigervision
}
if fingerprintParkerBros(data) {
return newParkerBros
}
return newAtari8k
}
func fingerprint16k(data []byte) func([]byte) (cartMapper, error) {
if fingerprintTigervision(data) {
return newTigervision
}
if fingerprintMnetwork(data) {
return newMnetwork
}
return newAtari16k
}
func fingerprint32k(data []byte) func([]byte) (cartMapper, error) {
if fingerprintTigervision(data) {
return newTigervision
}
return newAtari32k
}
func (cart *Cartridge) fingerprint(data []byte) error {
var err error
if fingerprintHarmony(data) {
// !!TODO: this might be a CFDJ cartridge. check for that.
cart.mapper, err = harmony.NewDPCplus(data)
return err
}
if fingerprintSuperchargerFastLoad(data) {
cart.mapper, err = supercharger.NewSupercharger(data)
return err
}
if fingerprint3ePlus(data) {
cart.mapper, err = new3ePlus(data)
return err
}
switch len(data) {
case 2048:
cart.mapper, err = newAtari2k(data)
if err != nil {
return err
}
case 4096:
cart.mapper, err = newAtari4k(data)
if err != nil {
return err
}
case 8192:
cart.mapper, err = fingerprint8k(data)(data)
if err != nil {
return err
}
case 10240:
fallthrough
case 10495:
cart.mapper, err = newDPC(data)
if err != nil {
return err
}
case 12288:
cart.mapper, err = newCBS(data)
if err != nil {
return err
}
case 16384:
cart.mapper, err = fingerprint16k(data)(data)
if err != nil {
return err
}
case 32768:
cart.mapper, err = fingerprint32k(data)(data)
if err != nil {
return err
}
case 65536:
return errors.New(errors.CartridgeError, "65536 bytes not yet supported")
default:
return errors.New(errors.CartridgeError, fmt.Sprintf("unrecognised cartridge size (%d bytes)", len(data)))
}
// if cartridge mapper implements the optionalSuperChip interface then try
// to add the additional RAM
if superchip, ok := cart.mapper.(optionalSuperchip); ok {
superchip.addSuperchip()
}
return nil
}