code wiki / _hdl_build / nx_solarsim_html.nx

nx_solarsim_html.nx source

↩ module page · 37 lines · 4140 B

1// nx_solarsim_html.nx -- SOVEREIGN packager: the real N-body solar-system wasm -> self-contained .html that runs in 2// any browser. The emitted JS does ONLY: (1) BLIT our wasm framebuffer to the canvas via putImageData (copy bytes, 3// NOT canvas drawing), (2) drive the sim each animation frame (ex.tick(0)) and forward R (reset) / space (pause). 4// ALL gravity physics + integer rasterization live in our sovereign wasm (no JS logic, no canvas-draw, no float, 5// no 3rd-party). Honest copy: this is the real N-body solar system whose periods EMERGE from the physics and match 6// real measured planetary data (validated by nx_sim_validation_real_gate + nx_solarsim_gate). 7// usage: nx_solarsim_html <in.wasm> <out.html>. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_base64.nx" 10import "nx_wasmfit.nx" 11 12func hw(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 13 14func main(argc: i64, argv: *i64) -> i64 { 15 if argc < 3 { hw(2, "usage: nx_solarsim_html <in.wasm> <out.html>\n" as *u8); return 2 } 16 let wpath: *u8 = argv[1] as *u8 17 let hpath: *u8 = argv[2] as *u8 18 let wlen_p: *i64 = sys_mmap(8) as *i64 19 let wbytes: *u8 = sys_read_file(wpath, wlen_p) 20 if (wbytes as i64) == 0 { hw(2, "read wasm failed\n" as *u8); return 3 } 21 let wlen: i64 = wlen_p[0] 22 // The estate's ONE fit ruler (nx_wasmfit). This emitter does not go through gpe_build_s, where the 23 // guard used to live, so it is wired here explicitly; nx_wasmemit_guard_gate enforces that it stays. 24 if wf_guard(wbytes, wlen, "nx_solarsim_html" as *u8) == WF_REFUSE { return 5 } 25 let b64: *u8 = sys_mmap(wlen * 2 + 64) as *u8 26 let b64len: i64 = b64_encode(wbytes, wlen, b64) 27 28 let fd: i64 = sys_openat_wr(hpath, 0x1a4) 29 if fd < 0 { hw(2, "open html failed\n" as *u8); return 4 } 30 hw(fd, "<!doctype html><html><head><meta charset='utf-8'><title>Nishi Solar System (sovereign wasm)</title><style>body{background:#05070f;margin:0;display:flex;flex-direction:column;justify-content:center;align-items:center;height:100vh;font-family:sans-serif;color:#cdd}canvas{image-rendering:pixelated;width:512px;height:512px;border:1px solid #2a3140;background:#05070f}p{margin:6px}b{color:#8eb}</style></head><body><h2>Nishi Solar System &mdash; real N-body gravity (sovereign WASM, no float)</h2><canvas id='c'></canvas><p id='hud'><b>R</b> reset &middot; <b>space</b> pause</p><p style='color:#667;font-size:12px;max-width:520px;text-align:center'>100% Nishi: each planet (Mercury&ndash;Jupiter) is leapfrog-integrated under the Sun's gravity (a=-GM&middot;r/|r|&sup3;) with integer isqrt &mdash; all in sovereign WASM; the browser only blits our framebuffer. The orbital PERIODS are not scripted: they EMERGE from the physics and follow Kepler's 3rd law, matching real measured planetary periods to within a few percent. Radial scale is sqrt-compressed for visibility; physics &amp; periods are in true units.</p><script>\n" as *u8) 31 hw(fd, "const B='" as *u8) 32 sys_write(fd, b64, b64len) 33 hw(fd, "';\nconst bytes=Uint8Array.from(atob(B),c=>c.charCodeAt(0));\nWebAssembly.instantiate(bytes).then(r=>{const ex=r.instance.exports;const W=Number(ex.ww()),H=Number(ex.hh()),off=Number(ex.fb_off());const cv=document.getElementById('c');cv.width=W;cv.height=H;const ctx=cv.getContext('2d');const img=ctx.createImageData(W,H);function blit(){const mem=new Uint8Array(ex.memory.buffer);for(let i=0;i<W*H;i++){const b=off+i*8;img.data[i*4]=mem[b];img.data[i*4+1]=mem[b+1];img.data[i*4+2]=mem[b+2];img.data[i*4+3]=255;}ctx.putImageData(img,0,0);}let paused=false;ex.init();function frame(){if(!paused){ex.tick(BigInt(0));ex.render();blit();}requestAnimationFrame(frame);}ex.render();blit();requestAnimationFrame(frame);document.addEventListener('keydown',function(e){const k=e.key.toLowerCase();if(k=='r'){ex.tick(BigInt(3));}else if(k==' '){paused=!paused;e.preventDefault();}ex.render();blit();});}).catch(e=>{document.body.innerHTML+='<pre>'+e.message+'</pre>';});\n</script></body></html>\n" as *u8) 34 sys_close(fd) 35 hw(1, "wrote solarsim html\n" as *u8) 36 return 0 37}