code wiki / _hdl_build / nx_pattern_emit_fabricnetlist_demo.nx

nx_pattern_emit_fabricnetlist_demo.nx source

↩ module page · 60 lines · 4491 B

1// nx_pattern_emit_fabricnetlist_demo.nx -- DEMO/GATE: the Builder runs the FABRIC_NETLIST emitter on a spec and it 2// AUTHORS a working W-bit LUT4 fabric builder (shape 25, no Claude logic). Spec: W=4, lut=32768 (AND at index 15). 3// T1 author succeeds for a valid spec (W=4) and REFUSES a bad spec (W=0). 4// T2 the AUTHORED module is real fabric code: contains _build + the baked lut '32768' + 'po[i]=npi+i'. 5// T3 determinism: re-authoring -> byte-identical module. 6// (Building fn_authored_test via the runner then proves the AUTHORED fabric COMPILES, runs on fab_eval, + PASSES.) 7// expect_exit: 0 license_tier: ORIGINAL 8import "nx_pattern_emit_fabricnetlist.nx" 9import "nx_syscalls.nx" 10const K_MAGIC_65536: i64 = 65536 11const K_MAGIC_16384: i64 = 16384 12const K_MAGIC_32768: i64 = 32768 13 14func dw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func dn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 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} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 16func read_all(path: *u8, lenbox: *i64) -> i64 { 17 let fd: i64=sys_openat_rd(path); if fd<0 { lenbox[0]=0; return 0 } 18 let buf: *u8=sys_mmap(K_MAGIC_65536); var off: i64=0; var go: i64=1 19 while go==1 { let r: i64=sys_read(fd, ((buf as i64)+off) as *u8, K_MAGIC_16384); if r<=0 { go=0 } else { off=off+r } } 20 sys_close(fd); lenbox[0]=off; return buf as i64 21} 22func has_sub(hay: *u8, hlen: i64, needle: *u8, nlen: i64) -> i64 { 23 var i: i64=0 24 while i+nlen<=hlen { var j: i64=0; var eq: i64=1; while j<nlen { if hay[i+j]!=needle[j] { eq=0; j=nlen } else { j=j+1 } } if eq==1 { return 1 } i=i+1 } 25 return 0 26} 27 28func main() -> i64 { 29 dw("=== nx_pattern_emit_fabricnetlist_demo: the Builder AUTHORS a FABRIC_NETLIST module from a spec (shape 25) ===\n" as *u8) 30 var pass: i64=0; var total: i64=0 31 let mp: *u8="runtime/_hdl_build/fn_authored.nx" as *u8 32 let tp: *u8="runtime/_hdl_build/fn_authored_test.nx" as *u8 33 let spec: *i64=sys_mmap(64) as *i64; spec[0]=4; spec[1]=K_MAGIC_32768 34 let ok: i64=pe_fn_author("fn_authored" as *u8, mp, tp, spec) 35 let badspec: *i64=sys_mmap(64) as *i64; badspec[0]=0; badspec[1]=K_MAGIC_32768 36 let bad: i64=pe_fn_author("fn_bad" as *u8, "runtime/_hdl_build/fn_bad.nx" as *u8, "runtime/_hdl_build/fn_bad_test.nx" as *u8, badspec) 37 38 total=total+1; if ok==1 { if bad==0 { pass=pass+1; dw(" [PASS] " as *u8) } else { dw(" [FAIL] " as *u8) } } else { dw(" [FAIL] " as *u8) } 39 dw("T1 AUTHOR+RAIL: valid spec authored=" as *u8); dn(ok); dw(", bad spec (W=0) refused=" as *u8); dn(1-bad); dw("\n" as *u8) 40 41 let lb: *i64=sys_mmap(16) as *i64; let buf: *i64=read_all(mp, lb) 42 let b: *u8=buf as *u8; let blen: i64=lb[0] 43 let h_build: i64=has_sub(b, blen, "_build(inits" as *u8, 12) 44 let h_lut: i64=has_sub(b, blen, "inits[i]=32768" as *u8, 14) 45 let h_po: i64=has_sub(b, blen, "po[i]=npi+i" as *u8, 11) 46 total=total+1; if h_build==1 { if h_lut==1 { if h_po==1 { pass=pass+1; dw(" [PASS] " as *u8) } else { dw(" [FAIL] " as *u8) } } else { dw(" [FAIL] " as *u8) } } else { dw(" [FAIL] " as *u8) } 47 dw("T2 AUTHORED REAL FABRIC: module=" as *u8); dn(blen); dw("B, _build=" as *u8); dn(h_build); dw(", baked lut '32768'=" as *u8); dn(h_lut); dw(", 'po[i]=npi+i'=" as *u8); dn(h_po); dw("\n" as *u8) 48 49 pe_fn_author("fn_authored" as *u8, "runtime/_hdl_build/fn_authored2.nx" as *u8, "runtime/_hdl_build/fn_authored2_test.nx" as *u8, spec) 50 let lb2: *i64=sys_mmap(16) as *i64; let buf2: *i64=read_all("runtime/_hdl_build/fn_authored2.nx" as *u8, lb2) 51 let b2: *u8=buf2 as *u8; var d: i64=0; if lb2[0]!=blen { d=1 } 52 var i: i64=0; while i<blen { if b[i]!=b2[i] { d=d+1 } i=i+1 } 53 total=total+1; if d==0 { pass=pass+1; dw(" [PASS] " as *u8) } else { dw(" [FAIL] " as *u8) } 54 dw("T3 DETERMINISM: re-authoring -> byte-identical module (diffs=" as *u8); dn(d); dw(")\n" as *u8) 55 56 dw("\n AUTONOMY CREDIT: shape 25 FABRIC_NETLIST is now TEAM-AUTHORABLE -- the Builder emits a LUT4 fabric, no Claude.\n" as *u8) 57 dw("EMIT-FABRICNETLIST-DEMO verdict=" as *u8) 58 if pass==total { dw("GREEN passes=" as *u8); dn(pass); dw("/" as *u8); dn(total); dw(" END\n" as *u8); sys_exit(0); return 0 } 59 dw("RED passes=" as *u8); dn(pass); dw("/" as *u8); dn(total); dw(" END\n" as *u8); sys_exit(1); return 1 60}