code wiki / _hdl_build / nx_vcodec_color_wasm_vm_gate.nx
nx_vcodec_color_wasm_vm_gate.nx source
↩ module page · 63 lines · 4055 B
1// nx_vcodec_color_wasm_vm_gate.nx -- SOVEREIGN VM byte-exact proof of the COLOR wasm codec. Loads the
2// freshly-built nx_vcodec_color.wasm, runs vc_color_wasm_encode/decode in the ecosystem's OWN wasm VM, and
3// demands the decoded RGB + the bit count match the NATIVE color codec (nx_vcodec_color_codec) byte-for-bit.
4// This is the H9 codec-half proof for COLOR -- the same de-risk nx_vcodec_inter_vm_gate did for luma. No
5// deploy, no live touch. expect_exit: 0 license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_gate_emit_lib.nx"
8import "nx_wasm_vm.nx"
9import "nx_vcodec_color_codec.nx"
10
11const COLOR_WASM: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/web_assets/_video_build/nx_vcodec_color.wasm"
12const O_RGB: i64 = 0
13const O_RGBOUT: i64 = 245760
14
15func main() -> i64 {
16 g_puts("nx_vcodec_color wasm-VM gate (COLOR encode/decode VM==native byte-exact)\n" as *u8)
17 var pass: i64 = 0; var total: i64 = 0
18
19 let box: *i64 = sys_mmap(16) as *i64
20 let wasm: *u8 = sys_read_file(COLOR_WASM, box)
21 if (wasm as i64) == 0 { g_puts(" FAIL cannot read nx_vcodec_color.wasm\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 }
22 let mod: *WasmMod = wm_new(wasm, box[0])
23 if wm_parse(mod) != 0 { g_puts(" FAIL parse\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 }
24 mod.mem = sys_mmap(mod.mem_bytes) as *u8
25
26 let W: i64 = 32; let H: i64 = 32; let N: i64 = W*H; let QP: i64 = 20; let SAD: i64 = 64
27
28 // smooth color frame (identical to the native color codec gate)
29 let rgb: *u8 = sys_mmap(N*3)
30 var y: i64 = 0
31 while y < H { var x: i64 = 0
32 while x < W { let p: i64=(y*W+x)*3; rgb[p]=(40+x*5) as u8; rgb[p+1]=(50+y*5) as u8; rgb[p+2]=128 as u8; x=x+1 } y=y+1 }
33
34 // ---- NATIVE authority ----
35 let strY: *u8 = sys_mmap(8192); let strCb: *u8 = sys_mmap(8192); let strCr: *u8 = sys_mmap(8192)
36 let bits: *i64 = sys_mmap(3*8) as *i64
37 let nbits: i64 = vc_color_encode_key(rgb, W, H, QP, SAD, strY, strCb, strCr, bits)
38 let nat_rgb: *u8 = sys_mmap(N*3)
39 vc_color_decode_key(W, H, QP, strY, strCb, strCr, nat_rgb)
40 g_puts(" [native authority done; nbits=" as *u8); g_pn(nbits); g_puts(" -- running VM (slow: interpreting the full codec)...]\n" as *u8)
41
42 // ---- VM: write RGB at O_RGB, run encode+decode, read RGB at O_RGBOUT ----
43 var i: i64 = 0
44 while i < N*3 { mod.mem[O_RGB + i] = rgb[i]; i = i + 1 }
45 let vbits: i64 = wm_run(mod, "vc_color_wasm_encode" as *u8, W, H, QP, 1, SAD, 5)
46 g_puts(" [VM encode done; vbits=" as *u8); g_pn(vbits); g_puts(" -- running VM decode...]\n" as *u8)
47 wm_run(mod, "vc_color_wasm_decode" as *u8, W, H, QP, 0, 0, 3)
48 var mm: i64 = 0
49 i = 0; while i < N*3 { if (mod.mem[O_RGBOUT + i] as i64) != (nat_rgb[i] as i64) { mm = mm + 1 } i = i + 1 }
50
51 g_puts(" color bits VM=" as *u8); g_pn(vbits); g_puts(" native=" as *u8); g_pn(nbits); g_puts(" decoded-RGB-mismatch-bytes=" as *u8); g_pn(mm); g_puts(" / " as *u8); g_pn(N*3); g_puts("\n" as *u8)
52 pass = pass + g_check("COLOR decoded RGB VM==native byte-exact (wat->wasm lowering faithful)" as *u8, mm == 0); total = total + 1
53 pass = pass + g_check("COLOR encode bit count VM==native" as *u8, (vbits == nbits) as i64); total = total + 1
54 pass = pass + g_check("the wasm codec really compressed (0 < bits < raw RGB)" as *u8, ((vbits > 0) as i64) * ((vbits < N*3*8) as i64)); total = total + 1
55
56 // accessor sanity: the wasm reports the same output offset the gate read from
57 let roff: i64 = wm_run(mod, "vc_color_rgbout_off" as *u8, 0, 0, 0, 0, 0)
58 pass = pass + g_check("wrapper RGBOUT offset accessor == gate's read offset" as *u8, (roff == O_RGBOUT) as i64); total = total + 1
59
60 g_puts("---- vcodec_color wasm-VM gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
61 if pass == total { g_puts("verdict=GREEN -- COLOR codec is wasm-faithful + compresses (H9 codec half is COLOR-ready)\n" as *u8); sys_exit(0); return 0 }
62 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
63}