From fb3fd5e8bb2b87c7757240c6f4a8b326d30b4ecb Mon Sep 17 00:00:00 2001 From: Dennis Ranke Date: Sun, 16 Jun 2024 14:10:18 +0200 Subject: [PATCH] add frame counter at memory location 72 --- examples/curlywas/tunnel.cwa | 16 +++++++++------- site/content/docs.md | 2 +- src/run-web.html | 2 +- src/run_native.rs | 5 +++++ web/src/microw8.js | 2 ++ 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/curlywas/tunnel.cwa b/examples/curlywas/tunnel.cwa index d76f61b..8d6bdb9 100644 --- a/examples/curlywas/tunnel.cwa +++ b/examples/curlywas/tunnel.cwa @@ -3,14 +3,16 @@ include "../include/microw8-api.cwa" export fn upd() { let i: i32; loop pixels { - let inline t = time() * 63 as f32; - let lazy x = (i % 320 - 160) as f32; - let lazy y = (i / 320 - 120) as f32; - let inline d = 40000 as f32 / sqrt(x * x + y * y); - let inline u = atan2(x, y) * (512.0 / 3.141); - let inline c = ((i32.trunc_sat_f32_s(d + t * 2 as f32) ^ i32.trunc_sat_f32_s(u + t)) & 255) >> 4; + let inline t = 16!56; + let inline x = (i % 320 - 160) as f32; + let inline y = (i #/ 320 - 120) as f32; + let inline d = 0xa000 as f32 / sqrt(x * x + y * y); + let inline a = atan2(x, y) * 163_f; // (512 / pi) + let inline u = i32.trunc_sat_f32_s(a) + t; + let inline v = i32.trunc_sat_f32_s(d) + t * 2; + let inline c = ((v ^ u) #/ 16) % 16; i?FRAMEBUFFER = c; branch_if (i := i + 1) < 320*240: pixels; } -} \ No newline at end of file +} diff --git a/site/content/docs.md b/site/content/docs.md index 5fa0e59..1e9e224 100644 --- a/site/content/docs.md +++ b/site/content/docs.md @@ -21,7 +21,7 @@ loaded. 00000-00040: user memory 00040-00044: time since module start in ms 00044-0004c: gamepad state -0004c-00050: reserved +0004c-00050: number of frames since module start 00050-00070: sound data (synced to sound thread) 00070-00078: reserved 00078-12c78: frame buffer diff --git a/src/run-web.html b/src/run-web.html index c56d30a..3fa6583 100644 --- a/src/run-web.html +++ b/src/run-web.html @@ -1 +1 @@ -uw8-run
\ No newline at end of file +uw8-run
\ No newline at end of file diff --git a/src/run_native.rs b/src/run_native.rs index 7d8b3c7..d762526 100644 --- a/src/run_native.rs +++ b/src/run_native.rs @@ -27,6 +27,7 @@ struct UW8Instance { end_frame: TypedFunc<(), ()>, update: Option>, start_time: Instant, + frame_counter: u32, watchdog: Arc>, sound_tx: Option>, } @@ -159,6 +160,7 @@ impl super::Runtime for MicroW8 { end_frame, update, start_time: Instant::now(), + frame_counter: 0, watchdog, sound_tx, }); @@ -191,8 +193,11 @@ impl super::Runtime for MicroW8 { let mem = instance.memory.data_mut(&mut instance.store); mem[64..68].copy_from_slice(&time.to_le_bytes()); mem[68..72].copy_from_slice(&input.gamepads); + mem[72..76].copy_from_slice(&instance.frame_counter.to_le_bytes()); } + instance.frame_counter = instance.frame_counter.wrapping_add(1); + instance.store.set_epoch_deadline(self.timeout as u64); if let Some(ref update) = instance.update { if let Err(err) = update.call(&mut instance.store, ()) { diff --git a/web/src/microw8.js b/web/src/microw8.js index dcdc845..a044783 100644 --- a/web/src/microw8.js +++ b/web/src/microw8.js @@ -240,6 +240,7 @@ export default function MicroW8(screen, config = {}) { await audioReadyPromise; let startTime = Date.now(); + let frameCounter = 0; const timePerFrame = 1000 / 60; @@ -306,6 +307,7 @@ export default function MicroW8(screen, config = {}) { let time = Date.now() - startTime; u32Mem[16] = time; u32Mem[17] = pad | gamepad; + u32Mem[18] = frameCounter++; if(instance.exports.upd) { instance.exports.upd(); }