code wiki / _hdl_build / nx_softdyn_gate.nx
nx_softdyn_gate.nx source
↩ module page · 146 lines · 6943 B
1// nx_softdyn_gate.nx -- prove the secondary-dynamics solver is REAL PHYSICS, not a wobble hack.
2// T1 INERTIAL LAG soft mass trails an accelerating anchor (that IS jiggle)
3// T2 OVERSHOOT when the anchor stops, the mass passes THROUGH it (sign change) = oscillation
4// T3 DAMPED SETTLE energy decays and it comes to rest (no perpetual motion)
5// T4 IMPULSE/RECOIL a kick on a still point displaces then returns -- the SAME solver serves firearms
6// T5 BOUNDED a violent anchor can never separate tissue past the clamp (divergence impossible)
7// T6 DETERMINISM identical replay, bit-for-bit
8// T7 ANTI-VACUITY a STIFF+damped config shows ~no lag => T1's lag is the SOLVER, not an always-on bug
9// license_tier: ORIGINAL expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_softdyn.nx"
12
13const SG_SOFT_K: i64 = 120
14const SG_SOFT_C: i64 = 100
15const SG_STIFF_K: i64 = 900
16const SG_STIFF_C: i64 = 800
17const SG_MAXD: i64 = 40
18
19func sg_hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
20func sg_pn(v: i64) -> i64 {
21 let b: *u8=sys_mmap(32); var x: i64=v; var ng: i64=0
22 if x<0 { ng=1; x=0-x }
23 var i: i64=31
24 if x==0 { b[i]=48 as u8; i=i-1 }
25 while x>0 { b[i]=(48+x%10) as u8; x=x/10; i=i-1 }
26 if ng==1 { b[i]=45 as u8; i=i-1 }
27 sys_write(1,(b as i64+i+1) as *u8,31-i); return 0
28}
29
30func main() -> i64 {
31 sg_hw("=== nx_softdyn_gate -- secondary dynamics (jiggle / recoil) is real physics ===\n" as *u8)
32 var fails: i64 = 0
33
34 // ---------- T1/T2/T3: anchor accelerates upward 30 ticks, then STOPS. ----------
35 let st: *i64 = sd_alloc(4)
36 sd_seat(st, 0, 0, 0, 0)
37 var maxlag: i64 = 0
38 var tk: i64 = 0
39 var ay: i64 = 0
40 while tk < 30 {
41 ay = ay + 3 // anchor rises 3 units/tick (bone motion)
42 sd_step(st, 0, 0, ay, 0, SG_SOFT_K, SG_SOFT_C, SG_MAXD)
43 let d: i64 = sd_disp(st, 0, 0, ay, 0)
44 if d > maxlag { maxlag = d }
45 tk = tk + 1
46 }
47 sg_hw(" T1 peak inertial lag while accelerating = " as *u8); sg_pn(maxlag); sg_hw(" units\n" as *u8)
48 if maxlag >= 3 { sg_hw(" T1 LAG present: PASS\n" as *u8) } else { sg_hw(" T1 FAIL (no lag = not soft)\n" as *u8); fails=fails+1 }
49
50 // anchor now HOLDS still; the mass must swing through it (overshoot) then settle
51 // measured in Q8 SUB-UNITS: a jiggle of a fraction of a model unit is still a real oscillation
52 var signflips: i64 = 0
53 var prev: i64 = sd_disp_y_q8(st, 0, ay)
54 var e0: i64 = sd_energy(st, 0)
55 var settle: i64 = 0 - 1
56 tk = 0
57 while tk < 400 {
58 sd_step(st, 0, 0, ay, 0, SG_SOFT_K, SG_SOFT_C, SG_MAXD)
59 let cur: i64 = sd_disp_y_q8(st, 0, ay)
60 if prev > 0 { if cur < 0 { signflips = signflips + 1 } }
61 if prev < 0 { if cur > 0 { signflips = signflips + 1 } }
62 prev = cur
63 if settle < 0 { if sd_disp_q8(st, 0, 0, ay, 0) < 26 { settle = tk } }
64 tk = tk + 1
65 }
66 let e1: i64 = sd_energy(st, 0)
67 sg_hw(" T2 overshoot sign-changes = " as *u8); sg_pn(signflips)
68 sg_hw(" T3 energy " as *u8); sg_pn(e0); sg_hw(" -> " as *u8); sg_pn(e1)
69 sg_hw(", settled at tick " as *u8); sg_pn(settle); sg_hw("\n" as *u8)
70 if signflips >= 1 { sg_hw(" T2 OVERSHOOT/oscillation: PASS\n" as *u8) } else { sg_hw(" T2 FAIL (no overshoot = not springy)\n" as *u8); fails=fails+1 }
71 if e1 < e0 { if settle >= 0 { sg_hw(" T3 DAMPED to rest: PASS\n" as *u8) } else { sg_hw(" T3 FAIL (never settled)\n" as *u8); fails=fails+1 } }
72 else { sg_hw(" T3 FAIL (energy did not decay)\n" as *u8); fails=fails+1 }
73
74 // ---------- T4: IMPULSE = firearm recoil on a still mount (same solver) ----------
75 sd_seat(st, 1, 0, 0, 0)
76 sd_impulse(st, 1, 0, 0, 0-3200) // sharp kick backward
77 var peak: i64 = 0
78 var back: i64 = 0 - 1
79 tk = 0
80 while tk < 300 {
81 sd_step(st, 1, 0, 0, 0, SG_SOFT_K, SG_SOFT_C, SG_MAXD)
82 let d: i64 = sd_disp(st, 1, 0, 0, 0)
83 if d > peak { peak = d }
84 if back < 0 { if tk > 5 { if sd_disp_q8(st, 1, 0, 0, 0) < 26 { back = tk } } }
85 tk = tk + 1
86 }
87 sg_hw(" T4 recoil peak displacement = " as *u8); sg_pn(peak)
88 sg_hw(" units, returned to rest at tick " as *u8); sg_pn(back); sg_hw("\n" as *u8)
89 if peak > 0 { if back > 0 { sg_hw(" T4 IMPULSE/RECOIL response: PASS\n" as *u8) } else { sg_hw(" T4 FAIL (never returned)\n" as *u8); fails=fails+1 } }
90 else { sg_hw(" T4 FAIL (impulse did nothing)\n" as *u8); fails=fails+1 }
91
92 // ---------- T5: BOUNDED under a violent anchor ----------
93 sd_seat(st, 2, 0, 0, 0)
94 var worst: i64 = 0
95 tk = 0
96 while tk < 600 {
97 var a: i64 = 0
98 if (tk/4)%2 == 0 { a = 900 } // teleport the anchor back and forth violently
99 sd_step(st, 2, 0, a, 0, SG_SOFT_K, SG_SOFT_C, SG_MAXD)
100 let d: i64 = sd_disp(st, 2, 0, a, 0)
101 if d > worst { worst = d }
102 tk = tk + 1
103 }
104 sg_hw(" T5 worst separation under violent anchor = " as *u8); sg_pn(worst)
105 sg_hw(" (clamp " as *u8); sg_pn(SG_MAXD); sg_hw(")\n" as *u8)
106 if worst <= SG_MAXD { sg_hw(" T5 BOUNDED (cannot diverge): PASS\n" as *u8) } else { sg_hw(" T5 FAIL (exceeded clamp)\n" as *u8); fails=fails+1 }
107
108 // ---------- T6: DETERMINISM ----------
109 let r1: *i64 = sd_alloc(1)
110 let r2: *i64 = sd_alloc(1)
111 sd_seat(r1,0,0,0,0); sd_seat(r2,0,0,0,0)
112 sd_impulse(r1,0,700,0-1500,300); sd_impulse(r2,0,700,0-1500,300)
113 var c1: i64 = 0; var c2: i64 = 0
114 tk = 0
115 while tk < 200 {
116 let a2: i64 = (tk*7)%50
117 sd_step(r1,0,a2,a2/2,0,SG_SOFT_K,SG_SOFT_C,SG_MAXD)
118 sd_step(r2,0,a2,a2/2,0,SG_SOFT_K,SG_SOFT_C,SG_MAXD)
119 c1 = (c1*31 + sd_disp(r1,0,a2,a2/2,0)) & 1073741823
120 c2 = (c2*31 + sd_disp(r2,0,a2,a2/2,0)) & 1073741823
121 tk = tk + 1
122 }
123 sg_hw(" T6 replay checksums " as *u8); sg_pn(c1); sg_hw(" vs " as *u8); sg_pn(c2); sg_hw("\n" as *u8)
124 if c1 == c2 { sg_hw(" T6 DETERMINISM: PASS\n" as *u8) } else { sg_hw(" T6 FAIL\n" as *u8); fails=fails+1 }
125
126 // ---------- T7 ANTI-VACUITY: stiff + heavily damped => essentially NO lag ----------
127 sd_seat(st, 3, 0, 0, 0)
128 var stifflag: i64 = 0
129 var ay2: i64 = 0
130 tk = 0
131 while tk < 30 {
132 ay2 = ay2 + 3
133 sd_step(st, 3, 0, ay2, 0, SG_STIFF_K, SG_STIFF_C, SG_MAXD)
134 let d: i64 = sd_disp(st, 3, 0, ay2, 0)
135 if d > stifflag { stifflag = d }
136 tk = tk + 1
137 }
138 sg_hw(" T7 stiff-config peak lag = " as *u8); sg_pn(stifflag)
139 sg_hw(" vs soft " as *u8); sg_pn(maxlag); sg_hw("\n" as *u8)
140 if stifflag < maxlag { sg_hw(" T7 ANTI-VACUITY (stiffness controls jiggle): PASS\n" as *u8) }
141 else { sg_hw(" T7 FAIL (config does not matter => solver is fake)\n" as *u8); fails=fails+1 }
142
143 if fails == 0 { sg_hw("VERDICT=GREEN 7/7\n" as *u8); return 0 }
144 sg_hw("VERDICT=RED fails=" as *u8); sg_pn(fails); sg_hw("\n" as *u8)
145 return 1
146}