-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathgba.js
406 lines (381 loc) · 8.72 KB
/
gba.js
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
398
399
400
401
402
403
404
405
406
class GameBoyAdvance {
constructor() {
this.LOG_ERROR = 1;
this.LOG_WARN = 2;
this.LOG_STUB = 4;
this.LOG_INFO = 8;
this.LOG_DEBUG = 16;
this.SYS_ID = "com.endrift.gbajs";
this.logLevel = this.LOG_ERROR | this.LOG_WARN;
this.rom = null;
this.cpu = new ARMCore();
this.mmu = new GameBoyAdvanceMMU();
this.irq = new GameBoyAdvanceInterruptHandler();
this.io = new GameBoyAdvanceIO();
this.audio = new GameBoyAdvanceAudio();
this.video = new GameBoyAdvanceVideo();
this.keypad = new GameBoyAdvanceKeypad();
this.sio = new GameBoyAdvanceSIO();
// TODO: simplify this graph
this.cpu.mmu = this.mmu;
this.cpu.irq = this.irq;
this.mmu.cpu = this.cpu;
this.mmu.core = this;
this.irq.cpu = this.cpu;
this.irq.io = this.io;
this.irq.audio = this.audio;
this.irq.video = this.video;
this.irq.core = this;
this.io.cpu = this.cpu;
this.io.audio = this.audio;
this.io.video = this.video;
this.io.keypad = this.keypad;
this.io.sio = this.sio;
this.io.core = this;
this.audio.cpu = this.cpu;
this.audio.core = this;
this.video.cpu = this.cpu;
this.video.core = this;
this.keypad.core = this;
this.sio.core = this;
this.keypad.registerHandlers();
this.doStep = this.waitFrame;
this.paused = false;
this.seenFrame = false;
this.seenSave = false;
this.lastVblank = 0;
this.queue = null;
this.reportFPS = null;
this.throttle = 16; // This is rough, but the 2/3ms difference gives us a good overhead
var self = this;
window.queueFrame = function (f) {
self.queue = window.setTimeout(f, self.throttle);
};
window.URL = window.URL || window.webkitURL;
this.video.vblankCallback = function () {
self.seenFrame = true;
};
}
setCanvas(canvas) {
if (canvas.offsetWidth != 240 || canvas.offsetHeight != 160) {
var self = this;
this.indirectCanvas = document.createElement("canvas");
this.indirectCanvas.setAttribute("height", "160");
this.indirectCanvas.setAttribute("width", "240");
this.targetCanvas = canvas;
this.setCanvasDirect(this.indirectCanvas);
var targetContext = canvas.getContext("2d");
this.video.drawCallback = function () {
targetContext.drawImage(
self.indirectCanvas,
0,
0,
canvas.width,
canvas.height
);
};
} else {
this.setCanvasDirect(canvas);
var self = this;
}
}
setCanvasDirect(canvas) {
this.context = canvas.getContext("2d");
this.video.setBacking(this.context);
}
setBios(bios, real) {
this.mmu.loadBios(bios, real);
}
setRom(rom) {
this.reset();
this.rom = this.mmu.loadRom(rom, true);
if (!this.rom) {
return false;
}
this.retrieveSavedata();
return true;
}
hasRom() {
return !!this.rom;
}
loadRomFromFile(romFile, callback) {
var reader = new FileReader();
var self = this;
reader.onload = function (e) {
var result = self.setRom(e.target.result);
if (callback) {
callback(result);
}
};
reader.readAsArrayBuffer(romFile);
}
reset() {
this.audio.pause(true);
this.mmu.clear();
this.io.clear();
this.audio.clear();
this.video.clear();
this.sio.clear();
this.mmu.mmap(this.mmu.REGION_IO, this.io);
this.mmu.mmap(
this.mmu.REGION_PALETTE_RAM,
this.video.renderPath.palette
);
this.mmu.mmap(this.mmu.REGION_VRAM, this.video.renderPath.vram);
this.mmu.mmap(this.mmu.REGION_OAM, this.video.renderPath.oam);
this.cpu.resetCPU(0);
}
step() {
while (this.doStep()) {
this.cpu.step();
}
}
waitFrame() {
var seen = this.seenFrame;
this.seenFrame = false;
return !seen;
}
pause() {
this.paused = true;
this.audio.pause(true);
if (this.queue) {
clearTimeout(this.queue);
this.queue = null;
}
}
advanceFrame() {
this.step();
if (this.seenSave) {
if (!this.mmu.saveNeedsFlush()) {
this.storeSavedata();
this.seenSave = false;
} else {
this.mmu.flushSave();
}
} else if (this.mmu.saveNeedsFlush()) {
this.seenSave = true;
this.mmu.flushSave();
}
}
runStable() {
if (this.interval) {
return; // Already running
}
var self = this;
var timer = 0;
var frames = 0;
var runFunc;
var start = Date.now();
this.paused = false;
this.audio.pause(false);
if (this.reportFPS) {
runFunc = function () {
try {
timer += Date.now() - start;
if (self.paused) {
return;
} else {
queueFrame(runFunc);
}
start = Date.now();
self.advanceFrame();
++frames;
if (frames == 60) {
self.reportFPS((frames * 1000) / timer);
frames = 0;
timer = 0;
}
} catch (exception) {
self.ERROR(exception);
if (exception.stack) {
self.logStackTrace(exception.stack.split("\n"));
}
throw exception;
}
};
} else {
runFunc = function () {
try {
if (self.paused) {
return;
} else {
queueFrame(runFunc);
}
self.advanceFrame();
} catch (exception) {
self.ERROR(exception);
if (exception.stack) {
self.logStackTrace(exception.stack.split("\n"));
}
throw exception;
}
};
}
queueFrame(runFunc);
}
setSavedata(data) {
this.mmu.loadSavedata(data);
}
loadSavedataFromFile(saveFile) {
var reader = new FileReader();
var self = this;
reader.onload = function (e) {
self.setSavedata(e.target.result);
};
reader.readAsArrayBuffer(saveFile);
}
decodeSavedata(string) {
this.setSavedata(this.decodeBase64(string));
}
decodeBase64(string) {
var length = (string.length * 3) / 4;
if (string[string.length - 2] == "=") {
length -= 2;
} else if (string[string.length - 1] == "=") {
length -= 1;
}
var buffer = new ArrayBuffer(length);
var view = new Uint8Array(buffer);
var bits = string.match(/..../g);
for (var i = 0; i + 2 < length; i += 3) {
var s = atob(bits.shift());
view[i] = s.charCodeAt(0);
view[i + 1] = s.charCodeAt(1);
view[i + 2] = s.charCodeAt(2);
}
if (i < length) {
var s = atob(bits.shift());
view[i++] = s.charCodeAt(0);
if (s.length > 1) {
view[i++] = s.charCodeAt(1);
}
}
return buffer;
}
encodeBase64(view) {
var data = [];
var b;
var wordstring = [];
var triplet;
for (var i = 0; i < view.byteLength; ++i) {
b = view.getUint8(i, true);
wordstring.push(String.fromCharCode(b));
while (wordstring.length >= 3) {
triplet = wordstring.splice(0, 3);
data.push(btoa(triplet.join("")));
}
}
if (wordstring.length) {
data.push(btoa(wordstring.join("")));
}
return data.join("");
}
downloadSavedata() {
var sram = this.mmu.save;
if (!sram) {
this.WARN("No save data available");
return null;
}
if (window.URL) {
var url = window.URL.createObjectURL(
new Blob([sram.buffer], { type: "application/octet-stream" })
);
window.open(url);
} else {
var data = this.encodeBase64(sram.view);
window.open(
"data:application/octet-stream;base64," + data,
this.rom.code + ".sav"
);
}
}
storeSavedata() {
var sram = this.mmu.save;
try {
var storage = window.localStorage;
storage[this.SYS_ID + "." + this.mmu.cart.code] = this.encodeBase64(
sram.view
);
} catch (e) {
this.WARN("Could not store savedata! " + e);
}
}
retrieveSavedata() {
try {
var storage = window.localStorage;
var data = storage[this.SYS_ID + "." + this.mmu.cart.code];
if (data) {
this.decodeSavedata(data);
return true;
}
} catch (e) {
this.WARN("Could not retrieve savedata! " + e);
}
return false;
}
freeze() {
return {
cpu: this.cpu.freeze(),
mmu: this.mmu.freeze(),
irq: this.irq.freeze(),
io: this.io.freeze(),
audio: this.audio.freeze(),
video: this.video.freeze()
};
}
defrost(frost) {
this.cpu.defrost(frost.cpu);
this.mmu.defrost(frost.mmu);
this.audio.defrost(frost.audio);
this.video.defrost(frost.video);
this.irq.defrost(frost.irq);
this.io.defrost(frost.io);
}
log(level, message) { }
setLogger(logger) {
this.log = logger;
}
logStackTrace(stack) {
var overflow = stack.length - 32;
this.ERROR("Stack trace follows:");
if (overflow > 0) {
this.log(-1, "> (Too many frames)");
}
for (var i = Math.max(overflow, 0); i < stack.length; ++i) {
this.log(-1, "> " + stack[i]);
}
}
ERROR(error) {
if (this.logLevel & this.LOG_ERROR) {
this.log(this.LOG_ERROR, error);
}
}
WARN(warn) {
if (this.logLevel & this.LOG_WARN) {
this.log(this.LOG_WARN, warn);
}
}
STUB(func) {
if (this.logLevel & this.LOG_STUB) {
this.log(this.LOG_STUB, func);
}
}
INFO(info) {
if (this.logLevel & this.LOG_INFO) {
this.log(this.LOG_INFO, info);
}
}
DEBUG(info) {
if (this.logLevel & this.LOG_DEBUG) {
this.log(this.LOG_DEBUG, info);
}
}
ASSERT_UNREACHED(err) {
throw new Error("Should be unreached: " + err);
}
ASSERT(test, err) {
if (!test) {
throw new Error("Assertion failed: " + err);
}
}
}