-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
134 lines (124 loc) · 3.92 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<title>WebUxn</title>
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: #0f0f0f;
display: flex;
align-items: center;
justify-content: center;
}
#screen {
image-rendering: pixelated;
image-rendering: crisp-edges;
cursor: none;
touch-action: none;
}
#load-button {
display: none;
border: 2px dashed #c0c0c0;
border-radius: 8px;
height: 100%;
}
#load-button:hover {
border-color: #f0f0f0;
}
#load-button, #screen {
flex: 1;
max-width: 768px;
max-height: 512px;
}
</style>
</head>
<body>
<canvas id="screen" width="384" height="256"></canvas>
<a id="load-button" href="#"></a>
<script type="module">
const screenCanvas = document.getElementById("screen");
const loadButton = document.getElementById("load-button");
async function fetchArrayBuffer (url) {
const response = await fetch(url);
return await response.arrayBuffer();
}
function decodeZ85 (string) {
const decoder = [
0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00,
0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
];
let dest = new Uint8Array(string.length*5/4),
byte_nbr = 0,
char_nbr = 0,
value = 0;
while (byte_nbr < dest.length) {
var idx = string.charCodeAt(char_nbr++) - 32;
value = (value * 85) + decoder[idx];
if ((char_nbr % 5) == 0) {
var divisor = 256 * 256 * 256;
while (divisor >= 1) {
dest[byte_nbr++] = (value / divisor) | 0;
divisor >>= 8;
}
value = 0;
}
}
return dest.buffer;
}
async function findRomBuffer () {
const url = new URL(document.location);
// Try to get an URL from the ?rom query string
const romUrl = url.searchParams.get("rom");
if (romUrl) {
return fetchArrayBuffer(romUrl);
}
// Try to decode a buffer from the ?raw query string
if (url.searchParams.has("raw") && url.hash) {
return decodeZ85(unescape(url.hash.substr(1)));
}
// Otherwise wait for input
loadButton.style.display = "inherit";
screenCanvas.style.display = "none";
return new Promise(resolve => {
const input = document.createElement("input");
input.type = "file";
input.onchange = function () {
const file = input.files[0];
if (file != null) {
loadButton.style.display = "none";
screenCanvas.style.display = "inherit";
resolve(file.arrayBuffer());
}
};
loadButton.onclick = function () {
input.click();
};
});
}
(async function () {
// Load all assets in parallel
const [webuxn, wasmBuffer, romBuffer] = await Promise.all([
import("./webuxn.js"),
fetchArrayBuffer("build/webuxn.wasm"),
findRomBuffer(),
]);
webuxn.run(wasmBuffer, romBuffer, screenCanvas);
})();
</script>
</body>
</html>