code wiki / (root) / nx_gen_gate.nx

nx_gen_gate.nx source

↩ module page · 122 lines · 6562 B

1// nx_gen_gate.nx -- ★R5: the unified generator surface, proven. One nx_gen(spec,res) shape drives every 2// heterogeneous organ; params control the output; the registry self-describes; unknown kinds fail closed. 3// T1 UNIFORMITY: the SAME call produces all 5 kinds (world/tree/creature/figure/face), each ok + sane W/H/content 4// T2 CONTROLLABILITY: on one envelope, changing a param yields a measurably different asset (tree species, seed) 5// T3 REGISTRY: kind_count + a non-empty name & schema for every kind + an UNKNOWN kind fails closed (ok=0) 6// T4 DETERMINISM (same spec -> same bytes) + a contact-sheet PNG knowledge/nx_gen_contact.png (one asset/kind) 7// license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9import "nx_png.nx" 10import "nx_gen.nx" 11 12func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 14func sl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 15func L1(a: *i64, b: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { var d: i64=(a[i]&255)-(b[i]&255); if d<0{d=0-d} s=s+d; i=i+1 } return s } 16 17func main() -> i64 { 18 hw("=== nx_gen_gate -- R5: the unified sovereign generator surface ===\n" as *u8) 19 var fails: i64 = 0 20 let spec: *i64 = sys_mmap(8*8) as *i64 21 // res for all 5 kinds (6 fields each) 22 let R: *i64 = sys_mmap(GEN_NKIND*6*8) as *i64 23 24 // ---- T1 uniformity: one call shape -> every kind ---- 25 var okall: i64 = 1 26 var k: i64 = 0 27 while k < GEN_NKIND { 28 spec[0]=k; spec[1]=7; spec[2]=0; spec[3]=0 29 let res: *i64 = (R as i64 + k*6*8) as *i64 30 nx_gen(spec, res) 31 hw(" kind "); pn(k); hw(" '"); sys_write(1, gen_kind_name(k), sl(gen_kind_name(k))); hw("' -> ok="); pn(res[0]); hw(" "); pn(res[1]); hw("x"); pn(res[2]); hw(" content="); pn(res[3]); hw("\n" as *u8) 32 if res[0] != 1 { okall = 0 } 33 if res[1] < 100 { okall = 0 } 34 if res[3] < 4000 { okall = 0 } 35 k = k + 1 36 } 37 var t1: i64 = 0 38 if okall == 1 { t1 = 1 } 39 if t1 == 1 { hw("T1 PASS one nx_gen(spec,res) shape produced all 5 heterogeneous asset kinds\n" as *u8) } 40 else { fails=fails+1; hw("T1 FAIL a kind failed to generate\n" as *u8) } 41 42 // ---- T2 controllability: params change the asset (tree species + seed), cheap kinds only ---- 43 let ra: *i64 = sys_mmap(8*8) as *i64 44 let rb: *i64 = sys_mmap(8*8) as *i64 45 spec[0]=GEN_TREE; spec[1]=9; spec[2]=0; spec[3]=0; nx_gen(spec, ra) // broadleaf 46 spec[0]=GEN_TREE; spec[1]=9; spec[2]=1; spec[3]=0; nx_gen(spec, rb) // conifer 47 let dsp: i64 = L1(ra[4] as *i64, rb[4] as *i64, ra[1]*ra[2]) 48 let rc: *i64 = sys_mmap(8*8) as *i64 49 spec[0]=GEN_TREE; spec[1]=42; spec[2]=0; spec[3]=0; nx_gen(spec, rc) // different seed 50 let dsd: i64 = L1(ra[4] as *i64, rc[4] as *i64, ra[1]*ra[2]) 51 // creature plan control 52 let rd: *i64 = sys_mmap(8*8) as *i64 53 let re: *i64 = sys_mmap(8*8) as *i64 54 spec[0]=GEN_CREATURE; spec[1]=4; spec[2]=0; spec[3]=0; nx_gen(spec, rd) // quadruped 55 spec[0]=GEN_CREATURE; spec[1]=4; spec[2]=1; spec[3]=0; nx_gen(spec, re) // bird 56 let dpl: i64 = L1(rd[4] as *i64, re[4] as *i64, rd[1]*rd[2]) 57 hw(" control: tree species-delta L1="); pn(dsp); hw(" tree seed-delta="); pn(dsd); hw(" creature plan-delta="); pn(dpl); hw("\n" as *u8) 58 var t2: i64 = 0 59 if dsp > 150000 { if dsd > 150000 { if dpl > 250000 { t2 = 1 } } } 60 if t2 == 1 { hw("T2 PASS the spec's params CONTROL the output (species/seed/plan each change the asset)\n" as *u8) } 61 else { fails=fails+1; hw("T2 FAIL params don't control\n" as *u8) } 62 63 // ---- T3 registry: self-description + fail-closed unknown ---- 64 var regok: i64 = 1 65 k = 0 66 while k < GEN_NKIND { 67 if sl(gen_kind_name(k)) < 2 { regok = 0 } 68 if sl(gen_param_schema(k)) < 4 { regok = 0 } 69 k = k + 1 70 } 71 spec[0]=99; spec[1]=1; spec[2]=0; spec[3]=0 72 let ru: *i64 = sys_mmap(8*8) as *i64 73 let unk: i64 = nx_gen(spec, ru) 74 hw(" registry: kinds="); pn(gen_kind_count()); hw(" all-described="); pn(regok); hw(" unknown-kind-ok="); pn(ru[0]); hw(" (want 0)\n" as *u8) 75 var t3: i64 = 0 76 if gen_kind_count() == GEN_NKIND { if regok == 1 { if unk == 0 { if ru[0] == 0 { t3 = 1 } } } } 77 if t3 == 1 { hw("T3 PASS registry self-describes every kind + unknown kind fails closed\n" as *u8) } 78 else { fails=fails+1; hw("T3 FAIL registry\n" as *u8) } 79 80 // ---- T4 determinism + contact sheet ---- 81 let rf: *i64 = sys_mmap(8*8) as *i64 82 spec[0]=GEN_TREE; spec[1]=9; spec[2]=1; spec[3]=0; nx_gen(spec, rf) // same as rb 83 var ddet: i64 = 0 84 let fbb: *i64 = rb[4] as *i64 85 let fbf: *i64 = rf[4] as *i64 86 var i: i64 = 0 87 while i < rb[1]*rb[2] { if fbb[i] != fbf[i] { ddet = ddet + 1 } i = i + 1 } 88 var t4: i64 = 0 89 if ddet == 0 { t4 = 1 } 90 if t4 == 1 { hw("T4 PASS deterministic (same spec -> identical bytes)\n" as *u8) } 91 else { fails=fails+1; hw("T4 FAIL nondeterministic ddet="); pn(ddet); hw("\n" as *u8) } 92 93 // contact sheet: each kind's T1 asset downsampled into a 380-wide tile, 5 across 94 let TW: i64 = 380 95 let TH: i64 = 285 96 let GWt: i64 = TW*GEN_NKIND 97 let gal: *i64 = sys_mmap(GWt*TH*8) as *i64 98 k = 0 99 while k < GEN_NKIND { 100 let res: *i64 = (R as i64 + k*6*8) as *i64 101 let sw: i64 = res[1]; let sh: i64 = res[2]; let sfb: *i64 = res[4] as *i64 102 var y: i64 = 0 103 while y < TH { 104 var x: i64 = 0 105 while x < TW { 106 let sx: i64 = x*sw/TW 107 let sy: i64 = y*sh/TH 108 gal[y*GWt + k*TW + x] = sfb[sy*sw+sx] 109 x = x + 1 110 } 111 y = y + 1 112 } 113 k = k + 1 114 } 115 write_png(gal, GWt, TH, "knowledge/nx_gen_contact.png" as *u8) 116 hw("T5 contact sheet -> knowledge/nx_gen_contact.png (world | tree | creature | figure | face, one API)\n" as *u8) 117 118 if fails == 0 { hw("GEN-GATE GREEN -- R5: a UNIFIED function-oriented generator surface over all organs (data-driven spec + self-describing registry + param control + fail-closed + deterministic) = the ProcFunc-class abstraction, sovereign\n" as *u8); sys_exit(0); return 0 } 119 hw("GEN-GATE RED fails="); pn(fails); hw("\n" as *u8) 120 sys_exit(1) 121 return 1 122}