clean up web runtime

This commit is contained in:
2021-11-21 15:47:27 +01:00
parent 40d684ea9a
commit 5aabf49b63
3 changed files with 42 additions and 45 deletions

View File

@@ -1,4 +1,4 @@
import "env.memory" memory(9);
import "env.memory" memory(4);
global mut base_end: i32 = 0;
@@ -8,17 +8,17 @@ export fn load_uw8(module_size: i32) -> i32 {
return module_size;
}
let module_end = 0x40000 + module_size;
let module_end = 0x1e000 + module_size;
if version & 1 {
module_end = uncompress(1, 0x40001);
module_end = uncompress(1, 0x1e001);
} else {
copy(0x40000, 0, module_size);
copy(0x1e000, 0, module_size);
}
copy(0, 0x84000, 8);
copy(0, 0x3c800, 8);
let base_start = 0x84008;
let base_start = 0x3c808;
let dest = 8;
let src = 0x40001;
let src = 0x1e001;
loop sections {
if src < module_end & (base_start >= base_end | ?src <= ?base_start) {
@@ -92,7 +92,7 @@ export fn uncompress(src_ptr: i32, dest_ptr: i32) -> i32 {
let i: i32;
loop init_contexts {
i!0x80000 = 0x8000;
i!0x3c000 = 0x8000;
branch_if (i := i + 4) < (256 + 1 + 128) * 4: init_contexts
}
@@ -142,7 +142,7 @@ fn upkr_length(context_index: i32) -> i32 {
}
fn upkr_bit(context_index: i32) -> i32 {
let prob = ((context_index * 4)!0x80000) as i64;
let prob = ((context_index * 4)!0x3c000) as i64;
loop refill {
if upkr_low >> 32i64 == (upkr_low + upkr_range - 1i64) >> 32i64 {
@@ -169,7 +169,7 @@ fn upkr_bit(context_index: i32) -> i32 {
prob = prob - (prob >> 4i64);
}
(context_index * 4)!0x80000 = prob as i32;
(context_index * 4)!0x3c000 = prob as i32;
bit
}
@@ -182,7 +182,7 @@ fn upkr_append_byte() {
}
start fn unpack_base() {
base_end = uncompress(0, 0x84000);
base_end = uncompress(0, 0x3c800);
}
data 0 {

View File

@@ -113,5 +113,6 @@ start fn setup() {
(i*4)!(120+320*240) = i * 0x10101;
branch_if (i := i + 1) < 256: colors
}
cls(0);
randomSeed(random());
}

View File

@@ -1,13 +1,6 @@
import loaderUrl from "data-url:../../platform/loader.wasm";
import platformUrl from "data-url:../../platform/platform.wasm";
async function loadWasm(url, imports) {
let wasm_module = await (await fetch(url)).arrayBuffer();
let compiled_module = await WebAssembly.compile(wasm_module);
return new WebAssembly.Instance(compiled_module, imports);
}
function setMessage(size, error) {
let html = size ? `${size} bytes` : 'Insert cart';
if (error) {
@@ -26,6 +19,8 @@ let canvasCtx = screen.getContext('2d');
let cancelFunction;
let U8 = (d) => new Uint8Array(d);
async function runModule(data) {
if (cancelFunction) {
cancelFunction();
@@ -39,12 +34,10 @@ async function runModule(data) {
return;
}
let dataU8Array = new Uint8Array(data);
let newURL = window.location.pathname;
if (cartridgeSize <= 1024) {
let dataString = '';
for (let byte of dataU8Array) {
for (let byte of U8(data)) {
dataString += String.fromCharCode(byte);
}
newURL += '#' + btoa(dataString);
@@ -56,31 +49,33 @@ async function runModule(data) {
screen.width = screen.width;
try {
let loaderImport = {
env: {
memory: new WebAssembly.Memory({ initial: 9 })
}
};
let loadMem = loaderImport.env.memory.buffer;
let loader = await loadWasm(loaderUrl, loaderImport);
if (dataU8Array[0] != 0) {
new Uint8Array(loadMem).set(dataU8Array);
let length = loader.exports.load_uw8(data.byteLength);
data = new ArrayBuffer(length);
new Uint8Array(data).set(new Uint8Array(loadMem).slice(0, length));
}
let memory = new WebAssembly.Memory({ initial: 4, maximum: 4 });
let memU8 = U8(memory.buffer);
let importObject = {
env: {
memory: new WebAssembly.Memory({ initial: 4, maximum: 4 }),
memory
},
};
let loader;
let loadModuleData = (data) => {
if (U8(data)[0] != 0) {
memU8.set(U8(data));
let length = loader.exports.load_uw8(data.byteLength);
data = new ArrayBuffer(length);
U8(data).set(memU8.slice(0, length));
}
return data;
}
let instantiate = async (data) => new WebAssembly.Instance(await WebAssembly.compile(data), importObject);
let loadModuleURL = async (url) => instantiate(loadModuleData(await (await fetch(url)).arrayBuffer()));
loader = await loadModuleURL(loaderUrl);
for (let n of ['acos', 'asin', 'atan', 'atan2', 'cos', 'exp', 'log', 'sin', 'tan', 'pow']) {
importObject.env[n] = Math[n];
}
@@ -93,13 +88,15 @@ async function runModule(data) {
importObject.env['g_reserved' + i] = 0;
}
let platform_instance = await loadWasm(platformUrl, importObject);
data = loadModuleData(data);
let platform_instance = await loadModuleURL(platformUrl);
for(let name in platform_instance.exports) {
importObject.env[name] = platform_instance.exports[name]
}
let instance = new WebAssembly.Instance(await WebAssembly.compile(data), importObject);
let instance = await instantiate(data);
let buffer = new Uint32Array(imageData.data.buffer);
@@ -116,10 +113,9 @@ async function runModule(data) {
try {
instance.exports.tic(Date.now() - startTime);
let framebuffer = new Uint8Array(importObject.env.memory.buffer.slice(120, 120 + 320 * 240));
let palette = new Uint32Array(importObject.env.memory.buffer.slice(76920, 76920 + 1024));
let palette = new Uint32Array(memory.buffer.slice(76920, 76920 + 1024));
for (let i = 0; i < 320 * 240; ++i) {
buffer[i] = palette[framebuffer[i]] | 0xff000000;
buffer[i] = palette[memU8[i + 120]] | 0xff000000;
}
framebufferCanvasCtx.putImageData(imageData, 0, 0);
canvasCtx.imageSmoothingEnabled = false;