-
Notifications
You must be signed in to change notification settings - Fork 77
/
emularity.js
387 lines (334 loc) · 11.8 KB
/
emularity.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
var emuRunner = null;
var emuConfig = null;
var machineConfig = null;
var machineId = "emularity";
var emulator = null;
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
vars[key] = value;
});
return vars;
}
function processMachineJson(data) {
var machineList = data.machines;
machineId = getUrlVars()["machine"];
for (i = 0; i < machineList.length; i++) {
var machine = machineList[i];
if (machine.id == machineId) {
processMachineConfig(machine);
}
}
}
function runDoxbox() {
console.log("running dosbox");
var emuArguments = [
DosBoxLoader.emulatorJS(emuConfig.emulatorJS),
DosBoxLoader.nativeResolution(emuConfig.nativeResolution.width, emuConfig.nativeResolution.height),
DosBoxLoader.locateAdditionalEmulatorJS(function (filename) {
if (emuConfig.fileLocations[filename]) {
return emuConfig.fileLocations[filename]
} else {
return filename;
}
}),
DosBoxLoader.startExe(machineConfig.startExe),
DosBoxLoader.fileSystemKey(machineId)
]
var fileParams = buildFileLoadParameters(DosBoxLoader);
emuArguments = emuArguments.concat(fileParams);
emulator = new Emulator(document.querySelector("#emularity-canvas"), postRun, DosBoxLoader.apply(this, emuArguments));
emulator.start({ waitAfterDownloading: true });
}
function runPC98Dosbox() {
console.log("running pc98dosbox");
var emuArguments = [
PC98DosBoxLoader.emulatorJS(emuConfig.emulatorJS),
PC98DosBoxLoader.emulatorWASM(emuConfig.emulatorWASM),
PC98DosBoxLoader.nativeResolution(emuConfig.nativeResolution.width, emuConfig.nativeResolution.height),
PC98DosBoxLoader.startExe(machineConfig.startExe),
PC98DosBoxLoader.fileSystemKey(machineId)
];
var fileParams = buildFileLoadParameters(PC98DosBoxLoader);
emuArguments = emuArguments.concat(fileParams);
emulator = new Emulator(document.querySelector("#emularity-canvas"), postRun, PC98DosBoxLoader.apply(this, emuArguments));
emulator.start({ waitAfterDownloading: true });
}
function runSAE() {
console.log("running sae");
var emuArguments = [
SAELoader.model(emuConfig.driverName),
SAELoader.nativeResolution(emuConfig.nativeResolution.width, emuConfig.nativeResolution.height),
SAELoader.emulatorJS(emuConfig.emulatorJS),
SAELoader.fileSystemKey(machineId)
];
var fileParams = buildFileLoadParameters(SAELoader);
emuArguments = emuArguments.concat(fileParams);
emuArguments.push(
SAELoader.rom(emuConfig.rom)
);
for (var i = 0; i < machineConfig.floppy.length; i++) {
emuArguments.push(
SAELoader.floppy(i, machineConfig.floppy[i])
);
}
emulator = new Emulator(document.querySelector("#emularity-canvas"), postRun, SAELoader.apply(this, emuArguments));
emulator.start({ waitAfterDownloading: true });
}
function runMAME() {
console.log("running mame");
var emuArguments = [
JSMAMELoader.driver(emuConfig.driverName),
JSMAMELoader.nativeResolution(emuConfig.nativeResolution.width, emuConfig.nativeResolution.height),
JSMAMELoader.emulatorJS(emuConfig.emulatorJS),
JSMAMELoader.emulatorWASM(emuConfig.emulatorWASM),
JSMAMELoader.fileSystemKey(machineId)
];
var fileParams = buildFileLoadParameters(JSMAMELoader);
emuArguments = emuArguments.concat(fileParams);
for (var i = 0; i < machineConfig.peripherals.length; i++) {
var peripheral = machineConfig.peripherals[i];
emuArguments.push(
JSMAMELoader.peripheral(peripheral.name, peripheral.value)
);
}
var extraArgs = [];
if (emuConfig.extraArgs) {
extraArgs = extraArgs.concat(emuConfig.extraArgs);
}
if (machineConfig.extraArgs) {
extraArgs = extraArgs.concat(machineConfig.extraArgs);
}
if (extraArgs.length > 0) {
emuArguments.push(
JSMAMELoader.extraArgs(extraArgs)
);
}
var canvas = document.querySelector("#emularity-canvas");
canvas.onclick = function () {
canvas.focus();
canvas.requestPointerLock();
}
emulator = new Emulator(document.querySelector("#emularity-canvas"), postRun, JSMESSLoader.apply(this, emuArguments));
emulator.start({ waitAfterDownloading: true });
}
function runPCE() {
var emuArguments = [
PCELoader.emulatorJS(emuConfig.emulatorJS),
PCELoader.nativeResolution(emuConfig.nativeResolution.width, emuConfig.nativeResolution.height),
PCELoader.fileSystemKey(machineId),
PCELoader.locateAdditionalEmulatorJS(function (filename) {
if (emuConfig.fileLocations[filename]) {
return emuConfig.fileLocations[filename]
} else {
return filename;
}
}),
PCELoader.model(emuConfig.driverName)
];
var fileParams = buildFileLoadParameters(PCELoader);
emuArguments = emuArguments.concat(fileParams);
var extraArgs = [];
if (emuConfig.extraArgs) {
extraArgs = extraArgs.concat(emuConfig.extraArgs);
}
if (machineConfig.extraArgs) {
extraArgs = extraArgs.concat(machineConfig.extraArgs);
}
if (extraArgs.length > 0) {
emuArguments.push(
PCELoader.extraArgs(extraArgs)
);
}
var canvas = document.querySelector("#emularity-canvas");
canvas.onclick = function () {
canvas.requestPointerLock();
}
emulator = new Emulator(document.querySelector("#emularity-canvas"), postRun, PCELoader.apply(this, emuArguments));
emulator.start({ waitAfterDownloading: true });
}
function runNP2() {
var emuArguments = [
NP2Loader.nativeResolution(emuConfig.nativeResolution.width, emuConfig.nativeResolution.height),
NP2Loader.emulatorJS(emuConfig.emulatorJS),
NP2Loader.emulatorWASM(emuConfig.emulatorWASM),
NP2Loader.fileSystemKey(machineId),
];
var fileParams = buildFileLoadParameters(NP2Loader);
emuArguments = emuArguments.concat(fileParams);
var extraArgs = [];
if (emuConfig.extraArgs) {
extraArgs = extraArgs.concat(emuConfig.extraArgs);
}
if (machineConfig.extraArgs) {
extraArgs = extraArgs.concat(machineConfig.extraArgs);
}
if (machineConfig.autoLoad) {
emuArguments.push(NP2Loader.autoLoad(machineConfig.autoLoad));
} else {
emuArguments.push(NP2Loader.autoLoad(" "));
}
if (extraArgs.length > 0) {
emuArguments.push(
NP2Loader.extraArgs(extraArgs)
);
}
emulator = new Emulator(document.querySelector("#emularity-canvas"), postRun, NP2Loader.apply(this, emuArguments));
emulator.start({ waitAfterDownloading: true });
}
function buildFileLoadParameters(someLoader) {
var fileParams = [];
if (emuConfig.mountFile) {
for (var i = 0; i < emuConfig.mountFile.length; i++) {
var file = emuConfig.mountFile[i];
fileParams.push(
someLoader.mountFile(
file.targetPath,
someLoader.fetchFile(file.title, file.source)
)
);
}
}
if (emuConfig.mountZip) {
for (var i = 0; i < emuConfig.mountZip.length; i++) {
var file = emuConfig.mountZip[i];
fileParams.push(
someLoader.mountZip(
file.targetPath,
someLoader.fetchFile(
file.title,
file.source
))
);
}
}
if (machineConfig.files) {
for (var i = 0; i < machineConfig.files.length; i++) {
var file = machineConfig.files[i];
fileParams.push(
someLoader.mountFile(
file.targetPath,
someLoader.fetchFile(file.title, file.source)
)
);
}
}
if (machineConfig.zips) {
for (var i = 0; i < machineConfig.zips.length; i++) {
var zip = machineConfig.zips[i];
fileParams.push(
someLoader.mountZip(
zip.targetPath,
someLoader.fetchFile(
zip.title,
zip.source
))
);
}
}
return fileParams;
}
function processMachineConfig(machine) {
var emularityConfigURL = "emularity-config/" + machine.emularity + ".json";
if (machine.emularity.indexOf("dosbox-websocket") === 0) {
setupJoystick();
}
if (machine.emularity.indexOf("dosbox") === 0) {
emuRunner = runDoxbox;
}
if (machine.emularity.indexOf("sae") === 0) {
emuRunner = runSAE;
}
if (machine.emularity.indexOf("np2") === 0) {
emuRunner = runNP2;
}
if (machine.emularity.indexOf("pc98dosbox") === 0) {
emuRunner = runPC98Dosbox;
}
if (machine.emularity.indexOf("mame-") === 0) {
emuRunner = runMAME;
}
if (machine.emularity.indexOf("pce-") === 0) {
emuRunner = runPCE;
}
$.getJSON(emularityConfigURL, function (data) {
emuConfig = data;
newDataLoaded();
});
$.getJSON(machine.url, function (data) {
machineConfig = data;
newDataLoaded();
});
}
function newDataLoaded() {
if (emuConfig && machineConfig) {
showIntroduction();
emuRunner();
}
}
$(document).ready(function () {
$.ajaxSetup({ cache: false });
var machineParam = getUrlVars()["machine"];
var emularityParam = getUrlVars()["emularity"];
var machineUrlParam = getUrlVars()["machineurl"];
if (machineParam && machineParam.length > 0) {
$.getJSON("machines.json", processMachineJson);
} else if (emularityParam && machineUrlParam.length > 0) {
machineId = emularityParam + "-" + machineUrlParam.replace(/[.:#\/\\]/g, '-');
var dummyMachineConfig = {
"machineId": machineId,
"emularity": emularityParam,
"url": machineUrlParam
}
console.log(dummyMachineConfig);
processMachineConfig(dummyMachineConfig);
}
});
function postRun() {
console.log("Emulator started");
var bodyWidth = $("body").width();
if (bodyWidth < 600) {
resizeCanvas();
}
}
function resizeCanvas() {
var bodyWidth = $("body").width();
var canvasWidth = $("#emularity-canvas").width();
var canvasHeight = $("#emularity-canvas").height();
if (bodyWidth < canvasWidth) {
//Resize canvas for mobile device
var newHeight = Math.round(canvasHeight * bodyWidth / canvasWidth);
$("#emularity-canvas").width(bodyWidth);
$("#emularity-canvas").height(newHeight);
}
}
function toggleMobileKeyboard() {
var inputElement = document.getElementById("mobile-keyboard-helper");
inputElement.style.visibility = 'visible'; // unhide the input
inputElement.focus(); // focus on it so keyboard pops
}
function showIntroduction() {
var introUrl = "document/pending.md";
if (machineConfig.introduction) {
introUrl = machineConfig.introduction;
}
var showdownConv = new showdown.Converter();
showdownConv.setOption('tables', true);
$.get(introUrl,
function (data) {
var htmlContent = showdownConv.makeHtml(data);
$("#introduction").html(htmlContent);
}
);
}
function setupJoystick() {
navigator.getGamepads_ = navigator.getGamepads;
navigator.getGamepads = function () {
var gamepadlist = navigator.getGamepads_();
var pads = [];
for (var i = 0; i < gamepadlist.length; i++) {
pads[i] = (gamepadlist[i] !== null ? gamepadlist[i] : undefined);
}
return pads;
}
}