code wiki / _hdl_build / nx_physlab_wasm_vet.nx

nx_physlab_wasm_vet.nx source

↩ module page · 95 lines · 4361 B

1// nx_physlab_wasm_vet.nx -- VET THE SHIPPED PHYSLAB BYTES: executes web_assets/nx_physlab.wasm on the 2// sovereign nx_wasm_vm against the native twin, step for step. PhysLab is ALL-INTEGER (no f32 anywhere), so 3// the ENTIRE surface vets byte-for-byte: 300 scripted ticks (mouse sweep + clicks + a reset) -> the physics 4// world, the jiggle world, AND a full rendered frame must be IDENTICAL vm vs native. A divergence here is 5// the exact thing that ships as a broken page. license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_wasm_physlab.nx" 8import "nx_wasm_vm.nx" 9const K_MAGIC_8256: i64 = 8256 10const K_MAGIC_614400: i64 = 614400 11const K_MAGIC_18496: i64 = 18496 12const K_MAGIC_721024: i64 = 721024 13 14func hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 15func pn(v: i64) -> i64 { 16 let b: *u8 = sys_mmap(32) as *u8 17 var x: i64 = v 18 var neg: i64 = 0 19 if x < 0 { neg = 1; x = 0 - x } 20 var i: i64 = 31 21 if x == 0 { b[i] = 48 as u8; i = i - 1 } 22 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 23 if neg == 1 { b[i] = 45 as u8; i = i - 1 } 24 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i) 25 return 0 26} 27 28func main() -> i64 { 29 var fails: i64 = 0 30 let lenp: *i64 = sys_mmap(16) as *i64 31 let wb: *u8 = sys_read_file("web_assets/nx_physlab.wasm" as *u8, lenp) 32 if (wb as i64) == 0 { hw("VET RED: cannot read web_assets/nx_physlab.wasm\n" as *u8); return 1 } 33 let mod: *WasmMod = wm_new(wb, lenp[0]) 34 if wm_parse(mod) != 0 { hw("VET RED: wasm parse failed\n" as *u8); return 1 } 35 hw("V1 parse OK bytes=" as *u8); pn(lenp[0]) 36 var missing: i64 = 0 37 if wm_find_export(mod, "start" as *u8) < 0 { missing = missing + 1 } 38 if wm_find_export(mod, "tick" as *u8) < 0 { missing = missing + 1 } 39 if wm_find_export(mod, "render" as *u8) < 0 { missing = missing + 1 } 40 if missing == 0 { hw(" exports OK\n" as *u8) } else { hw("\nV1 FAIL missing exports\n" as *u8); return 1 } 41 42 // native twin + identical scripted session on both 43 let nb: i64 = sys_mmap(mem_bytes()) as i64 44 start_at(nb) 45 wm_run(mod, "start" as *u8, 0, 0, 0, 0, 0, 0) 46 var t: i64 = 0 47 while t < 300 { 48 var mx: i64 = 40 + (t * 5) % 240 49 var my: i64 = 50 + (t * 3) % 120 50 var bt: i64 = 0 51 if (t % 71) == 30 { bt = 1 } 52 if t == 150 { bt = 2 } // mid-script RESET must also be identical 53 tick_at(nb, mx, my, bt) 54 wm_run(mod, "tick" as *u8, mx, my, bt, 0, 0, 3) 55 t = t + 1 56 } 57 // V2 physics world identical 58 var diff: i64 = 0 59 var k: i64 = 0 60 while k < K_MAGIC_8256 / 8 { 61 let pv: i64 = wm_ld64(mod.mem, K_MAGIC_614400 + k * 8) 62 let pn2: *i64 = (nb + K_MAGIC_614400) as *i64 63 if pv != pn2[k] { diff = diff + 1 } 64 k = k + 1 65 } 66 if diff == 0 { hw("V2 PASS 300-tick scripted session (sweep+clicks+RESET) -> physics world IDENTICAL vm vs native\n" as *u8) } 67 else { hw("V2 FAIL phys diff-slots=" as *u8); pn(diff); hw("\n" as *u8); fails = fails + 1 } 68 // V3 jiggle world identical 69 diff = 0 70 k = 0 71 while k < K_MAGIC_18496 / 8 { 72 let sv: i64 = wm_ld64(mod.mem, K_MAGIC_721024 + k * 8) 73 let sn: *i64 = (nb + K_MAGIC_721024) as *i64 74 if sv != sn[k] { diff = diff + 1 } 75 k = k + 1 76 } 77 if diff == 0 { hw("V3 PASS jiggle world (nodes+capsules) IDENTICAL vm vs native\n" as *u8) } 78 else { hw("V3 FAIL jiggle diff-slots=" as *u8); pn(diff); hw("\n" as *u8); fails = fails + 1 } 79 // V4 full rendered frame identical 80 render_at(nb) 81 wm_run(mod, "render" as *u8, 0, 0, 0, 0, 0, 0) 82 diff = 0 83 var pi: i64 = 0 84 let nfb: *i64 = (nb + fb_off()) as *i64 85 while pi < ww() * hh() { 86 if wm_ld64(mod.mem, fb_off() + pi * 8) != nfb[pi] { diff = diff + 1 } 87 pi = pi + 1 88 } 89 if diff == 0 { hw("V4 PASS full rendered FRAME byte-identical (" as *u8); pn(ww() * hh()); hw(" px) -- the whole surface vets (all-integer)\n" as *u8) } 90 else { hw("V4 FAIL frame diff px=" as *u8); pn(diff); hw("\n" as *u8); fails = fails + 1 } 91 92 if fails == 0 { hw("VET GREEN: the SHIPPED physlab wasm executes IDENTICALLY to native -- physics, jiggle, and pixels\n" as *u8); return 0 } 93 hw("VET RED fails=" as *u8); pn(fails); hw("\n" as *u8) 94 return fails 95}