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
|
"use strict";
var gameboy = null; //GameBoyCore object.
var gbRunInterval = null; //GameBoyCore Timer
var settings = [ //Some settings.
true, //Turn on sound.
true, //Boot with boot ROM first?
false, //Give priority to GameBoy mode
1, //Volume level set.
true, //Colorize GB mode?
false, //Disallow typed arrays?
8, //Interval for the emulator loop.
10, //Audio buffer minimum span amount over x interpreter iterations.
20, //Audio buffer maximum span amount over x interpreter iterations.
false, //Override to allow for MBC1 instead of ROM only (compatibility for broken 3rd-party cartridges).
false, //Override MBC RAM disabling and always allow reading and writing to the banks.
false, //Use the GameBoy boot ROM instead of the GameBoy Color boot ROM.
false, //Scale the canvas in JS, or let the browser scale the canvas?
true, //Use image smoothing based scaling?
[true, true, true, true] //User controlled channel enables.
];
function start(canvas, ROM) {
clearLastEmulation();
autoSave(); //If we are about to load a new game, then save the last one...
gameboy = new GameBoyCore(canvas, ROM);
gameboy.openMBC = openSRAM;
gameboy.openRTC = openRTC;
gameboy.start();
run();
}
function run() {
if (GameBoyEmulatorInitialized()) {
if (!GameBoyEmulatorPlaying()) {
gameboy.stopEmulator &= 1;
cout("Starting the iterator.", 0);
var dateObj = new Date();
gameboy.firstIteration = dateObj.getTime();
gameboy.iterations = 0;
gbRunInterval = setInterval(function () {
if (!document.hidden && !document.msHidden && !document.mozHidden && !document.webkitHidden) {
gameboy.run();
}
}, settings[6]);
}
else {
cout("The GameBoy core is already running.", 1);
}
}
else {
cout("GameBoy core cannot run while it has not been initialized.", 1);
}
}
function pause() {
if (GameBoyEmulatorInitialized()) {
if (GameBoyEmulatorPlaying()) {
autoSave();
clearLastEmulation();
}
else {
cout("GameBoy core has already been paused.", 1);
}
}
else {
cout("GameBoy core cannot be paused while it has not been initialized.", 1);
}
}
function clearLastEmulation() {
if (GameBoyEmulatorInitialized() && GameBoyEmulatorPlaying()) {
clearInterval(gbRunInterval);
gameboy.stopEmulator |= 2;
cout("The previous emulation has been cleared.", 0);
}
else {
cout("No previous emulation was found to be cleared.", 0);
}
}
function save() {
if (GameBoyEmulatorInitialized()) {
var state_suffix = 0;
while (findValue("FREEZE_" + gameboy.name + "_" + state_suffix) != null) {
state_suffix++;
}
saveState("FREEZE_" + gameboy.name + "_" + state_suffix);
}
else {
cout("GameBoy core cannot be saved while it has not been initialized.", 1);
}
}
function saveSRAM() {
if (GameBoyEmulatorInitialized()) {
if (gameboy.cBATT) {
try {
var sram = gameboy.saveSRAMState();
if (sram.length > 0) {
cout("Saving the SRAM...", 0);
if (findValue("SRAM_" + gameboy.name) != null) {
//Remove the outdated storage format save:
cout("Deleting the old SRAM save due to outdated format.", 0);
deleteValue("SRAM_" + gameboy.name);
}
setValue("B64_SRAM_" + gameboy.name, arrayToBase64(sram));
}
else {
cout("SRAM could not be saved because it was empty.", 1);
}
}
catch (error) {
cout("Could not save the current emulation state(\"" + error.message + "\").", 2);
}
}
else {
cout("Cannot save a game that does not have battery backed SRAM specified.", 1);
}
saveRTC();
}
else {
cout("GameBoy core cannot be saved while it has not been initialized.", 1);
}
}
function saveRTC() { //Execute this when SRAM is being saved as well.
if (GameBoyEmulatorInitialized()) {
if (gameboy.cTIMER) {
try {
cout("Saving the RTC...", 0);
setValue("RTC_" + gameboy.name, gameboy.saveRTCState());
}
catch (error) {
cout("Could not save the RTC of the current emulation state(\"" + error.message + "\").", 2);
}
}
}
else {
cout("GameBoy core cannot be saved while it has not been initialized.", 1);
}
}
function autoSave() {
if (GameBoyEmulatorInitialized()) {
cout("Automatically saving the SRAM.", 0);
saveSRAM();
saveRTC();
}
}
function openSRAM(filename) {
try {
if (findValue("B64_SRAM_" + filename) != null) {
cout("Found a previous SRAM state (Will attempt to load).", 0);
return base64ToArray(findValue("B64_SRAM_" + filename));
}
else if (findValue("SRAM_" + filename) != null) {
cout("Found a previous SRAM state (Will attempt to load).", 0);
return findValue("SRAM_" + filename);
}
else {
cout("Could not find any previous SRAM copy for the current ROM.", 0);
}
}
catch (error) {
cout("Could not open the SRAM of the saved emulation state.", 2);
}
return [];
}
function openRTC(filename) {
try {
if (findValue("RTC_" + filename) != null) {
cout("Found a previous RTC state (Will attempt to load).", 0);
return findValue("RTC_" + filename);
}
else {
cout("Could not find any previous RTC copy for the current ROM.", 0);
}
}
catch (error) {
cout("Could not open the RTC data of the saved emulation state.", 2);
}
return [];
}
function saveState(filename) {
if (GameBoyEmulatorInitialized()) {
try {
setValue(filename, gameboy.saveState());
cout("Saved the current state as: " + filename, 0);
}
catch (error) {
cout("Could not save the current emulation state(\"" + error.message + "\").", 2);
}
}
else {
cout("GameBoy core cannot be saved while it has not been initialized.", 1);
}
}
function openState(filename, canvas) {
try {
if (findValue(filename) != null) {
try {
clearLastEmulation();
cout("Attempting to run a saved emulation state.", 0);
gameboy = new GameBoyCore(canvas, "");
gameboy.savedStateFileName = filename;
gameboy.returnFromState(findValue(filename));
run();
}
catch (error) {
alert(error.message + " file: " + error.fileName + " line: " + error.lineNumber);
}
}
else {
cout("Could not find the save state " + filename + "\".", 2);
}
}
catch (error) {
cout("Could not open the saved emulation state.", 2);
}
}
function import_save(blobData) {
blobData = decodeBlob(blobData);
if (blobData && blobData.blobs) {
if (blobData.blobs.length > 0) {
for (var index = 0; index < blobData.blobs.length; ++index) {
cout("Importing blob \"" + blobData.blobs[index].blobID + "\"", 0);
if (blobData.blobs[index].blobContent) {
if (blobData.blobs[index].blobID.substring(0, 5) == "SRAM_") {
setValue("B64_" + blobData.blobs[index].blobID, base64(blobData.blobs[index].blobContent));
}
else {
setValue(blobData.blobs[index].blobID, JSON.parse(blobData.blobs[index].blobContent));
}
}
else if (blobData.blobs[index].blobID) {
cout("Save file imported had blob \"" + blobData.blobs[index].blobID + "\" with no blob data interpretable.", 2);
}
else {
cout("Blob chunk information missing completely.", 2);
}
}
}
else {
cout("Could not decode the imported file.", 2);
}
}
else {
cout("Could not decode the imported file.", 2);
}
}
function generateBlob(keyName, encodedData) {
//Append the file format prefix:
var saveString = "EMULATOR_DATA";
var consoleID = "GameBoy";
//Figure out the length:
var totalLength = (saveString.length + 4 + (1 + consoleID.length)) + ((1 + keyName.length) + (4 + encodedData.length));
//Append the total length in bytes:
saveString += to_little_endian_dword(totalLength);
//Append the console ID text's length:
saveString += to_byte(consoleID.length);
//Append the console ID text:
saveString += consoleID;
//Append the blob ID:
saveString += to_byte(keyName.length);
saveString += keyName;
//Now append the save data:
saveString += to_little_endian_dword(encodedData.length);
saveString += encodedData;
return saveString;
}
function generateMultiBlob(blobPairs) {
var consoleID = "GameBoy";
//Figure out the initial length:
var totalLength = 13 + 4 + 1 + consoleID.length;
//Append the console ID text's length:
var saveString = to_byte(consoleID.length);
//Append the console ID text:
saveString += consoleID;
var keyName = "";
var encodedData = "";
//Now append all the blobs:
for (var index = 0; index < blobPairs.length; ++index) {
keyName = blobPairs[index][0];
encodedData = blobPairs[index][1];
//Append the blob ID:
saveString += to_byte(keyName.length);
saveString += keyName;
//Now append the save data:
saveString += to_little_endian_dword(encodedData.length);
saveString += encodedData;
//Update the total length:
totalLength += 1 + keyName.length + 4 + encodedData.length;
}
//Now add the prefix:
saveString = "EMULATOR_DATA" + to_little_endian_dword(totalLength) + saveString;
return saveString;
}
function decodeBlob(blobData) {
/*Format is as follows:
- 13 byte string "EMULATOR_DATA"
- 4 byte total size (including these 4 bytes).
- 1 byte Console type ID length
- Console type ID text of 8 bit size
blobs {
- 1 byte blob ID length
- blob ID text (Used to say what the data is (SRAM/freeze state/etc...))
- 4 byte blob length
- blob length of 32 bit size
}
*/
var length = blobData.length;
var blobProperties = {};
blobProperties.consoleID = null;
var blobsCount = -1;
blobProperties.blobs = [];
if (length > 17) {
if (blobData.substring(0, 13) == "EMULATOR_DATA") {
var length = Math.min(((blobData.charCodeAt(16) & 0xFF) << 24) | ((blobData.charCodeAt(15) & 0xFF) << 16) | ((blobData.charCodeAt(14) & 0xFF) << 8) | (blobData.charCodeAt(13) & 0xFF), length);
var consoleIDLength = blobData.charCodeAt(17) & 0xFF;
if (length > 17 + consoleIDLength) {
blobProperties.consoleID = blobData.substring(18, 18 + consoleIDLength);
var blobIDLength = 0;
var blobLength = 0;
for (var index = 18 + consoleIDLength; index < length;) {
blobIDLength = blobData.charCodeAt(index++) & 0xFF;
if (index + blobIDLength < length) {
blobProperties.blobs[++blobsCount] = {};
blobProperties.blobs[blobsCount].blobID = blobData.substring(index, index + blobIDLength);
index += blobIDLength;
if (index + 4 < length) {
blobLength = ((blobData.charCodeAt(index + 3) & 0xFF) << 24) | ((blobData.charCodeAt(index + 2) & 0xFF) << 16) | ((blobData.charCodeAt(index + 1) & 0xFF) << 8) | (blobData.charCodeAt(index) & 0xFF);
index += 4;
if (index + blobLength <= length) {
blobProperties.blobs[blobsCount].blobContent = blobData.substring(index, index + blobLength);
index += blobLength;
}
else {
cout("Blob length check failed, blob determined to be incomplete.", 2);
break;
}
}
else {
cout("Blob was incomplete, bailing out.", 2);
break;
}
}
else {
cout("Blob was incomplete, bailing out.", 2);
break;
}
}
}
}
}
return blobProperties;
}
function matchKey(key) { //Maps a keyboard key to a gameboy key.
//Order: Right, Left, Up, Down, A, B, Select, Start
var keymap = ["right", "left", "up", "down", "a", "b", "select", "start"]; //Keyboard button map.
for (var index = 0; index < keymap.length; index++) {
if (keymap[index] == key) {
return index;
}
}
return -1;
}
function GameBoyEmulatorInitialized() {
return (typeof gameboy == "object" && gameboy != null);
}
function GameBoyEmulatorPlaying() {
return ((gameboy.stopEmulator & 2) == 0);
}
function GameBoyKeyDown(key) {
if (GameBoyEmulatorInitialized() && GameBoyEmulatorPlaying()) {
GameBoyJoyPadEvent(matchKey(key), true);
}
}
function GameBoyJoyPadEvent(keycode, down) {
if (GameBoyEmulatorInitialized() && GameBoyEmulatorPlaying()) {
if (keycode >= 0 && keycode < 8) {
gameboy.JoyPadEvent(keycode, down);
}
}
}
function GameBoyKeyUp(key) {
if (GameBoyEmulatorInitialized() && GameBoyEmulatorPlaying()) {
GameBoyJoyPadEvent(matchKey(key), false);
}
}
function GameBoyGyroSignalHandler(e) {
if (GameBoyEmulatorInitialized() && GameBoyEmulatorPlaying()) {
if (e.gamma || e.beta) {
gameboy.GyroEvent(e.gamma * Math.PI / 180, e.beta * Math.PI / 180);
}
else {
gameboy.GyroEvent(e.x, e.y);
}
try {
e.preventDefault();
}
catch (error) { }
}
}
//The emulator will call this to sort out the canvas properties for (re)initialization.
function initNewCanvas() {
if (GameBoyEmulatorInitialized()) {
gameboy.canvas.width = gameboy.canvas.clientWidth;
gameboy.canvas.height = gameboy.canvas.clientHeight;
}
}
//Call this when resizing the canvas:
function initNewCanvasSize() {
if (GameBoyEmulatorInitialized()) {
if (!settings[12]) {
if (gameboy.onscreenWidth != 160 || gameboy.onscreenHeight != 144) {
gameboy.initLCD();
}
}
else {
if (gameboy.onscreenWidth != gameboy.canvas.clientWidth || gameboy.onscreenHeight != gameboy.canvas.clientHeight) {
gameboy.initLCD();
}
}
}
}
|