code wiki / _hdl_build / nx_simlab_gate.nx
nx_simlab_gate.nx source
↩ module page · 59 lines · 4111 B
1// nx_simlab_gate.nx -- NATIVE eyeball+physics verify of the browser sim-lab (base = mmap). This is the SAME
2// base-relative integer code that runs in the browser (base = 0 = sovereign WASM linear memory); with no browser
3// in-harness, the native render-to-PNG + physics asserts ARE the proof the wasm logic+raster are correct (the
4// established "verify wasm by native PNG" loop). Runs the orbit in leapfrog (must stay bounded + conserve energy)
5// then in Euler (must drift/spiral) and writes both frames to PNG. license_tier: ORIGINAL expect_exit: 0
6import "nx_syscalls.nx"
7import "nx_simlab.nx"
8import "nx_png.nx"
9
10func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 }
12func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 }
13func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x }
14
15func main() -> i64 {
16 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0
17 g_w("=== NX-SIMLAB native verify (base-relative; SAME code runs in sovereign wasm) ===\n")
18 let base: i64 = sys_mmap(1048576) as i64
19 let st: *i64 = (base + O_ST) as *i64
20
21 // ---- LEAPFROG ~6 orbits ----
22 init_impl(base)
23 var t: i64=0; while t<600 { tick_impl(base, 0); t=t+1 }
24 let r_lf: i64 = isqrt(st[0]*st[0]+st[1]*st[1])
25 let e0: i64 = st[6]; var ae0: i64=e0; if ae0<0 { ae0=0-ae0 }
26 let cur_lf: i64 = energy2(st[0],st[1],st[2],st[3]); var dev_lf: i64=cur_lf-e0; if dev_lf<0 { dev_lf=0-dev_lf }
27 let pm_lf: i64 = dev_lf*1000/ae0
28 let steps_lf: i64 = st[5]; let tc_lf: i64 = st[7]
29 render_impl(base)
30 write_png((base + O_FB) as *i64, W, H, "knowledge/nx_simlab_leapfrog.png" as *u8)
31 let fb: *i64 = (base + O_FB) as *i64
32 let star: i64 = fb[CY*W + CX]
33
34 // ---- EULER ~6 orbits ----
35 tick_impl(base, 2)
36 t=0; while t<600 { tick_impl(base, 0); t=t+1 }
37 let r_eu: i64 = isqrt(st[0]*st[0]+st[1]*st[1])
38 let cur_eu: i64 = energy2(st[0],st[1],st[2],st[3]); var dev_eu: i64=cur_eu-e0; if dev_eu<0 { dev_eu=0-dev_eu }
39 let pm_eu: i64 = dev_eu*1000/ae0
40 render_impl(base)
41 write_png((base + O_FB) as *i64, W, H, "knowledge/nx_simlab_euler.png" as *u8)
42
43 g_w(" leapfrog: steps="); g_n(steps_lf); g_w(" trail="); g_n(tc_lf); g_w(" r/SC*1000="); g_n(r_lf*1000/SC); g_w(" Edev_permille="); g_n(pm_lf); g_w("\n")
44 g_w(" euler: r/SC*1000="); g_n(r_eu*1000/SC); g_w(" Edev_permille="); g_n(pm_eu); g_w("\n")
45 g_w(" star pixel rgb="); g_n(star & 255); g_w(","); g_n((star>>8)&255); g_w(","); g_n((star>>16)&255); g_w(" -> knowledge/nx_simlab_{leapfrog,euler}.png\n")
46
47 var sim_ran: i64=0; if steps_lf >= 600*STEPS_PER_TICK { if tc_lf>0 { sim_ran=1 } }
48 g_row("SIM RUNS: leapfrog advanced the orbit by all steps and recorded a trajectory trail" as *u8, sim_ran, pass)
49 g_row("CONSERVATION (leapfrog): orbital energy stays bounded over ~6 orbits (deviation < 5%)" as *u8, (pm_lf < 50) as i64, pass)
50 g_row("ORBIT BOUNDED (leapfrog): final radius stays near the initial orbit (0.75..1.25 SC)" as *u8, ((r_lf > SC*3/4) as i64)*((r_lf < SC*5/4) as i64), pass)
51 g_row("EULER SPIRALS: forward-Euler drifts energy far more than leapfrog (>=3x), the naive instability" as *u8, (pm_eu > pm_lf*3) as i64, pass)
52 g_row("RASTER: the framebuffer is drawn -- central mass pixel is the star colour (non-empty frame)" as *u8, ((star & 255)>200) as i64, pass)
53 var iface: i64=0; if ww()==W { if hh()==H { if fb_off()==O_FB { iface=1 } } }
54 g_row("WASM-READY: init/tick/render/ww/hh/fb_off interface intact -- the exact base-relative code wasm runs" as *u8, iface, pass)
55
56 g_w("NX-SIMLAB-GATE rows=6 pass="); g_n(pass[0])
57 if pass[0]==6 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 }
58 g_w(" verdict=RED\n"); sys_exit(1); return 1
59}