(module (memory (export "m") 2 16) (global $x (mut i32) (i32.const 80)) (global $y (mut i32) (i32.const 45)) ;; Paint an RGBA pixel at (x,y). Out-of-bounds coordinates are ignored. (func $px (param $x i32) (param $y i32) (param $c i32) (if (i32.and (i32.and (i32.ge_s (local.get $x) (i32.const 0)) (i32.lt_s (local.get $x) (i32.const 160))) (i32.and (i32.ge_s (local.get $y) (i32.const 0)) (i32.lt_s (local.get $y) (i32.const 90)))) (then (i32.store (i32.shl (i32.add (i32.mul (local.get $y) (i32.const 160)) (local.get $x)) (i32.const 2)) (local.get $c))))) ;; ABI: f(frame, input, seed) -> framebuffer byte offset. (func (export "f") (param $frame i32) (param $input i32) (param $seed i32) (result i32) (local $i i32) (local $h i32) (local $dx i32) (local $dy i32) ;; First frame: a deterministic, nearly black sky with tiny seeded stars. (if (i32.eqz (local.get $frame)) (then (local.set $i (i32.const 0)) (loop $sky (local.set $h (i32.xor (i32.mul (local.get $i) (i32.const 1103515245)) (local.get $seed))) (i32.store (i32.shl (local.get $i) (i32.const 2)) (select (i32.const -723369985) ;; RGBA: 255,225,225,212 (i32.const -16444398) ;; RGBA: 18,18,5,255 (i32.eqz (i32.and (local.get $h) (i32.const 1023))))) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br_if $sky (i32.lt_u (local.get $i) (i32.const 14400)))))) ;; Seven input bits: arrows move, A blooms, B scatters, Start returns home. (if (i32.and (local.get $input) (i32.const 1)) (then (global.set $x (i32.sub (global.get $x) (i32.const 1))))) (if (i32.and (local.get $input) (i32.const 2)) (then (global.set $x (i32.add (global.get $x) (i32.const 1))))) (if (i32.and (local.get $input) (i32.const 4)) (then (global.set $y (i32.sub (global.get $y) (i32.const 1))))) (if (i32.and (local.get $input) (i32.const 8)) (then (global.set $y (i32.add (global.get $y) (i32.const 1))))) (if (i32.and (local.get $input) (i32.const 64)) (then (global.set $x (i32.const 80)) (global.set $y (i32.const 45)))) ;; Clamp the seed to the visible world. (if (i32.lt_s (global.get $x) (i32.const 1)) (then (global.set $x (i32.const 1)))) (if (i32.gt_s (global.get $x) (i32.const 158)) (then (global.set $x (i32.const 158)))) (if (i32.lt_s (global.get $y) (i32.const 1)) (then (global.set $y (i32.const 1)))) (if (i32.gt_s (global.get $y) (i32.const 88)) (then (global.set $y (i32.const 88)))) ;; Movement leaves a living green-gold trail. (call $px (global.get $x) (global.get $y) (i32.const -10832640)) ;; A: plant a nine-pixel flower. Its petals change with time. (if (i32.and (local.get $input) (i32.const 16)) (then (call $px (global.get $x) (global.get $y) (i32.const -18752)) (call $px (i32.sub (global.get $x) (i32.const 2)) (global.get $y) (i32.const -8846593)) (call $px (i32.add (global.get $x) (i32.const 2)) (global.get $y) (i32.const -8846593)) (call $px (global.get $x) (i32.sub (global.get $y) (i32.const 2)) (i32.const -8846593)) (call $px (global.get $x) (i32.add (global.get $y) (i32.const 2)) (i32.const -8846593)) (call $px (i32.sub (global.get $x) (i32.const 1)) (i32.sub (global.get $y) (i32.const 1)) (i32.const -32385)) (call $px (i32.add (global.get $x) (i32.const 1)) (i32.sub (global.get $y) (i32.const 1)) (i32.const -32385)) (call $px (i32.sub (global.get $x) (i32.const 1)) (i32.add (global.get $y) (i32.const 1)) (i32.const -32385)) (call $px (i32.add (global.get $x) (i32.const 1)) (i32.add (global.get $y) (i32.const 1)) (i32.const -32385)))) ;; B: cast eight sparks farther into the dark. (if (i32.and (local.get $input) (i32.const 32)) (then (local.set $i (i32.const 0)) (loop $sparks (local.set $h (i32.add (i32.mul (local.get $frame) (i32.const 17)) (i32.mul (local.get $i) (i32.const 73)))) (local.set $dx (i32.sub (i32.and (local.get $h) (i32.const 15)) (i32.const 7))) (local.set $dy (i32.sub (i32.and (i32.shr_u (local.get $h) (i32.const 4)) (i32.const 15)) (i32.const 7))) (call $px (i32.add (global.get $x) (local.get $dx)) (i32.add (global.get $y) (local.get $dy)) (i32.const -18752)) (local.set $i (i32.add (local.get $i) (i32.const 1))) (br_if $sparks (i32.lt_u (local.get $i) (i32.const 8)))))) ;; A pulsing white cursor: the last seed itself. (call $px (global.get $x) (global.get $y) (select (i32.const -1) (i32.const -18752) (i32.eqz (i32.and (local.get $frame) (i32.const 8))))) (i32.const 0)) )