first poc for a web runtime

This commit is contained in:
2021-10-28 21:44:30 +02:00
parent 57eed68be8
commit ddc9a70006
6 changed files with 4630 additions and 0 deletions

3
web/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.parcel-cache/
dist/
node_modules/

7
web/package.json Normal file
View File

@@ -0,0 +1,7 @@
{
"devDependencies": {
"@parcel/optimizer-data-url": "^2.0.0",
"@parcel/transformer-inline-string": "^2.0.0",
"parcel": "^2.0.0"
}
}

13
web/src/index.html Normal file
View File

@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<meta charset="utf8" />
<title>MicroW8</title>
</head>
<body>
<canvas id="screen" width="320" height="256" />
<script type="module">
import "./main.js";
</script>
</body>
</html>

45
web/src/main.js Normal file
View File

@@ -0,0 +1,45 @@
import wasmDataUrl from "data-url:./trainride.uw8";
async function main() {
let wasm_module = await (await fetch(wasmDataUrl)).arrayBuffer();
let compiled_module = await WebAssembly.compile(wasm_module);
let import_object = {
uw8: {
ram: new WebAssembly.Memory({ initial: 8, maximum: 8 }),
time: new WebAssembly.Global({value: 'i32', mutable: true}, 0),
}
};
let instance = new WebAssembly.Instance(compiled_module, import_object);
let canvasCtx = document.getElementById('screen').getContext('2d');
let imageData = canvasCtx.createImageData(320, 256);
let buffer = imageData.data;
for(let i = 0; i < 320*256; ++i) {
buffer[i * 4 + 3] = 255;
}
let startTime = Date.now();
function mainloop() {
import_object.uw8.time.value = Date.now() - startTime;
instance.exports.tic();
let framebuffer = new Uint8Array(import_object.uw8.ram.buffer.slice(120, 120 + 320*256));
for(let i = 0; i < 320*256; ++i) {
buffer[i * 4] = framebuffer[i];
buffer[i * 4 + 1] = framebuffer[i];
buffer[i * 4 + 2] = framebuffer[i];
}
canvasCtx.putImageData(imageData, 0, 0);
window.requestAnimationFrame(mainloop);
}
mainloop();
}
main();

BIN
web/src/trainride.uw8 Normal file

Binary file not shown.

4562
web/yarn.lock Normal file

File diff suppressed because it is too large Load Diff