-
Notifications
You must be signed in to change notification settings - Fork 78
/
tinyemu.js
126 lines (121 loc) · 3.61 KB
/
tinyemu.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
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
var Module = {
preRun: [],
postRun: [
function () {
init_drag_and_drop();
},
],
print: (function () {
return function (text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
};
})(),
printErr: function (text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
},
canvas: (function () {
var canvas = document.getElementById('canvas');
canvas.addEventListener("webglcontextlost", function (e) { alert('FIXME: WebGL context lost, please reload the page'); e.preventDefault(); }, false);
return canvas;
})(),
setStatus: function (text) {
console.log("status: " + text);
},
monitorRunDependencies: function (left) {
console.log("monitor run deps: " + left);
},
};
window.onerror = function (event) {
console.log("onerror: " + event);
};
function id(id) {
return document.getElementById(id);
}
function init_drag_and_drop() {
id('canvas').addEventListener('dragenter', load_dragenter, false);
id('canvas').addEventListener('dragleave', load_dragleave, false);
id('canvas').addEventListener('dragover', load_dragover, false);
id('canvas').addEventListener('drop', load_drop, false);
}
function load_dragenter(e) {
e.stopPropagation();
e.preventDefault();
}
function load_dragleave(e) {
e.stopPropagation();
e.preventDefault();
}
function load_dragover(e) {
e.stopPropagation();
e.preventDefault();
}
function load_drop(e) {
e.stopPropagation();
e.preventDefault();
load_file(e.dataTransfer.files);
}
function load_file(files) {
if (files.length > 0) {
let file = files[0];
if (file.size < (1024 * 1024)) {
let reader = new FileReader();
reader.onload = function (loadEvent) {
console.log('file loaded!')
let content = loadEvent.target.result;
if (content) {
console.log('content length: ' + content.byteLength);
let uint8Array = new Uint8Array(content);
let res = Module['ccall']('emsc_load_data',
'int',
['string', 'array', 'number'], // name, data, size
[file.name, uint8Array, uint8Array.length]);
if (res == 0) {
console.warn('emsc_loadfile() failed!');
}
}
else {
console.warn('load result empty!');
}
};
reader.readAsArrayBuffer(file);
}
else {
console.warn('ignoring dropped file because it is too big')
}
}
}
var emulator_server = "https://dnbwg.cdn.bcebos.com/tinyemu/"
window.onload = function (event) {
var machineTypes = { "cpc464": "cpc", "cpc6128": "cpc" }
var machineKey = getParameterByName("type");
var machineScript = window.document.createElement('script');
machineScript.src = emulator_server + machineTypes[machineKey] + ".js";
window.document.body.appendChild(machineScript);
this.showIntroduction(machineKey);
}
function showIntroduction(machineKey) {
var docs = {"cpc464":"document/cpc464.md"}
var introUrl = "document/pending.md";
if (docs[machineKey]) {
introUrl = docs[machineKey];
}
var showdownConv = new showdown.Converter();
showdownConv.setOption('tables', true);
$.get(introUrl,
function (data) {
var htmlContent = showdownConv.makeHtml(data);
$("#introduction").html(htmlContent);
}
);
}