mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
Compare commits
3 Commits
b7e8ddf0f1
...
26206a312a
| Author | SHA1 | Date | |
|---|---|---|---|
| 26206a312a | |||
| 6a75988489 | |||
| 397ff19d80 |
54
Cargo.lock
generated
54
Cargo.lock
generated
@@ -674,6 +674,15 @@ version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
@@ -698,6 +707,12 @@ version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "id-arena"
|
||||
version = "2.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.7.0"
|
||||
@@ -1529,6 +1544,12 @@ version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.9"
|
||||
@@ -1585,6 +1606,7 @@ dependencies = [
|
||||
"pbr",
|
||||
"pico-args",
|
||||
"upkr",
|
||||
"walrus",
|
||||
"wasm-encoder",
|
||||
"wasmparser 0.81.0",
|
||||
]
|
||||
@@ -1618,6 +1640,32 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[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]]
|
||||
name = "wasi"
|
||||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
@@ -1687,6 +1735,12 @@ dependencies = [
|
||||
"leb128",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasmparser"
|
||||
version = "0.77.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b35c86d22e720a07d954ebbed772d01180501afe7d03d464f413bb5f8914a8d6"
|
||||
|
||||
[[package]]
|
||||
name = "wasmparser"
|
||||
version = "0.80.2"
|
||||
|
||||
@@ -13,7 +13,11 @@ data 0x20000 {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
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 && \
|
||||
uw8 filter-exports tunnel.wasm tunnel.wasm && \
|
||||
wasm-opt -Oz -o tunnel.wasm tunnel.wasm
|
||||
|
||||
@@ -5,8 +5,8 @@ A nightly rust compiler is needed for the unstable sqrtf32
|
||||
intrinsic.
|
||||
|
||||
Simply compiling with rustc as shown in build.sh results in a
|
||||
342 byte tunnel.wasm. Using wasm-opt this can be reduced to
|
||||
243 bytes.
|
||||
371 byte tunnel.wasm. Using wasm-opt this can be reduced to
|
||||
260 bytes.
|
||||
|
||||
When you disassemble this wasm file using wasm2wat you can see
|
||||
these globals and exports:
|
||||
@@ -18,8 +18,10 @@ these globals and exports:
|
||||
|
||||
They are meant to be used for heap allocations and stack for any
|
||||
values that are not simple scalars (i32, f32, etc.). Since our
|
||||
code doesn't actually use any of that, we can just delete them
|
||||
in a text editor and assemble the code again with wat2wasm.
|
||||
code doesn't actually use any of that, the globals are only
|
||||
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
|
||||
uw8-tool pack brings us to the final size of 137 bytes.
|
||||
This gives us a 216 byte wasm file. Running this through
|
||||
uw8 pack brings us to the final size of 119 bytes.
|
||||
@@ -9,12 +9,20 @@ mod env {
|
||||
extern "C" {
|
||||
pub fn atan2(x: f32, y: f32) -> f32;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
pub fn time() -> f32;
|
||||
}
|
||||
}
|
||||
|
||||
fn atan2(x: f32, y: f32) -> f32 {
|
||||
unsafe { env::atan2(x, y) }
|
||||
}
|
||||
|
||||
fn time() -> f32 {
|
||||
unsafe { env::time() }
|
||||
}
|
||||
|
||||
fn sqrt(v: f32) -> f32 {
|
||||
unsafe { core::intrinsics::sqrtf32(v) }
|
||||
}
|
||||
@@ -29,14 +37,14 @@ fn ftoi(v: f32) -> i32 {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn tic(time: i32) {
|
||||
pub fn upd() {
|
||||
for i in 0..320 * 240 {
|
||||
let t = time as f32 / 10 as f32;
|
||||
let t = time() * 63 as f32;
|
||||
let x = (i % 320 - 160) as f32;
|
||||
let y = (i / 320 - 120) as f32;
|
||||
let d = 40000 as f32 / sqrt(x * x + y * y + 1 as f32);
|
||||
let u = atan2(x, y) * 512f32 / 3.141;
|
||||
let c = (ftoi(d + t * 2 as f32) ^ ftoi(u + t)) as u8;
|
||||
let c = (ftoi(d + t * 2 as f32) ^ ftoi(u + t)) as u8 >> 4;
|
||||
unsafe {
|
||||
*((120 + i) as *mut u8) = c;
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -326,7 +326,7 @@ fn printSingleChar(char: i32) {
|
||||
|
||||
if char == 4 | char == 5 {
|
||||
graphicsText = char == 5;
|
||||
textCursorX = 0;
|
||||
textCursorX = 0;
|
||||
textCursorY = 0;
|
||||
return;
|
||||
}
|
||||
@@ -341,6 +341,9 @@ fn printSingleChar(char: i32) {
|
||||
}
|
||||
|
||||
if char == 9 {
|
||||
if !graphicsText & textCursorX >= 320 {
|
||||
printChar(0xd0a);
|
||||
}
|
||||
textCursorX = textCursorX + 8;
|
||||
return;
|
||||
}
|
||||
@@ -406,6 +409,10 @@ fn printSingleChar(char: i32) {
|
||||
return;
|
||||
}
|
||||
|
||||
if char < 31 {
|
||||
return;
|
||||
}
|
||||
|
||||
drawChar(char);
|
||||
}
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ possible but no examples are provided, yet.
|
||||
|
||||
## Examples
|
||||
* [Fireworks](v0.1pre5#AgwvgP+M59snqjl4CMKw5sqm1Zw9yJCbSviMjeLUdHus2a3yl/a99+uiBeqZgP/2jqSjrLjRk73COMM6OSLpsxK8ugT1kuk/q4hQUqqPpGozHoa0laulzGGcahzdfdJsYaK1sIdeIYS9M5PnJx/Wk9H+PvWEPy2Zvv7I6IW7Fg==) (127 bytes): Some fireworks to welcome 2022.
|
||||
* [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
|
||||
* [Skip Ahead](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](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](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
|
||||
* [Font & Palette](v0.1pre4#AgKaeeOuwg5gCKvFIeiitEwMpUI2rymEcu+DDB1vMu9uBoufvUxIr4Y5p4Jj2ukoNO4PE7QS5cN1ZyDMCRfSzYIGZxKlN2J6NKEWK7KVPk9wVUgn1Ip+hsMinWgEO8ETKfPuHoIa4kjI+ULFOMad7vd3rt/lh1Vy9w+R2MXG/7T61d3c7C6KY+eQNS0eW3ys4iU8R6SycuWZuuZ2Sg3Qxp826s+Kt+2qBojpzNOSoyFqyrVyYMTKEkSl0BZOj59Cs1hPm5bq0F1MmVhGAzMhW9V4YeAe): Just a simple viewer for the default font and palette.
|
||||
|
||||
File diff suppressed because one or more lines are too long
21
src/main.rs
21
src/main.rs
@@ -19,16 +19,18 @@ fn main() -> Result<()> {
|
||||
match args.subcommand()?.as_ref().map(|s| s.as_str()) {
|
||||
Some("run") => run(args),
|
||||
Some("pack") => pack(args),
|
||||
Some(other) => {
|
||||
eprintln!("Unknown command '{}'", other);
|
||||
process::exit(1);
|
||||
}
|
||||
None => {
|
||||
Some("filter-exports") => filter_exports(args),
|
||||
Some("help") | 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>");
|
||||
println!(" uw8 filter-exports <in-wasm> <out-wasm>");
|
||||
Ok(())
|
||||
}
|
||||
Some(other) => {
|
||||
eprintln!("Unknown command '{}'", other);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,3 +155,12 @@ fn pack(mut args: Arguments) -> Result<()> {
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
100
uw8-tool/Cargo.lock
generated
100
uw8-tool/Cargo.lock
generated
@@ -56,6 +56,21 @@ dependencies = [
|
||||
"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]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
@@ -74,6 +89,15 @@ version = "0.2.112"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
@@ -101,6 +125,24 @@ version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "sacabase"
|
||||
version = "2.0.0"
|
||||
@@ -110,6 +152,17 @@ dependencies = [
|
||||
"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]]
|
||||
name = "time"
|
||||
version = "0.1.44"
|
||||
@@ -121,6 +174,18 @@ dependencies = [
|
||||
"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]]
|
||||
name = "upkr"
|
||||
version = "0.1.0"
|
||||
@@ -140,8 +205,35 @@ dependencies = [
|
||||
"pbr",
|
||||
"pico-args",
|
||||
"upkr",
|
||||
"walrus",
|
||||
"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]]
|
||||
@@ -159,6 +251,12 @@ dependencies = [
|
||||
"leb128",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasmparser"
|
||||
version = "0.77.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b35c86d22e720a07d954ebbed772d01180501afe7d03d464f413bb5f8914a8d6"
|
||||
|
||||
[[package]]
|
||||
name = "wasmparser"
|
||||
version = "0.81.0"
|
||||
|
||||
@@ -8,6 +8,7 @@ edition = "2021"
|
||||
[dependencies]
|
||||
wasmparser = "0.81"
|
||||
wasm-encoder = "0.8"
|
||||
walrus = "0.19"
|
||||
anyhow = "1"
|
||||
pico-args = "0.4"
|
||||
upkr = { git = "https://github.com/exoticorn/upkr.git", rev = "2e7983fc" }
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
mod base_module;
|
||||
mod pack;
|
||||
mod filter_exports;
|
||||
|
||||
pub use base_module::BaseModule;
|
||||
pub use pack::{pack, pack_file, unpack, unpack_file, PackConfig};
|
||||
pub use filter_exports::filter_exports;
|
||||
|
||||
@@ -27,6 +27,11 @@ fn main() -> Result<()> {
|
||||
let dest: PathBuf = args.free_from_str()?;
|
||||
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);
|
||||
print_help();
|
||||
@@ -44,6 +49,7 @@ fn print_help() {
|
||||
"Usage:
|
||||
uw8-tool make-base <version>
|
||||
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>"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user