code wiki / _hdl_build / nx_vmtest_vm_gate.nx

nx_vmtest_vm_gate.nx source

↩ module page · 55 lines · 3152 B

1// nx_vmtest_vm_gate.nx -- isolate nx_wasm_vm's per-opcode correctness on known answers (task #21). Runs each tiny 2// exported bit-op through the VM and checks the exact result. If xor/not pass here, the VM's ops are sound and the 3// SHA-256 wasm discrepancy is a wat-backend lowering issue; if they fail, the VM opcode handler is the bug. 4import "nx_syscalls.nx" 5import "nx_wasm_vm.nx" 6import "nx_gate_verdict.nx" 7 8func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func g_pn(v: i64) -> i64 { 10 let b: *u8 = sys_mmap(28); var x: i64 = v 11 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x } 12 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 } 13 var d: i64=0; var y: i64=x 14 while y>0 { d=d+1; y=y/10 } 15 var i: i64=d-1; y=x 16 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 17 sys_write(1,b,d); return 0 18} 19func chk(mod: *WasmMod, name: *u8, a: i64, b: i64, na: i64, want: i64) -> i64 { 20 let got: i64 = wm_run(mod, name, a, b, 0, 0, 0, na) 21 g_puts(" " as *u8); g_puts(name); g_puts(" -> " as *u8); g_pn(got); g_puts(" (want " as *u8); g_pn(want); g_puts(")" as *u8) 22 if got == want { g_puts(" PASS\n" as *u8); return 1 } 23 g_puts(" FAIL\n" as *u8); return 0 24} 25 26func main() -> i64 { 27 g_puts("nx_wasm_vm opcode isolation (known-answer bit-ops via the VM)\n" as *u8) 28 let box: *i64 = sys_mmap(16) as *i64 29 let wasm: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/web_assets/_video_build/nx_vmtest.wasm" as *u8, box) 30 if (wasm as i64) == 0 { g_puts(" FAIL read\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 31 let mod: *WasmMod = wm_new(wasm, box[0]) 32 if wm_parse(mod) != 0 { g_puts(" FAIL parse\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 33 mod.mem = sys_mmap(65536) as *u8 34 35 var pass: i64 = 0; var total: i64 = 0 36 pass = pass + chk(mod, "vmt_and" as *u8, 255, 15, 2, 15); total=total+1 37 pass = pass + chk(mod, "vmt_or" as *u8, 240, 15, 2, 255); total=total+1 38 pass = pass + chk(mod, "vmt_shl" as *u8, 1, 4, 2, 16); total=total+1 39 pass = pass + chk(mod, "vmt_shr" as *u8, 256, 4, 2, 16); total=total+1 40 pass = pass + chk(mod, "vmt_sub" as *u8, 10, 3, 2, 7); total=total+1 41 pass = pass + chk(mod, "vmt_add" as *u8, 10, 3, 2, 13); total=total+1 42 pass = pass + chk(mod, "vmt_xor" as *u8, 255, 15, 2, 240); total=total+1 43 pass = pass + chk(mod, "vmt_not" as *u8, 0, 0, 1, 0 - 1); total=total+1 44 45 g_puts("---- vmtest VM gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 46 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 47 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 48 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 49 let ctr__dry: *i64 = gv_ctr() 50 ctr__dry[0] = pass 51 ctr__dry[1] = total 52 let rc__dry: i64 = gv_verdict("VMTEST-VM-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 53 sys_exit(rc__dry) 54 return rc__dry 55}