code wiki / _hdl_build / nx_wasm_breeders_gate.nx
nx_wasm_breeders_gate.nx source
↩ module page · 155 lines · 6667 B
1// nx_wasm_breeders_gate.nx -- verifies the SOVEREIGN breeders build NATIVELY. There is no browser here,
2// so rendering the identical base-relative code to a PNG IS the proof the wasm logic + raster are right
3// (the established "verify wasm by native PNG" loop). Same code, base = mmap instead of 0.
4// license_tier: ORIGINAL expect_exit: 0
5import "nx_wasm_breeders.nx"
6import "nx_png_write.nx"
7import "nx_gate_verdict.nx"
8
9func vw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func vn(v: i64) -> i64 {
11 let t: *u8 = sys_mmap(32) as *u8
12 var m: i64 = v; var w: i64 = 0
13 if m<0 { t[w]=45 as u8; w=w+1; m=0-m }
14 if m==0 { t[w]=48 as u8; sys_write(1,t,w+1); return 0 }
15 let d: *u8 = sys_mmap(32) as *u8
16 var k: i64=0
17 while m>0 { d[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
18 var j: i64=0
19 while j<k { t[w]=d[k-1-j]; w=w+1; j=j+1 }
20 sys_write(1,t,w); return 0
21}
22func emit(base: i64, path: *u8) -> i64 {
23 let rgbbuf: *u8 = sys_mmap(W*H*3) as *u8
24 let f: *i64 = fb(base)
25 var p: i64 = 0
26 while p < W*H {
27 let v: i64 = f[p]
28 rgbbuf[p*3] = (v & 255) as u8
29 rgbbuf[p*3+1] = ((v >> 8) & 255) as u8
30 rgbbuf[p*3+2] = ((v >> 16) & 255) as u8
31 p = p + 1
32 }
33 return nx_png_write_rgb(path, rgbbuf, W, H)
34}
35func main() -> i64 {
36 let base: i64 = sys_mmap(WORDS*8) as i64
37 var pass: i64 = 0
38 let total: i64 = 7
39 vw("=== nx_wasm_breeders native verify (base-relative; the SAME code runs in wasm) ===\n" as *u8)
40
41 init_impl(base, 20260727)
42 // T1 the world generated with a sane tile mix (not the 64%-water class of failure)
43 var wcount: i64 = 0
44 var tcount: i64 = 0
45 var gcount: i64 = 0
46 var ty: i64 = 0
47 while ty < MH { var tx: i64 = 0
48 while tx < MW { let t: i64 = tile(base,tx,ty)
49 if t==T_WATER {wcount=wcount+1} if t==T_TREE {tcount=tcount+1} if t==T_GRASS {gcount=gcount+1}
50 tx=tx+1 }
51 ty=ty+1 }
52 let tot: i64 = MW*MH
53 vw("T1 tiles grass=" as *u8); vn(gcount*1000/tot); vw(" tree=" as *u8); vn(tcount*1000/tot)
54 vw(" water=" as *u8); vn(wcount*1000/tot); vw(" permil\n" as *u8)
55 if gcount*1000/tot > 200 { if tcount > 0 { if wcount*1000/tot < 500 { pass=pass+1 } } }
56
57 // T2 render a real frame and prove it is non-empty and varied
58 render_impl(base)
59 let f: *i64 = fb(base)
60 var distinct: i64 = 0
61 var seen: *i64 = sys_mmap(4096*8) as *i64
62 var q: i64 = 0
63 while q < 4096 { seen[q]=0; q=q+1 }
64 var p2: i64 = 0
65 while p2 < W*H {
66 let k: i64 = ((f[p2] & 255)/16)*256 + (((f[p2]>>8)&255)/16)*16 + ((f[p2]>>16)&255)/16
67 if seen[k%4096]==0 { seen[k%4096]=1; distinct=distinct+1 }
68 p2 = p2 + 1
69 }
70 vw("T2 frame distinct colours=" as *u8); vn(distinct); vw("\n" as *u8)
71 if distinct > 40 { pass = pass + 1 }
72 emit(base, "knowledge/nx_wasm_breeders_start.png" as *u8)
73
74 // T3 movement actually moves and walls actually block
75 let s: *i64 = st(base)
76 let x0: i64 = s[S_PX]
77 var m: i64 = 0
78 while m < 12 { tick_impl(base, 3); m = m + 1 }
79 vw("T3 moved dx=" as *u8); vn(s[S_PX]-x0); vw("\n" as *u8)
80 if s[S_PX] != x0 { pass = pass + 1 }
81
82 // T4 MENDEL: two heterozygous founders -> 1:2:1 on the coat locus, MEASURED
83 let ga: i64 = founder_g(11)
84 let gb: i64 = founder_g(29)
85 var c0: i64 = 0
86 var c1: i64 = 0
87 var c2: i64 = 0
88 var n: i64 = 0
89 while n < 1200 {
90 let kid: i64 = gcross(base, ga, gb)
91 let ph: i64 = phen(kid, G_COAT)
92 if ph==0 {c0=c0+1} if ph==1 {c1=c1+1} if ph==2 {c2=c2+1}
93 n = n + 1
94 }
95 vw("T4 coat white/rose/crimson = " as *u8); vn(c0*1000/1200); vw("/" as *u8); vn(c1*1000/1200)
96 vw("/" as *u8); vn(c2*1000/1200); vw(" permil (textbook 250/500/250)\n" as *u8)
97 var d1: i64 = c1*1000/1200 - 500
98 if d1 < 0 { d1 = 0 - d1 }
99 if d1 <= 70 { if c0 > 0 { if c2 > 0 { pass = pass + 1 } } }
100
101 // T5 walk a few steps then render a second frame -- proves animation/camera actually change pixels
102 var ck1: i64 = 0
103 var z: i64 = 0
104 while z < W*H { ck1 = (ck1 ^ f[z]) * 3 % 1000000007; z = z + 40 }
105 var mv: i64 = 0
106 while mv < 20 { tick_impl(base, 1); mv = mv + 1 }
107 render_impl(base)
108 var ck2: i64 = 0
109 var z2: i64 = 0
110 while z2 < W*H { ck2 = (ck2 ^ f[z2]) * 3 % 1000000007; z2 = z2 + 40 }
111 vw("T5 frame checksum moved " as *u8); vn(ck1); vw(" -> " as *u8); vn(ck2); vw("\n" as *u8)
112 if ck1 != ck2 { pass = pass + 1 }
113 emit(base, "knowledge/nx_wasm_breeders_walk.png" as *u8)
114
115 // T6 CAUSAL INPUT (F1102 wiring): the device-agnostic layer drives the game -- D held through
116 // step_impl moves the player, release stops him; and the organ's input arena matches the lib.
117 let base2: i64 = sys_mmap(WORDS*8) as i64
118 init_impl(base2, 20260727)
119 let s6: *i64 = st(base2)
120 let x6: i64 = s6[S_PX]
121 ia_key(inp(base2), 68, 1)
122 var m6: i64 = 0
123 while m6 < 12 { step_impl(base2); m6 = m6 + 1 }
124 let moved: i64 = s6[S_PX] - x6
125 ia_key(inp(base2), 68, 0)
126 step_impl(base2)
127 let stay: i64 = s6[S_PX] - x6 - moved
128 vw("T6 D-held dx=" as *u8); vn(moved); vw(" after-release ddx=" as *u8); vn(stay)
129 vw(" ia_words=" as *u8); vn(ia_words()); vw("\n" as *u8)
130 if moved > 0 { if stay == 0 { if ia_words() == 64 { pass = pass + 1 } } }
131
132 // T7 EDGE HONESTY through the WIRED loop: E held across two steps fires interact ONCE.
133 // fresh arena so no wild is in range: the no-target branch sets msgt=90 on the edge step, and
134 // the second step only DECREMENTS it -> 89. A repeat-firing layer would re-arm it to 90.
135 let base3: i64 = sys_mmap(WORDS*8) as i64
136 init_impl(base3, 20260727)
137 let s7: *i64 = st(base3)
138 ia_key(inp(base3), 69, 1)
139 step_impl(base3)
140 step_impl(base3)
141 vw("T7 msg=" as *u8); vn(s7[S_MSG]); vw(" msgt=" as *u8); vn(s7[S_MSGT])
142 vw(" (1/89 = one no-target interact, no held-repeat)\n" as *u8)
143 if s7[S_MSG] == 1 { if s7[S_MSGT] == 89 { pass = pass + 1 } }
144
145 vw("nx_wasm_breeders_gate: " as *u8); vn(pass); vw("/" as *u8); vn(total); vw("\n" as *u8)
146 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
147 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
148 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
149 let ctr__dry: *i64 = gv_ctr()
150 ctr__dry[0] = pass
151 ctr__dry[1] = total
152 let rc__dry: i64 = gv_verdict("WASM-BREEDERS-GATE" as *u8, ctr__dry, "sovereign breeders verified natively; ready for the wasm chain" as *u8)
153 sys_exit(rc__dry)
154 return rc__dry
155}