39 Commits

Author SHA1 Message Date
44b8656f29 make both native and browser runtime optional 2022-02-21 22:54:14 +01:00
90467f7c5b add some more polish to web runner 2022-02-20 23:54:28 +01:00
c56196bd2e actual run --browser instead of just optimized technotunnel 2022-02-20 18:26:57 +01:00
266493ca1c run --browser is working (very unpolished) 2022-02-19 23:58:13 +01:00
4c75ba2e44 start implementing run --browser 2022-02-19 16:21:02 +01:00
f1493ebded Split core web runtime into separate js 2022-02-17 23:27:57 +01:00
5d41733142 add download links to 0.1.1 2022-02-04 23:23:29 +01:00
c25a52b61b Prepare 0.1.1 release
add devkit mode to web runtime
add unpack and compile commands to uw8
2022-02-04 22:25:17 +01:00
619ea903ba add support for table/element section in pack command 2022-02-02 00:02:55 +01:00
9b900a49e4 add screenshot on F9, increase video rate + docs 2022-01-30 20:03:13 +01:00
f21497dd2e implement more robust file watcher 2022-01-29 15:22:14 +01:00
381eaf970f Add first basic logo version 2022-01-29 15:21:35 +01:00
9632adb57f add basic video recording on F10 in web runtime 2022-01-24 09:05:10 +01:00
33e08e9b73 add watchdog to interupt hanging update 2022-01-23 20:18:07 +01:00
cacde9136c update dependencies, disable wayland support
wayland support doesn't fully work with gnome
(missing window decoration)
2022-01-23 13:16:04 +01:00
81a38c2d75 Add download links to released 0.1.0 2022-01-13 06:40:01 +01:00
c9ae04e652 last preparations for 0.1.0 release 2022-01-13 06:20:00 +01:00
179b3eaed1 Finish docs, add hero screenshot 2022-01-12 22:18:36 +01:00
b7d0004307 write up usage instructions for the uw8 tool 2022-01-12 00:03:54 +01:00
b34685b1c2 finished api docs 2022-01-10 23:52:09 +01:00
3d79239a01 work on api documentation 2022-01-09 23:30:14 +01:00
f062e545f6 implement "itch.io support" 2022-01-04 22:34:21 +01:00
68890e62ad don't exit on runtime error in watch mode 2022-01-03 23:36:58 +01:00
10ba7ed3bb make use of nontrapping-fptoint, add tunnel.cwa as reference 2022-01-02 16:34:11 +01:00
acea5cb6e0 enable nontrapping fptoint feature in c and zig example 2022-01-02 16:06:01 +01:00
d239775411 forgot to add file 2022-01-02 15:42:28 +01:00
20365a0dd0 port tunnel example to C 2022-01-02 15:22:11 +01:00
88ee0e1bef port rust tunnel example to zig 2022-01-02 13:58:56 +01:00
26206a312a add filter-exports command to automatically remove unused exports
this removes the need for a manual step in the rust example
2022-01-01 19:02:58 +01:00
6a75988489 update rust example 2022-01-01 15:51:18 +01:00
397ff19d80 test remaining control codes 2022-01-01 15:37:17 +01:00
b7e8ddf0f1 test new control codes 2022-01-01 14:19:58 +01:00
ed9d9fdeb5 implement more control codes 2021-12-31 23:09:49 +01:00
cd1275a78f implement framework for control codes 2021-12-31 22:12:21 +01:00
32345876b9 v0.1pre5 + some virtual fireworks for today 2021-12-31 11:08:31 +01:00
6ca63b87e5 add rectangle_outline function 2021-12-30 22:03:46 +01:00
bf7604d385 implement circle_outline function 2021-12-30 16:25:53 +01:00
0f22b1efb9 some more loader size optimizations 2021-12-28 21:43:58 +01:00
4521fb73ef size optimized uncompress function in loader 2021-12-28 17:32:11 +01:00
59 changed files with 3445 additions and 984 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
/target /target
.cargo/ .cargo/
.vscode/ .vscode/
/examples/**/*.wasm
/examples/**/*.uw8

1196
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,26 @@
[package] [package]
name = "uw8" name = "uw8"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["native", "browser"]
native = ["wasmtime"]
browser = ["warp", "tokio", "tokio-stream", "webbrowser"]
[dependencies] [dependencies]
wasmtime = "0.30" wasmtime = { version = "0.30", optional = true }
anyhow = "1" anyhow = "1"
minifb = "0.19" minifb = { version = "0.20", default-features = false, features = ["x11"] }
notify = "4" notify = "4"
pico-args = "0.4" pico-args = "0.4"
curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "196719b" } curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "196719b" }
wat = "1" wat = "1"
uw8-tool = { path = "uw8-tool" } uw8-tool = { path = "uw8-tool" }
same-file = "1"
warp = { version = "0.3.2", optional = true }
tokio = { version = "1.17.0", features = ["sync", "rt"], optional = true }
tokio-stream = { version = "0.1.8", features = ["sync"], optional = true }
webbrowser = { version = "0.6.0", optional = true }

View File

@@ -1 +1,81 @@
See [here](https://exoticorn.github.io/microw8/). # MicroW8
MicroW8 is a WebAssembly based fantasy console inspired by the likes of [TIC-80](https://tic80.com/), [WASM-4](https://wasm4.org/) and [PICO-8](https://www.lexaloffle.com/pico-8.php).
The initial motivation behind MicroW8 was to explore whether there was a way to make WebAssembly viable for size-coding. (Size coding being the art of creating tiny (often <= 256 bytes) graphical effects and games.) The available examples so far are all in this space, however, I very carefully made sure that all design decisions make sense from the point of view of bigger projects as well.
See [here](https://exoticorn.github.io/microw8/) for more information and docs.
## Specs
* Screen: 320x240, 256 colors, 60Hz
* Modules: Up to 256KB (WASM)
* Memory: 256KB
* Gamepad input (D-Pad + 4 Buttons)
## Downloads
* [Linux](https://github.com/exoticorn/microw8/releases/download/v0.1.0/microw8-0.1.0-linux.tgz)
* [MacOS](https://github.com/exoticorn/microw8/releases/download/v0.1.0/microw8-0.1.0-macos.tgz)
* [Windows](https://github.com/exoticorn/microw8/releases/download/v0.1.0/microw8-0.1.0-windows.zip)
The download includes
* `microw8.html`: The web runtime, a small, self-contained html file that can be opened in any modern browser to load and run MicroW8 carts.
* `uw8`/`uw8.exe`: The MicroW8 dev tool, including a native runtime.
* `examples`: Example source code in CurlyWas and Wat (WebAssembly text format).
* `carts`: The examples compiled to `.uw8` carts.
## uw8 dev tool
```
uw8 run [<options>] <file>
Runs <file> which can be a binary WebAssembly module, an `.uw8` cart, a wat (WebAssembly text format) source file or a CurlyWas source file.
Options:
-t, --timeout FRAMES : Sets the timeout in frames (1/60s)
-w, --watch : Reloads the given file every time it changes on disk.
-p, --pack : Pack the file into an .uw8 cart before running it and print the resulting size.
-u, --uncompressed : Use the uncompressed uw8 format for packing.
-l LEVEL, --level LEVEL : Compression level (0-9). Higher compression levels are really slow.
-o FILE, --output FILE : Write the loaded and optionally packed cart back to disk.
uw8 pack [<options>] <infile> <outfile>
Packs the WebAssembly module or text file, or CurlyWas source file into a .uw8 cart.
Options:
-u, --uncompressed : Use the uncompressed uw8 format for packing.
-l LEVEL, --level LEVEL : Compression level (0-9). Higher compression levels are really slow.
uw8 unpack <infile> <outfile>
Unpacks a MicroW8 module into a standard WebAssembly module.
uw8 compile [<options>] <infile> <outfile>
Compiles a CurlyWas source file to a standard WebAssembly module. Most useful together with
the --debug option to get a module that works well in the Chrome debugger.
Options:
-d, --debug : Generate a name section to help debugging
uw8 filter-exports <infile> <outfile>
Reads a binary WebAssembly module, removes all exports not used by the MicroW8 platform + everything that is unreachable without those exports and writes the resulting module to <outfile>.
```
## Examples
* [Fireworks](https://exoticorn.github.io/microw8/v0.1pre5#AgwvgP+M59snqjl4CMKw5sqm1Zw9yJCbSviMjeLUdHus2a3yl/a99+uiBeqZgP/2jqSjrLjRk73COMM6OSLpsxK8ugT1kuk/q4hQUqqPpGozHoa0laulzGGcahzdfdJsYaK1sIdeIYS9M5PnJx/Wk9H+PvWEPy2Zvv7I6IW7Fg==) (127 bytes): Some fireworks to welcome 2022.
* [Skip Ahead](https://exoticorn.github.io/microw8/v0.1pre5#AgyfpZ80wkW28kiUZ9VIK4v+RPnVxqjK1dz2BcDoNyQPsS2g4OgEzkTe6jyoAfFOmqKrS8SM2aRljBal9mjNn8i4fP9eBK+RehQKxxGtJa9FqftvqEnh3ez1YaYxqj7jgTdzJ/WAYVmKMovBT1myrX3FamqKSOgMsNedLhVTLAhQup3sNcYEjGNo8b0HZ5+AgMgCwYRGCe//XQOMAaAAzqDILgmpEZ/43RKHcQpHEQwbURfNQJpadJe2sz3q5FlQnTGXQ9oSMokidhlC+aR/IpNHieuBGLhFZ2GfnwVQ0geBbQpTPA==) (229 bytes): A port of my [TIC-80 256byte game](http://tic80.com/play?cart=1735) from LoveByte'21
* [OhNoAnotherTunnel](https://exoticorn.github.io/microw8/v0.1pre4#Ag95rdCB5Ww5NofyQaKF4P1mrNRso4azgiem4hK99Gh8OMzSpFq3NsNDo7O7pqln10D11l9uXr/ritw7OEzKwbEfCdvaRnS2Z0Kz0iDEZt/gIqOdvFmxsL1MjPQ4XInPbUJpQUonhQq29oP2omFabnQxn0bzoK7mZjcwc5GetHG+hGajkJcRr8oOnjfCol8RD+ha33GYtPnut+GLe4ktzf5UxZwGs6oT9qqC61lRDakN) (177 bytes): A port of my [entry](http://tic80.com/play?cart=1871) in the Outline'21 bytebattle final
* [Technotunnel](https://exoticorn.github.io/microw8/v0.1pre4#AqL8HeK1M9dn2nWNIF5vaq/Vh64pMt5nJIFoFKpBMPUsGtDtpqjo1JbT9LzPhAxCqJ7Yh4TA6oTGd4xhLowf+cWZMY73+7AZmfXJJsBi4cej/hH+4wlAgxFIrnOYnr/18IpnZbsHf0eGm1BhahX74+cVR0TRmNQmYC7GhCNS3mv/3MJn74lCj7t28aBJPjEZhP9fGXdG2u5Egh/Tjdg=) (158 bytes): A port of my [entry](https://tic80.com/play?cart=1873) in the Outline'21 bytebattle quater final

6
examples/c/build.sh Executable file
View File

@@ -0,0 +1,6 @@
#!/bin/bash
clang -O2 -Wno-incompatible-library-redeclaration --no-standard-libraries -ffast-math -Xclang -target-feature -Xclang +nontrapping-fptoint -Wl,--no-entry,--export-all,--import-memory,--initial-memory=262144,--global-base=81920,-zstack-size=4096 -o cart.wasm cart.c --target=wasm32 && \
uw8 filter-exports cart.wasm cart.wasm && \
wasm-opt -Oz --fast-math --strip-producers -o cart.wasm cart.wasm && \
uw8 pack -l 9 cart.wasm cart.uw8

27
examples/c/cart.c Normal file
View File

@@ -0,0 +1,27 @@
#define IMPORT(MODULE, NAME) __attribute__((import_module(MODULE), import_name(NAME)))
IMPORT("env", "atan2") extern float atan2(float, float);
IMPORT("env", "time") extern float time();
float sqrt(float v) {
return __builtin_sqrt(v);
}
#define FRAMEBUFFER ((unsigned char*)120)
void upd() {
int i = 0;
for( ;; ) {
float t = time() * 63.0f;
float x = (float)(i % 320 - 160);
float y = (float)(i / 320 - 120);
float d = 40000.0f / sqrt(x * x + y * y);
float u = atan2(x, y) * 512.0f / 3.141f;
unsigned char c = (unsigned char)((int)(d + t * 2.0f) ^ (int)(u + t)) >> 4;
FRAMEBUFFER[i] = c;
i += 1;
if(i >= 320*240) break;
}
}

View File

@@ -0,0 +1,23 @@
import "env.memory" memory(4);
import "env.printString" fn printString(i32);
export fn upd() {
printString(0x20000);
}
data 0x20000 {
i8(14, 0xfd, 15, 15, 12) // clear screen to color 0xfd
"Top left"
i8(14, 10, 11, 11, 11, 11) // scroll up 4 lines
i8(31, 28, 29, 14, 0xfd) "Bottom right"
i8(14, 10, 10, 10) // scroll down 2 lines
i8(31, 40, 3, 14, 10, 15, 0xf0) "Other colors"
i8(24, 0xb0) "inverted"
i8(13, 10, 8, 8) "->"
i8(10, 10, 9, 9, 1) "|<-"
i8(5, 31, 7, 28+17, 15, 0xe3) "Graphics text!"
i8(5, 31, 6, 28+16, 15, 0xe5) "Graphics text!"
i8(4, 24, 14, 10, 0x90, 0x80, 0xf1)
i8(31, 37, 29, 0xf1, 0x80, 0x90)
i8(0)
}

View File

@@ -0,0 +1,32 @@
import "env.time" fn time() -> f32;
import "env.circle" fn circle(f32, f32, f32, i32);
import "env.cls" fn cls(i32);
import "env.randomSeed" fn seed(i32);
import "env.randomf" fn randomf() -> f32;
import "env.sin" fn sin(f32) -> f32;
import "env.cos" fn cos(f32) -> f32;
import "env.fmod" fn fmod(f32, f32) -> f32;
export fn upd() {
cls(0);
let i: i32;
loop pixels {
let inline rocket = i #>> 9;
let lazy local_time = fmod(time() + rocket as f32 / 5 as f32, 2 as f32);
let lazy rocket = rocket + nearest(time() - local_time) as i32 * 10;
seed(rocket);
let inline x = randomf() * 645 as f32;
let y = randomf() * 133 as f32;
let lazy angle = { seed(i); randomf() } * 44 as f32;
let inline dx = sin(angle);
let inline dy = cos(angle);
let lazy dist = local_time * (randomf() * 44 as f32);
circle(
x + dx * dist,
y + dy * dist + local_time * local_time * 24 as f32,
1 as f32, (rocket % 11 + 1) * 16 - (local_time * 7 as f32) as i32 - (i % 4)
);
branch_if (i := i + 1) < 5120: pixels;
}
}

View File

@@ -1,102 +1,21 @@
import "env.memory" memory(4); import "env.memory" memory(4);
import "env.cls" fn cls(i32); import "env.cls" fn cls(i32);
import "env.setPixel" fn setPixel(i32, i32, i32);
import "env.time" fn time() -> f32; import "env.time" fn time() -> f32;
import "env.line" fn line(f32, f32, f32, f32, i32);
import "env.sin" fn sin(f32) -> f32; import "env.sin" fn sin(f32) -> f32;
import "env.cos" fn cos(f32) -> f32; import "env.cos" fn cos(f32) -> f32;
fn line(x1: f32, y1: f32, x2: f32, y2: f32, col: i32) {
let swapTmp: f32;
if x1 > x2 {
swapTmp = x1;
x1 = x2;
x2 = swapTmp;
swapTmp = y1;
y1 = y2;
y2 = swapTmp;
}
if x1 < 0 as f32 & x2 >= 0 as f32 {
y1 = y1 + (y2 - y1) * -x1 / (x2 - x1);
x1 = 0 as f32;
}
if x1 < 320 as f32 & x2 >= 320 as f32 {
y2 = y2 + (y2 - y1) * (320 as f32 - x2) / (x2 - x1);
x2 = 320 as f32;
}
if y1 > y2 {
swapTmp = x1;
x1 = x2;
x2 = swapTmp;
swapTmp = y1;
y1 = y2;
y2 = swapTmp;
}
if y1 < 0 as f32 & y2 >= 0 as f32 {
x1 = x1 + (x2 - x1) * -y1 / (y2 - y1);
y1 = 0 as f32;
}
if y1 < 240 as f32 & y2 >= 240 as f32 {
x2 = x2 + (x2 - x1) * (240 as f32 - y2) / (y2 - y1);
y2 = 240 as f32;
}
let lazy dx = x2 - x1;
let lazy dy = y2 - y1;
let max_axis: f32;
let p: f32;
if abs(dx) >= dy {
max_axis = dx;
p = x1;
} else {
max_axis = dy;
p = y1;
}
let steps = floor(p + max_axis) as i32 - floor(p) as i32;
p = floor(p) + 0.5 - p;
if max_axis < 0 as f32 {
steps = -steps;
p = -p;
max_axis = -max_axis;
}
dx = dx / max_axis;
dy = dy / max_axis;
let f = min(max_axis, max(0 as f32, p));
setPixel((x1 + f * dx) as i32, (y1 + f * dy) as i32, col);
if !steps {
return;
}
x1 = x1 + (1 as f32 + p) * dx;
y1 = y1 + (1 as f32 + p) * dy;
p = p + steps as f32;
loop pixels {
if steps := steps - 1 {
setPixel(x1 as i32, y1 as i32, col);
x1 = x1 + dx;
y1 = y1 + dy;
branch pixels;
}
}
f = min(max_axis, p) - p;
setPixel((x1 + f * dx) as i32, (y1 + f * dy) as i32, col);
}
export fn upd() { export fn upd() {
cls(0); cls(0);
// line(0.0, 4.0, 7.0, -2.0, 15);
// return;
let i: i32; let i: i32;
loop lines { loop lines {
let angle = i as f32 * (3.1415 / 25.0) + time() * 0.1; let angle = i as f32 * (3.1415 / 25.0) + time() * 0.125;
line(160.0, 120.0, 160.0 + sin(angle) * 100.0, 120.0 + cos(angle) * 100.0, 47); line(
160 as f32, 120 as f32,
160 as f32 + sin(angle) * 100 as f32,
120 as f32 + cos(angle) * 100 as f32,
47);
branch_if (i := i + 1) < 50: lines; branch_if (i := i + 1) < 50: lines;
} }
} }

View File

@@ -1,25 +1,29 @@
import "env.memory" memory(4); import "env.memory" memory(4);
import "env.sin" fn sin(f32) -> f32; import "env.sin" fn sin(f32) -> f32;
import "env.time" fn time() -> f32; import "env.time" fn time() -> f32;
import "env.setPixel" fn setPixel(i32, i32, i32);
export fn upd() { export fn upd() {
let i: i32; let x: i32;
let y: i32;
loop screen { loop screen {
let inline t = time() / 2 as f32; let inline t = time() / 2 as f32;
let lazy o = sin(t) * 0.8; let lazy o = sin(t) * 0.75;
let lazy q = (i % 320) as f32 - 160.1; let inline q = x as f32 - 160.5;
let lazy w = (i / 320 - 120) as f32; let inline w = (y - 120) as f32;
let lazy r = sqrt(q*q + w*w); let lazy r = sqrt(q*q + w*w);
let lazy z = q / r; let lazy z = q / r;
let lazy s = z * o + sqrt(z * z * o * o + 1 as f32 - o * o); let lazy s = z * o + sqrt(z * z * o * o + 1 as f32 - o * o);
let lazy q2 = (z * s - o) * 10 as f32 + t; let inline q2 = (z * s - o) * 10 as f32 + t;
let lazy w2 = w / r * s * 10 as f32 + t; let inline w2 = w / r * s * 10 as f32 + t;
let lazy s2 = s * 100 as f32 / r; let inline s2 = s * 100 as f32 / r;
i?120 = max( let inline color = max(
0 as f32, 0 as f32,
((q2 as i32 ^ w2 as i32 & ((s2 + time()) * 10 as f32) as i32) & 5) as f32 * ((q2 as i32 ^ w2 as i32 & ((s2 + time()) * 10 as f32) as i32) & 5) as f32 *
(4 as f32 - s2) as f32 (4 as f32 - s2) as f32
) as i32 - 32; ) as i32 - 32;
branch_if (i := i + 1) < 320*240: screen setPixel(x, y, color);
branch_if x := (x + 1) % 320: screen;
branch_if y := (y + 1) % 320: screen;
} }
} }

View File

@@ -0,0 +1,19 @@
import "env.memory" memory(4);
import "env.atan2" fn atan2(f32, f32) -> f32;
import "env.time" fn time() -> f32;
export fn upd() {
let i: i32;
loop pixels {
let inline t = time() * 63 as f32;
let lazy x = (i % 320 - 160) as f32;
let lazy y = (i / 320 - 120) as f32;
let inline d = 40000 as f32 / sqrt(x * x + y * y);
let inline u = atan2(x, y) * (512.0 / 3.141);
let inline c = ((i32.trunc_sat_f32_s(d + t * 2 as f32) ^ i32.trunc_sat_f32_s(u + t)) & 255) >> 4;
i?120 = c;
branch_if (i := i + 1) < 320*240: pixels;
}
}

View File

@@ -1,2 +1,4 @@
rustc --target=wasm32-unknown-unknown --crate-type cdylib -C opt-level="z" -C "link-args=--import-memory --initial-memory=262144 -zstack-size=65536" -o tunnel.wasm tunnel.rs && \ rustc --target=wasm32-unknown-unknown -C target-feature=+nontrapping-fptoint --crate-type cdylib -C opt-level="z" -C "link-args=--import-memory --initial-memory=262144 -zstack-size=90000" -o tunnel.wasm tunnel.rs && \
wasm-opt -Oz -o tunnel.wasm tunnel.wasm uw8 filter-exports tunnel.wasm tunnel.wasm && \
wasm-opt -Oz --strip-producers -o tunnel.wasm tunnel.wasm && \
uw8 pack -l 9 tunnel.wasm tunnel.uw8

View File

@@ -5,21 +5,23 @@ A nightly rust compiler is needed for the unstable sqrtf32
intrinsic. intrinsic.
Simply compiling with rustc as shown in build.sh results in a Simply compiling with rustc as shown in build.sh results in a
342 byte tunnel.wasm. Using wasm-opt this can be reduced to 361 byte tunnel.wasm. Using wasm-opt this can be reduced to
243 bytes. 255 bytes.
When you disassemble this wasm file using wasm2wat you can see When you disassemble this wasm file using wasm2wat you can see
these globals and exports: these globals and exports:
(global (;0;) i32 (i32.const 65536)) (global (;0;) i32 (i32.const 90000))
(global (;1;) i32 (i32.const 65536)) (global (;1;) i32 (i32.const 90000))
(export "__data_end" (global 0)) (export "__data_end" (global 0))
(export "__heap_base" (global 1)) (export "__heap_base" (global 1))
They are meant to be used for heap allocations and stack for any They are meant to be used for heap allocations and stack for any
values that are not simple scalars (i32, f32, etc.). Since our values that are not simple scalars (i32, f32, etc.). Since our
code doesn't actually use any of that, we can just delete them code doesn't actually use any of that, the globals are only
in a text editor and assemble the code again with wat2wasm. referenced by the exports and we can remove them using
'uw8 filter-exports' (preferably before running wasm-opt) which
removes all exports except those used by the MicroW8 platform.
This gives us a 199 byte wasm file. Running this through This gives us a 211 byte wasm file. Running this through
uw8-tool pack brings us to the final size of 137 bytes. uw8 pack brings us to the final size of 119 bytes.

View File

@@ -9,37 +9,41 @@ mod env {
extern "C" { extern "C" {
pub fn atan2(x: f32, y: f32) -> f32; pub fn atan2(x: f32, y: f32) -> f32;
} }
extern "C" {
pub fn time() -> f32;
}
} }
fn atan2(x: f32, y: f32) -> f32 { fn atan2(x: f32, y: f32) -> f32 {
unsafe { env::atan2(x, y) } unsafe { env::atan2(x, y) }
} }
fn time() -> f32 {
unsafe { env::time() }
}
fn sqrt(v: f32) -> f32 { fn sqrt(v: f32) -> f32 {
unsafe { core::intrinsics::sqrtf32(v) } unsafe { core::intrinsics::sqrtf32(v) }
} }
fn ftoi(v: f32) -> i32 {
// The compiler is allowed to do bad things to our code if this
// ever results in a value that doesn't fit in an i32.
// (the joy of undefined behavior)
// But that would trap in wasm anyway, so we don't really
// care.
unsafe { v.to_int_unchecked() }
}
#[no_mangle] #[no_mangle]
pub fn tic(time: i32) { pub fn upd() {
for i in 0..320 * 240 { let mut i: i32 = 0;
let t = time as f32 / 10 as f32; loop {
let t = time() * 63.;
let x = (i % 320 - 160) as f32; let x = (i % 320 - 160) as f32;
let y = (i / 320 - 120) as f32; let y = (i / 320 - 120) as f32;
let d = 40000 as f32 / sqrt(x * x + y * y + 1 as f32); let d = 40000 as f32 / sqrt(x * x + y * y);
let u = atan2(x, y) * 512f32 / 3.141; let u = atan2(x, y) * 512. / 3.141;
let c = (ftoi(d + t * 2 as f32) ^ ftoi(u + t)) as u8; let c = ((d + t * 2.) as i32 ^ (u + t) as i32) as u8 >> 4;
unsafe { unsafe {
*((120 + i) as *mut u8) = c; *((120 + i) as *mut u8) = c;
} }
i += 1;
if i >= 320*240 {
break;
}
} }
} }

Binary file not shown.

2
examples/zig/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/zig-cache/
/zig-out/

41
examples/zig/build.zig Normal file
View File

@@ -0,0 +1,41 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const mode = std.builtin.Mode.ReleaseSmall;
const lib = b.addSharedLibrary("cart", "main.zig", .unversioned);
lib.setBuildMode(mode);
lib.setTarget(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
.cpu_features_add = std.Target.wasm.featureSet(&.{ .nontrapping_fptoint })
});
lib.import_memory = true;
lib.initial_memory = 262144;
lib.max_memory = 262144;
lib.global_base = 81920;
lib.stack_size = 8192;
lib.install();
if (lib.install_step) |install_step| {
const run_filter_exports = b.addSystemCommand(&[_][]const u8{
"uw8", "filter-exports", "zig-out/lib/cart.wasm", "zig-out/lib/cart-filtered.wasm"
});
run_filter_exports.step.dependOn(&install_step.step);
const run_wasm_opt = b.addSystemCommand(&[_][]const u8{
"wasm-opt", "-Oz", "-o", "zig-out/cart.wasm", "zig-out/lib/cart-filtered.wasm"
});
run_wasm_opt.step.dependOn(&run_filter_exports.step);
const run_uw8_pack = b.addSystemCommand(&[_][]const u8{
"uw8", "pack", "-l", "9", "zig-out/cart.wasm", "zig-out/cart.uw8"
});
run_uw8_pack.step.dependOn(&run_wasm_opt.step);
const make_opt = b.step("make_opt", "make size optimized cart");
make_opt.dependOn(&run_uw8_pack.step);
b.default_step = make_opt;
}
}

20
examples/zig/main.zig Normal file
View File

@@ -0,0 +1,20 @@
extern fn atan2(x: f32, y: f32) f32;
extern fn time() f32;
pub const FRAMEBUFFER: *[320*240]u8 = @intToPtr(*[320*240]u8, 120);
export fn upd() void {
var i: u32 = 0;
while(true) {
var t = time() * 63.0;
var x = @intToFloat(f32, (@intCast(i32, i % 320) - 160));
var y = @intToFloat(f32, (@intCast(i32, i / 320) - 120));
var d = 40000.0 / @sqrt(x * x + y * y);
var u = atan2(x, y) * 512.0 / 3.141;
var c = @intCast(u8, (@floatToInt(i32, d + t * 2.0) ^ @floatToInt(i32, u + t)) & 255) >> 4;
FRAMEBUFFER[@as(usize, i)] = c;
i += 1;
if(i >= 320*240) { break; }
}
}

65
logo.svg Normal file
View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="120"
height="120"
viewBox="0 0 120 120"
version="1.1"
id="svg5"
sodipodi:docname="logo.svg"
inkscape:version="1.1.1 (3bf5ae0d25, 2021-09-20, custom)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:document-units="px"
showgrid="false"
inkscape:zoom="3.6041667"
inkscape:cx="21.780347"
inkscape:cy="63.260116"
inkscape:window-width="1916"
inkscape:window-height="1041"
inkscape:window-x="0"
inkscape:window-y="18"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" />
<defs
id="defs2" />
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="fill:#85a6b2;fill-rule:evenodd;fill-opacity:1"
id="rect31"
width="112.05161"
height="109.64198"
x="3.9731982"
y="6.3613" />
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:117.333px;line-height:1.25;font-family:'Fira Code';-inkscape-font-specification:'Fira Code Bold';fill:#ffffff;fill-opacity:1;stroke:none"
x="-6.2686396"
y="117.70291"
id="text2691"><tspan
sodipodi:role="line"
id="tspan2689"
x="-6.2686396"
y="117.70291">W8</tspan></text>
<circle
style="fill:#ffffff;stroke-width:1.32327"
id="path121"
cx="60.962471"
cy="6.7644148"
r="14.855442" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

Binary file not shown.

View File

@@ -43,17 +43,16 @@ export fn load_uw8(module_size: i32) -> i32 {
} }
fn section_size(ptr: i32) -> i32 { fn section_size(ptr: i32) -> i32 {
let p = ptr + 1; let p = ptr;
let l = 0; let l: i32;
let shift = 0; let shift: i32;
loop size { loop size {
let lazy b = p?0; let lazy b = (p := p + 1)?0;
l = l | ((b & 127) << shift); l = l | ((b & 127) << shift);
shift = shift + 7; shift = shift + 7;
p = p + 1; branch_if b >> 7: size;
branch_if b & 128: size;
} }
p - ptr + l p + 1 - ptr + l
} }
fn copy_section(dest: i32, src: i32) -> i32 { fn copy_section(dest: i32, src: i32) -> i32 {
@@ -63,10 +62,10 @@ fn copy_section(dest: i32, src: i32) -> i32 {
} }
fn copy(dest: i32, src: i32, len: i32) { fn copy(dest: i32, src: i32, len: i32) {
if len > 0 { loop bytes {
loop bytes { if len > 0 {
(dest + (len := len - 1))?0 = (src + len)?0; (dest + (len := len - 1))?0 = (src + len)?0;
branch_if len: bytes branch bytes;
} }
} }
} }
@@ -84,23 +83,21 @@ export fn uncompress(src_ptr: i32, dest_ptr: i32) -> i32 {
let offset: i32; let offset: i32;
let byte: i32;
let i: i32; let i: i32;
loop init_contexts { loop init_contexts {
i?0x3c000 = 0x80; i?0x3c000 = 0x80;
branch_if (i := i + 1) < 256 + 1 + 128: init_contexts branch_if (i := i + 1) < 256 + 1 + 128: init_contexts;
} }
let prev_was_match: i32; let prev_was_match: i32;
block finished { block finished {
loop unpack_loop { loop unpack_loop {
if upkr_bit(0) { let lazy is_match = upkr_bit(0);
if is_match {
let inline new_offset = if prev_was_match { 1 } else { upkr_bit(256) }; let inline new_offset = if prev_was_match { 1 } else { upkr_bit(256) };
if new_offset { if new_offset {
offset = upkr_length(257) - 1; branch_if !(offset := upkr_length(257) - 1): finished;
branch_if !offset: finished
} }
let length = upkr_length(257 + 64); let length = upkr_length(257 + 64);
loop copy { loop copy {
@@ -108,19 +105,16 @@ export fn uncompress(src_ptr: i32, dest_ptr: i32) -> i32 {
dest_ptr = dest_ptr + 1; dest_ptr = dest_ptr + 1;
branch_if (length := length - 1): copy; branch_if (length := length - 1): copy;
} }
prev_was_match = 1;
} else { } else {
// literal // literal
i = 0; let byte = 1;
byte = 1;
loop literal { loop literal {
byte = (byte << 1) | upkr_bit(byte); branch_if (byte := (byte << 1) | upkr_bit(byte)) < 256: literal;
branch_if (i := i + 1) < 8: literal;
} }
dest_ptr?0 = byte; dest_ptr?0 = byte;
dest_ptr = dest_ptr + 1; dest_ptr = dest_ptr + 1;
prev_was_match = 0;
} }
prev_was_match = is_match;
branch unpack_loop; branch unpack_loop;
} }
} }
@@ -132,9 +126,8 @@ fn upkr_length(context_index: i32) -> i32 {
let length: i32; let length: i32;
let bit_pos: i32; let bit_pos: i32;
loop bits { loop bits {
if upkr_bit(context_index) { if upkr_bit(context_index + bit_pos) {
length = length | (upkr_bit(context_index + 1) << bit_pos); length = length | (upkr_bit(context_index + bit_pos + 32) << bit_pos);
context_index = context_index + 2;
bit_pos = bit_pos + 1; bit_pos = bit_pos + 1;
branch bits; branch bits;
} }
@@ -143,7 +136,7 @@ fn upkr_length(context_index: i32) -> i32 {
} }
fn upkr_bit(context_index: i32) -> i32 { fn upkr_bit(context_index: i32) -> i32 {
let prob = context_index?0x3c000; let lazy prob = context_index?0x3c000;
loop refill { loop refill {
if upkr_state < 1<<12 { if upkr_state < 1<<12 {
@@ -154,17 +147,12 @@ fn upkr_bit(context_index: i32) -> i32 {
} }
let lazy state_low = upkr_state & 0xff; let lazy state_low = upkr_state & 0xff;
let bit = state_low < prob; let lazy state_hi = upkr_state >> 8;
let lazy bit = state_low < prob;
if bit { upkr_state = state_low + select(bit, prob * state_hi, (0x100 - prob) * state_hi - prob);
upkr_state = prob * (upkr_state >> 8) + state_low;
prob = prob + ((0x108 - prob) >> 4);
} else {
upkr_state = (0x100 - prob) * (upkr_state >> 8) + state_low - prob;
prob = prob - ((prob + 8) >> 4);
}
context_index?0x3c000 = prob; context_index?0x3c000 = prob + ((7 + bit * 257 - prob) >> 4);
bit bit
} }

View File

@@ -115,6 +115,26 @@ export fn rectangle(x: f32, y: f32, w: f32, h: f32, col: i32) {
} }
} }
export fn rectangle_outline(x: f32, y: f32, w: f32, h: f32, col: i32) {
let xl = nearest(x) as i32;
let xr = nearest(x + w) as i32;
let yt = nearest(y) as i32;
let yb = nearest(y + h) as i32;
hline(xl, xr, yt, col);
if yt < yb {
hline(xl, xr, yb - 1, col);
loop y {
setPixel(xl, yt, col);
if xl < xr {
setPixel(xr - 1, yt, col);
}
branch_if (yt := yt + 1) < yb: y;
}
}
}
export fn circle(cx: f32, cy: f32, radius: f32, col: i32) { export fn circle(cx: f32, cy: f32, radius: f32, col: i32) {
let y = clamp(nearest(cy - radius) as i32, 0, 240); let y = clamp(nearest(cy - radius) as i32, 0, 240);
let maxY = clamp(nearest(cy + radius) as i32, 0, 240); let maxY = clamp(nearest(cy + radius) as i32, 0, 240);
@@ -136,6 +156,58 @@ export fn circle(cx: f32, cy: f32, radius: f32, col: i32) {
} }
} }
export fn circle_outline(cx: f32, cy: f32, radius: f32, col: i32) {
let prev_w: f32;
let y = clamp(nearest(cy - radius) as i32, -1, 241);
let maxY = clamp(nearest(cy + radius) as i32, -1, 241);
loop lines {
let lazy dy = y as f32 - cy + 0.5;
let inline q = radius * radius - dy * dy;
let w = sqrt(max(0 as f32, q));
let xlp = nearest(cx - prev_w) as i32;
let xl = nearest(cx - w) as i32;
let xrp = nearest(cx + prev_w) as i32;
let xr = nearest(cx + w) as i32;
if w >= prev_w {
if xl < xlp {
hline(xl, xlp, y, col);
} else {
if xl < xr {
setPixel(xl, y, col);
}
}
if xr > xrp {
hline(xrp, xr, y, col);
} else {
if xl < xr {
setPixel(xr - 1, y, col);
}
}
} else {
if xl > xlp {
hline(xlp, xl, y - 1, col);
} else {
if xlp < xrp {
setPixel(xlp, y - 1, col);
}
}
if xr < xrp {
hline(xr, xrp, y - 1, col);
} else {
if xlp < xrp {
setPixel(xrp - 1, y - 1, col);
}
}
}
y = y + 1;
prev_w = w;
branch_if y <= maxY: lines;
}
}
export fn line(x1: f32, y1: f32, x2: f32, y2: f32, col: i32) { export fn line(x1: f32, y1: f32, x2: f32, y2: f32, col: i32) {
let swapTmp: f32; let swapTmp: f32;
if x1 > x2 { if x1 > x2 {
@@ -236,14 +308,47 @@ export fn printChar(char: i32) {
} }
} }
global mut controlCodeLength = 0;
fn printSingleChar(char: i32) { fn printSingleChar(char: i32) {
if char == 4 | char == 5 { controlCodeLength?0x12d20 = char;
graphicsText = char == 5; controlCodeLength = controlCodeLength + 1;
char = 0x12d20?0;
if char < 32 & controlCodeLength < char?0x12d00 {
return;
}
controlCodeLength = 0;
if char == 1 {
drawChar(0x12d20?1);
return; return;
} }
if char == 10 | (!graphicsText & textCursorX >= 320) { if char == 4 | char == 5 {
textCursorX = 0; graphicsText = char == 5;
textCursorX = 0;
textCursorY = 0;
return;
}
if char == 8 {
textCursorX = textCursorX - 8;
if !graphicsText & textCursorX < 0 {
textCursorX = 320-8;
printSingleChar(11);
}
return;
}
if char == 9 {
if !graphicsText & textCursorX >= 320 {
printChar(0xd0a);
}
textCursorX = textCursorX + 8;
return;
}
if char == 10 {
textCursorY = textCursorY + 8; textCursorY = textCursorY + 8;
if !graphicsText & textCursorY >= 240 { if !graphicsText & textCursorY >= 240 {
textCursorY = 240 - 8; textCursorY = 240 - 8;
@@ -257,6 +362,78 @@ fn printSingleChar(char: i32) {
return; return;
} }
if char == 11 {
textCursorY = textCursorY - 8;
if !graphicsText & textCursorY < 0 {
textCursorY = 0;
let i = 320 * (240 - 8);
loop scroll_copy {
i!(116 + 320 * 8) = i!116;
branch_if (i := i - 4): scroll_copy;
}
rectangle(0 as f32, 0 as f32, 320 as f32, 8 as f32, bgColor);
}
return;
}
if char == 12 {
cls(bgColor);
return;
}
if char == 13 {
textCursorX = 0;
return;
}
if char == 14 {
bgColor = 0x12d20?1;
return;
}
if char == 15 {
textColor = 0x12d20?1;
return;
}
if char == 24 {
let tmp = textColor;
textColor = bgColor;
bgColor = tmp;
return;
}
if char == 31 {
textCursorX = 0x12d20?1 * (8 - graphicsText * 6);
textCursorY = 0x12d20?2 * (8 - graphicsText * 7);
return;
}
if char < 31 {
return;
}
drawChar(char);
}
data(0x12d00) {
i8(
1, 2, 1, 1, // 0-3
1, 1, 1, 1, // 4-7
1, 1, 1, 1, // 8-11
1, 1, 2, 2, // 12-15,
1, 1, 1, 1, // 16-19,
1, 1, 1, 1, // 20-23,
1, 1, 1, 1, // 24-27,
1, 1, 1, 3 // 28-31
)
}
fn drawChar(char: i32) {
if !graphicsText & textCursorX >= 320 {
printChar(0xd0a);
}
let y: i32; let y: i32;
loop rows { loop rows {
let bits = (char * 8 + y)?0x13400; let bits = (char * 8 + y)?0x13400;

2
release/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/binaries
/build

54
release/make-release Executable file
View File

@@ -0,0 +1,54 @@
#!/bin/bash
set -e
cd $(dirname $0)
# build web runtime
pushd ../web
rm -rf .parcel-cache
yarn parcel build src/index.html
popd
rm -rf build
mkdir -p build/microw8-linux
mkdir build/microw8-macos
mkdir build/microw8-windows
# unzip binaries build by github actions
pushd binaries
unzip -o uw8-linux.zip
chmod +x uw8
mv uw8 ../build/microw8-linux
unzip -o uw8-macos.zip
chmod +x uw8
mv uw8 ../build/microw8-macos
unzip -o uw8-windows.zip
mv uw8.exe ../build/microw8-windows
popd
for dir in build/*; do
mkdir $dir/examples
for example in ../examples/curlywas/*.cwa; do
cp $example $dir/examples
done
for example in ../examples/wat/*.wat; do
cp $example $dir/examples
done
mkdir $dir/carts
for example in $dir/examples/*; do
build/microw8-linux/uw8 pack -l 9 $example $dir/carts/$(basename ${example%.*}).uw8
done
cp ../web/dist/index.html $dir/microw8.html
cp ../README.md $dir
cp ../UNLICENSE $dir
done
VERSION=$(build/microw8-linux/uw8 version)
cd build
tar czf microw8-$VERSION-linux.tgz microw8-linux
tar czf microw8-$VERSION-macos.tgz microw8-macos
zip -r -9 microw8-$VERSION-windows.zip microw8-windows

0
site/.SRCINFO Normal file
View File

View File

@@ -18,7 +18,7 @@ highlight_theme = "ascetic-white"
[extra] [extra]
# Put all your custom variables here # Put all your custom variables here
juice_logo_name = "MicroW8" juice_logo_name = "MicroW8"
juice_logo_path = "microw8.svg" juice_logo_path = "img/microw8.svg"
juice_extra_menu = [ juice_extra_menu = [
{ title = "Github", link = "https://github.com/exoticorn/microw8" } { title = "Github", link = "https://github.com/exoticorn/microw8" }
] ]

View File

@@ -1,125 +1,55 @@
+++ +++
+++ +++
## Versions ## About
* [v0.1pre1](v0.1pre1) MicroW8 is a WebAssembly based fantasy console inspired by the likes of [TIC-80](https://tic80.com/), [WASM-4](https://wasm4.org/) and [PICO-8](https://www.lexaloffle.com/pico-8.php).
* [v0.1pre2](v0.1pre2)
* [v0.1pre3](v0.1pre3)
* [v0.1pre4](v0.1pre4)
## Spec The initial motivation behind MicroW8 was to explore whether there was a way to make WebAssembly viable for size-coding. (Size coding being the art of creating tiny (often <= 256 bytes) graphical effects and games.) The available examples so far are all in this space, however, I very carefully made sure that all design decisions make sense from the point of view of bigger projects as well.
MicroW8 loads WebAssembly modules with a maximum size of 256kb. You module needs to export ## Specs
a function `fn upd()` which will be called once per frame.
After calling `upd` MicroW8 will display the 320x240 8bpp framebuffer located
at offset 120 in memory with the 32bpp palette located at 0x13000.
The memory has to be imported as `"env" "memory"` and has a maximum size of 256kb (4 pages). * Screen: 320x240, 256 colors, 60Hz
* Modules: Up to 256KB (WASM)
Other imports provided by the platform, also all in module `env`: * Memory: 256KB
* Gamepad input (D-Pad + 4 Buttons)
* `fn acos(f32) -> f32`
* `fn asin(f32) -> f32`
* `fn atan(f32) -> f32`
* `fn atan2(f32, f32) -> f32`
* `fn cos(f32) -> f32`
* `fn exp(f32, f32) -> f32`
* `fn log(f32) -> f32`
* `fn sin(f32) -> f32`
* `fn tan(f32) -> f32`
* `fn pow(f32) -> f32`
* `fn fmod(f32, f32) -> f32`
* `fn random() -> i32`
* `fn randomf() -> f32`
* `fn randomSeed(i32)`
* `fn cls(color: i32)`
* `fn setPixel(x: i32, y: i32, color: i32)`
* `fn getPixel(x: i32, y: i32) -> i32`
* `fn hline(left: i32, right: i32, y: i32, color: i32)`
* `fn rectangle(x1: f32, y1: f32, x2: f32, y2: f32, color: i32)`
* `fn circle(cx: f32, cy: f32, radius: f32, color: i32)`
* `fn line(x1: f32, y1: f32, x2: f32, y2: f32, color: i32)`
* `fn time() -> f32`
* `fn isButtonPressed(btn: i32) -> i32`
* `fn isButtonTriggered(btn: i32) -> i32`
* `fn printChar(char: i32)`
* `fn printString(ptr: i32)`
* `fn printInt(num: i32)`
* `fn setTextColor(color: i32)`
* `fn setBackgroundColor(color: i32)`
* `fn setCursorPosition(x: i32, y: i32)`
### Memory map
```
00000-00040: user memory
00040-00044: time since module start in ms
00044-0004c: gamepad state
0004c-00078: reserved
00078-12c78: frame buffer
12c78-13000: reserved
13000-13400: palette
13400-13c00: font
13c00-14000: reserved
14000-40000: user memory
```
## `.uw8` format
The first byte of the file specifies the format version:
#### Format version `00`:
This file is simply a standard WebAssembly module
#### Format version `01`:
The rest of this file is the same as a WebAssembly
module with the 8 byte header removed. This module
can leave out sections which are then taken from
a base module provided by MicroW8.
You can generate this base module yourself using
`uw8-tool`. As a quick summary, it provides all function
types with up to 5 parameters (i32 or f32) where the
`f32` parameters always preceed the `i32` parameters.
Then it includes all imports that MicroW8 provides,
a function section with a single function of type
`() -> void` and an export section that exports
the first function in the file under the name `upd`.
#### Format version `02`:
Same as version `01` except everything after the first byte is compressed
using a [custom LZ compression scheme](https://github.com/exoticorn/upkr).
## Tooling
The [Web Assembly Binary Toolkit](https://github.com/WebAssembly/wabt) includes
a few useful tools, eg. `wat2wasm` to compile the WebAssemby text format to binary
wasm and `wasm2wat` to disassemble wasm binaries.
If you don't like the look of the `.wat` text format, you might want to take a
look at [CurlyWas](https://github.com/exoticorn/curlywas), a curly-braces infix
syntax for WebAssembly.
Once you have a size-efficient `.wasm` file, you can use [uw8-tool](https://github.com/exoticorn/microw8/tree/master/uw8-tool)
(currently included in the MicroW8 repository) to strip off sections of the
WebAssembly module that are provided by the MicroW8 platform.
Writing code for MicroW8 in C, Rust, AssemblyScript etc. should absolutely
possible but no examples are provided, yet.
## Examples ## Examples
* [Skip Ahead](v0.1pre4#AiP94km33q5hBFbBcKl2W1ZMY67wHByUz15/qRHIr71Dvq8bTVux/DXZzSTKU5MufRXxnGEn6mOul4dil/5WLT1Y+Y/4TGd1E3U4rQaxlpcKObxc5YLdr4xo6jyBND96a569LoECv6Q4EIqxJi+3QPE21r/++js41XsBLr44X/O2XpP3kHQva3B8o6duD5WkLCf7PhsxjTBeND1ADIaJe05JCrBim18RPY9VYFmltqQ9gVzbxE/ZpojR/GJ4aCbx0hRn73RPhpA7Cd4jk0AVSOeRx+7kuHrLzpaeqeYWK5pYd/4Rv+8=) (230 bytes): A port of my [TIC-80 256byte game](http://tic80.com/play?cart=1735) from LoveByte'21 * [Fireworks](v0.1.1#AgwvgP+M59snqjl4CMKw5sqm1Zw9yJCbSviMjeLUdHus2a3yl/a99+uiBeqZgP/2jqSjrLjRk73COMM6OSLpsxK8ugT1kuk/q4hQUqqPpGozHoa0laulzGGcahzdfdJsYaK1sIdeIYS9M5PnJx/Wk9H+PvWEPy2Zvv7I6IW7Fg==) (127 bytes): Some fireworks to welcome 2022.
* [OhNoAnotherTunnel](v0.1pre4#Ag95rdCB5Ww5NofyQaKF4P1mrNRso4azgiem4hK99Gh8OMzSpFq3NsNDo7O7pqln10D11l9uXr/ritw7OEzKwbEfCdvaRnS2Z0Kz0iDEZt/gIqOdvFmxsL1MjPQ4XInPbUJpQUonhQq29oP2omFabnQxn0bzoK7mZjcwc5GetHG+hGajkJcRr8oOnjfCol8RD+ha33GYtPnut+GLe4ktzf5UxZwGs6oT9qqC61lRDakN) (177 bytes): A port of my [entry](http://tic80.com/play?cart=1871) in the Outline'21 bytebattle final * [Skip Ahead](v0.1.1#AgyfpZ80wkW28kiUZ9VIK4v+RPnVxqjK1dz2BcDoNyQPsS2g4OgEzkTe6jyoAfFOmqKrS8SM2aRljBal9mjNn8i4fP9eBK+RehQKxxGtJa9FqftvqEnh3ez1YaYxqj7jgTdzJ/WAYVmKMovBT1myrX3FamqKSOgMsNedLhVTLAhQup3sNcYEjGNo8b0HZ5+AgMgCwYRGCe//XQOMAaAAzqDILgmpEZ/43RKHcQpHEQwbURfNQJpadJe2sz3q5FlQnTGXQ9oSMokidhlC+aR/IpNHieuBGLhFZ2GfnwVQ0geBbQpTPA==) (229 bytes): A port of my [TIC-80 256byte game](http://tic80.com/play?cart=1735) from LoveByte'21
* [Technotunnel](v0.1pre4#AqL8HeK1M9dn2nWNIF5vaq/Vh64pMt5nJIFoFKpBMPUsGtDtpqjo1JbT9LzPhAxCqJ7Yh4TA6oTGd4xhLowf+cWZMY73+7AZmfXJJsBi4cej/hH+4wlAgxFIrnOYnr/18IpnZbsHf0eGm1BhahX74+cVR0TRmNQmYC7GhCNS3mv/3MJn74lCj7t28aBJPjEZhP9fGXdG2u5Egh/Tjdg=) (158 bytes): A port of my [entry](https://tic80.com/play?cart=1873) in the Outline'21 bytebattle quater final * [OhNoAnotherTunnel](v0.1.1#AgPP1oEFvPzY/rBZwTumtYn37zeMFgpir1Bkn91jsNcp26VzoUpkAOOJTtnzVBfW+/dGnnIdbq/irBUJztY5wuua80DORTYZndgdwZHcSk15ajc4nyO0g1A6kGWyW56oZk0iPYJA9WtUmoj0Plvy1CGwIZrMe57X7QZcdqc3u6zjTA41Tpiqi9vnO3xbhi8o594Vx0XPXwVzpYq1ZCTYenfAGaXKkDmAFJqiVIsiCg==) (175 bytes): A port of my [entry](http://tic80.com/play?cart=1871) in the Outline'21 bytebattle final
* [Font & Palette](v0.1pre4#AgKaeeOuwg5gCKvFIeiitEwMpUI2rymEcu+DDB1vMu9uBoufvUxIr4Y5p4Jj2ukoNO4PE7QS5cN1ZyDMCRfSzYIGZxKlN2J6NKEWK7KVPk9wVUgn1Ip+hsMinWgEO8ETKfPuHoIa4kjI+ULFOMad7vd3rt/lh1Vy9w+R2MXG/7T61d3c7C6KY+eQNS0eW3ys4iU8R6SycuWZuuZ2Sg3Qxp826s+Kt+2qBojpzNOSoyFqyrVyYMTKEkSl0BZOj59Cs1hPm5bq0F1MmVhGAzMhW9V4YeAe): Just a simple viewer for the default font and palette. * [Technotunnel](v0.1.1#AhPXpq894LaUhp5+HQf39f39/Jc8g5zUrBSc0uyKh36ivskczhY84h55zL8gWpkdvKuRQI+KIt80isKzh8jkM8nILcx0RUvyk8yjE8TgNsgkcORVI0RY5k3qE4ySjaycxa2DVZH61UWZuLsCouuwT7I80TbmmetQSbMywJ/avrrCZIAH0UzQfvOiCJNG48NI0FFY1vjB7a7dcp8Uqg==) (157 bytes): A port of my [entry](https://tic80.com/play?cart=1873) in the Outline'21 bytebattle quater final
* [Font & Palette](v0.1.1#At/p39+IBnj6ry1TRe7jzVy2A4tXgBvmoW2itzoyF2aM28pGy5QDiKxqrk8l9sbWZLtnAb+jgOfU+9QhpuyCAkhN6gPOU481IUL/df96vNe3h288Dqwhd3sfFpothIVFsMwRK72kW2hiR7zWsaXyy5pNmjR6BJk4piWx9ApT1ZwoUajhk6/zij6itq/FD1U3jj/J3MOwqZ2ef8Bv6ZPQlJIYVf62icGa69wS6SI1qBpIFiF14F8PcztRVbKIxLpT4ArCS6nz6FPnyUkqATGSBNPJ): Just a simple viewer for the default font and palette.
Examplers for older versions:
* [Technotunnel B/W](v0.1pre2#AQrDAQHAAQIBfwp9A0AgAUEAsiABQcACb7JDmhkgQ5MiBCAEIASUIAFBwAJtQfgAa7IiBSAFlJKRIgaVIgcgByAAskHQD7KVIgIQAEPNzEw/lCIDlCAHIAeUIAOUIAOUQQGykiADIAOUk5GSIgiUIAOTQQqylCACkiIJqCAFIAaVIAiUQQqylCACkiIKqHMgCEEyspQgBpUiCyACkkEUspSocUEFcbJBArIgC5OUQRaylJeoOgB4IAFBAWoiAUGA2ARIDQALCw==) (199 bytes uncompressed): A port of my [entry](https://tic80.com/play?cart=1873) in the Outline'21 bytebattle quater final (older MicroW8 version with monochrome palette) * [Technotunnel B/W](v0.1pre2#AQrDAQHAAQIBfwp9A0AgAUEAsiABQcACb7JDmhkgQ5MiBCAEIASUIAFBwAJtQfgAa7IiBSAFlJKRIgaVIgcgByAAskHQD7KVIgIQAEPNzEw/lCIDlCAHIAeUIAOUIAOUQQGykiADIAOUk5GSIgiUIAOTQQqylCACkiIJqCAFIAaVIAiUQQqylCACkiIKqHMgCEEyspQgBpUiCyACkkEUspSocUEFcbJBArIgC5OUQRaylJeoOgB4IAFBAWoiAUGA2ARIDQALCw==) (199 bytes uncompressed): A port of my [entry](https://tic80.com/play?cart=1873) in the Outline'21 bytebattle quater final (older MicroW8 version with monochrome palette)
* [XorScroll](v0.1pre2#AQovAS0BAX8DQCABIAFBwAJvIABBCm1qIAFBwAJtczoAeCABQQFqIgFBgNgESA0ACws=) (50 bytes uncompressed): A simple scrolling XOR pattern. Fun fact: This is the pre-loaded effect when entering a bytebattle. * [XorScroll](v0.1pre2#AQovAS0BAX8DQCABIAFBwAJvIABBCm1qIAFBwAJtczoAeCABQQFqIgFBgNgESA0ACws=) (50 bytes uncompressed): A simple scrolling XOR pattern. Fun fact: This is the pre-loaded effect when entering a bytebattle.
* [CircleWorm](v0.1pre2#AQp7AXkCAX8CfUEgEA0DQCABskEEspUiAkECspUgALJBiCeylSIDQQWylJIQAEEBspJBoAGylCACQQOylSADQQSylJIQAEEBspJB+ACylCADQRGylCACQQKylJIQAEECspJBELKUIAFBAmxBP2oQEiABQQFqIgFBP0gNAAsL) (126 bytes uncompressed): Just a test for the circle fill function. * [CircleWorm](v0.1pre2#AQp7AXkCAX8CfUEgEA0DQCABskEEspUiAkECspUgALJBiCeylSIDQQWylJIQAEEBspJBoAGylCACQQOylSADQQSylJIQAEEBspJB+ACylCADQRGylCACQQKylJIQAEECspJBELKUIAFBAmxBP2oQEiABQQFqIgFBP0gNAAsL) (126 bytes uncompressed): Just a test for the circle fill function.
## Versions
### v0.1.1
* [Web runtime](v0.1.1)
* [Linux](https://github.com/exoticorn/microw8/releases/download/v0.1.1/microw8-0.1.1-linux.tgz)
* [MacOS](https://github.com/exoticorn/microw8/releases/download/v0.1.1/microw8-0.1.1-macos.tgz)
* [Windows](https://github.com/exoticorn/microw8/releases/download/v0.1.1/microw8-0.1.1-windows.zip)
Changes:
* implement more robust file watcher
* add basic video recording on F10 in web runtime
* add screenshot on F9
* add watchdog to interrupt hanging update in native runtime
* add devkit mode to web runtime
* add unpack and compile commands to uw8
* add support for table/element section in pack command
* disable wayland support (caused missing window decorations in gnome)
### v0.1.0
* [Web runtime](v0.1.0)
* [Linux](https://github.com/exoticorn/microw8/releases/download/v0.1.0/microw8-0.1.0-linux.tgz)
* [MacOS](https://github.com/exoticorn/microw8/releases/download/v0.1.0/microw8-0.1.0-macos.tgz)
* [Windows](https://github.com/exoticorn/microw8/releases/download/v0.1.0/microw8-0.1.0-windows.zip)

443
site/content/docs.md Normal file
View File

@@ -0,0 +1,443 @@
+++
title = "Docs"
description = "Docs"
+++
# Overview
MicroW8 loads WebAssembly modules with a maximum size of 256kb. You module needs to export
a function `fn upd()` which will be called once per frame.
After calling `upd` MicroW8 will display the 320x240 8bpp framebuffer located
at offset 120 in memory with the 32bpp palette located at 0x13000.
The memory has to be imported as `env` `memory` and has a maximum size of 256kb (4 pages).
# Memory map
```
00000-00040: user memory
00040-00044: time since module start in ms
00044-0004c: gamepad state
0004c-00078: reserved
00078-12c78: frame buffer
12c78-13000: reserved
13000-13400: palette
13400-13c00: font
13c00-14000: reserved
14000-40000: user memory
```
# API
All API functions are found in the `env` module.
## Math
These all do what you'd expect them to. All angles are in radians.
### fn asin(x: f32) -> f32
Returns the arcsine of `x`.
### fn acos(x: f32) -> f32
Returns the arccosine of `x`.
### fn atan(f32) -> f32
Returns the arctangent of `x`.
### fn atan2(y: f32, y: f32) -> f32
Returns the angle between the point `(x, y)` and the positive x-axis.
### fn sin(angle: f32) -> f32
Returns the sine of `angle`.
### fn tan(angle: f32) -> f32
Returns the tangent of `angle`.
### fn cos(angle: f32) -> f32
Returns the cosine of `angle`.
### fn exp(x: f32) -> f32
Returns `e^x`.
### fn log(x: f32) -> f32
Returns the natural logarithmus of `x`. Ie. `e^log(x) == x`.
### fn pow(x: f32, y: f32) -> f32
Returns `x^y`.
### fn fmod(x: f32, y: f32) -> f32
Returns `x` modulo `y`, ie. `x - floor(x / y) * y`. This means the sign of the result of `fmod` is the same as `y`.
## Random
MicroW8 provides a pretty good PRNG, namely xorshift64*. It is initialized to a constant seed at each startup, so if you
want to vary the random sequence you'll need to provide a seed yourself.
### fn random() -> i32
Returns a (pseudo-)random 32bit integer.
### fn randomf() -> f32
Returns a (pseudo-)random float equally distributed in `[0,1)`.
### fn randomSeed(seed: i32)
Seeds the PRNG with the given seed. The seed function is reasonably strong so that you can use
```
randomSeed(index);
random()
```
as a cheap random-access PRNG (aka noise function).
## Graphics
The default palette can be seen [here](../v0.1.0#At/p39+IBnj6ry1TRe7jzVy2A4tXgBvmoW2itzoyF2aM28pGy5QDiKxqrk8l9sbWZLtnAb+jgOfU+9QhpuyCAkhN6gPOU481IUL/df96vNe3h288Dqwhd3sfFpothIVFsMwRK72kW2hiR7zWsaXyy5pNmjR6BJk4piWx9ApT1ZwoUajhk6/zij6itq/FD1U3jj/J3MOwqZ2ef8Bv6ZPQlJIYVf62icGa69wS6SI1qBpIFiF14F8PcztRVbKIxLpT4ArCS6nz6FPnyUkqATGSBNPJ). (Press Z on the keyboard to switch to palette.)
The palette can be changed by writing 32bit rgba colors to addresses 0x13000-0x13400.
The drawing functions are sub-pixel accurate where applicable (line, circle). Pixel centers lie halfway between integer
coordinates. Ie. the top-left pixel covers the area `0,0 - 1,1`, with `0.5,0.5` being the pixel center.
### fn cls(color: i32)
Clears the screen to the given color index. Also sets the text cursor to `0, 0` and disables graphical text mode.
### fn setPixel(x: i32, y: i32, color: i32)
Sets the pixel at `x, y` to the given color index.
### fn getPixel(x: i32, y: i32) -> i32
Returns the color index at `x, y`. Returns `0` if the given coordinates are outside the screen.
### fn hline(left: i32, right: i32, y: i32, color: i32)
Fills the horizontal line `[left, right), y` with the given color index.
### fn rectangle(x: f32, y: f32, w: f32, h: f32, color: i32)
Fills the rectangle `x,y - x+w,y+h` with the given color index.
(Sets all pixels where the pixel center lies inside the rectangle.)
### fn circle(cx: f32, cy: f32, radius: f32, color: i32)
Fills the circle at `cx, cy` and with `radius` with the given color index.
(Sets all pixels where the pixel center lies inside the circle.)
### fn rectangle_outline(x: f32, y: f32, w: f32, h: f32, color: i32)
Draws a one pixel outline on the inside of the given rectangle.
(Draws the outermost pixels that are still inside the rectangle area.)
### fn circle_outline(cx: f32, cy: f32, radius: f32, color: i32)
Draws a one pixel outline on the inside of the given circle.
(Draws the outermost pixels that are still inside the circle area.)
### fn line(x1: f32, y1: f32, x2: f32, y2: f32, color: i32)
Draws a line from `x1,y1` to `x2,y2` in the given color index.
## Input
MicroW8 provides input from a gamepad with one D-Pad and 4 buttons, or a keyboard emulation thereof.
The buttons are numbered
| Button | Keyboard | Index |
| ------ | ----------- | ----- |
| Up | Arrow-Up | 0 |
| Down | Arrow-Down | 1 |
| Left | Arrow-Left | 2 |
| Right | Arrow-Right | 3 |
| A | Z | 4 |
| B | X | 5 |
| X | A | 6 |
| Y | S | 7 |
In addition to using the API functions below, the gamepad state can also be read as a bitfield of
pressed buttons at address 0x44. 0x48 holds the buttons that were pressed last frame.
### fn isButtonPressed(btn: i32) -> i32
Returns whether the buttons with the given index is pressed this frame.
### fn isButtonTriggered(btn: i32) -> i32
Returns whether the given button is newly pressed this frame.
### fn time() -> f32
Returns the time in seconds since the start of the cart.
The integer time in milliseconds can also be read at address 0x40.
## Text output
The default font can be seen [here](../v0.1.0#At/p39+IBnj6ry1TRe7jzVy2A4tXgBvmoW2itzoyF2aM28pGy5QDiKxqrk8l9sbWZLtnAb+jgOfU+9QhpuyCAkhN6gPOU481IUL/df96vNe3h288Dqwhd3sfFpothIVFsMwRK72kW2hiR7zWsaXyy5pNmjR6BJk4piWx9ApT1ZwoUajhk6/zij6itq/FD1U3jj/J3MOwqZ2ef8Bv6ZPQlJIYVf62icGa69wS6SI1qBpIFiF14F8PcztRVbKIxLpT4ArCS6nz6FPnyUkqATGSBNPJ).
The font can be changed by writing 1bpp 8x8 characters to addresses 0x13400-0x13c00.
All text printing is done at the cursor position, which is advanced after printing each character.
The cursor is not visible.
Text printing can operate in two modes - normal and graphics. After startup and after `cls()` normal mode is active.
### Normal mode
In normal mode, text printing is constrained to an 8x8 character grid. Setting the cursor position to `2,3` will start printing at pixel coordinates `16,24`.
When printing characters, the full 8x8 pixels are painted with the text and background colors according to the character graphics in the font.
When moving/printing past the left or right border the cursor will automatically wrap to the previous/next line. When moving/printing past the upper/lower border, the screen will be scrolled down/up 8 pixels, filling the fresh line with the background color.
### Graphics mode
In graphics mode, text can be printed to any pixel position, the cursor position is set in pixel coordinates.
When printing characters only the foreground pixels are set, the background is "transparent".
Moving/printing past any border does not cause any special operation, the cursor just goes off-screen.
### Control chars
Characters 0-31 are control characters and don't print by default. They take the next 0-2 following characters as parameters.
Avoid the reserved control chars, they are currently NOPs but their behavior can change in later MicroW8 versions.
| Code | Parameters | Operation |
| ----- | ---------- | ------------------------------------ |
| 0 | - | Nop |
| 1 | char | Print char (including control chars) |
| 2-3 | - | Reserved |
| 4 | - | Switch to normal mode |
| 5 | - | Switch to graphics mode |
| 6-7 | - | Reserved |
| 8 | - | Move cursor left |
| 9 | - | Move cursor right |
| 10 | - | Move cursor down |
| 11 | - | Move cursor up |
| 12 | - | do `cls(background_color)` |
| 13 | - | Move cursor to the left border |
| 14 | color | Set the background color |
| 15 | color | Set the text color |
| 16-23 | - | Reserved |
| 24 | - | Swap text/background colors |
| 25-30 | - | Reserved |
| 31 | x, y | Set cursor position (*) |
(*) In graphics mode, the x coordinate is doubled when using control char 31 to be able to cover the whole screen with one byte.
### fn printChar(char: i32)
Prints the character in the lower 8 bits of `char`. If the upper 24 bits are non-zero, right-shifts `char` by 8 bits and loops back to the beginning.
### fn printString(ptr: i32)
Prints the zero-terminated string at the given memory address.
### fn printInt(num: i32)
Prints `num` as a signed decimal number.
### fn setTextColor(color: i32)
Sets the text color.
### fn setBackgroundColor(color: i32)
Sets the background color.
### fn setCursorPosition(x: i32, y: i32)
Sets the cursor position. In normal mode `x` and `y` are multiplied by 8 to get the pixel position, in graphics mode they are used as is.
# The `uw8` tool
The `uw8` tool included in the MicroW8 download includes a number of useful tools for developing MicroW8 carts. For small productions written in
wat or CurlyWas you don't need anything apart from `uw8` and a text editor of your choice.
## `uw8 run`
Usage:
`uw8 run [<options>] <file>`
Runs `<file>` which can be a binary WebAssembly module, an `.uw8` cart, a wat (WebAssembly text format) source file or a [CurlyWas](https://github.com/exoticorn/curlywas) source file.
Options:
* `-t FRAMES`, `--timeout FRAMES` : Sets the timeout in frames (1/60s). If the start or update function runs longer than this it is forcibly interupted
and execution of the cart is stopped. Defaults to 30 (0.5s)
* `-w`, `--watch`: Reloads the given file every time it changes on disk.
* `-p`, `--pack`: Pack the file into an `.uw8` cart before running it and print the resulting size.
* `-u`, `--uncompressed`: Use the uncompressed `uw8` format for packing.
* `-l LEVEL`, `--level LEVEL`: Compression level (0-9). Higher compression levels are really slow.
* `-o FILE`, `--output FILE`: Write the loaded and optionally packed cart back to disk.
## `uw8 pack`
Usage:
`uw8 pack [<options>] <infile> <outfile>`
Packs the WebAssembly module or text file, or [CurlyWas](https://github.com/exoticorn/curlywas) source file into a `.uw8` cart.
Options:
* `-u`, `--uncompressed`: Use the uncompressed `uw8` format for packing.
* `-l LEVEL`, `--level LEVEL`: Compression level (0-9). Higher compression levels are really slow.
## `uw8 unpack`
Usage:
`uw8 unpack <infile> <outfile>`
Unpacks a MicroW8 module into a standard WebAssembly module.
## `uw8 compile`
Usage:
`uw8 compile [<options>] <infile> <outfile>`
Compiles a [CurlyWas](https://github.com/exoticorn/curlywas) source file to a standard WebAssembly module. Most useful together with
the `--debug` option to get a module that works well in the Chrome debugger.
Options:
* `-d`, `--debug`: Generate a name section to help debugging
## `uw8 filter-exports`
Usage:
`uw8 filter-exports <infile> <outfile>`
Reads a binary WebAssembly module, removes all exports not used by the MicroW8 platform + everything that is unreachable without those exports and writes the resulting module to `outfile`.
When compiling C code (or Rust, zig or others) to WebAssembly, you end up with a few exported global variables that are used for managing the heap and C stack, even if the code doesn't actually use those features. You can use this command to automatically remove them and gain a few bytes. See the C, Rust and zig examples in the MicroW8 repository.
# Other useful tools
The [Web Assembly Binary Toolkit](https://github.com/WebAssembly/wabt) includes
a few useful tools, eg. `wat2wasm` to compile the WebAssemby text format to binary
wasm and `wasm2wat` to disassemble wasm binaries.
[Binaryen](https://github.com/WebAssembly/binaryen) includes `wasm-opt` which enable additional optimizations over what LLVM (the backend that is used by most compilers that target WebAssembly) can do.
# Distribution
The classical distribution option is just to put the `.uw8` cart into a zip file, let people run it themselves, either in the `uw8` tool or in the web runtime.
If you want to go this way, you might consider including `microw8.html` in your download. It's specifically designed to be a small (~10KB at the moment), self-contained HTML file for just this reason. That way, anyone who has downloaded you production can run it, even when offline, provided they have a modern web browser at hand. Also, should future versions of MicroW8 ever introduce any kind of incompatibilities, they'd still have a compatible version right there without hunting arround for an old version.
## Base64 encoded link
For small productions (<= 1024 bytes), when you load them in the web runtime, the URL is automatically updated to include the cart as base64 encoded data. You can just give that URL to others for them to run your prod.
## url parameter
Another option is to put the cart on a webserver and add `#url=url/to/the/cart.uw8` to the end of the web runtime URL. ([Like this](../v0.1pre5#url=../uw8/skipahead.uw8))
If the cart and the web runtime are on different domains, you'll have to make sure that [CORS header](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#the_http_response_headers) are enabled for the cart, otherwise the web runtime won't be able to load it.
Feel free to put the web runtime on your own server if it makes sense to you, its [license](https://unlicense.org/) allows you to do anything you want with it.
## `.html` + `.uw8`
At startup the web runtime will try to load a cart in the same directory as the `.html` file. If the URL of the web runtime ends in `.html` it will try to load a cart with the same name and the extension `.uw8`. If the URL of the web runtime ends in a `/` it will try to load a `cart.uw8` at that location.
So, you could for example serve the web runtime as `https://example.org/mytunnel.html` and the cart as `https://example.org/mytunnel.uw8` and send people to the HTML page to run the cart. Or you could put them up as `https://example.org/mytunnel/index.html` and `https://example.org/mytunnel/cart.uw8` and send people to `https://example.org/mytunnel`.
If a cart is found and loaded in this way, the load button is hidden.
## Itch.io
The above `.html` + `.uw8` option works great on [Itch.io](https://itch.io) as well. Put these two files into a zip archive:
* `index.html`: a copy of the web runtime (`microw8.html` in the MicroW8 download)
* `index.uw8`: Your game cart
Upload the zip file to itch.io and make sure to set the embedded viewport size to exactly (!) 640x480 pixel. At that exact size the web runtime hides everything except for the MicroW8 screen.
If instead you actually *want* to display the border around the screen and the byte size you can try a size of about 720x620.
[See here for an example upload.](https://exoticorn.itch.io/skipahead)
# `.uw8` format
The first byte of the file specifies the format version:
## Format version `00`:
This file is simply a standard WebAssembly module
## Format version `01`:
The rest of this file is the same as a WebAssembly
module with the 8 byte header removed. This module
can leave out sections which are then taken from
a base module provided by MicroW8.
You can generate this base module yourself using
`uw8-tool`. As a quick summary, it provides all function
types with up to 5 parameters (i32 or f32) where the
`f32` parameters always preceed the `i32` parameters.
Then it includes all imports that MicroW8 provides,
a function section with a single function of type
`() -> void` and an export section that exports
the first function in the file under the name `upd`.
## Format version `02`:
Same as version `01` except everything after the first byte is compressed
using a [custom LZ compression scheme](https://github.com/exoticorn/upkr).
# The web runtime
Load carts into the web runtime either by using the "Load cart..." button, or by dragging the file
onto the screen area.
## Input
For input, you can either use a standard gamepad or keyboard. On a keyboard use the arrow keys and the keys Z, X, A and S to emulate the A, B, X and Y buttons.
## Video recording
Press F10 to start recording, press again to stop. Then a download dialog will open for the video file.
The file might miss some metadata needed to load in some video editing tools, in that case you can run
it through ffmpeg like this `ffmpeg -i IN_NAME.webm -c copy -o OUT_NAME.webm to fix it up.
To convert it to 1280x720, for example for a lovebyte upload, you can use:
```
ffmpeg -i IN.webm -vf "scale=960:720:flags=neighbor,pad=1280:720:160:0" -r 60 OUT.mp4
```
## Screenshot
Pressing F9 opens a download dialog with a screenshot.
## Devkit mode
Append `#devkit` to the web runtime url in order to switch to devkit mode. In devkit mode, standard web assembly modules
are loaded bypassing the loader, removing all size restrictions. At the same time, the memory limit is increased to 1GB.

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg width="120" height="120" version="1.1" viewBox="0 0 120 120" xmlns="http://www.w3.org/2000/svg">
<path d="m3.9727 6.3613v27.945h7.8691l2.8281 66.549 6.9199-56.922h16.969l6.4375 56.922 4.1523-66.549h17.148l-9.5488 81.697h27.195c-4.1759-2.1972-7.3595-5.016-9.5234-8.4707-2.2464-3.6905-3.3691-7.6805-3.3691-11.973 0-4.7735 1.3845-8.9251 4.1523-12.455 2.7679-3.53 6.4981-6.3194 11.191-8.3652-4.2521-2.7277-7.3402-5.4948-9.2656-8.3027-1.8854-2.8481-2.8281-6.5783-2.8281-11.191 0-4.7334 1.223-8.8256 3.6699-12.275 2.4469-3.4498 5.7559-6.0981 9.9277-7.9434 4.212-1.8854 8.9647-2.8262 14.26-2.8262 5.2203 0 9.8397 0.84602 13.867 2.5234v-28.363h-40.227a14.855 14.855 0 0 1 0.019531 0.40234 14.855 14.855 0 0 1-14.855 14.855 14.855 14.855 0 0 1-14.855-14.855 14.855 14.855 0 0 1 0.052734-0.40234zm98.186 38.775c-3.0888 0-5.4939 0.863-7.2187 2.5879-1.6848 1.6848-2.5273 4.192-2.5273 7.5215 0 2.9283 0.98169 5.3538 2.9473 7.2793 1.9656 1.8854 5.0352 3.6113 9.207 5.1758 2.7277-2.0057 4.5928-3.971 5.5957-5.8965 1.0028-1.9656 1.5039-4.1129 1.5039-6.4395 0-2.9684-0.8017-5.4144-2.4062-7.3398-1.5644-1.9255-3.9326-2.8887-7.1016-2.8887zm-72.203 12.938-6.4902 57.93h12.393zm68.113 21.781c-2.4469 1.5644-4.3938 3.5502-5.8379 5.957-1.4441 2.4068-2.166 5.2741-2.166 8.6035 0 3.4498 1.0041 6.2781 3.0098 8.4844 2.0458 2.1662 5.0726 3.25 9.084 3.25 4.1317 0 7.142-1.1043 9.0274-3.3106 1.8854-2.2464 2.8281-4.8723 2.8281-7.8809 0-2.7679-0.56235-5.0153-1.6856-6.7402-1.0831-1.7249-2.7886-3.2096-5.1152-4.4531-2.3266-1.2836-5.3738-2.5864-9.1445-3.9102z" fill="#85a6b2" fill-rule="evenodd"/>
</svg>

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -2,12 +2,16 @@
{% block hero %} {% block hero %}
<div> <div>
<section> <section>
<h1 class="text-center heading-text">A WebAssembly based sizecoding platform</h1> <h1 class="text-center heading-text">A WebAssembly based fantasy console</h1>
</section> </section>
<a href="v0.1pre4"> <a href="v0.1.1">
<div class="demonstration-gif" style="width:640px;height:512px;background-color:black"></div> <img class="demonstration-gif" style="width:640px;height:480px;image-rendering:pixelated" src="img/technotunnel.png"></img>
</a> </a>
</div> </div>
<div class="explore-more text"
onclick="document.getElementById('features').scrollIntoView({behavior: 'smooth'})">
Explore More ⇩
</div>
{% endblock hero %} {% endblock hero %}
{% block footer %} {% block footer %}

70
src/filewatcher.rs Normal file
View File

@@ -0,0 +1,70 @@
use anyhow::{bail, Result};
use notify::{DebouncedEvent, Watcher, RecommendedWatcher};
use std::{
collections::BTreeSet,
path::{Path, PathBuf},
sync::mpsc,
time::Duration,
};
pub struct FileWatcher {
_watcher: RecommendedWatcher,
watched_files: BTreeSet<PathBuf>,
rx: mpsc::Receiver<DebouncedEvent>,
}
pub struct FileWatcherBuilder(BTreeSet<PathBuf>);
impl FileWatcher {
pub fn builder() -> FileWatcherBuilder {
FileWatcherBuilder(BTreeSet::new())
}
pub fn poll_changed_file(&self) -> Result<Option<PathBuf>> {
let event = self.rx.try_recv();
match event {
Ok(DebouncedEvent::Create(path) | DebouncedEvent::Write(path)) => {
let handle = same_file::Handle::from_path(&path)?;
for file in &self.watched_files {
if handle == same_file::Handle::from_path(file)? {
return Ok(Some(path));
}
}
}
Err(mpsc::TryRecvError::Disconnected) => bail!("File watcher disconnected"),
_ => (),
}
Ok(None)
}
}
impl FileWatcherBuilder {
pub fn add_file<P: Into<PathBuf>>(&mut self, path: P) -> &mut Self {
self.0.insert(path.into());
self
}
pub fn build(self) -> Result<FileWatcher> {
let mut directories: BTreeSet<&Path> = BTreeSet::new();
for file in &self.0 {
if let Some(directory) = file.parent() {
directories.insert(directory);
}
}
let (tx, rx) = mpsc::channel();
let mut watcher = notify::watcher(tx, Duration::from_millis(100))?;
for directory in directories {
watcher.watch(directory, notify::RecursiveMode::NonRecursive)?;
}
Ok(FileWatcher {
_watcher: watcher,
watched_files: self.0,
rx,
})
}
}

View File

@@ -1,191 +1,22 @@
use std::io::prelude::*; mod filewatcher;
use std::path::Path; #[cfg(feature = "native")]
use std::{fs::File, time::Instant}; mod run_native;
#[cfg(feature = "browser")]
mod run_web;
pub use filewatcher::FileWatcher;
#[cfg(feature = "native")]
pub use run_native::MicroW8;
#[cfg(feature = "browser")]
pub use run_web::RunWebServer;
use anyhow::Result; use anyhow::Result;
use minifb::{Key, Window, WindowOptions};
use wasmtime::{
Engine, GlobalType, Memory, MemoryType, Module, Mutability, Store, TypedFunc, ValType,
};
static GAMEPAD_KEYS: &'static [Key] = &[Key::Up, Key::Down, Key::Left, Key::Right, Key::Z, Key::X, Key::A, Key::S]; pub trait Runtime {
fn is_open(&self) -> bool;
pub struct MicroW8 { fn set_timeout(&mut self, _timeout: u32) {
engine: Engine, eprintln!("Warning: runtime doesn't support timeout");
loader_module: Module, }
window: Window, fn load(&mut self, module_data: &[u8]) -> Result<()>;
window_buffer: Vec<u32>, fn run_frame(&mut self) -> Result<()>;
instance: Option<UW8Instance>,
}
struct UW8Instance {
store: Store<()>,
memory: Memory,
end_frame: TypedFunc<(), ()>,
update: TypedFunc<(), ()>,
start_time: Instant,
module: Vec<u8>
}
impl MicroW8 {
pub fn new() -> Result<MicroW8> {
let engine = wasmtime::Engine::default();
let loader_module =
wasmtime::Module::new(&engine, include_bytes!("../platform/bin/loader.wasm"))?;
let mut options = WindowOptions::default();
options.scale = minifb::Scale::X2;
options.scale_mode = minifb::ScaleMode::AspectRatioStretch;
options.resize = true;
let mut window = Window::new("MicroW8", 320, 240, options)?;
window.limit_update_rate(Some(std::time::Duration::from_micros(16666)));
Ok(MicroW8 {
engine,
loader_module,
window,
window_buffer: vec![0u32; 320 * 240],
instance: None,
})
}
pub fn is_open(&self) -> bool {
self.window.is_open() && !self.window.is_key_down(Key::Escape)
}
fn reset(&mut self) {
self.instance = None;
for v in &mut self.window_buffer {
*v = 0;
}
}
pub fn load_from_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
self.reset();
let mut module = vec![];
File::open(path)?.read_to_end(&mut module)?;
self.load_from_memory(&module)
}
pub fn load_from_memory(&mut self, module_data: &[u8]) -> Result<()> {
self.reset();
let mut store = wasmtime::Store::new(&self.engine, ());
let memory = wasmtime::Memory::new(&mut store, MemoryType::new(4, Some(4)))?;
let mut linker = wasmtime::Linker::new(&self.engine);
linker.define("env", "memory", memory.clone())?;
let loader_instance = linker.instantiate(&mut store, &self.loader_module)?;
let load_uw8 = loader_instance.get_typed_func::<i32, i32, _>(&mut store, "load_uw8")?;
let platform_data = include_bytes!("../platform/bin/platform.uw8");
memory.data_mut(&mut store)[..platform_data.len()].copy_from_slice(platform_data);
let platform_length =
load_uw8.call(&mut store, platform_data.len() as i32)? as u32 as usize;
let platform_module =
wasmtime::Module::new(&self.engine, &memory.data(&store)[..platform_length])?;
memory.data_mut(&mut store)[..module_data.len()].copy_from_slice(module_data);
let module_length = load_uw8.call(&mut store, module_data.len() as i32)? as u32 as usize;
let module = wasmtime::Module::new(&self.engine, &memory.data(&store)[..module_length])?;
linker.func_wrap("env", "acos", |v: f32| v.acos())?;
linker.func_wrap("env", "asin", |v: f32| v.asin())?;
linker.func_wrap("env", "atan", |v: f32| v.atan())?;
linker.func_wrap("env", "atan2", |x: f32, y: f32| x.atan2(y))?;
linker.func_wrap("env", "cos", |v: f32| v.cos())?;
linker.func_wrap("env", "exp", |v: f32| v.exp())?;
linker.func_wrap("env", "log", |v: f32| v.ln())?;
linker.func_wrap("env", "sin", |v: f32| v.sin())?;
linker.func_wrap("env", "tan", |v: f32| v.tan())?;
linker.func_wrap("env", "pow", |a: f32, b: f32| a.powf(b))?;
for i in 9..64 {
linker.func_wrap("env", &format!("reserved{}", i), || {})?;
}
for i in 0..16 {
linker.define(
"env",
&format!("g_reserved{}", i),
wasmtime::Global::new(
&mut store,
GlobalType::new(ValType::I32, Mutability::Const),
0.into(),
)?,
)?;
}
let platform_instance = linker.instantiate(&mut store, &platform_module)?;
for export in platform_instance.exports(&mut store) {
linker.define(
"env",
export.name(),
export
.into_func()
.expect("platform surely only exports functions"),
)?;
}
let instance = linker.instantiate(&mut store, &module)?;
let end_frame = platform_instance.get_typed_func::<(), (), _>(&mut store, "endFrame")?;
let update = instance.get_typed_func::<(), (), _>(&mut store, "upd")?;
self.instance = Some(UW8Instance {
store,
memory,
end_frame,
update,
start_time: Instant::now(),
module: module_data.into()
});
Ok(())
}
pub fn run_frame(&mut self) -> Result<()> {
if let Some(mut instance) = self.instance.take() {
{
let time = instance.start_time.elapsed().as_millis() as i32;
let mut gamepad: u32 = 0;
for key in self.window.get_keys().unwrap_or(Vec::new()) {
if let Some(index) = GAMEPAD_KEYS.iter().enumerate().find(|(_, &k)| k == key).map(|(i, _)| i) {
gamepad |= 1 << index;
}
}
let mem = instance.memory.data_mut(&mut instance.store);
mem[64..68].copy_from_slice(&time.to_le_bytes());
mem[68..72].copy_from_slice(&gamepad.to_le_bytes());
}
instance.update.call(&mut instance.store, ())?;
instance.end_frame.call(&mut instance.store, ())?;
let memory = instance.memory.data(&instance.store);
let framebuffer = &memory[120..];
let palette = &memory[0x13000..];
for i in 0..320 * 240 {
let offset = framebuffer[i] as usize * 4;
self.window_buffer[i] = 0xff000000
| ((palette[offset + 0] as u32) << 16)
| ((palette[offset + 1] as u32) << 8)
| palette[offset + 2] as u32;
}
if self.window.is_key_pressed(Key::R, minifb::KeyRepeat::No) {
self.load_from_memory(&instance.module)?;
} else {
self.instance = Some(instance);
}
}
self.window
.update_with_buffer(&self.window_buffer, 320, 240)?;
Ok(())
}
} }

View File

@@ -1,39 +1,54 @@
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process; use std::process;
use std::sync::mpsc;
use std::time::Duration;
use std::{
path::{Path, PathBuf},
process::exit,
};
use anyhow::{bail, Result}; use anyhow::Result;
use notify::{DebouncedEvent, Watcher};
use pico_args::Arguments; use pico_args::Arguments;
#[cfg(feature = "native")]
use uw8::MicroW8; use uw8::MicroW8;
#[cfg(feature = "browser")]
use uw8::RunWebServer;
#[cfg(any(feature = "native", feature = "browser"))]
use uw8::Runtime;
fn main() -> Result<()> { fn main() -> Result<()> {
let mut args = Arguments::from_env(); let mut args = Arguments::from_env();
match args.subcommand()?.as_ref().map(|s| s.as_str()) { match args.subcommand()?.as_deref() {
Some("version") => {
println!("{}", env!("CARGO_PKG_VERSION"));
Ok(())
}
#[cfg(any(feature = "native", feature = "browser"))]
Some("run") => run(args), Some("run") => run(args),
Some("pack") => pack(args), Some("pack") => pack(args),
Some("unpack") => unpack(args),
Some("compile") => compile(args),
Some("filter-exports") => filter_exports(args),
Some("help") | None => {
println!("uw8 {}", env!("CARGO_PKG_VERSION"));
println!();
println!("Usage:");
#[cfg(any(feature = "native", feature = "browser"))]
println!(" uw8 run [-t/--timeout <frames>] [--b/--browser] [-w/--watch] [-p/--pack] [-u/--uncompressed] [-l/--level] [-o/--output <out-file>] <file>");
println!(" uw8 pack [-u/--uncompressed] [-l/--level] <in-file> <out-file>");
println!(" uw8 unpack <in-file> <out-file>");
println!(" uw8 compile [-d/--debug] <in-file> <out-file>");
println!(" uw8 filter-exports <in-wasm> <out-wasm>");
Ok(())
}
Some(other) => { Some(other) => {
eprintln!("Unknown command '{}'", other); eprintln!("Unknown command '{}'", other);
process::exit(1); process::exit(1);
} }
None => {
println!("Usage:");
println!(" uw8 run [-w/--watch] [-p/--pack] [-u/--uncompressed] [-l/--level] [-o/--output <out-file>] <file>");
println!(" uw8 pack [-u/--uncompressed] [-l/--level] <in-file> <out-file>");
Ok(())
}
} }
} }
#[cfg(any(feature = "native", feature = "browser"))]
fn run(mut args: Arguments) -> Result<()> { fn run(mut args: Arguments) -> Result<()> {
let watch_mode = args.contains(["-w", "--watch"]); let watch_mode = args.contains(["-w", "--watch"]);
let timeout: Option<u32> = args.opt_value_from_str(["-t", "--timeout"])?;
let mut config = Config::default(); let mut config = Config::default();
if args.contains(["-p", "--pack"]) { if args.contains(["-p", "--pack"]) {
@@ -55,36 +70,59 @@ fn run(mut args: Arguments) -> Result<()> {
config.output_path = Some(path); config.output_path = Some(path);
} }
#[cfg(feature = "native")]
let run_browser = args.contains(["-b", "--browser"]);
#[cfg(not(feature = "native"))]
let run_browser = args.contains(["-b", "--browser"]) || true;
let filename = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?; let filename = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let mut uw8 = MicroW8::new()?; let mut watcher = uw8::FileWatcher::builder();
let (tx, rx) = mpsc::channel();
let mut watcher = notify::watcher(tx, Duration::from_millis(100))?;
if watch_mode { if watch_mode {
watcher.watch(&filename, notify::RecursiveMode::NonRecursive)?; watcher.add_file(&filename);
} }
if let Err(err) = start_cart(&filename, &mut uw8, &config) { let watcher = watcher.build()?;
use std::process::exit;
let mut runtime: Box<dyn Runtime> = if !run_browser {
#[cfg(not(feature = "native"))]
unimplemented!();
#[cfg(feature = "native")]
Box::new(MicroW8::new()?)
} else {
#[cfg(not(feature = "browser"))]
unimplemented!();
#[cfg(feature = "browser")]
Box::new(RunWebServer::new())
};
if let Some(timeout) = timeout {
runtime.set_timeout(timeout);
}
if let Err(err) = start_cart(&filename, &mut *runtime, &config) {
eprintln!("Load error: {}", err); eprintln!("Load error: {}", err);
if !watch_mode { if !watch_mode {
exit(1); exit(1);
} }
} }
while uw8.is_open() { while runtime.is_open() {
match rx.try_recv() { if watcher.poll_changed_file()?.is_some() {
Ok(DebouncedEvent::Create(_) | DebouncedEvent::Write(_)) => { if let Err(err) = start_cart(&filename, &mut *runtime, &config) {
if let Err(err) = start_cart(&filename, &mut uw8, &config) { eprintln!("Load error: {}", err);
eprintln!("Load error: {}", err);
}
} }
Err(mpsc::TryRecvError::Disconnected) => bail!("File watcher disconnected"),
_ => (),
} }
uw8.run_frame()?; if let Err(err) = runtime.run_frame() {
eprintln!("Runtime error: {}", err);
if !watch_mode {
exit(1);
}
}
} }
Ok(()) Ok(())
@@ -96,7 +134,7 @@ struct Config {
output_path: Option<PathBuf>, output_path: Option<PathBuf>,
} }
fn load_cart(filename: &Path, pack: &Option<uw8_tool::PackConfig>) -> Result<Vec<u8>> { fn load_cart(filename: &Path, config: &Config) -> Result<Vec<u8>> {
let mut cart = vec![]; let mut cart = vec![];
File::open(filename)?.read_to_end(&mut cart)?; File::open(filename)?.read_to_end(&mut cart)?;
@@ -109,22 +147,23 @@ fn load_cart(filename: &Path, pack: &Option<uw8_tool::PackConfig>) -> Result<Vec
}; };
} }
if let Some(pack_config) = pack { if let Some(ref pack_config) = config.pack {
cart = uw8_tool::pack(&cart, pack_config)?; cart = uw8_tool::pack(&cart, pack_config)?;
println!("packed size: {} bytes", cart.len()); println!("packed size: {} bytes", cart.len());
} }
Ok(cart)
}
fn start_cart(filename: &Path, uw8: &mut MicroW8, config: &Config) -> Result<()> {
let cart = load_cart(filename, &config.pack)?;
if let Some(ref path) = config.output_path { if let Some(ref path) = config.output_path {
File::create(path)?.write_all(&cart)?; File::create(path)?.write_all(&cart)?;
} }
if let Err(err) = uw8.load_from_memory(&cart) { Ok(cart)
}
#[cfg(any(feature = "native", feature = "browser"))]
fn start_cart(filename: &Path, runtime: &mut dyn Runtime, config: &Config) -> Result<()> {
let cart = load_cart(filename, config)?;
if let Err(err) = runtime.load(&cart) {
eprintln!("Load error: {}", err); eprintln!("Load error: {}", err);
Err(err) Err(err)
} else { } else {
@@ -147,9 +186,46 @@ fn pack(mut args: Arguments) -> Result<()> {
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?; let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let cart = load_cart(&in_file, &Some(pack_config))?; let cart = load_cart(
&in_file,
&Config {
pack: Some(pack_config),
output_path: None,
},
)?;
File::create(out_file)?.write_all(&cart)?; File::create(out_file)?.write_all(&cart)?;
Ok(()) Ok(())
} }
fn unpack(mut args: Arguments) -> Result<()> {
let in_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
uw8_tool::unpack_file(&in_file, &out_file)
}
fn compile(mut args: Arguments) -> Result<()> {
let mut options = curlywas::Options::default();
if args.contains(["-d", "--debug"]) {
options = options.with_debug();
}
let in_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let module = curlywas::compile_file(in_file, options)?;
File::create(out_file)?.write_all(&module)?;
Ok(())
}
fn filter_exports(mut args: Arguments) -> Result<()> {
let in_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let out_file = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
uw8_tool::filter_exports(&in_file, &out_file)?;
Ok(())
}

1
src/run-web.html Normal file

File diff suppressed because one or more lines are too long

260
src/run_native.rs Normal file
View File

@@ -0,0 +1,260 @@
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::{thread, time::Instant};
use anyhow::Result;
use minifb::{Key, Window, WindowOptions};
use wasmtime::{
Engine, GlobalType, Memory, MemoryType, Module, Mutability, Store, TypedFunc, ValType,
};
static GAMEPAD_KEYS: &[Key] = &[
Key::Up,
Key::Down,
Key::Left,
Key::Right,
Key::Z,
Key::X,
Key::A,
Key::S,
];
pub struct MicroW8 {
engine: Engine,
loader_module: Module,
window: Window,
window_buffer: Vec<u32>,
instance: Option<UW8Instance>,
timeout: u32,
}
struct UW8Instance {
store: Store<()>,
memory: Memory,
end_frame: TypedFunc<(), ()>,
update: TypedFunc<(), ()>,
start_time: Instant,
module: Vec<u8>,
watchdog: Arc<Mutex<UW8WatchDog>>,
}
impl Drop for UW8Instance {
fn drop(&mut self) {
if let Ok(mut watchdog) = self.watchdog.lock() {
watchdog.stop = true;
}
}
}
struct UW8WatchDog {
interupt: wasmtime::InterruptHandle,
timeout: u32,
stop: bool,
}
impl MicroW8 {
pub fn new() -> Result<MicroW8> {
let engine = wasmtime::Engine::new(wasmtime::Config::new().interruptable(true))?;
let loader_module =
wasmtime::Module::new(&engine, include_bytes!("../platform/bin/loader.wasm"))?;
let options = WindowOptions {
scale: minifb::Scale::X2,
scale_mode: minifb::ScaleMode::AspectRatioStretch,
resize: true,
..Default::default()
};
let mut window = Window::new("MicroW8", 320, 240, options)?;
window.limit_update_rate(Some(std::time::Duration::from_micros(16666)));
Ok(MicroW8 {
engine,
loader_module,
window,
window_buffer: vec![0u32; 320 * 240],
instance: None,
timeout: 30,
})
}
fn reset(&mut self) {
self.instance = None;
for v in &mut self.window_buffer {
*v = 0;
}
}
}
impl super::Runtime for MicroW8 {
fn is_open(&self) -> bool {
self.window.is_open() && !self.window.is_key_down(Key::Escape)
}
fn set_timeout(&mut self, timeout: u32) {
self.timeout = timeout;
}
fn load(&mut self, module_data: &[u8]) -> Result<()> {
self.reset();
let mut store = wasmtime::Store::new(&self.engine, ());
let memory = wasmtime::Memory::new(&mut store, MemoryType::new(4, Some(4)))?;
let mut linker = wasmtime::Linker::new(&self.engine);
linker.define("env", "memory", memory)?;
let loader_instance = linker.instantiate(&mut store, &self.loader_module)?;
let load_uw8 = loader_instance.get_typed_func::<i32, i32, _>(&mut store, "load_uw8")?;
let platform_data = include_bytes!("../platform/bin/platform.uw8");
memory.data_mut(&mut store)[..platform_data.len()].copy_from_slice(platform_data);
let platform_length =
load_uw8.call(&mut store, platform_data.len() as i32)? as u32 as usize;
let platform_module =
wasmtime::Module::new(&self.engine, &memory.data(&store)[..platform_length])?;
memory.data_mut(&mut store)[..module_data.len()].copy_from_slice(module_data);
let module_length = load_uw8.call(&mut store, module_data.len() as i32)? as u32 as usize;
let module = wasmtime::Module::new(&self.engine, &memory.data(&store)[..module_length])?;
linker.func_wrap("env", "acos", |v: f32| v.acos())?;
linker.func_wrap("env", "asin", |v: f32| v.asin())?;
linker.func_wrap("env", "atan", |v: f32| v.atan())?;
linker.func_wrap("env", "atan2", |x: f32, y: f32| x.atan2(y))?;
linker.func_wrap("env", "cos", |v: f32| v.cos())?;
linker.func_wrap("env", "exp", |v: f32| v.exp())?;
linker.func_wrap("env", "log", |v: f32| v.ln())?;
linker.func_wrap("env", "sin", |v: f32| v.sin())?;
linker.func_wrap("env", "tan", |v: f32| v.tan())?;
linker.func_wrap("env", "pow", |a: f32, b: f32| a.powf(b))?;
for i in 9..64 {
linker.func_wrap("env", &format!("reserved{}", i), || {})?;
}
for i in 0..16 {
linker.define(
"env",
&format!("g_reserved{}", i),
wasmtime::Global::new(
&mut store,
GlobalType::new(ValType::I32, Mutability::Const),
0.into(),
)?,
)?;
}
let platform_instance = linker.instantiate(&mut store, &platform_module)?;
for export in platform_instance.exports(&mut store) {
linker.define(
"env",
export.name(),
export
.into_func()
.expect("platform surely only exports functions"),
)?;
}
let watchdog = Arc::new(Mutex::new(UW8WatchDog {
interupt: store.interrupt_handle()?,
timeout: self.timeout,
stop: false,
}));
{
let watchdog = watchdog.clone();
thread::spawn(move || loop {
thread::sleep(Duration::from_millis(17));
if let Ok(mut watchdog) = watchdog.lock() {
if watchdog.stop {
break;
}
if watchdog.timeout > 0 {
watchdog.timeout -= 1;
if watchdog.timeout == 0 {
watchdog.interupt.interrupt();
}
}
} else {
break;
}
});
}
let instance = linker.instantiate(&mut store, &module)?;
if let Ok(mut watchdog) = watchdog.lock() {
watchdog.timeout = 0;
}
let end_frame = platform_instance.get_typed_func::<(), (), _>(&mut store, "endFrame")?;
let update = instance.get_typed_func::<(), (), _>(&mut store, "upd")?;
self.instance = Some(UW8Instance {
store,
memory,
end_frame,
update,
start_time: Instant::now(),
module: module_data.into(),
watchdog,
});
Ok(())
}
fn run_frame(&mut self) -> Result<()> {
let mut result = Ok(());
if let Some(mut instance) = self.instance.take() {
{
let time = instance.start_time.elapsed().as_millis() as i32;
let mut gamepad: u32 = 0;
for key in self.window.get_keys() {
if let Some(index) = GAMEPAD_KEYS
.iter()
.enumerate()
.find(|(_, &k)| k == key)
.map(|(i, _)| i)
{
gamepad |= 1 << index;
}
}
let mem = instance.memory.data_mut(&mut instance.store);
mem[64..68].copy_from_slice(&time.to_le_bytes());
mem[68..72].copy_from_slice(&gamepad.to_le_bytes());
}
if let Ok(mut watchdog) = instance.watchdog.lock() {
watchdog.timeout = self.timeout;
}
result = instance.update.call(&mut instance.store, ());
if let Ok(mut watchdog) = instance.watchdog.lock() {
watchdog.timeout = 0;
}
instance.end_frame.call(&mut instance.store, ())?;
let memory = instance.memory.data(&instance.store);
let framebuffer = &memory[120..(120 + 320 * 240)];
let palette = &memory[0x13000..];
for (i, &color_index) in framebuffer.iter().enumerate() {
let offset = color_index as usize * 4;
self.window_buffer[i] = 0xff000000
| ((palette[offset] as u32) << 16)
| ((palette[offset + 1] as u32) << 8)
| palette[offset + 2] as u32;
}
if self.window.is_key_pressed(Key::R, minifb::KeyRepeat::No) {
self.load(&instance.module)?;
} else if result.is_ok() {
self.instance = Some(instance);
}
}
self.window
.update_with_buffer(&self.window_buffer, 320, 240)?;
result?;
Ok(())
}
}

89
src/run_web.rs Normal file
View File

@@ -0,0 +1,89 @@
use anyhow::Result;
use std::{
net::SocketAddr,
sync::{Arc, Mutex},
thread,
};
use tokio::sync::broadcast;
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
use warp::{http::Response, Filter};
pub struct RunWebServer {
cart: Arc<Mutex<Vec<u8>>>,
tx: broadcast::Sender<()>,
}
impl RunWebServer {
pub fn new() -> RunWebServer {
let cart = Arc::new(Mutex::new(Vec::new()));
let (tx, _) = broadcast::channel(1);
let server_cart = cart.clone();
let server_tx = tx.clone();
thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.expect("Failed to create tokio runtime");
rt.block_on(async {
let html = warp::path::end().map(|| {
Response::builder()
.header("Content-Type", "text/html")
.body(include_str!("run-web.html"))
});
let cart = warp::path("cart")
.map(move || server_cart.lock().map_or(Vec::new(), |c| c.clone()));
let events = warp::path("events").and(warp::get()).map(move || {
fn event_stream(
tx: &broadcast::Sender<()>,
) -> impl Stream<Item = Result<warp::sse::Event, std::convert::Infallible>>
{
BroadcastStream::new(tx.subscribe())
.map(|_| Ok(warp::sse::Event::default().data("L")))
}
warp::sse::reply(warp::sse::keep_alive().stream(event_stream(&server_tx)))
});
let socket_addr = "127.0.0.1:3030"
.parse::<SocketAddr>()
.expect("Failed to parse socket address");
let server_future = warp::serve(html.or(cart).or(events)).bind(socket_addr);
println!("Point browser at http://{}", socket_addr);
let _ignore_result = webbrowser::open(&format!("http://{}", socket_addr));
server_future.await
});
});
RunWebServer { cart, tx }
}
}
impl super::Runtime for RunWebServer {
fn load(&mut self, module_data: &[u8]) -> Result<()> {
if let Ok(mut lock) = self.cart.lock() {
lock.clear();
lock.extend_from_slice(module_data);
}
let _ignore_result = self.tx.send(());
Ok(())
}
fn is_open(&self) -> bool {
true
}
fn run_frame(&mut self) -> Result<()> {
std::thread::sleep(std::time::Duration::from_millis(100));
Ok(())
}
}
impl Default for RunWebServer {
fn default() -> RunWebServer {
RunWebServer::new()
}
}

13
test.cwa Normal file
View File

@@ -0,0 +1,13 @@
import "env.memory" memory(4);
import "env.printString" fn print(i32);
export fn upd() {
}
start fn start() {
print(0);
}
data 0 {
"Press " i8(0xe0) " and " i8(0xe1) " to adjust, " i8(0xcc) " to commit." i8(0)
}

BIN
test.wasm Normal file

Binary file not shown.

100
uw8-tool/Cargo.lock generated
View File

@@ -56,6 +56,21 @@ dependencies = [
"lazy_static", "lazy_static",
] ]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "id-arena"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@@ -74,6 +89,15 @@ version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
name = "log"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.14" version = "0.2.14"
@@ -101,6 +125,24 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468" checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468"
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "sacabase" name = "sacabase"
version = "2.0.0" version = "2.0.0"
@@ -110,6 +152,17 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "syn"
version = "1.0.84"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecb2e6da8ee5eb9a61068762a32fa9619cc591ceb055b3687f4cd4051ec2e06b"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.44" version = "0.1.44"
@@ -121,6 +174,18 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]] [[package]]
name = "upkr" name = "upkr"
version = "0.1.0" version = "0.1.0"
@@ -140,8 +205,35 @@ dependencies = [
"pbr", "pbr",
"pico-args", "pico-args",
"upkr", "upkr",
"walrus",
"wasm-encoder", "wasm-encoder",
"wasmparser", "wasmparser 0.81.0",
]
[[package]]
name = "walrus"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb08e48cde54c05f363d984bb54ce374f49e242def9468d2e1b6c2372d291f8"
dependencies = [
"anyhow",
"id-arena",
"leb128",
"log",
"walrus-macro",
"wasmparser 0.77.0",
]
[[package]]
name = "walrus-macro"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a6e5bd22c71e77d60140b0bd5be56155a37e5bd14e24f5f87298040d0cc40d7"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
] ]
[[package]] [[package]]
@@ -159,6 +251,12 @@ dependencies = [
"leb128", "leb128",
] ]
[[package]]
name = "wasmparser"
version = "0.77.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b35c86d22e720a07d954ebbed772d01180501afe7d03d464f413bb5f8914a8d6"
[[package]] [[package]]
name = "wasmparser" name = "wasmparser"
version = "0.81.0" version = "0.81.0"

View File

@@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
wasmparser = "0.81" wasmparser = "0.81"
wasm-encoder = "0.8" wasm-encoder = "0.8"
walrus = "0.19"
anyhow = "1" anyhow = "1"
pico-args = "0.4" pico-args = "0.4"
upkr = { git = "https://github.com/exoticorn/upkr.git", rev = "2e7983fc" } upkr = { git = "https://github.com/exoticorn/upkr.git", rev = "2e7983fc" }

View File

@@ -89,6 +89,9 @@ impl BaseModule {
add_function(&mut functions, &type_map, "setBackgroundColor", &[I32], None); add_function(&mut functions, &type_map, "setBackgroundColor", &[I32], None);
add_function(&mut functions, &type_map, "setCursorPosition", &[I32, I32], None); add_function(&mut functions, &type_map, "setCursorPosition", &[I32, I32], None);
add_function(&mut functions, &type_map, "rectangle_outline", &[F32, F32, F32, F32, I32], None);
add_function(&mut functions, &type_map, "circle_outline", &[F32, F32, F32, I32], None);
for i in functions.len()..64 { for i in functions.len()..64 {
add_function( add_function(
&mut functions, &mut functions,

View File

@@ -0,0 +1,21 @@
use std::path::Path;
use anyhow::Result;
pub fn filter_exports(in_path: &Path, out_path: &Path) -> Result<()> {
let mut module = walrus::Module::from_file(in_path)?;
let exports_to_delete: Vec<_> = module.exports.iter().filter_map(|export| match export.name.as_str() {
"upd" => None,
_ => Some(export.id())
}).collect();
for id in exports_to_delete {
module.exports.delete(id);
}
walrus::passes::gc::run(&mut module);
module.emit_wasm_file(out_path)?;
Ok(())
}

View File

@@ -1,5 +1,7 @@
mod base_module; mod base_module;
mod pack; mod pack;
mod filter_exports;
pub use base_module::BaseModule; pub use base_module::BaseModule;
pub use pack::{pack, pack_file, unpack, unpack_file, PackConfig}; pub use pack::{pack, pack_file, unpack, unpack_file, PackConfig};
pub use filter_exports::filter_exports;

View File

@@ -27,6 +27,11 @@ fn main() -> Result<()> {
let dest: PathBuf = args.free_from_str()?; let dest: PathBuf = args.free_from_str()?;
uw8_tool::unpack_file(&source, &dest)?; uw8_tool::unpack_file(&source, &dest)?;
} }
"filter-exports" => {
let source: PathBuf = args.free_from_str()?;
let dest: PathBuf = args.free_from_str()?;
uw8_tool::filter_exports(&source, &dest)?;
}
_ => { _ => {
eprintln!("Unknown subcommand '{}'", cmd); eprintln!("Unknown subcommand '{}'", cmd);
print_help(); print_help();
@@ -44,6 +49,7 @@ fn print_help() {
"Usage: "Usage:
uw8-tool make-base <version> uw8-tool make-base <version>
uw8-tool pack <wasm file> <uw8 file> uw8-tool pack <wasm file> <uw8 file>
uw8-tool unpack <uw8 file> <wasm file>" uw8-tool unpack <uw8 file> <wasm file>
uw8-tool filter-exports <wasm file> <wasm file>"
); );
} }

View File

@@ -10,7 +10,7 @@ use std::{
use wasm_encoder as enc; use wasm_encoder as enc;
use wasmparser::{ use wasmparser::{
BinaryReader, ExportSectionReader, ExternalKind, FunctionBody, FunctionSectionReader, BinaryReader, ExportSectionReader, ExternalKind, FunctionBody, FunctionSectionReader,
ImportSectionEntryType, ImportSectionReader, TypeSectionReader, ImportSectionEntryType, ImportSectionReader, TableSectionReader, TypeSectionReader,
}; };
pub struct PackConfig { pub struct PackConfig {
@@ -31,7 +31,9 @@ impl PackConfig {
impl Default for PackConfig { impl Default for PackConfig {
fn default() -> PackConfig { fn default() -> PackConfig {
PackConfig { compression: Some(2) } PackConfig {
compression: Some(2),
}
} }
} }
@@ -156,6 +158,8 @@ struct ParsedModule<'a> {
start_section: Option<u32>, start_section: Option<u32>,
function_bodies: Vec<wasmparser::FunctionBody<'a>>, function_bodies: Vec<wasmparser::FunctionBody<'a>>,
data_section: Option<Section<()>>, data_section: Option<Section<()>>,
table_section: Option<Section<()>>,
element_section: Option<Vec<Element>>,
} }
impl<'a> ParsedModule<'a> { impl<'a> ParsedModule<'a> {
@@ -170,6 +174,8 @@ impl<'a> ParsedModule<'a> {
let mut start_section = None; let mut start_section = None;
let mut function_bodies = Vec::new(); let mut function_bodies = Vec::new();
let mut data_section = None; let mut data_section = None;
let mut table_section = None;
let mut element_section = None;
let mut offset = 0; let mut offset = 0;
@@ -209,6 +215,17 @@ impl<'a> ParsedModule<'a> {
Payload::DataSection(_) => { Payload::DataSection(_) => {
data_section = Some(Section::new(range, ())); data_section = Some(Section::new(range, ()));
} }
Payload::TableSection(reader) => {
validate_table_section(reader)?;
table_section = Some(Section::new(range, ()));
}
Payload::ElementSection(mut reader) => {
let mut elements = Vec::with_capacity(reader.get_count() as usize);
for _ in 0..reader.get_count() {
elements.push(Element::parse(reader.read()?)?);
}
element_section = Some(elements);
}
Payload::CodeSectionStart { .. } => (), Payload::CodeSectionStart { .. } => (),
Payload::CodeSectionEntry(body) => function_bodies.push(body), Payload::CodeSectionEntry(body) => function_bodies.push(body),
Payload::CustomSection { .. } => (), Payload::CustomSection { .. } => (),
@@ -229,6 +246,8 @@ impl<'a> ParsedModule<'a> {
start_section, start_section,
function_bodies, function_bodies,
data_section, data_section,
table_section,
element_section,
}) })
} }
@@ -405,6 +424,10 @@ impl<'a> ParsedModule<'a> {
module.section(&function_section); module.section(&function_section);
} }
if let Some(tables) = self.table_section {
copy_section(&mut module, &self.data[tables.range.clone()])?;
}
if let Some(ref globals) = self.globals { if let Some(ref globals) = self.globals {
copy_section(&mut module, &self.data[globals.range.clone()])?; copy_section(&mut module, &self.data[globals.range.clone()])?;
for i in 0..globals.data { for i in 0..globals.data {
@@ -446,6 +469,25 @@ impl<'a> ParsedModule<'a> {
}); });
} }
if let Some(elements) = self.element_section {
let mut element_section = wasm_encoder::ElementSection::new();
for element in elements {
let mut functions = Vec::with_capacity(element.functions.len());
for index in element.functions {
functions.push(*function_map.get(&index).ok_or_else(|| {
anyhow!("Function index {} not found in function map", index)
})?);
}
element_section.active(
None,
&wasm_encoder::Instruction::I32Const(element.start_index as i32),
ValType::FuncRef,
wasm_encoder::Elements::Functions(&functions),
);
}
module.section(&element_section);
}
{ {
let mut code_section = enc::CodeSection::new(); let mut code_section = enc::CodeSection::new();
@@ -502,6 +544,19 @@ fn read_type_section(reader: TypeSectionReader) -> Result<Vec<base_module::Funct
Ok(function_types) Ok(function_types)
} }
fn validate_table_section(mut reader: TableSectionReader) -> Result<()> {
if reader.get_count() != 1 {
bail!("Only up to one table supported");
}
let type_ = reader.read()?;
if type_.element_type != wasmparser::Type::FuncRef {
bail!("Only one funcref table is supported");
}
Ok(())
}
#[derive(Debug)] #[derive(Debug)]
struct Section<T> { struct Section<T> {
range: std::ops::Range<usize>, range: std::ops::Range<usize>,
@@ -579,6 +634,51 @@ impl ImportSection {
} }
} }
#[derive(Debug)]
struct Element {
start_index: u32,
functions: Vec<u32>,
}
impl Element {
fn parse(element: wasmparser::Element) -> Result<Element> {
if element.ty != wasmparser::Type::FuncRef {
bail!("Table element type is not FuncRef");
}
let start_index = if let wasmparser::ElementKind::Active {
init_expr,
table_index: 0,
} = element.kind
{
let mut init_reader = init_expr.get_operators_reader();
if let wasmparser::Operator::I32Const { value: start_index } = init_reader.read()? {
start_index as u32
} else {
bail!("Table element start index is not a integer constant");
}
} else {
bail!("Unsupported table element kind");
};
let mut items_reader = element.items.get_items_reader()?;
let mut functions = Vec::with_capacity(items_reader.get_count() as usize);
for _ in 0..items_reader.get_count() {
if let wasmparser::ElementItem::Func(index) = items_reader.read()? {
functions.push(index);
} else {
bail!("Table element item is not a function");
}
}
Ok(Element {
start_index,
functions,
})
}
}
#[derive(Debug)] #[derive(Debug)]
struct FunctionImport { struct FunctionImport {
module: String, module: String,
@@ -678,8 +778,13 @@ fn remap_function(
.get(&function_index) .get(&function_index)
.ok_or_else(|| anyhow!("Function index out of range: {}", function_index))?, .ok_or_else(|| anyhow!("Function index out of range: {}", function_index))?,
), ),
De::CallIndirect { .. } De::CallIndirect { index, table_index } => En::CallIndirect {
| De::ReturnCall { .. } ty: *type_map
.get(&index)
.ok_or_else(|| anyhow!("Unknown function type in call indirect"))?,
table: table_index,
},
De::ReturnCall { .. }
| De::ReturnCallIndirect { .. } | De::ReturnCallIndirect { .. }
| De::Delegate { .. } | De::Delegate { .. }
| De::CatchAll => todo!(), | De::CatchAll => todo!(),

2
web/build-run-web Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/bash
rm -rf .parcel-cache && yarn parcel build src/run-web.html && cp dist/run-web.html ../src/

View File

@@ -9,10 +9,15 @@
</style> </style>
</head> </head>
<body> <body>
<div id="uw8">
<a href="https://exoticorn.github.io/microw8">MicroW8</a> 0.1.1
</div>
<div id="centered"> <div id="centered">
<canvas id="screen" width="320" height="240"></canvas> <canvas id="screen" width="320" height="240">
<div id="message"></div> </canvas>
<button id="cartButton">Load cart...</button> <div id="timer" hidden="true"></div>
<div id="message"></div>
<button id="cartButton" style="visibility:hidden">Load cart...</button>
</div> </div>
<div id="footer"> <div id="footer">
<a href="http://unlicense.org/" ref="license">Unlicense</a> <a href="http://unlicense.org/" ref="license">Unlicense</a>

View File

@@ -1,5 +1,4 @@
import loaderUrl from "data-url:../../platform/bin/loader.wasm"; import MicroW8 from './microw8.js';
import platformUrl from "data-url:../../platform/bin/platform.uw8";
function setMessage(size, error) { function setMessage(size, error) {
let html = size ? `${size} bytes` : 'Insert cart'; let html = size ? `${size} bytes` : 'Insert cart';
@@ -9,257 +8,81 @@ function setMessage(size, error) {
document.getElementById('message').innerHTML = html; document.getElementById('message').innerHTML = html;
} }
let screen = document.getElementById('screen'); let uw8 = MicroW8(document.getElementById('screen'), {
let canvasCtx = screen.getContext('2d'); setMessage,
let imageData = canvasCtx.createImageData(320, 240); keyboardElement: window,
timerElement: document.getElementById("timer"),
let cancelFunction; });
let currentData;
let U8 = (d) => new Uint8Array(d);
let U32 = (d) => new Uint32Array(d);
let pad = 0;
let keyHandler = (e) => {
let isKeyDown = e.type == 'keydown';
let mask;
switch (e.code) {
case 'ArrowUp':
mask = 1;
break;
case 'ArrowDown':
mask = 2;
break;
case 'ArrowLeft':
mask = 4;
break;
case 'ArrowRight':
mask = 8;
break;
case 'KeyZ':
mask = 16;
break;
case 'KeyX':
mask = 32;
break;
case 'KeyA':
mask = 64;
break;
case 'KeyS':
mask = 128;
break;
case 'KeyR':
if (isKeyDown) {
runModule(currentData);
}
break;
}
if (isKeyDown) {
pad |= mask;
} else {
pad &= ~mask;
}
};
window.onkeydown = keyHandler;
window.onkeyup = keyHandler;
async function runModule(data, keepUrl) {
if (cancelFunction) {
cancelFunction();
cancelFunction = null;
}
let cartridgeSize = data.byteLength;
setMessage(cartridgeSize);
if (cartridgeSize == 0) {
return;
}
currentData = data;
let newURL = window.location.pathname;
if (cartridgeSize <= 1024 && !keepUrl) {
let dataString = '';
for (let byte of U8(data)) {
dataString += String.fromCharCode(byte);
}
newURL += '#' + btoa(dataString);
if (newURL != window.location.pathname + window.location.hash) {
history.pushState(null, null, newURL);
}
}
screen.width = screen.width;
try {
let memory = new WebAssembly.Memory({ initial: 4, maximum: 4 });
let memU8 = U8(memory.buffer);
let importObject = {
env: {
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) => (await WebAssembly.instantiate(data, importObject)).instance;
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];
}
for (let i = 9; i < 64; ++i) {
importObject.env['reserved' + i] = () => { };
}
for (let i = 0; i < 16; ++i) {
importObject.env['g_reserved' + i] = 0;
}
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 = await instantiate(data);
let buffer = U32(imageData.data.buffer);
let startTime = Date.now();
let keepRunning = true;
cancelFunction = () => keepRunning = false;
const timePerFrame = 1000 / 60;
let nextFrame = startTime;
function mainloop() {
if (!keepRunning) {
return;
}
try {
let now = Date.now();
let restart = false;
if (now >= nextFrame) {
let gamepads = navigator.getGamepads();
let gamepad = 0;
for (let i = 0; i < 4; ++i) {
let pad = gamepads[i];
if (!pad) {
continue;
}
for (let j = 0; j < 8; ++j) {
let buttonIdx = (j + 12) % 16;
if (pad.buttons.length > buttonIdx && pad.buttons[buttonIdx].pressed) {
gamepad |= 1 << (i * 8 + j);
}
}
if (pad.axes.length > 1) {
for (let j = 0; j < 4; ++j) {
let v = pad.axes[1 - (j >> 1)];
if (((j & 1) ? v : -v) > 0.5) {
gamepad |= 1 << (i * 8 + j);
}
}
}
if (pad.buttons.length > 9 && pad.buttons[9].pressed) {
restart = true;
}
}
let u32Mem = U32(memory.buffer);
u32Mem[16] = now - startTime;
u32Mem[17] = pad | gamepad;
instance.exports.upd();
platform_instance.exports.endFrame();
let palette = U32(memory.buffer.slice(0x13000, 0x13000 + 1024));
for (let i = 0; i < 320 * 240; ++i) {
buffer[i] = palette[memU8[i + 120]] | 0xff000000;
}
canvasCtx.putImageData(imageData, 0, 0);
nextFrame = Math.max(nextFrame + timePerFrame, now);
}
if (restart) {
runModule(currentData);
} else {
window.requestAnimationFrame(mainloop);
}
} catch (err) {
setMessage(cartridgeSize, err.toString());
}
}
mainloop();
} catch (err) {
setMessage(cartridgeSize, err.toString());
}
}
async function runModuleFromURL(url, keepUrl) {
runModule(await (await fetch(url)).arrayBuffer(), keepUrl);
}
function runModuleFromHash() { function runModuleFromHash() {
let hash = window.location.hash.slice(1); let hash = window.location.hash.slice(1);
if(hash == 'devkit') {
uw8.setDevkitMode(true);
return;
}
uw8.setDevkitMode(false);
if (hash.length > 0) { if (hash.length > 0) {
if (hash.startsWith("url=")) { if (hash.startsWith("url=")) {
runModuleFromURL(hash.slice(4), true); uw8.runModuleFromURL(hash.slice(4), true);
} else { } else {
runModuleFromURL('data:;base64,' + hash); uw8.runModuleFromURL('data:;base64,' + hash);
} }
} else { } else {
runModule(new ArrayBuffer(0)); uw8.runModule(new ArrayBuffer(0));
} }
} }
window.onhashchange = runModuleFromHash; window.onhashchange = runModuleFromHash;
runModuleFromHash();
document.getElementById('cartButton').onclick = () => { let setupLoad = () => {
let fileInput = document.createElement('input'); let loadButton = document.getElementById('cartButton');
fileInput.type = 'file'; loadButton.style = '';
fileInput.accept = '.wasm,.uw8,application/wasm'; loadButton.onclick = () => {
fileInput.onchange = () => { let fileInput = document.createElement('input');
if (fileInput.files.length > 0) { fileInput.type = 'file';
runModuleFromURL(URL.createObjectURL(fileInput.files[0])); fileInput.accept = '.wasm,.uw8,application/wasm';
} fileInput.onchange = () => {
if (fileInput.files.length > 0) {
uw8.runModuleFromURL(URL.createObjectURL(fileInput.files[0]));
}
};
fileInput.click();
}; };
fileInput.click();
};
screen.ondragover = (e) => { screen.ondragover = (e) => {
e.preventDefault();
};
screen.ondrop = (e) => {
let files = e.dataTransfer && e.dataTransfer.files;
if(files && files.length == 1) {
e.preventDefault(); e.preventDefault();
runModuleFromURL(URL.createObjectURL(e.dataTransfer.files[0])); };
screen.ondrop = (e) => {
let files = e.dataTransfer && e.dataTransfer.files;
if(files && files.length == 1) {
e.preventDefault();
uw8.runModuleFromURL(URL.createObjectURL(e.dataTransfer.files[0]));
}
} }
runModuleFromHash();
};
let location = window.location;
if(location.hash.length != 0) {
setupLoad();
} else {
(async () => {
let url = location.href;
if(url.endsWith('.html')) {
url = url.slice(0, url.length - 4) + 'uw8';
} else {
if(!url.endsWith('/')) {
url += '/';
}
url += 'cart.uw8';
}
try {
await uw8.runModuleFromURL(url, true);
} catch(e) {
setupLoad();
}
})();
} }

319
web/src/microw8.js Normal file
View File

@@ -0,0 +1,319 @@
import loaderUrl from "data-url:../../platform/bin/loader.wasm";
import platformUrl from "data-url:../../platform/bin/platform.uw8";
export default function MicroW8(screen, config = {}) {
if(!config.setMessage) {
config.setMessage = (s, e) => {
if(e) {
console.log('error: ' + e);
}
}
}
let canvasCtx = screen.getContext('2d');
let imageData = canvasCtx.createImageData(320, 240);
let devkitMode = config.devkitMode;
let cancelFunction;
let currentData;
let U8 = (d) => new Uint8Array(d);
let U32 = (d) => new Uint32Array(d);
let pad = 0;
let keyboardElement = config.keyboardElement == undefined ? screen : config.keyboardElement;
if(keyboardElement) {
let keyHandler = (e) => {
let isKeyDown = e.type == 'keydown';
let mask;
switch (e.code) {
case 'ArrowUp':
mask = 1;
break;
case 'ArrowDown':
mask = 2;
break;
case 'ArrowLeft':
mask = 4;
break;
case 'ArrowRight':
mask = 8;
break;
case 'KeyZ':
mask = 16;
break;
case 'KeyX':
mask = 32;
break;
case 'KeyA':
mask = 64;
break;
case 'KeyS':
mask = 128;
break;
case 'KeyR':
if (isKeyDown) {
runModule(currentData, true);
}
break;
case 'F9':
if(isKeyDown) {
screen.toBlob(blob => {
downloadBlob(blob, '.png');
});
}
e.preventDefault();
break;
case 'F10':
if(isKeyDown) {
recordVideo();
}
e.preventDefault();
break;
}
if (isKeyDown) {
pad |= mask;
} else {
pad &= ~mask;
}
};
keyboardElement.onkeydown = keyHandler;
keyboardElement.onkeyup = keyHandler;
}
async function runModule(data, keepUrl) {
if (cancelFunction) {
cancelFunction();
cancelFunction = null;
}
let cartridgeSize = data.byteLength;
config.setMessage(cartridgeSize);
if (cartridgeSize == 0) {
return;
}
currentData = data;
let newURL = window.location.pathname;
if (cartridgeSize <= 1024 && !keepUrl) {
let dataString = '';
for (let byte of U8(data)) {
dataString += String.fromCharCode(byte);
}
newURL += '#' + btoa(dataString);
if (newURL != window.location.pathname + window.location.hash) {
history.pushState(null, null, newURL);
}
}
screen.width = screen.width;
try {
let memSize = { initial: 4 };
if(!devkitMode) {
memSize.maximum = 4;
}
let memory = new WebAssembly.Memory({ initial: 4, maximum: devkitMode ? 16 : 4 });
let memU8 = U8(memory.buffer);
let importObject = {
env: {
memory
},
};
let loader;
let loadModuleData = (data) => {
if (loader && (!devkitMode || 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) => (await WebAssembly.instantiate(data, importObject)).instance;
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];
}
for (let i = 9; i < 64; ++i) {
importObject.env['reserved' + i] = () => { };
}
for (let i = 0; i < 16; ++i) {
importObject.env['g_reserved' + i] = 0;
}
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 = await instantiate(data);
let buffer = U32(imageData.data.buffer);
let startTime = Date.now();
let keepRunning = true;
cancelFunction = () => keepRunning = false;
const timePerFrame = 1000 / 60;
let nextFrame = startTime;
function mainloop() {
if (!keepRunning) {
return;
}
try {
let now = Date.now();
let restart = false;
if (now >= nextFrame) {
let gamepads = navigator.getGamepads();
let gamepad = 0;
for (let i = 0; i < 4; ++i) {
let pad = gamepads[i];
if (!pad) {
continue;
}
for (let j = 0; j < 8; ++j) {
let buttonIdx = (j + 12) % 16;
if (pad.buttons.length > buttonIdx && pad.buttons[buttonIdx].pressed) {
gamepad |= 1 << (i * 8 + j);
}
}
if (pad.axes.length > 1) {
for (let j = 0; j < 4; ++j) {
let v = pad.axes[1 - (j >> 1)];
if (((j & 1) ? v : -v) > 0.5) {
gamepad |= 1 << (i * 8 + j);
}
}
}
if (pad.buttons.length > 9 && pad.buttons[9].pressed) {
restart = true;
}
}
let u32Mem = U32(memory.buffer);
u32Mem[16] = now - startTime;
u32Mem[17] = pad | gamepad;
instance.exports.upd();
platform_instance.exports.endFrame();
let palette = U32(memory.buffer.slice(0x13000, 0x13000 + 1024));
for (let i = 0; i < 320 * 240; ++i) {
buffer[i] = palette[memU8[i + 120]] | 0xff000000;
}
canvasCtx.putImageData(imageData, 0, 0);
nextFrame = Math.max(nextFrame + timePerFrame, now);
}
if (restart) {
runModule(currentData);
} else {
window.requestAnimationFrame(mainloop);
}
} catch (err) {
config.setMessage(cartridgeSize, err.toString());
}
}
mainloop();
} catch (err) {
config.setMessage(cartridgeSize, err.toString());
}
}
function downloadBlob(blob, ext) {
let a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'microw8_' + new Date().toISOString() + ext;
a.click();
URL.revokeObjectURL(a.href);
}
let videoRecorder;
let videoStartTime;
function recordVideo() {
if(videoRecorder) {
videoRecorder.stop();
videoRecorder = null;
return;
}
videoRecorder = new MediaRecorder(screen.captureStream(), {
mimeType: 'video/webm',
videoBitsPerSecond: 25000000
});
let chunks = [];
videoRecorder.ondataavailable = e => {
chunks.push(e.data);
};
let timer = config.timerElement;
if(timer) {
timer.hidden = false;
timer.innerText = "00:00";
}
videoRecorder.onstop = () => {
if(timer) {
timer.hidden = true;
}
downloadBlob(new Blob(chunks, {type: 'video/webm'}), '.webm');
};
videoRecorder.start();
videoStartTime = Date.now();
function updateTimer() {
if(!videoStartTime) {
return;
}
if(timer) {
let duration = Math.floor((Date.now() - videoStartTime) / 1000);
timer.innerText = Math.floor(duration / 60).toString().padStart(2, '0') + ':' + (duration % 60).toString().padStart(2, '0');
}
setTimeout(updateTimer, 1000);
}
setTimeout(updateTimer, 1000);
}
async function runModuleFromURL(url, keepUrl) {
let response = await fetch(url);
let type = response.headers.get('Content-Type');
if(type && type.includes('html')) {
throw false;
}
runModule(await response.arrayBuffer(), keepUrl || devkitMode);
}
return {
runModule,
runModuleFromURL,
setDevkitMode: (m) => devkitMode = m,
};
}

46
web/src/run-web.css Normal file
View File

@@ -0,0 +1,46 @@
html, body, canvas {
padding: 0;
margin: 0;
background-color: #202024;
}
html {
height: 100%;
}
body {
height: 100%;
display: grid;
grid-template-rows: 1fr;
}
#screen {
align-self: center;
justify-self: center;
image-rendering: pixelated;
border: 4px solid #303040;
}
#message {
position: absolute;
width: calc(100% - 16px);
background-color: rgba(0, 0, 0, 0.4);
color: #c64;
padding: 8px;
font: bold 12pt sans-serif;
z-index: 2;
}
@media (min-width: 648px) and (min-height: 488px) {
#screen {
width: 640px;
height: 480px;
}
}
@media (min-width: 968px) and (min-height: 728px) {
#screen {
width: 960px;
height: 720px;
}
}

18
web/src/run-web.html Normal file
View File

@@ -0,0 +1,18 @@
<!doctype html>
<html>
<head>
<meta charset="utf8" />
<title>uw8-run</title>
<style>
@import "run-web.css";
</style>
</head>
<body>
<canvas id="screen" width="320" height="240" tabindex="1">
</canvas>
<div id="message"></div>
</body>
<script type="module">
import "./run-web.js";
</script>
</html>

19
web/src/run-web.js Normal file
View File

@@ -0,0 +1,19 @@
import MicroW8 from './microw8.js';
let uw8 = MicroW8(document.getElementById('screen'), {
setMessage: (_, err) => {
let elem = document.getElementById('message');
if(err) {
elem.innerText = err;
}
elem.hidden = !err;
}
});
let events = new EventSource('events');
events.onmessage = event => {
console.log(event.data);
if(event.data == 'L') {
uw8.runModuleFromURL('cart', true);
}
};
uw8.runModuleFromURL('cart', true);

View File

@@ -16,15 +16,19 @@ body {
grid-template-rows: 1fr 0fr; grid-template-rows: 1fr 0fr;
} }
#uw8 {
position: absolute;
}
#uw8 a {
font-size: 130%;
}
#centered { #centered {
align-self: center; align-self: center;
justify-self: center; justify-self: center;
} }
.footer {
color: #202420;
}
a { a {
color: #303630; color: #303630;
} }
@@ -44,6 +48,16 @@ a:hover {
cursor: none; cursor: none;
} }
#timer::before {
content: '';
display: inline-block;
width: 12px;
height: 12px;
border-radius: 6px;
background-color: red;
margin-right: 3px;
}
#message { #message {
margin-bottom: 8px; margin-bottom: 8px;
} }
@@ -69,7 +83,7 @@ button:active {
background-color: #504450; background-color: #504450;
} }
@media (min-width: 680px) and (min-height: 560px) { @media (min-width: 680px) and (min-height: 620px) {
#screen { #screen {
width: 640px; width: 640px;
height: 480px; height: 480px;
@@ -82,3 +96,16 @@ button:active {
height: 720px; height: 720px;
} }
} }
@media (width:640px) and (height:480px) {
#screen {
width: 640px;
height: 480px;
border: 0;
margin: 0;
}
body {
overflow: hidden;
}
}