-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgba.js
11004 lines (11001 loc) · 358 KB
/
mgba.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var mGBA = (function () {
var _scriptDir =
typeof document !== "undefined" && document.currentScript
? document.currentScript.src
: undefined;
if (typeof __filename !== "undefined") _scriptDir = _scriptDir || __filename;
return function (mGBA) {
mGBA = mGBA || {};
var Module = typeof mGBA !== "undefined" ? mGBA : {};
function findFunctions(mod) {
let keys = [
"A",
"B",
"L",
"R",
"Start",
"Select",
"Up",
"Down",
"Left",
"Right",
"Forward",
];
keys.forEach((key) => {
mod[`getKey${key}`] = cwrap(`getKey${key}`, "string", []);
mod[`setKey${key}`] = cwrap(`setKey${key}`, "null", ["string"]);
});
}
Module.loadFile = (function () {
var loadGame = cwrap("loadGame", "number", ["string"]);
return function (name) {
if (loadGame(name)) {
var arr = name.split(".");
arr.pop();
Module.gameName = name;
Module.saveName = arr.join(".") + ".sav";
return true;
}
return false;
};
})();
Module.getSave = function () {
return FS.readFile("/data/saves/" + Module.saveName);
};
findFunctions(Module);
var moduleOverrides = {};
var key;
for (key in Module) {
if (Module.hasOwnProperty(key)) {
moduleOverrides[key] = Module[key];
}
}
var arguments_ = [];
var thisProgram = "./this.program";
var quit_ = function (status, toThrow) {
throw toThrow;
};
var ENVIRONMENT_IS_WEB = false;
var ENVIRONMENT_IS_WORKER = false;
var ENVIRONMENT_IS_NODE = false;
var ENVIRONMENT_HAS_NODE = false;
var ENVIRONMENT_IS_SHELL = false;
ENVIRONMENT_IS_WEB = typeof window === "object";
ENVIRONMENT_IS_WORKER = typeof importScripts === "function";
ENVIRONMENT_HAS_NODE =
typeof process === "object" &&
typeof process.versions === "object" &&
typeof process.versions.node === "string";
ENVIRONMENT_IS_NODE =
ENVIRONMENT_HAS_NODE && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
ENVIRONMENT_IS_SHELL =
!ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
var scriptDirectory = "";
function locateFile(path) {
if (Module["locateFile"]) {
return Module["locateFile"](path, scriptDirectory);
}
return scriptDirectory + path;
}
var read_, readAsync, readBinary, setWindowTitle;
var nodeFS;
var nodePath;
if (ENVIRONMENT_IS_NODE) {
scriptDirectory = __dirname + "/";
read_ = function shell_read(filename, binary) {
if (!nodeFS) nodeFS = require("fs");
if (!nodePath) nodePath = require("path");
filename = nodePath["normalize"](filename);
return nodeFS["readFileSync"](filename, binary ? null : "utf8");
};
readBinary = function readBinary(filename) {
var ret = read_(filename, true);
if (!ret.buffer) {
ret = new Uint8Array(ret);
}
assert(ret.buffer);
return ret;
};
if (process["argv"].length > 1) {
thisProgram = process["argv"][1].replace(/\\/g, "/");
}
arguments_ = process["argv"].slice(2);
process["on"]("uncaughtException", function (ex) {
if (!(ex instanceof ExitStatus)) {
throw ex;
}
});
process["on"]("unhandledRejection", abort);
quit_ = function (status) {
process["exit"](status);
};
Module["inspect"] = function () {
return "[Emscripten Module object]";
};
} else if (ENVIRONMENT_IS_SHELL) {
if (typeof read != "undefined") {
read_ = function shell_read(f) {
return read(f);
};
}
readBinary = function readBinary(f) {
var data;
if (typeof readbuffer === "function") {
return new Uint8Array(readbuffer(f));
}
data = read(f, "binary");
assert(typeof data === "object");
return data;
};
if (typeof scriptArgs != "undefined") {
arguments_ = scriptArgs;
} else if (typeof arguments != "undefined") {
arguments_ = arguments;
}
if (typeof quit === "function") {
quit_ = function (status) {
quit(status);
};
}
if (typeof print !== "undefined") {
if (typeof console === "undefined") console = {};
console.log = print;
console.warn = console.error =
typeof printErr !== "undefined" ? printErr : print;
}
} else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
if (ENVIRONMENT_IS_WORKER) {
scriptDirectory = self.location.href;
} else if (document.currentScript) {
scriptDirectory = document.currentScript.src;
}
if (_scriptDir) {
scriptDirectory = _scriptDir;
}
if (scriptDirectory.indexOf("blob:") !== 0) {
scriptDirectory = scriptDirectory.substr(
0,
scriptDirectory.lastIndexOf("/") + 1,
);
} else {
scriptDirectory = "";
}
{
read_ = function shell_read(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.send(null);
return xhr.responseText;
};
if (ENVIRONMENT_IS_WORKER) {
readBinary = function readBinary(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.responseType = "arraybuffer";
xhr.send(null);
return new Uint8Array(xhr.response);
};
}
readAsync = function readAsync(url, onload, onerror) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "arraybuffer";
xhr.onload = function xhr_onload() {
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
onload(xhr.response);
return;
}
onerror();
};
xhr.onerror = onerror;
xhr.send(null);
};
}
setWindowTitle = function (title) {
document.title = title;
};
} else {
}
var out = Module["print"] || console.log.bind(console);
var err = Module["printErr"] || console.warn.bind(console);
for (key in moduleOverrides) {
if (moduleOverrides.hasOwnProperty(key)) {
Module[key] = moduleOverrides[key];
}
}
moduleOverrides = null;
if (Module["arguments"]) arguments_ = Module["arguments"];
if (Module["thisProgram"]) thisProgram = Module["thisProgram"];
if (Module["quit"]) quit_ = Module["quit"];
Module["setWindowTitle"] = setWindowTitle;
var STACK_ALIGN = 16;
function dynamicAlloc(size) {
var ret = HEAP32[DYNAMICTOP_PTR >> 2];
var end = (ret + size + 15) & -16;
if (end > _emscripten_get_heap_size()) {
abort();
}
HEAP32[DYNAMICTOP_PTR >> 2] = end;
return ret;
}
function getNativeTypeSize(type) {
switch (type) {
case "i1":
case "i8":
return 1;
case "i16":
return 2;
case "i32":
return 4;
case "i64":
return 8;
case "float":
return 4;
case "double":
return 8;
default: {
if (type[type.length - 1] === "*") {
return 4;
} else if (type[0] === "i") {
var bits = parseInt(type.substr(1));
assert(
bits % 8 === 0,
"getNativeTypeSize invalid bits " + bits + ", type " + type,
);
return bits / 8;
} else {
return 0;
}
}
}
}
function warnOnce(text) {
if (!warnOnce.shown) warnOnce.shown = {};
if (!warnOnce.shown[text]) {
warnOnce.shown[text] = 1;
err(text);
}
}
var asm2wasmImports = {
"f64-rem": function (x, y) {
return x % y;
},
debugger: function () {},
};
var jsCallStartIndex = 1;
var functionPointers = new Array(0);
function convertJsFunctionToWasm(func, sig) {
if (typeof WebAssembly.Function === "function") {
var typeNames = { i: "i32", j: "i64", f: "f32", d: "f64" };
var type = {
parameters: [],
results: sig[0] == "v" ? [] : [typeNames[sig[0]]],
};
for (var i = 1; i < sig.length; ++i) {
type.parameters.push(typeNames[sig[i]]);
}
return new WebAssembly.Function(type, func);
}
var typeSection = [1, 0, 1, 96];
var sigRet = sig.slice(0, 1);
var sigParam = sig.slice(1);
var typeCodes = { i: 127, j: 126, f: 125, d: 124 };
typeSection.push(sigParam.length);
for (var i = 0; i < sigParam.length; ++i) {
typeSection.push(typeCodes[sigParam[i]]);
}
if (sigRet == "v") {
typeSection.push(0);
} else {
typeSection = typeSection.concat([1, typeCodes[sigRet]]);
}
typeSection[1] = typeSection.length - 2;
var bytes = new Uint8Array(
[0, 97, 115, 109, 1, 0, 0, 0].concat(
typeSection,
[2, 7, 1, 1, 101, 1, 102, 0, 0, 7, 5, 1, 1, 102, 0, 0],
),
);
var module = new WebAssembly.Module(bytes);
var instance = new WebAssembly.Instance(module, { e: { f: func } });
var wrappedFunc = instance.exports["f"];
return wrappedFunc;
}
var funcWrappers = {};
function dynCall(sig, ptr, args) {
if (args && args.length) {
return Module["dynCall_" + sig].apply(null, [ptr].concat(args));
} else {
return Module["dynCall_" + sig].call(null, ptr);
}
}
var tempRet0 = 0;
var setTempRet0 = function (value) {
tempRet0 = value;
};
var getTempRet0 = function () {
return tempRet0;
};
var wasmBinary;
if (Module["wasmBinary"]) wasmBinary = Module["wasmBinary"];
var noExitRuntime;
if (Module["noExitRuntime"]) noExitRuntime = Module["noExitRuntime"];
if (typeof WebAssembly !== "object") {
err("no native wasm support detected");
}
function setValue(ptr, value, type, noSafe) {
type = type || "i8";
if (type.charAt(type.length - 1) === "*") type = "i32";
switch (type) {
case "i1":
HEAP8[ptr >> 0] = value;
break;
case "i8":
HEAP8[ptr >> 0] = value;
break;
case "i16":
HEAP16[ptr >> 1] = value;
break;
case "i32":
HEAP32[ptr >> 2] = value;
break;
case "i64":
(tempI64 = [
value >>> 0,
((tempDouble = value),
+Math_abs(tempDouble) >= 1
? tempDouble > 0
? (Math_min(+Math_floor(tempDouble / 4294967296), 4294967295) |
0) >>>
0
: ~~+Math_ceil(
(tempDouble - +(~~tempDouble >>> 0)) / 4294967296,
) >>> 0
: 0),
]),
(HEAP32[ptr >> 2] = tempI64[0]),
(HEAP32[(ptr + 4) >> 2] = tempI64[1]);
break;
case "float":
HEAPF32[ptr >> 2] = value;
break;
case "double":
HEAPF64[ptr >> 3] = value;
break;
default:
abort("invalid type for setValue: " + type);
}
}
var wasmMemory;
var wasmTable = new WebAssembly.Table({
initial: 4916,
maximum: 4916,
element: "anyfunc",
});
var ABORT = false;
var EXITSTATUS = 0;
function assert(condition, text) {
if (!condition) {
abort("Assertion failed: " + text);
}
}
function getCFunc(ident) {
var func = Module["_" + ident];
assert(
func,
"Cannot call unknown function " + ident + ", make sure it is exported",
);
return func;
}
function ccall(ident, returnType, argTypes, args, opts) {
var toC = {
string: function (str) {
var ret = 0;
if (str !== null && str !== undefined && str !== 0) {
var len = (str.length << 2) + 1;
ret = stackAlloc(len);
stringToUTF8(str, ret, len);
}
return ret;
},
array: function (arr) {
var ret = stackAlloc(arr.length);
writeArrayToMemory(arr, ret);
return ret;
},
};
function convertReturnValue(ret) {
if (returnType === "string") return UTF8ToString(ret);
if (returnType === "boolean") return Boolean(ret);
return ret;
}
var func = getCFunc(ident);
var cArgs = [];
var stack = 0;
if (args) {
for (var i = 0; i < args.length; i++) {
var converter = toC[argTypes[i]];
if (converter) {
if (stack === 0) stack = stackSave();
cArgs[i] = converter(args[i]);
} else {
cArgs[i] = args[i];
}
}
}
var ret = func.apply(null, cArgs);
ret = convertReturnValue(ret);
if (stack !== 0) stackRestore(stack);
return ret;
}
function cwrap(ident, returnType, argTypes, opts) {
argTypes = argTypes || [];
var numericArgs = argTypes.every(function (type) {
return type === "number";
});
var numericRet = returnType !== "string";
if (numericRet && numericArgs && !opts) {
return getCFunc(ident);
}
return function () {
return ccall(ident, returnType, argTypes, arguments, opts);
};
}
var ALLOC_NORMAL = 0;
var ALLOC_NONE = 3;
function allocate(slab, types, allocator, ptr) {
var zeroinit, size;
if (typeof slab === "number") {
zeroinit = true;
size = slab;
} else {
zeroinit = false;
size = slab.length;
}
var singleType = typeof types === "string" ? types : null;
var ret;
if (allocator == ALLOC_NONE) {
ret = ptr;
} else {
ret = [_malloc, stackAlloc, dynamicAlloc][allocator](
Math.max(size, singleType ? 1 : types.length),
);
}
if (zeroinit) {
var stop;
ptr = ret;
assert((ret & 3) == 0);
stop = ret + (size & ~3);
for (; ptr < stop; ptr += 4) {
HEAP32[ptr >> 2] = 0;
}
stop = ret + size;
while (ptr < stop) {
HEAP8[ptr++ >> 0] = 0;
}
return ret;
}
if (singleType === "i8") {
if (slab.subarray || slab.slice) {
HEAPU8.set(slab, ret);
} else {
HEAPU8.set(new Uint8Array(slab), ret);
}
return ret;
}
var i = 0,
type,
typeSize,
previousType;
while (i < size) {
var curr = slab[i];
type = singleType || types[i];
if (type === 0) {
i++;
continue;
}
if (type == "i64") type = "i32";
setValue(ret + i, curr, type);
if (previousType !== type) {
typeSize = getNativeTypeSize(type);
previousType = type;
}
i += typeSize;
}
return ret;
}
function getMemory(size) {
if (!runtimeInitialized) return dynamicAlloc(size);
return _malloc(size);
}
var UTF8Decoder =
typeof TextDecoder !== "undefined" ? new TextDecoder("utf8") : undefined;
function UTF8ArrayToString(u8Array, idx, maxBytesToRead) {
var endIdx = idx + maxBytesToRead;
var endPtr = idx;
while (u8Array[endPtr] && !(endPtr >= endIdx)) ++endPtr;
if (endPtr - idx > 16 && u8Array.subarray && UTF8Decoder) {
return UTF8Decoder.decode(u8Array.subarray(idx, endPtr));
} else {
var str = "";
while (idx < endPtr) {
var u0 = u8Array[idx++];
if (!(u0 & 128)) {
str += String.fromCharCode(u0);
continue;
}
var u1 = u8Array[idx++] & 63;
if ((u0 & 224) == 192) {
str += String.fromCharCode(((u0 & 31) << 6) | u1);
continue;
}
var u2 = u8Array[idx++] & 63;
if ((u0 & 240) == 224) {
u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
} else {
u0 =
((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (u8Array[idx++] & 63);
}
if (u0 < 65536) {
str += String.fromCharCode(u0);
} else {
var ch = u0 - 65536;
str += String.fromCharCode(55296 | (ch >> 10), 56320 | (ch & 1023));
}
}
}
return str;
}
function UTF8ToString(ptr, maxBytesToRead) {
return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead) : "";
}
function stringToUTF8Array(str, outU8Array, outIdx, maxBytesToWrite) {
if (!(maxBytesToWrite > 0)) return 0;
var startIdx = outIdx;
var endIdx = outIdx + maxBytesToWrite - 1;
for (var i = 0; i < str.length; ++i) {
var u = str.charCodeAt(i);
if (u >= 55296 && u <= 57343) {
var u1 = str.charCodeAt(++i);
u = (65536 + ((u & 1023) << 10)) | (u1 & 1023);
}
if (u <= 127) {
if (outIdx >= endIdx) break;
outU8Array[outIdx++] = u;
} else if (u <= 2047) {
if (outIdx + 1 >= endIdx) break;
outU8Array[outIdx++] = 192 | (u >> 6);
outU8Array[outIdx++] = 128 | (u & 63);
} else if (u <= 65535) {
if (outIdx + 2 >= endIdx) break;
outU8Array[outIdx++] = 224 | (u >> 12);
outU8Array[outIdx++] = 128 | ((u >> 6) & 63);
outU8Array[outIdx++] = 128 | (u & 63);
} else {
if (outIdx + 3 >= endIdx) break;
outU8Array[outIdx++] = 240 | (u >> 18);
outU8Array[outIdx++] = 128 | ((u >> 12) & 63);
outU8Array[outIdx++] = 128 | ((u >> 6) & 63);
outU8Array[outIdx++] = 128 | (u & 63);
}
}
outU8Array[outIdx] = 0;
return outIdx - startIdx;
}
function stringToUTF8(str, outPtr, maxBytesToWrite) {
return stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite);
}
function lengthBytesUTF8(str) {
var len = 0;
for (var i = 0; i < str.length; ++i) {
var u = str.charCodeAt(i);
if (u >= 55296 && u <= 57343)
u = (65536 + ((u & 1023) << 10)) | (str.charCodeAt(++i) & 1023);
if (u <= 127) ++len;
else if (u <= 2047) len += 2;
else if (u <= 65535) len += 3;
else len += 4;
}
return len;
}
var UTF16Decoder =
typeof TextDecoder !== "undefined"
? new TextDecoder("utf-16le")
: undefined;
function allocateUTF8(str) {
var size = lengthBytesUTF8(str) + 1;
var ret = _malloc(size);
if (ret) stringToUTF8Array(str, HEAP8, ret, size);
return ret;
}
function allocateUTF8OnStack(str) {
var size = lengthBytesUTF8(str) + 1;
var ret = stackAlloc(size);
stringToUTF8Array(str, HEAP8, ret, size);
return ret;
}
function writeArrayToMemory(array, buffer) {
HEAP8.set(array, buffer);
}
function writeAsciiToMemory(str, buffer, dontAddNull) {
for (var i = 0; i < str.length; ++i) {
HEAP8[buffer++ >> 0] = str.charCodeAt(i);
}
if (!dontAddNull) HEAP8[buffer >> 0] = 0;
}
var PAGE_SIZE = 16384;
var WASM_PAGE_SIZE = 65536;
function alignUp(x, multiple) {
if (x % multiple > 0) {
x += multiple - (x % multiple);
}
return x;
}
var buffer,
HEAP8,
HEAPU8,
HEAP16,
HEAPU16,
HEAP32,
HEAPU32,
HEAPF32,
HEAPF64;
function updateGlobalBufferAndViews(buf) {
buffer = buf;
Module["HEAP8"] = HEAP8 = new Int8Array(buf);
Module["HEAP16"] = HEAP16 = new Int16Array(buf);
Module["HEAP32"] = HEAP32 = new Int32Array(buf);
Module["HEAPU8"] = HEAPU8 = new Uint8Array(buf);
Module["HEAPU16"] = HEAPU16 = new Uint16Array(buf);
Module["HEAPU32"] = HEAPU32 = new Uint32Array(buf);
Module["HEAPF32"] = HEAPF32 = new Float32Array(buf);
Module["HEAPF64"] = HEAPF64 = new Float64Array(buf);
}
var STACK_BASE = 249120,
DYNAMIC_BASE = 5492e3,
DYNAMICTOP_PTR = 248928;
var INITIAL_TOTAL_MEMORY = Module["TOTAL_MEMORY"] || 67108864;
if (Module["wasmMemory"]) {
wasmMemory = Module["wasmMemory"];
} else {
wasmMemory = new WebAssembly.Memory({
initial: INITIAL_TOTAL_MEMORY / WASM_PAGE_SIZE,
});
}
if (wasmMemory) {
buffer = wasmMemory.buffer;
}
INITIAL_TOTAL_MEMORY = buffer.byteLength;
updateGlobalBufferAndViews(buffer);
HEAP32[DYNAMICTOP_PTR >> 2] = DYNAMIC_BASE;
function callRuntimeCallbacks(callbacks) {
while (callbacks.length > 0) {
var callback = callbacks.shift();
if (typeof callback == "function") {
callback();
continue;
}
var func = callback.func;
if (typeof func === "number") {
if (callback.arg === undefined) {
Module["dynCall_v"](func);
} else {
Module["dynCall_vi"](func, callback.arg);
}
} else {
func(callback.arg === undefined ? null : callback.arg);
}
}
}
var __ATPRERUN__ = [];
var __ATINIT__ = [];
var __ATMAIN__ = [];
var __ATEXIT__ = [];
var __ATPOSTRUN__ = [];
var runtimeInitialized = false;
var runtimeExited = false;
function preRun() {
if (Module["preRun"]) {
if (typeof Module["preRun"] == "function")
Module["preRun"] = [Module["preRun"]];
while (Module["preRun"].length) {
addOnPreRun(Module["preRun"].shift());
}
}
callRuntimeCallbacks(__ATPRERUN__);
}
function initRuntime() {
runtimeInitialized = true;
if (!Module["noFSInit"] && !FS.init.initialized) FS.init();
TTY.init();
callRuntimeCallbacks(__ATINIT__);
}
function preMain() {
FS.ignorePermissions = false;
callRuntimeCallbacks(__ATMAIN__);
}
function exitRuntime() {
runtimeExited = true;
}
function postRun() {
if (Module["postRun"]) {
if (typeof Module["postRun"] == "function")
Module["postRun"] = [Module["postRun"]];
while (Module["postRun"].length) {
addOnPostRun(Module["postRun"].shift());
}
}
callRuntimeCallbacks(__ATPOSTRUN__);
}
function addOnPreRun(cb) {
__ATPRERUN__.unshift(cb);
}
function addOnPostRun(cb) {
__ATPOSTRUN__.unshift(cb);
}
var Math_abs = Math.abs;
var Math_ceil = Math.ceil;
var Math_floor = Math.floor;
var Math_min = Math.min;
var runDependencies = 0;
var runDependencyWatcher = null;
var dependenciesFulfilled = null;
function getUniqueRunDependency(id) {
return id;
}
function addRunDependency(id) {
runDependencies++;
if (Module["monitorRunDependencies"]) {
Module["monitorRunDependencies"](runDependencies);
}
}
function removeRunDependency(id) {
runDependencies--;
if (Module["monitorRunDependencies"]) {
Module["monitorRunDependencies"](runDependencies);
}
if (runDependencies == 0) {
if (runDependencyWatcher !== null) {
clearInterval(runDependencyWatcher);
runDependencyWatcher = null;
}
if (dependenciesFulfilled) {
var callback = dependenciesFulfilled;
dependenciesFulfilled = null;
callback();
}
}
}
Module["preloadedImages"] = {};
Module["preloadedAudios"] = {};
function abort(what) {
if (Module["onAbort"]) {
Module["onAbort"](what);
}
what += "";
out(what);
err(what);
ABORT = true;
EXITSTATUS = 1;
what = "abort(" + what + "). Build with -s ASSERTIONS=1 for more info.";
throw new WebAssembly.RuntimeError(what);
}
var dataURIPrefix = "data:application/octet-stream;base64,";
function isDataURI(filename) {
return String.prototype.startsWith
? filename.startsWith(dataURIPrefix)
: filename.indexOf(dataURIPrefix) === 0;
}
var wasmBinaryFile = "/js/mgba.wasm";
if (!isDataURI(wasmBinaryFile)) {
wasmBinaryFile = locateFile(wasmBinaryFile);
}
function getBinary() {
try {
if (wasmBinary) {
return new Uint8Array(wasmBinary);
}
if (readBinary) {
return readBinary(wasmBinaryFile);
} else {
throw "both async and sync fetching of the wasm failed";
}
} catch (err) {
abort(err);
}
}
function getBinaryPromise() {
if (
!wasmBinary &&
(ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) &&
typeof fetch === "function"
) {
return fetch(wasmBinaryFile, { credentials: "same-origin" })
.then(function (response) {
if (!response["ok"]) {
throw (
"failed to load wasm binary file at '" + wasmBinaryFile + "'"
);
}
return response["arrayBuffer"]();
})
.catch(function () {
return getBinary();
});
}
return new Promise(function (resolve, reject) {
resolve(getBinary());
});
}
function createWasm() {
var info = {
env: asmLibraryArg,
wasi_unstable: asmLibraryArg,
global: { NaN: NaN, Infinity: Infinity },
"global.Math": Math,
asm2wasm: asm2wasmImports,
};
function receiveInstance(instance, module) {
var exports = instance.exports;
Module["asm"] = exports;
removeRunDependency("wasm-instantiate");
}
addRunDependency("wasm-instantiate");
function receiveInstantiatedSource(output) {
receiveInstance(output["instance"]);
}
function instantiateArrayBuffer(receiver) {
return getBinaryPromise()
.then(function (binary) {
return WebAssembly.instantiate(binary, info);
})
.then(receiver, function (reason) {
err("failed to asynchronously prepare wasm: " + reason);
abort(reason);
});
}
function instantiateAsync() {
if (
!wasmBinary &&
typeof WebAssembly.instantiateStreaming === "function" &&
!isDataURI(wasmBinaryFile) &&
typeof fetch === "function"
) {
fetch(wasmBinaryFile, { credentials: "same-origin" }).then(
function (response) {
var result = WebAssembly.instantiateStreaming(response, info);
return result.then(receiveInstantiatedSource, function (reason) {
err("wasm streaming compile failed: " + reason);
err("falling back to ArrayBuffer instantiation");
instantiateArrayBuffer(receiveInstantiatedSource);
});
},
);
} else {
return instantiateArrayBuffer(receiveInstantiatedSource);
}
}
if (Module["instantiateWasm"]) {
try {
var exports = Module["instantiateWasm"](info, receiveInstance);
return exports;
} catch (e) {
err("Module.instantiateWasm callback failed with error: " + e);
return false;
}
}
instantiateAsync();
return {};
}
Module["asm"] = createWasm;
var tempDouble;
var tempI64;
var ASM_CONSTS = [
function ($0, $1, $2, $3, $4, $5, $6) {
Module.version = {
gitCommit: UTF8ToString($0),
gitShort: UTF8ToString($1),
gitBranch: UTF8ToString($2),
gitRevision: $3,
binaryName: UTF8ToString($4),
projectName: UTF8ToString($5),
projectVersion: UTF8ToString($6),
};
},
function () {
FS.mkdir("/data");
FS.mount(IDBFS, {}, "/data");
FS.mkdir("/data/saves");
FS.mkdir("/data/states");
FS.syncfs(true, function (err) {
if (!err) return;
console.log("FS.syncfs:");
console.log(err);
});
},
function () {
return screen.width;
},
function () {
return screen.height;
},
function ($0) {
if (typeof Module["setWindowTitle"] !== "undefined") {
Module["setWindowTitle"](UTF8ToString($0));
}
return 0;
},
function ($0, $1, $2) {
var w = $0;
var h = $1;
var pixels = $2;
if (!Module["SDL2"]) Module["SDL2"] = {};
var SDL2 = Module["SDL2"];
if (SDL2.ctxCanvas !== Module["canvas"]) {
SDL2.ctx = Module["createContext"](Module["canvas"], false, true);
SDL2.ctxCanvas = Module["canvas"];
}
if (SDL2.w !== w || SDL2.h !== h || SDL2.imageCtx !== SDL2.ctx) {
SDL2.image = SDL2.ctx.createImageData(w, h);
SDL2.w = w;
SDL2.h = h;
SDL2.imageCtx = SDL2.ctx;
}
var data = SDL2.image.data;
var src = pixels >> 2;
var dst = 0;
var num;
if (
typeof CanvasPixelArray !== "undefined" &&
data instanceof CanvasPixelArray
) {
num = data.length;
while (dst < num) {
var val = HEAP32[src];
data[dst] = val & 255;
data[dst + 1] = (val >> 8) & 255;
data[dst + 2] = (val >> 16) & 255;
data[dst + 3] = 255;
src++;
dst += 4;
}
} else {
if (SDL2.data32Data !== data) {
SDL2.data32 = new Int32Array(data.buffer);
SDL2.data8 = new Uint8Array(data.buffer);
}
var data32 = SDL2.data32;
num = data32.length;
data32.set(HEAP32.subarray(src, src + num));
var data8 = SDL2.data8;
var i = 3;
var j = i + 4 * num;
if (num % 8 == 0) {
while (i < j) {
data8[i] = 255;
i = (i + 4) | 0;
data8[i] = 255;
i = (i + 4) | 0;
data8[i] = 255;
i = (i + 4) | 0;
data8[i] = 255;
i = (i + 4) | 0;
data8[i] = 255;
i = (i + 4) | 0;
data8[i] = 255;
i = (i + 4) | 0;
data8[i] = 255;
i = (i + 4) | 0;
data8[i] = 255;
i = (i + 4) | 0;
}
} else {
while (i < j) {
data8[i] = 255;
i = (i + 4) | 0;
}
}
}
SDL2.ctx.putImageData(SDL2.image, 0, 0);