Files
microw8/examples/rust/tunnel.rs
2022-01-01 15:51:18 +01:00

58 lines
1.4 KiB
Rust

#![no_std]
#![feature(core_intrinsics)]
mod env {
// "env" is the default module for imports, but it is still needed here
// since there is a compiler builtin of the same name which is used
// if we don't make it clear that this is a module import.
#[link(wasm_import_module = "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) }
}
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]
pub fn upd() {
for i in 0..320 * 240 {
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 >> 4;
unsafe {
*((120 + i) as *mut u8) = c;
}
}
}
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}