Files
curlywas/examples/microw8/random.cwa

36 lines
867 B
Plaintext

import "env.memory" memory(4);
global mut randomState: i64 = 37i64;
fn random() -> i32 {
(random64() >> 32i64) as i32
}
fn random64() -> i64 {
let state: i64;
randomState = (state := (
state := randomState ^ (randomState #>> 12i64)
) ^ (state << 25i64)
) ^ (state #>> 27i64);
randomState * 0x2545f4914f6cdd1di64
}
fn randomf() -> f32 {
f32.reinterpret_i32(1065353216 | (random() #>> 9)) - 1 as f32
}
fn seed(s: i32) {
randomState = (s as i64 << 32i64) ^ ((63 - s) as i64);
randomState = random64();
randomState = random64();
}
export fn tic(time: i32) {
let i: i32;
loop pixels {
seed(i + (time / 10) * 320);
i?120 = (randomf() * 256 as f32 + time as f32 / 10 as f32) as i32 & 128;
branch_if (i := i + 1) < 320*240: pixels
}
}