-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameBoyAdvanceCartridge.as
280 lines (267 loc) · 6.71 KB
/
GameBoyAdvanceCartridge.as
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
package {
import Catridge.GameBoyAdvanceFlash;
import Catridge.GameBoyAdvanceSRAM;
import utils.ArrayHelper;
public class GameBoyAdvanceCartridge {
public var ROM;
public var ROM16;
public var ROM32;
public var saveType;
public var saveSize;
public var saveRTC;
public var rtc;
public var sram;
public var gameID;
public var IOCore;
public var ROMLength;
public var readROM16;
public var readROM32;
public function GameBoyAdvanceCartridge(IOCore) {
// constructor code
this.IOCore = IOCore;
this.initialize();
}
public function initialize() {
this.ROM = this.getROMArray(this.IOCore.emulatorCore.ROM);
this.ROM16 = ROM;
this.ROM32 = ROM;
this.preprocessROMAccess();
this.saveType = 0;
this.saveSize = 0;
this.saveRTC = false;
this.lookupCartridgeType();
}
public function getROMArray(old_array) {
this.ROMLength = (old_array.length >> 2) << 2;
var newArray = ArrayHelper.buildArray(this.ROMLength);
for (var index = 0; index < this.ROMLength; ++index) {
newArray[index] = old_array[index];
}
return newArray;
}
public function preprocessROMAccess() {
//this.readROM16 = (this.ROM16) ? this.readROM16Optimized : this.readROM16Slow;
//this.readROM32 = (this.ROM32) ? this.readROM32Optimized : this.readROM32Slow;
this.readROM16 = readROM16Slow;
this.readROM32 = readROM32Slow;
}
public function readROM(address) {
if ((address | 0) >= (this.ROMLength | 0)) {
return 0;
}
return this.ROM[address & 0x1FFFFFF] | 0;
/*if (!this.saveRTC) {
return this.ROM[address & 0x1FFFFFF] | 0;
}
else {
//GPIO Chip (RTC):
switch (address) {
case 0xC4:
return this.rtc.read0();
case 0xC5:
return 0;
case 0xC6:
return this.rtc.read1();
case 0xC7:
return 0;
case 0xC8:
return this.rtc.read2();
case 0xC9:
return 0;
default:
return this.ROM[address & 0x1FFFFFF] | 0;
}
}*/
}
public function readROM16Slow(address) {
if ((address | 0) >= (this.ROMLength | 0)) {
return 0;
}
return (this.ROM[address] | (this.ROM[address | 1] << 8)) >>> 0;
}
public function readROM16Optimized(address) {
if ((address | 0) >= (this.ROMLength | 0)) {
return 0;
}
return this.ROM16[(address >> 1) & 0xFFFFFF] | 0;
}
public function readROM32Slow(address) {
if ((address | 0) >= (this.ROMLength | 0)) {
return 0;
}
return (this.ROM[address] | (this.ROM[address | 1] << 8) | (this.ROM[address | 2] << 16) | (this.ROM[address | 3] << 24)) >>> 0;
}
public function readROM32Optimized(address) {
if ((address | 0) >= (this.ROMLength | 0)) {
return 0;
}
return this.ROM32[(address >> 2) & 0x7FFFFF] | 0;
}
public function writeROM(address, data) {
if (this.saveRTC) {
//GPIO Chip (RTC):
switch (address) {
case 0xC4:
this.rtc.write0(data);
case 0xC6:
this.rtc.write1(data);
case 0xC8:
this.rtc.write2(data);
}
}
}
public function readSRAM(address) {
address = address | 0;
return (this.saveType > 0) ? this.sram.read(address | 0) : 0;
}
public function writeSRAM(address, data) {
address = address | 0;
data = data | 0;
if (this.saveType > 0) {
this.sram.write(address | 0, data | 0);
}
}
public function lookupCartridgeType() {
this.gameID = ([
String.fromCharCode(this.ROM[0xAC]),
String.fromCharCode(this.ROM[0xAD]),
String.fromCharCode(this.ROM[0xAE]),
String.fromCharCode(this.ROM[0xAF])
]).join("");
this.IDLookup();
//Initialize the SRAM:
this.mapSRAM();
//Initialize the RTC:
//this.mapRTC();
}
public function mapSRAM() {
switch (this.saveType) {
//Flash
case 1:
this.sram = new GameBoyAdvanceFlash(this, this.saveSize);
this.loadExisting();
break;
//SRAM
case 2:
this.sram = new GameBoyAdvanceSRAM(this);
this.loadExisting();
break;
//EEPROM
/*case 3:
this.sram = new GameBoyAdvanceEEPROM(this, this.saveSize);
this.loadExisting();*/
default:
this.saveType = 0;
}
}
public function mapRTC() {
if (this.saveRTC) {
//this.rtc = new GameBoyAdvanceRTC(this);
var data = this.IOCore.emulatorCore.loadRTC(this.gameID);
if (data && data.length) {
this.rtc.load(data);
}
}
}
public function IDLookup() {
var found = 0;
var length = this.ROM.length - 6;
for (var index = 0; index < length; ++index) {
switch (this.ROM[index]) {
/*case 0x45: //E
if (this.isEEPROMCart(index)) {
found |= 2;
if (found == 3) {
return;
}
}
break;*/
case 0x46: //F
if (this.isFLASHCart(index)) {
found |= 2;
if (found == 3) {
return;
}
}
break;
case 0x52: //R
if (this.isRTCCart(index)) {
found |= 1;
if (found == 3) {
return;
}
}
break;
case 0x53: //S
if (this.isSRAMCart(index)) {
found |= 2;
if (found == 3) {
return;
}
}
}
}
}
public function isFLASHCart(index) {
if (String.fromCharCode(this.ROM[++index]) == "L") {
if (String.fromCharCode(this.ROM[++index]) == "A") {
if (String.fromCharCode(this.ROM[++index]) == "S") {
if (String.fromCharCode(this.ROM[++index]) == "H") {
switch (String.fromCharCode(this.ROM[index])) {
case "_":
case "5":
this.saveType = 1;
this.saveSize = 0x10000;
return true;
case "1":
this.saveType = 1;
this.saveSize = 0x20000;
return true;
}
}
}
}
}
return false;
}
public function isRTCCart(index) {
if (String.fromCharCode(this.ROM[++index]) == "T") {
if (String.fromCharCode(this.ROM[++index]) == "C") {
if (String.fromCharCode(this.ROM[++index]) == "_") {
if (String.fromCharCode(this.ROM[index]) == "V") {
this.saveRTC = true;
return true;
}
}
}
}
return false;
}
public function isSRAMCart(index) {
if (String.fromCharCode(this.ROM[++index]) == "R") {
if (String.fromCharCode(this.ROM[++index]) == "A") {
if (String.fromCharCode(this.ROM[++index]) == "M") {
if (String.fromCharCode(this.ROM[++index]) == "_") {
if (String.fromCharCode(this.ROM[index]) == "V") {
this.saveType = 2;
this.saveSize = 0x8000;
return true;
}
}
}
}
}
return false;
}
public function loadExisting() {
var data = this.IOCore.emulatorCore.loadSRAM(this.gameID);
if (data && data.length) {
this.sram.load(data);
}
}
public function nextIRQEventTime() {
//Nothing yet implement that would fire an IRQ:
return -1;
}
}
}