-
Notifications
You must be signed in to change notification settings - Fork 77
/
v86.js
114 lines (107 loc) · 3.19 KB
/
v86.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
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
vars[key] = value;
});
return vars;
}
var config = {
"bios_url": "",
"vbios_url": "",
"hdd_zip_url": "",
"img_filename": "",
"ram": 128,
"vram": 8,
"document": "",
"img_filename": ""
}
function loadFS() {
var xhr = new XMLHttpRequest();
xhr.open('GET', config['hdd_zip_url'], true);
xhr.responseType = 'arraybuffer';
xhr.onprogress = function (e) {
var loaded = Math.round(e.loaded / 1024) + " KB";
var total = Math.round(e.total / 1024) + " KB";
var loadingState = loaded + " / " + total;
document.getElementById("loading_status").textContent = loadingState;
};
xhr.onload = function (e) {
if (xhr.status === 200) {
document.getElementById("loading_status").textContent = "";
prepareFs(xhr.response);
}
}
xhr.send();
}
function prepareFs(zipData) {
var Buffer = BrowserFS.BFSRequire('buffer').Buffer;
BrowserFS.configure({
fs: "MountableFileSystem",
options: {
"/zip": {
fs: "ZipFS",
options: {
// Wrap as Buffer object.
zipData: Buffer.from(zipData)
}
}
}
}, function (e) {
if (e) {
// An error occurred.
throw e;
}
// Otherwise, BrowserFS is ready to use!
var fs = BrowserFS.BFSRequire('fs');
fs.readdir('/', function (e, contents) {
var diskBuffer = fs.readFileSync("zip/" + config["img_filename"]).buffer;
loadv86(diskBuffer);
});
});
}
function loadv86(diskBuffer) {
var emulator = window.emulator = new V86Starter({
wasm_path: 'https://dnbwg.cdn.bcebos.com/v86/v86.wasm',
memory_size: config["ram"] * 1024 * 1024,
vga_memory_size: config["vram"] * 1024 * 1024,
screen_container: document.getElementById("screen_container"),
bios: {
url: config['bios_url']
},
vga_bios: {
url: config['vbios_url'],
},
hda: {
buffer: diskBuffer,
},
autostart: true,
});
}
function showIntro() {
var showdownConv = new showdown.Converter();
var introUrl = config["document"];
if (!introUrl)
introUrl = "document/pending.md";
$.get(introUrl,
function (data) {
var htmlContent = showdownConv.makeHtml(data);
$("#introduction").html(htmlContent);
});
}
$(document).ready(function () {
var machineId = getUrlVars()["machine"];
var remote_url = "v86-machine/" + machineId + ".json"
config = $.ajax({
type: "GET",
url: remote_url,
async: false,
dataType: "json"
}).responseJSON;
showIntro();
loadFS();
var displayCanvas = document.getElementById("display_canvas");
displayCanvas.onclick = function () {
displayCanvas.requestPointerLock = displayCanvas.requestPointerLock || displayCanvas.mozRequestPointerLock;
displayCanvas.requestPointerLock();
}
});