code wiki / _hdl_build / nx_look_compose_gate.nx

nx_look_compose_gate.nx source

↩ module page · 182 lines · 8348 B

1// nx_look_compose_gate.nx -- pixel-truth gate for the sovereign LOOK composer (2026-07-29). 2// Builds three tiny synthetic panes IN MEMORY, writes them as BMPs, runs the real composer twice 3// (full strip + 2x crop), reads the composites back and asserts: 4// T1 full strip: pane0==src, pane1==a, pane2==b pixel-for-pixel; gutters + row-pad are WHITE 5// T2 crop+2x: every 2x2 output block == its source pixel (nearest, no resampling opinions) 6// T3 (negative) dim-mismatched inputs are REFUSED (nonzero rc), output absent 7// The composer is forked as a real process (the gate tests the SHIPPED path, not a code copy). 8// license: ORIGINAL 9import "nx_syscalls.nx" 10 11const GW2: i64 = 16 12const GH2: i64 = 8 13const GUT: i64 = 4 14const HDRB: i64 = 1078 15 16func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func gn(v: i64) -> i64 { 18 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 19 let t: *u8=sys_mmap(28); 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} 20 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 21func wr32le(b: *u8, off: i64, v: i64) -> i64 { 22 b[off]=(v&0xff) as u8; b[off+1]=((v>>8)&0xff) as u8 23 b[off+2]=((v>>16)&0xff) as u8; b[off+3]=((v>>24)&0xff) as u8; return 0 } 24func wr16le(b: *u8, off: i64, v: i64) -> i64 { 25 b[off]=(v&0xff) as u8; b[off+1]=((v>>8)&0xff) as u8; return 0 } 26func rd32le(b: *u8, off: i64) -> i64 { 27 return (b[off]&0xff) + ((b[off+1]&0xff)<<8) + ((b[off+2]&0xff)<<16) + ((b[off+3]&0xff)<<24) } 28 29func bmp_write(path: *u8, luma: *u8, w: i64, h: i64) -> i64 { 30 let isz: i64 = w*h 31 let fsz: i64 = HDRB + isz 32 let buf: *u8 = sys_mmap(fsz + 64) 33 buf[0]=66 as u8; buf[1]=77 as u8 34 wr32le(buf, 2, fsz); wr32le(buf, 6, 0); wr32le(buf, 10, HDRB) 35 wr32le(buf, 14, 40); wr32le(buf, 18, w); wr32le(buf, 22, h) 36 wr16le(buf, 26, 1); wr16le(buf, 28, 8) 37 wr32le(buf, 30, 0); wr32le(buf, 34, isz) 38 wr32le(buf, 38, 2835); wr32le(buf, 42, 2835) 39 wr32le(buf, 46, 256); wr32le(buf, 50, 0) 40 var c: i64=0 41 while c<256 { let o: i64=54+c*4 42 buf[o]=c as u8; buf[o+1]=c as u8; buf[o+2]=c as u8; buf[o+3]=0 as u8; c=c+1 } 43 var y: i64=0 44 while y<h { var x: i64=0 45 while x<w { buf[HDRB + (h-1-y)*w + x] = luma[y*w + x]; x=x+1 } y=y+1 } 46 let fd: i64 = sys_openat_wr(path, 0x1a4) 47 if fd < 0 { return 1 } 48 sys_write(fd, buf, fsz) 49 sys_close(fd) 50 return 0 } 51 52func bmp_read(path: *u8, wh: *i64) -> *u8 { 53 let box: *i64 = sys_mmap(16) as *i64 54 let d: *u8 = sys_read_file(path, box) 55 if (d as i64) == 0 { return 0 as *u8 } 56 let off: i64 = rd32le(d, 10) 57 let w: i64 = rd32le(d, 18) 58 let h: i64 = rd32le(d, 22) 59 let out: *u8 = sys_mmap(w*h + 64) 60 var y: i64 = 0 61 while y < h { var x: i64 = 0 62 while x < w { out[y*w + x] = d[off + (h-1-y)*w + x]; x = x + 1 } y = y + 1 } 63 wh[0] = w; wh[1] = h 64 return out } 65 66func run_composer(a1: *u8, a2: *u8, a3: *u8, a4: *u8, cx: i64, cy: i64, cw: i64, ch: i64) -> i64 { 67 let av: *i64 = sys_mmap(8*12) as *i64 68 let nb: *u8 = sys_mmap(256) 69 av[0] = "_offc/nx_look_compose.elf" as *u8 as i64 70 av[1] = a1 as i64; av[2] = a2 as i64; av[3] = a3 as i64; av[4] = a4 as i64 71 var na: i64 = 5 72 if cw > 0 { 73 // render the four ints as separate NUL strings inside nb 74 var off: i64 = 0 75 var vi: i64 = 0 76 while vi < 4 { 77 var v: i64 = cx 78 if vi == 1 { v = cy } 79 if vi == 2 { v = cw } 80 if vi == 3 { v = ch } 81 av[na] = ((nb as i64) + off) as i64 82 if v == 0 { nb[off] = 48 as u8; off = off + 1 } 83 let t: *u8 = sys_mmap(28) 84 var k: i64 = 0 85 while v > 0 { t[k] = (48 + (v % 10)) as u8; v = v / 10; k = k + 1 } 86 while k > 0 { k = k - 1; nb[off] = t[k]; off = off + 1 } 87 nb[off] = 0 as u8; off = off + 1 88 na = na + 1; vi = vi + 1 89 } 90 } 91 av[na] = 0 92 let envp: *i64 = sys_mmap(16) as *i64 93 envp[0] = 0 94 let pid: i64 = sys_fork() 95 if pid == 0 { sys_execve("_offc/nx_look_compose.elf" as *u8, av, envp); sys_exit(127) } 96 let st: *i64 = sys_mmap(16) as *i64 97 sys_wait4(pid, st, 0) 98 return (st[0] >> 8) & 0xff } 99 100func main() -> i64 { 101 gw("=== nx_look_compose_gate: pixel-truth teeth for the sovereign LOOK composer ===\n" as *u8) 102 var pass: i64 = 0 103 var fail: i64 = 0 104 // synthetic panes: src = gradient, a = inverted, b = checker 105 let ps: *u8 = sys_mmap(GW2*GH2 + 16) 106 let pa: *u8 = sys_mmap(GW2*GH2 + 16) 107 let pb: *u8 = sys_mmap(GW2*GH2 + 16) 108 var y: i64 = 0 109 while y < GH2 { var x: i64 = 0 110 while x < GW2 { 111 ps[y*GW2+x] = ((x*16 + y*2) % 256) as u8 112 pa[y*GW2+x] = (255 - ((x*16 + y*2) % 256)) as u8 113 var v: i64 = 0 114 if ((x+y) % 2) == 1 { v = 200 } 115 pb[y*GW2+x] = v as u8 116 x = x + 1 } y = y + 1 } 117 bmp_write("_build/_lcg_s.bmp" as *u8, ps, GW2, GH2) 118 bmp_write("_build/_lcg_a.bmp" as *u8, pa, GW2, GH2) 119 bmp_write("_build/_lcg_b.bmp" as *u8, pb, GW2, GH2) 120 // T1: full strip 121 let rc1: i64 = run_composer("_build/_lcg_s.bmp" as *u8, "_build/_lcg_a.bmp" as *u8, "_build/_lcg_b.bmp" as *u8, "_build/_lcg_full.bmp" as *u8, 0, 0, 0, 0) 122 let wh: *i64 = sys_mmap(32) as *i64 123 var t1: i64 = 0 124 if rc1 == 0 { 125 let cm: *u8 = bmp_read("_build/_lcg_full.bmp" as *u8, wh) 126 let ow: i64 = wh[0] 127 let exw: i64 = (((GW2*3 + GUT*2) + 3) / 4) * 4 128 if ow == exw { if wh[1] == GH2 { 129 t1 = 1 130 var p: i64 = 0 131 while p < 3 { 132 var ref: *u8 = ps 133 if p == 1 { ref = pa } 134 if p == 2 { ref = pb } 135 let ox: i64 = p * (GW2 + GUT) 136 y = 0 137 while y < GH2 { var x: i64 = 0 138 while x < GW2 { if cm[y*ow + ox + x] != ref[y*GW2 + x] { t1 = 0 } x = x + 1 } y = y + 1 } 139 // gutter after panes 0,1 must be white 140 if p < 2 { y = 0 141 while y < GH2 { var g: i64 = 0 142 while g < GUT { if cm[y*ow + ox + GW2 + g] != (255 as u8) { t1 = 0 } g = g + 1 } y = y + 1 } } 143 p = p + 1 } 144 } } 145 } 146 if t1 == 1 { pass = pass + 1; gw(" T1 full strip pane+gutter pixel-truth: PASS\n" as *u8) } 147 else { fail = fail + 1; gw(" T1 full strip pane+gutter pixel-truth: FAIL\n" as *u8) } 148 // T2: crop 2x (crop 4,2 8x4) 149 let rc2: i64 = run_composer("_build/_lcg_s.bmp" as *u8, "_build/_lcg_a.bmp" as *u8, "_build/_lcg_b.bmp" as *u8, "_build/_lcg_crop.bmp" as *u8, 4, 2, 8, 4) 150 var t2: i64 = 0 151 if rc2 == 0 { 152 let cm2: *u8 = bmp_read("_build/_lcg_crop.bmp" as *u8, wh) 153 let ow2: i64 = wh[0] 154 if wh[1] == 8 { 155 t2 = 1 156 var p2: i64 = 0 157 while p2 < 3 { 158 var ref2: *u8 = ps 159 if p2 == 1 { ref2 = pa } 160 if p2 == 2 { ref2 = pb } 161 let ox2: i64 = p2 * (16 + GUT) 162 y = 0 163 while y < 8 { var x: i64 = 0 164 while x < 16 { 165 if cm2[y*ow2 + ox2 + x] != ref2[(2 + y/2)*GW2 + 4 + x/2] { t2 = 0 } 166 x = x + 1 } y = y + 1 } 167 p2 = p2 + 1 } 168 } 169 } 170 if t2 == 1 { pass = pass + 1; gw(" T2 crop+2x nearest pixel-truth: PASS\n" as *u8) } 171 else { fail = fail + 1; gw(" T2 crop+2x nearest pixel-truth: FAIL\n" as *u8) } 172 // T3 negative: mismatched dims REFUSED (a 8x8 pane vs 16x8 others) 173 let pm: *u8 = sys_mmap(64 + 16) 174 var i3: i64 = 0 175 while i3 < 64 { pm[i3] = 7 as u8; i3 = i3 + 1 } 176 bmp_write("_build/_lcg_m.bmp" as *u8, pm, 8, 8) 177 let rc3: i64 = run_composer("_build/_lcg_s.bmp" as *u8, "_build/_lcg_m.bmp" as *u8, "_build/_lcg_b.bmp" as *u8, "_build/_lcg_bad.bmp" as *u8, 0, 0, 0, 0) 178 if rc3 != 0 { pass = pass + 1; gw(" T3 dim-mismatch REFUSED (negative): PASS\n" as *u8) } 179 else { fail = fail + 1; gw(" T3 dim-mismatch REFUSED (negative): FAIL (rc=0!)\n" as *u8) } 180 gw("NX-LOOK-COMPOSE-GATE passed " as *u8); gn(pass); gw("/" as *u8); gn(pass+fail) 181 if fail == 0 { gw(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 182 gw(" verdict=RED\n" as *u8); sys_exit(1); return 1 }