diff --git a/plasma.cwa b/examples/microw8/plasma.cwa similarity index 100% rename from plasma.cwa rename to examples/microw8/plasma.cwa diff --git a/examples/microw8/random.cwa b/examples/microw8/random.cwa new file mode 100644 index 0000000..3e41ad0 --- /dev/null +++ b/examples/microw8/random.cwa @@ -0,0 +1,35 @@ +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 * 2685821657736338717i64 +} + +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 / 100) * 320); + i?120 = (randomf() * 256 as f32 + time as f32 / 10 as f32) as i32; + branch_if (i := i + 1) < 320*256: pixels + } +} diff --git a/technotunnel.cwa b/examples/microw8/technotunnel.cwa similarity index 100% rename from technotunnel.cwa rename to examples/microw8/technotunnel.cwa diff --git a/trainride.cwa b/examples/microw8/trainride.cwa similarity index 100% rename from trainride.cwa rename to examples/microw8/trainride.cwa diff --git a/examples/microw8/tunnel.cwa b/examples/microw8/tunnel.cwa new file mode 100644 index 0000000..3badbec --- /dev/null +++ b/examples/microw8/tunnel.cwa @@ -0,0 +1,14 @@ +import "env.memory" memory(4); +import "env.atan2" fn atan2(f32, f32) -> f32; + +export fn tic(time: i32) { + let i: i32; + loop screen { + let defer t = time as f32 / 10 as f32; + let defer x = (i % 320) as f32 - 160.1; + let defer y = (i / 320 - 128) as f32; + + i?120 = ((20000 as f32 / sqrt(x * x + y * y) + t) as i32 ^ (atan2(x, y) * 512 as f32 / 3.141 + t) as i32); + branch_if (i := i + 1) < 320*256: screen + } +} \ No newline at end of file diff --git a/warptunnel.cwa b/examples/microw8/warptunnel.cwa similarity index 100% rename from warptunnel.cwa rename to examples/microw8/warptunnel.cwa diff --git a/xorscroll.cwa b/examples/microw8/xorscroll.cwa similarity index 100% rename from xorscroll.cwa rename to examples/microw8/xorscroll.cwa diff --git a/examples/wasm4/skipahead.cwa b/examples/wasm4/skipahead.cwa new file mode 100644 index 0000000..e12496b --- /dev/null +++ b/examples/wasm4/skipahead.cwa @@ -0,0 +1,62 @@ +import "env.memory" memory(1); + +import "env.rect" fn rect(i32, i32, i32, i32); +import "env.oval" fn oval(i32, i32, i32, i32); + +global mut pz: f32 = 0.0; +global mut px: f32 = 2.0; +global mut py: f32 = 0.0; +global mut s: f32 = 0.0; +global mut f: f32 = 0.0; + +fn xorshift(state: i32) -> i32 { + 100902443 * ( + (state := + (state := state ^ (state << 13)) + ^ (state >> 17)) + ^ (state << 5) + ) +} + +export fn update() { + let y: i32; + loop lines { + let z = (200 as f32 / (y := y + 1) as f32 + pz) as i32; + let x = (xorshift(xorshift(xorshift(z))) & 3) as f32 / 2 as f32 - px; + let w = 6 as f32 / sqrt(z as f32); + let rx = 80 + (y as f32 * x) as i32; + let rw = (y as f32 * w) as i32; + + let defer c = ((z & 1) + 2) * 17; + ?20 = c; + rect(rx, y, rw, y / 9); + ?20 = c + 17; + rect(rx, y + 1, rw, y / 9); + + if y == 120 & py > 0.0 { + if x+w < 0.0 | x > 0.0 { + return; + } + py = 0.0; + s = 0.0; + f = 0.8; + } + + branch_if y < 160: lines; + }; + + ?20 = 50; + oval(80 - 11, 114 - 11 + py as i32, 22, 22); + ?20 = 17; + oval(80 - 6, 114 - 6 + py as i32, 6, 6); + let defer pad = ?22; + let defer control_speed = 0.03; + px = px + (((pad >> 5) & 1) - ((pad >> 4) & 1)) as f32 * control_speed; + if pad & 1 { + s = s - f - control_speed; + } + s = s + 0.1; + py = py + s; + pz = pz + 0.05; + f = f * 0.7; +} \ No newline at end of file diff --git a/random.cwa b/random.cwa deleted file mode 100644 index 0127ce6..0000000 --- a/random.cwa +++ /dev/null @@ -1,12 +0,0 @@ -import "env.memory" memory(4); -import "env.random" fn random() -> i32; -import "env.randomSeed" fn seed(i32); - -export fn tic(time: i32) { - let i: i32; - loop pixels { - seed(i + (time / 100) * 320); - i?120 = random(); - branch_if (i := i + 1) < 320*256: pixels - } -} diff --git a/src/emit.rs b/src/emit.rs index da87a8a..78611e7 100644 --- a/src/emit.rs +++ b/src/emit.rs @@ -619,8 +619,8 @@ fn emit_expression<'a>(ctx: &mut FunctionContext<'a>, expr: &'a ast::Expression) ast::Expr::Return { value } => { if let Some(value) = value { emit_expression(ctx, value); - ctx.function.instruction(&Instruction::Return); } + ctx.function.instruction(&Instruction::Return); } ast::Expr::Error => unreachable!(), } diff --git a/uw8loader.cwa b/uw8loader.cwa deleted file mode 100644 index cc8b56b..0000000 --- a/uw8loader.cwa +++ /dev/null @@ -1,64 +0,0 @@ -import "env.memory" memory(8); - -export fn load_uw8(module_start: i32, module_end: i32, base_start: i32, base_end: i32) -> i32 { - if ?module_start == 0 { - let defer length = module_end - module_start; - copy(base_end, module_start, length); - return base_end + length; - } - - copy(base_end, base_start, 8); - base_start = base_start + 8; - let dest = base_end + 8; - let src = module_start + 1; - - loop sections { - if src < module_end & (base_start >= base_end | ?src <= ?base_start) { - let defer length2 = copy_section(dest, src); - dest = dest + length2; - if base_start < base_end & ?src == ?base_start { - base_start = base_start + section_size(base_start); - } - src = src + length2; - branch sections; - } - - if base_start < base_end { - let defer length3 = copy_section(dest, base_start); - dest = dest + length3; - base_start = base_start + length3; - branch sections; - } - } - - dest -} - -fn section_size(ptr: i32) -> i32 { - let p = ptr + 1; - let l = 0; - let shift = 0; - loop size { - let defer b = ?p; - l = l | ((b & 127) << shift); - shift = shift + 7; - p = p + 1; - branch_if b & 128: size; - } - p - ptr + l -} - -fn copy_section(dest: i32, src: i32) -> i32 { - let defer length = section_size(src); - copy(dest, src, length); - length -} - -fn copy(dest: i32, src: i32, len: i32) { - if len > 0 { - loop bytes { - ?(dest + (len := len - 1)) = ?(src + len); - branch_if len: bytes - } - } -} \ No newline at end of file